test_pagure_lib_git_diff_pr.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources # noqa
  9. import json # noqa
  10. import unittest # noqa
  11. import shutil # noqa
  12. import sys # noqa
  13. import tempfile # noqa
  14. import time # noqa
  15. import os # noqa
  16. import pygit2 # noqa
  17. from mock import patch, MagicMock # noqa
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib # noqa
  21. import tests # noqa
  22. from pagure.lib.repo import PagureRepo # noqa
  23. class PagureFlaskForkPrtests(tests.Modeltests):
  24. """ Tests for flask fork controller of pagure regarding diffing PRs """
  25. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  26. def setUp(self):
  27. """ Set up the environnment, ran before every tests. """
  28. super(PagureFlaskForkPrtests, self).setUp()
  29. # Create the main project in the DB
  30. item = pagure.lib.model.Project(
  31. user_id=1, # pingou
  32. name='test',
  33. description='test project #1',
  34. hook_token='aaabbbccc',
  35. )
  36. item.close_status = [
  37. 'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  38. self.session.add(item)
  39. self.session.commit()
  40. # Create the fork
  41. item = pagure.lib.model.Project(
  42. user_id=1, # pingou
  43. name='test',
  44. description='test project #1',
  45. hook_token='aaabbbcccdd',
  46. parent_id=1,
  47. is_fork=True,
  48. )
  49. item.close_status = [
  50. 'Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  51. self.session.add(item)
  52. self.session.commit()
  53. # Create two git repos, one has 6 commits, the other 4 of which only
  54. # 1 isn't present in the first repo
  55. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  56. pygit2.init_repository(gitrepo, bare=True)
  57. gitrepo2 = os.path.join(
  58. self.path, 'repos', 'forks', 'pingou', 'test.git')
  59. pygit2.init_repository(gitrepo2, bare=True)
  60. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  61. repopath = os.path.join(newpath, 'test')
  62. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  63. # Do 3 commits to the main repo
  64. for i in range(3):
  65. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  66. stream.write('foo%s\n bar%s\n' % (i, i))
  67. clone_repo.index.add('sources')
  68. clone_repo.index.write()
  69. parents = []
  70. try:
  71. last_commit = clone_repo.revparse_single('HEAD')
  72. parents = [last_commit.oid.hex]
  73. except KeyError:
  74. pass
  75. # Commits the files added
  76. tree = clone_repo.index.write_tree()
  77. author = pygit2.Signature(
  78. 'Alice Author', 'alice@authors.tld')
  79. committer = pygit2.Signature(
  80. 'Cecil Committer', 'cecil@committers.tld')
  81. clone_repo.create_commit(
  82. 'refs/heads/master', # the name of the reference to update
  83. author,
  84. committer,
  85. 'Editing the file sources for testing #%s' % i,
  86. # binary string representing the tree object ID
  87. tree,
  88. # list of binary strings representing parents of the new commit
  89. parents
  90. )
  91. time.sleep(1)
  92. # Push to the main repo
  93. refname = 'refs/heads/master:refs/heads/master'
  94. ori_remote = clone_repo.remotes[0]
  95. PagureRepo.push(ori_remote, refname)
  96. # Push to the fork repo
  97. remote = clone_repo.create_remote('pingou_fork', gitrepo2)
  98. PagureRepo.push(remote, refname)
  99. # Do another 3 commits to the main repo
  100. for i in range(3, 6):
  101. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  102. stream.write('foo%s\n bar%s\n' % (i, i))
  103. clone_repo.index.add('sources')
  104. clone_repo.index.write()
  105. last_commit = clone_repo.revparse_single('HEAD')
  106. # Commits the files added
  107. tree = clone_repo.index.write_tree()
  108. author = pygit2.Signature(
  109. 'Alice Author', 'alice@authors.tld')
  110. committer = pygit2.Signature(
  111. 'Cecil Committer', 'cecil@committers.tld')
  112. clone_repo.create_commit(
  113. 'refs/heads/master', # the name of the reference to update
  114. author,
  115. committer,
  116. 'Editing the file sources for testing #%s' % i,
  117. # binary string representing the tree object ID
  118. tree,
  119. # list of binary strings representing parents of the new commit
  120. [last_commit.oid.hex]
  121. )
  122. time.sleep(1)
  123. # Push to the main repo
  124. refname = 'refs/heads/master:refs/heads/master'
  125. ori_remote = clone_repo.remotes[0]
  126. PagureRepo.push(ori_remote, refname)
  127. # Add two commits to the fork repo
  128. repopath = os.path.join(newpath, 'pingou_test')
  129. clone_repo = pygit2.clone_repository(gitrepo2, repopath)
  130. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  131. stream.write('foo\n bar\n')
  132. clone_repo.index.add('sources')
  133. clone_repo.index.write()
  134. last_commit = clone_repo.revparse_single('HEAD')
  135. # Commits the files added
  136. tree = clone_repo.index.write_tree()
  137. author = pygit2.Signature(
  138. 'Alice Author', 'alice@authors.tld')
  139. committer = pygit2.Signature(
  140. 'Cecil Committer', 'cecil@committers.tld')
  141. last_commit = clone_repo.create_commit(
  142. 'refs/heads/feature_foo', # the name of the reference to update
  143. author,
  144. committer,
  145. 'New edition on side branch of the file sources for testing',
  146. # binary string representing the tree object ID
  147. tree,
  148. # list of binary strings representing parents of the new commit
  149. [last_commit.oid.hex]
  150. )
  151. time.sleep(1)
  152. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  153. stream.write('foo\n bar\nbaz\n')
  154. clone_repo.index.add('sources')
  155. clone_repo.index.write()
  156. # Commits the files added
  157. tree = clone_repo.index.write_tree()
  158. author = pygit2.Signature(
  159. 'Alice Author', 'alice@authors.tld')
  160. committer = pygit2.Signature(
  161. 'Cecil Committer', 'cecil@committers.tld')
  162. last_commit = clone_repo.create_commit(
  163. 'refs/heads/feature_foo', # the name of the reference to update
  164. author,
  165. committer,
  166. 'Second edit on side branch of the file sources for testing',
  167. # binary string representing the tree object ID
  168. tree,
  169. # list of binary strings representing parents of the new commit
  170. [last_commit.hex]
  171. )
  172. # Push to the fork repo
  173. ori_remote = clone_repo.remotes[0]
  174. refname = 'refs/heads/feature_foo:refs/heads/feature_foo'
  175. PagureRepo.push(ori_remote, refname)
  176. shutil.rmtree(newpath)
  177. # Create the PR between the two repos
  178. repo = pagure.lib.get_authorized_project(self.session, 'test')
  179. forked_repo = pagure.lib.get_authorized_project(
  180. self.session, 'test', user='pingou')
  181. req = pagure.lib.new_pull_request(
  182. session=self.session,
  183. repo_from=forked_repo,
  184. branch_from='feature_foo',
  185. repo_to=repo,
  186. branch_to='master',
  187. title='test pull-request',
  188. user='pingou',
  189. requestfolder=None,
  190. )
  191. self.assertEqual(req.id, 1)
  192. self.assertEqual(req.title, 'test pull-request')
  193. def test_get_pr_info(self):
  194. """ Test pagure.ui.fork._get_pr_info """
  195. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  196. gitrepo2 = os.path.join(
  197. self.path, 'repos', 'forks', 'pingou', 'test.git')
  198. diff, diff_commits, orig_commit = pagure.lib.git.get_diff_info(
  199. repo_obj=PagureRepo(gitrepo2),
  200. orig_repo=PagureRepo(gitrepo),
  201. branch_from='feature_foo',
  202. branch_to='master'
  203. )
  204. self.assertEqual(len(diff_commits), 2)
  205. self.assertEqual(
  206. diff_commits[0].message,
  207. 'Second edit on side branch of the file sources for testing'
  208. )
  209. self.assertEqual(
  210. diff_commits[1].message,
  211. 'New edition on side branch of the file sources for testing'
  212. )
  213. self.assertEqual(
  214. orig_commit.message,
  215. 'Editing the file sources for testing #5'
  216. )
  217. def test_get_pr_info_raises(self):
  218. """ Test pagure.ui.fork._get_pr_info """
  219. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  220. gitrepo2 = os.path.join(
  221. self.path, 'repos', 'forks', 'pingou', 'test.git')
  222. self.assertRaises(
  223. pagure.exceptions.BranchNotFoundException,
  224. pagure.lib.git.get_diff_info,
  225. repo_obj=PagureRepo(gitrepo2),
  226. orig_repo=PagureRepo(gitrepo),
  227. branch_from='feature',
  228. branch_to='master'
  229. )
  230. self.assertRaises(
  231. pagure.exceptions.BranchNotFoundException,
  232. pagure.lib.git.get_diff_info,
  233. repo_obj=PagureRepo(gitrepo2),
  234. orig_repo=PagureRepo(gitrepo),
  235. branch_from='feature_foo',
  236. branch_to='bar'
  237. )
  238. def test_diff_pull_request(self):
  239. """ Test pagure.lib.git.diff_pull_request """
  240. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  241. gitrepo2 = os.path.join(
  242. self.path, 'repos', 'forks', 'pingou', 'test.git')
  243. request = pagure.lib.search_pull_requests(
  244. self.session, requestid=1, project_id=1)
  245. diff_commits, diff = pagure.lib.git.diff_pull_request(
  246. self.session,
  247. request=request,
  248. repo_obj=PagureRepo(gitrepo2),
  249. orig_repo=PagureRepo(gitrepo),
  250. requestfolder=None,
  251. with_diff=True
  252. )
  253. self.assertEqual(len(diff_commits), 2)
  254. self.assertEqual(
  255. diff_commits[0].message,
  256. 'Second edit on side branch of the file sources for testing'
  257. )
  258. self.assertEqual(
  259. diff_commits[1].message,
  260. 'New edition on side branch of the file sources for testing'
  261. )
  262. # Check that the PR has its PR refs
  263. # we don't know the task id but we'll give it 30 sec to finish
  264. cnt = 0
  265. repo = PagureRepo(gitrepo)
  266. while 1:
  267. if 'refs/pull/1/head' in list(repo.listall_references()):
  268. break
  269. cnt += 1
  270. if cnt == 60:
  271. break
  272. time.sleep(0.5)
  273. self.assertTrue(cnt < 60)
  274. pr_ref = repo.lookup_reference('refs/pull/1/head')
  275. commit = pr_ref.get_object()
  276. self.assertEqual(
  277. commit.oid.hex,
  278. diff_commits[0].oid.hex
  279. )
  280. def test_diff_pull_request_updated(self):
  281. """ Test that calling pagure.lib.git.diff_pull_request on an updated
  282. PR updates the PR reference
  283. """
  284. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  285. gitrepo2 = os.path.join(
  286. self.path, 'repos', 'forks', 'pingou', 'test.git')
  287. request = pagure.lib.search_pull_requests(
  288. self.session, requestid=1, project_id=1)
  289. # Get the diff corresponding to the PR and check its ref
  290. diff_commits, diff = pagure.lib.git.diff_pull_request(
  291. self.session,
  292. request=request,
  293. repo_obj=PagureRepo(gitrepo2),
  294. orig_repo=PagureRepo(gitrepo),
  295. requestfolder=None,
  296. with_diff=True
  297. )
  298. self.assertEqual(len(diff_commits), 2)
  299. # Check that the PR has its PR refs
  300. # we don't know the task id but we'll give it 30 sec to finish
  301. cnt = 0
  302. repo = PagureRepo(gitrepo)
  303. while 1:
  304. if 'refs/pull/1/head' in list(repo.listall_references()):
  305. break
  306. cnt += 1
  307. if cnt == 60:
  308. break
  309. time.sleep(0.5)
  310. self.assertTrue(cnt < 60)
  311. pr_ref = repo.lookup_reference('refs/pull/1/head')
  312. commit = pr_ref.get_object()
  313. self.assertEqual(
  314. commit.oid.hex,
  315. diff_commits[0].oid.hex
  316. )
  317. # Add a new commit on the fork
  318. repopath = os.path.join(self.path, 'pingou_test2')
  319. clone_repo = pygit2.clone_repository(
  320. gitrepo2, repopath, checkout_branch='feature_foo')
  321. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  322. stream.write('foo\n bar\nbaz\nhey there\n')
  323. clone_repo.index.add('sources')
  324. clone_repo.index.write()
  325. last_commit = clone_repo.lookup_branch('feature_foo').get_object()
  326. # Commits the files added
  327. tree = clone_repo.index.write_tree()
  328. author = pygit2.Signature(
  329. 'Alice Author', 'alice@authors.tld')
  330. committer = pygit2.Signature(
  331. 'Cecil Committer', 'cecil@committers.tld')
  332. last_commit = clone_repo.create_commit(
  333. 'refs/heads/feature_foo', # the name of the reference to update
  334. author,
  335. committer,
  336. 'Third edit on side branch of the file sources for testing',
  337. # binary string representing the tree object ID
  338. tree,
  339. # list of binary strings representing parents of the new commit
  340. [last_commit.oid.hex]
  341. )
  342. # Push to the fork repo
  343. ori_remote = clone_repo.remotes[0]
  344. refname = 'refs/heads/feature_foo:refs/heads/feature_foo'
  345. PagureRepo.push(ori_remote, refname)
  346. # Get the new diff for that PR and check its new ref
  347. diff_commits, diff = pagure.lib.git.diff_pull_request(
  348. self.session,
  349. request=request,
  350. repo_obj=PagureRepo(gitrepo2),
  351. orig_repo=PagureRepo(gitrepo),
  352. requestfolder=None,
  353. with_diff=True
  354. )
  355. self.assertEqual(len(diff_commits), 3)
  356. # Check that the PR has its PR refs
  357. # we don't know the task id but we'll give it 30 sec to finish
  358. cnt = 0
  359. repo = PagureRepo(gitrepo)
  360. while 1:
  361. if 'refs/pull/1/head' in list(repo.listall_references()):
  362. break
  363. cnt += 1
  364. if cnt == 60:
  365. break
  366. time.sleep(0.5)
  367. self.assertTrue(cnt < 60)
  368. pr_ref = repo.lookup_reference('refs/pull/1/head')
  369. commit2 = pr_ref.get_object()
  370. self.assertEqual(
  371. commit2.oid.hex,
  372. diff_commits[0].oid.hex
  373. )
  374. self.assertNotEqual(
  375. commit.oid.hex,
  376. commit2.oid.hex,
  377. )
  378. def test_two_diff_pull_request_sequentially(self):
  379. """ Test calling pagure.lib.git.diff_pull_request twice returns
  380. the same data
  381. """
  382. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  383. gitrepo2 = os.path.join(
  384. self.path, 'repos', 'forks', 'pingou', 'test.git')
  385. request = pagure.lib.search_pull_requests(
  386. self.session, requestid=1, project_id=1)
  387. # Get the diff corresponding to the PR and check its ref
  388. diff_commits, diff = pagure.lib.git.diff_pull_request(
  389. self.session,
  390. request=request,
  391. repo_obj=PagureRepo(gitrepo2),
  392. orig_repo=PagureRepo(gitrepo),
  393. requestfolder=None,
  394. with_diff=True
  395. )
  396. self.assertEqual(len(diff_commits), 2)
  397. # Check that the PR has its PR refs
  398. # we don't know the task id but we'll give it 30 sec to finish
  399. cnt = 0
  400. repo = PagureRepo(gitrepo)
  401. while 1:
  402. if 'refs/pull/1/head' in list(repo.listall_references()):
  403. break
  404. cnt += 1
  405. if cnt == 60:
  406. break
  407. time.sleep(0.5)
  408. self.assertTrue(cnt < 60)
  409. pr_ref = repo.lookup_reference('refs/pull/1/head')
  410. commit = pr_ref.get_object()
  411. self.assertEqual(
  412. commit.oid.hex,
  413. diff_commits[0].oid.hex
  414. )
  415. # Run diff_pull_request a second time
  416. diff_commits2, diff = pagure.lib.git.diff_pull_request(
  417. self.session,
  418. request=request,
  419. repo_obj=PagureRepo(gitrepo2),
  420. orig_repo=PagureRepo(gitrepo),
  421. requestfolder=None,
  422. with_diff=True
  423. )
  424. self.assertEqual(len(diff_commits2), 2)
  425. self.assertEqual(
  426. [d.oid.hex for d in diff_commits2],
  427. [d.oid.hex for d in diff_commits])
  428. # Check that the PR has its PR refs
  429. # we don't know the task id but we'll give it 30 sec to finish
  430. cnt = 0
  431. repo = PagureRepo(gitrepo)
  432. while 1:
  433. if 'refs/pull/1/head' in list(repo.listall_references()):
  434. break
  435. cnt += 1
  436. if cnt == 60:
  437. break
  438. time.sleep(0.5)
  439. self.assertTrue(cnt < 60)
  440. pr_ref = repo.lookup_reference('refs/pull/1/head')
  441. commit2 = pr_ref.get_object()
  442. self.assertEqual(
  443. commit2.oid.hex,
  444. diff_commits[0].oid.hex
  445. )
  446. self.assertEqual(
  447. commit.oid.hex,
  448. commit2.oid.hex
  449. )
  450. if __name__ == '__main__':
  451. unittest.main(verbosity=2)