test_pagure_lib_git_diff_pr.py 17 KB

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