test_pagure_lib_git_diff_pr.py 17 KB

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