test_pagure_lib_git_diff_pr.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. requestfolder=None,
  191. )
  192. self.assertEqual(req.id, 1)
  193. self.assertEqual(req.title, 'test pull-request')
  194. def test_get_pr_info(self):
  195. """ Test pagure.ui.fork._get_pr_info """
  196. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  197. gitrepo2 = os.path.join(
  198. self.path, 'repos', 'forks', 'pingou', 'test.git')
  199. diff, diff_commits, orig_commit = pagure.lib.git.get_diff_info(
  200. repo_obj=PagureRepo(gitrepo2),
  201. orig_repo=PagureRepo(gitrepo),
  202. branch_from='feature_foo',
  203. branch_to='master'
  204. )
  205. self.assertEqual(len(diff_commits), 2)
  206. self.assertEqual(
  207. diff_commits[0].message,
  208. 'Second edit on side branch of the file sources for testing'
  209. )
  210. self.assertEqual(
  211. diff_commits[1].message,
  212. 'New edition on side branch of the file sources for testing'
  213. )
  214. self.assertEqual(
  215. orig_commit.message,
  216. 'Editing the file sources for testing #5'
  217. )
  218. def test_get_pr_info_raises(self):
  219. """ Test pagure.ui.fork._get_pr_info """
  220. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  221. gitrepo2 = os.path.join(
  222. self.path, 'repos', 'forks', 'pingou', 'test.git')
  223. self.assertRaises(
  224. pagure.exceptions.BranchNotFoundException,
  225. pagure.lib.git.get_diff_info,
  226. repo_obj=PagureRepo(gitrepo2),
  227. orig_repo=PagureRepo(gitrepo),
  228. branch_from='feature',
  229. branch_to='master'
  230. )
  231. self.assertRaises(
  232. pagure.exceptions.BranchNotFoundException,
  233. pagure.lib.git.get_diff_info,
  234. repo_obj=PagureRepo(gitrepo2),
  235. orig_repo=PagureRepo(gitrepo),
  236. branch_from='feature_foo',
  237. branch_to='bar'
  238. )
  239. def test_diff_pull_request(self):
  240. """ Test pagure.lib.git.diff_pull_request """
  241. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  242. gitrepo2 = os.path.join(
  243. self.path, 'repos', 'forks', 'pingou', 'test.git')
  244. request = pagure.lib.search_pull_requests(
  245. self.session, requestid=1, project_id=1)
  246. diff_commits, diff = pagure.lib.git.diff_pull_request(
  247. self.session,
  248. request=request,
  249. repo_obj=PagureRepo(gitrepo2),
  250. orig_repo=PagureRepo(gitrepo),
  251. requestfolder=None,
  252. with_diff=True
  253. )
  254. self.assertEqual(len(diff_commits), 2)
  255. self.assertEqual(
  256. diff_commits[0].message,
  257. 'Second edit on side branch of the file sources for testing'
  258. )
  259. self.assertEqual(
  260. diff_commits[1].message,
  261. 'New edition on side branch of the file sources for testing'
  262. )
  263. # Check that the PR has its PR refs
  264. # we don't know the task id but we'll give it 30 sec to finish
  265. cnt = 0
  266. repo = PagureRepo(gitrepo)
  267. while 1:
  268. if 'refs/pull/1/head' in list(repo.listall_references()):
  269. break
  270. cnt += 1
  271. if cnt == 60:
  272. break
  273. time.sleep(0.5)
  274. self.assertTrue(cnt < 60)
  275. pr_ref = repo.lookup_reference('refs/pull/1/head')
  276. commit = pr_ref.get_object()
  277. self.assertEqual(
  278. commit.oid.hex,
  279. diff_commits[0].oid.hex
  280. )
  281. def test_diff_pull_request_updated(self):
  282. """ Test that calling pagure.lib.git.diff_pull_request on an updated
  283. PR updates the PR reference
  284. """
  285. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  286. gitrepo2 = os.path.join(
  287. self.path, 'repos', 'forks', 'pingou', 'test.git')
  288. request = pagure.lib.search_pull_requests(
  289. self.session, requestid=1, project_id=1)
  290. # Get the diff corresponding to the PR and check its ref
  291. diff_commits, diff = pagure.lib.git.diff_pull_request(
  292. self.session,
  293. request=request,
  294. repo_obj=PagureRepo(gitrepo2),
  295. orig_repo=PagureRepo(gitrepo),
  296. requestfolder=None,
  297. with_diff=True
  298. )
  299. self.assertEqual(len(diff_commits), 2)
  300. # Check that the PR has its PR refs
  301. # we don't know the task id but we'll give it 30 sec to finish
  302. cnt = 0
  303. repo = PagureRepo(gitrepo)
  304. while 1:
  305. if 'refs/pull/1/head' in list(repo.listall_references()):
  306. break
  307. cnt += 1
  308. if cnt == 60:
  309. break
  310. time.sleep(0.5)
  311. self.assertTrue(cnt < 60)
  312. pr_ref = repo.lookup_reference('refs/pull/1/head')
  313. commit = pr_ref.get_object()
  314. self.assertEqual(
  315. commit.oid.hex,
  316. diff_commits[0].oid.hex
  317. )
  318. # Add a new commit on the fork
  319. repopath = os.path.join(self.path, 'pingou_test2')
  320. clone_repo = pygit2.clone_repository(
  321. gitrepo2, repopath, checkout_branch='feature_foo')
  322. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  323. stream.write('foo\n bar\nbaz\nhey there\n')
  324. clone_repo.index.add('sources')
  325. clone_repo.index.write()
  326. last_commit = clone_repo.lookup_branch('feature_foo').get_object()
  327. # Commits the files added
  328. tree = clone_repo.index.write_tree()
  329. author = pygit2.Signature(
  330. 'Alice Author', 'alice@authors.tld')
  331. committer = pygit2.Signature(
  332. 'Cecil Committer', 'cecil@committers.tld')
  333. last_commit = clone_repo.create_commit(
  334. 'refs/heads/feature_foo', # the name of the reference to update
  335. author,
  336. committer,
  337. 'Third edit on side branch of the file sources for testing',
  338. # binary string representing the tree object ID
  339. tree,
  340. # list of binary strings representing parents of the new commit
  341. [last_commit.oid.hex]
  342. )
  343. # Push to the fork repo
  344. ori_remote = clone_repo.remotes[0]
  345. refname = 'refs/heads/feature_foo:refs/heads/feature_foo'
  346. PagureRepo.push(ori_remote, refname)
  347. # Get the new diff for that PR and check its new ref
  348. diff_commits, diff = pagure.lib.git.diff_pull_request(
  349. self.session,
  350. request=request,
  351. repo_obj=PagureRepo(gitrepo2),
  352. orig_repo=PagureRepo(gitrepo),
  353. requestfolder=None,
  354. with_diff=True
  355. )
  356. self.assertEqual(len(diff_commits), 3)
  357. # Check that the PR has its PR refs
  358. # we don't know the task id but we'll give it 30 sec to finish
  359. cnt = 0
  360. repo = PagureRepo(gitrepo)
  361. while 1:
  362. if 'refs/pull/1/head' in list(repo.listall_references()):
  363. break
  364. cnt += 1
  365. if cnt == 60:
  366. break
  367. time.sleep(0.5)
  368. self.assertTrue(cnt < 60)
  369. pr_ref = repo.lookup_reference('refs/pull/1/head')
  370. commit2 = pr_ref.get_object()
  371. self.assertEqual(
  372. commit2.oid.hex,
  373. diff_commits[0].oid.hex
  374. )
  375. self.assertNotEqual(
  376. commit.oid.hex,
  377. commit2.oid.hex,
  378. )
  379. def test_two_diff_pull_request_sequentially(self):
  380. """ Test calling pagure.lib.git.diff_pull_request twice returns
  381. the same data
  382. """
  383. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  384. gitrepo2 = os.path.join(
  385. self.path, 'repos', 'forks', 'pingou', 'test.git')
  386. request = pagure.lib.search_pull_requests(
  387. self.session, requestid=1, project_id=1)
  388. # Get the diff corresponding to the PR and check its ref
  389. diff_commits, diff = pagure.lib.git.diff_pull_request(
  390. self.session,
  391. request=request,
  392. repo_obj=PagureRepo(gitrepo2),
  393. orig_repo=PagureRepo(gitrepo),
  394. requestfolder=None,
  395. with_diff=True
  396. )
  397. self.assertEqual(len(diff_commits), 2)
  398. # Check that the PR has its PR refs
  399. # we don't know the task id but we'll give it 30 sec to finish
  400. cnt = 0
  401. repo = PagureRepo(gitrepo)
  402. while 1:
  403. if 'refs/pull/1/head' in list(repo.listall_references()):
  404. break
  405. cnt += 1
  406. if cnt == 60:
  407. break
  408. time.sleep(0.5)
  409. self.assertTrue(cnt < 60)
  410. pr_ref = repo.lookup_reference('refs/pull/1/head')
  411. commit = pr_ref.get_object()
  412. self.assertEqual(
  413. commit.oid.hex,
  414. diff_commits[0].oid.hex
  415. )
  416. # Run diff_pull_request a second time
  417. diff_commits2, diff = pagure.lib.git.diff_pull_request(
  418. self.session,
  419. request=request,
  420. repo_obj=PagureRepo(gitrepo2),
  421. orig_repo=PagureRepo(gitrepo),
  422. requestfolder=None,
  423. with_diff=True
  424. )
  425. self.assertEqual(len(diff_commits2), 2)
  426. self.assertEqual(
  427. [d.oid.hex for d in diff_commits2],
  428. [d.oid.hex for d in diff_commits])
  429. # Check that the PR has its PR refs
  430. # we don't know the task id but we'll give it 30 sec to finish
  431. cnt = 0
  432. repo = PagureRepo(gitrepo)
  433. while 1:
  434. if 'refs/pull/1/head' in list(repo.listall_references()):
  435. break
  436. cnt += 1
  437. if cnt == 60:
  438. break
  439. time.sleep(0.5)
  440. self.assertTrue(cnt < 60)
  441. pr_ref = repo.lookup_reference('refs/pull/1/head')
  442. commit2 = pr_ref.get_object()
  443. self.assertEqual(
  444. commit2.oid.hex,
  445. diff_commits[0].oid.hex
  446. )
  447. self.assertEqual(
  448. commit.oid.hex,
  449. commit2.oid.hex
  450. )
  451. if __name__ == '__main__':
  452. unittest.main(verbosity=2)