test_pagure_lib_git_diff_pr.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. import pagure_messages
  17. from fedora_messaging import api, testing
  18. from mock import ANY, patch, MagicMock
  19. sys.path.insert(
  20. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  21. )
  22. import pagure.lib.query # noqa
  23. import tests # noqa
  24. from pagure.lib.repo import PagureRepo # noqa
  25. class PagureFlaskForkPrtests(tests.Modeltests):
  26. """Tests for flask fork controller of pagure regarding diffing PRs"""
  27. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  28. def setUp(self):
  29. """Set up the environnment, ran before every tests."""
  30. super(PagureFlaskForkPrtests, self).setUp()
  31. # Create the main project in the DB
  32. item = pagure.lib.model.Project(
  33. user_id=1, # pingou
  34. name="test",
  35. description="test project #1",
  36. hook_token="aaabbbccc",
  37. )
  38. item.close_status = [
  39. "Invalid",
  40. "Insufficient data",
  41. "Fixed",
  42. "Duplicate",
  43. ]
  44. self.session.add(item)
  45. self.session.commit()
  46. # Create the fork
  47. item = pagure.lib.model.Project(
  48. user_id=1, # pingou
  49. name="test",
  50. description="test project #1",
  51. hook_token="aaabbbcccdd",
  52. parent_id=1,
  53. is_fork=True,
  54. )
  55. item.close_status = [
  56. "Invalid",
  57. "Insufficient data",
  58. "Fixed",
  59. "Duplicate",
  60. ]
  61. self.session.add(item)
  62. self.session.commit()
  63. # Create two git repos, one has 6 commits, the other 4 of which only
  64. # 1 isn't present in the first repo
  65. gitrepo = os.path.join(self.path, "repos", "test.git")
  66. pygit2.init_repository(gitrepo, bare=True)
  67. gitrepo2 = os.path.join(
  68. self.path, "repos", "forks", "pingou", "test.git"
  69. )
  70. pygit2.init_repository(gitrepo2, bare=True)
  71. newpath = tempfile.mkdtemp(prefix="pagure-fork-test")
  72. repopath = os.path.join(newpath, "test")
  73. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  74. # Do 3 commits to the main repo
  75. for i in range(3):
  76. with open(os.path.join(repopath, "sources"), "w") as stream:
  77. stream.write("foo%s\n bar%s\n" % (i, i))
  78. clone_repo.index.add("sources")
  79. clone_repo.index.write()
  80. parents = []
  81. try:
  82. last_commit = clone_repo.revparse_single("HEAD")
  83. parents = [last_commit.oid.hex]
  84. except KeyError:
  85. pass
  86. # Commits the files added
  87. tree = clone_repo.index.write_tree()
  88. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  89. committer = pygit2.Signature(
  90. "Cecil Committer", "cecil@committers.tld"
  91. )
  92. clone_repo.create_commit(
  93. "refs/heads/master", # the name of the reference to update
  94. author,
  95. committer,
  96. "Editing the file sources for testing #%s" % i,
  97. # binary string representing the tree object ID
  98. tree,
  99. # list of binary strings representing parents of the new commit
  100. parents,
  101. )
  102. # Push to the main repo
  103. refname = "refs/heads/master:refs/heads/master"
  104. ori_remote = clone_repo.remotes[0]
  105. PagureRepo.push(ori_remote, refname)
  106. # Push to the fork repo
  107. remote = clone_repo.remotes.create("pingou_fork", gitrepo2)
  108. PagureRepo.push(remote, refname)
  109. # Do another 3 commits to the main repo
  110. for i in range(3, 6):
  111. with open(os.path.join(repopath, "sources"), "w") as stream:
  112. stream.write("foo%s\n bar%s\n" % (i, i))
  113. clone_repo.index.add("sources")
  114. clone_repo.index.write()
  115. last_commit = clone_repo.revparse_single("HEAD")
  116. # Commits the files added
  117. tree = clone_repo.index.write_tree()
  118. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  119. committer = pygit2.Signature(
  120. "Cecil Committer", "cecil@committers.tld"
  121. )
  122. clone_repo.create_commit(
  123. "refs/heads/master", # the name of the reference to update
  124. author,
  125. committer,
  126. "Editing the file sources for testing #%s" % i,
  127. # binary string representing the tree object ID
  128. tree,
  129. # list of binary strings representing parents of the new commit
  130. [last_commit.oid.hex],
  131. )
  132. # Push to the main repo
  133. refname = "refs/heads/master:refs/heads/master"
  134. ori_remote = clone_repo.remotes[0]
  135. PagureRepo.push(ori_remote, refname)
  136. # Add two commits to the fork repo
  137. repopath = os.path.join(newpath, "pingou_test")
  138. clone_repo = pygit2.clone_repository(gitrepo2, repopath)
  139. with open(os.path.join(repopath, "sources"), "w") as stream:
  140. stream.write("foo\n bar\n")
  141. clone_repo.index.add("sources")
  142. clone_repo.index.write()
  143. last_commit = clone_repo.revparse_single("HEAD")
  144. # Commits the files added
  145. tree = clone_repo.index.write_tree()
  146. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  147. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  148. last_commit = clone_repo.create_commit(
  149. "refs/heads/feature_foo", # the name of the reference to update
  150. author,
  151. committer,
  152. "New edition on side branch of the file sources for testing",
  153. # binary string representing the tree object ID
  154. tree,
  155. # list of binary strings representing parents of the new commit
  156. [last_commit.oid.hex],
  157. )
  158. with open(os.path.join(repopath, "sources"), "w") as stream:
  159. stream.write("foo\n bar\nbaz\n")
  160. clone_repo.index.add("sources")
  161. clone_repo.index.write()
  162. # Commits the files added
  163. tree = clone_repo.index.write_tree()
  164. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  165. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  166. last_commit = clone_repo.create_commit(
  167. "refs/heads/feature_foo", # the name of the reference to update
  168. author,
  169. committer,
  170. "Second edit on side branch of the file sources for testing",
  171. # binary string representing the tree object ID
  172. tree,
  173. # list of binary strings representing parents of the new commit
  174. [last_commit.hex],
  175. )
  176. # Push to the fork repo
  177. ori_remote = clone_repo.remotes[0]
  178. refname = "refs/heads/feature_foo:refs/heads/feature_foo"
  179. PagureRepo.push(ori_remote, refname)
  180. shutil.rmtree(newpath)
  181. # Create the PR between the two repos
  182. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  183. forked_repo = pagure.lib.query.get_authorized_project(
  184. self.session, "test", user="pingou"
  185. )
  186. req = pagure.lib.query.new_pull_request(
  187. session=self.session,
  188. repo_from=forked_repo,
  189. branch_from="feature_foo",
  190. repo_to=repo,
  191. branch_to="master",
  192. title="test pull-request",
  193. user="pingou",
  194. )
  195. self.assertEqual(req.id, 1)
  196. self.assertEqual(req.title, "test pull-request")
  197. def test_get_pr_info(self):
  198. """Test pagure.ui.fork._get_pr_info"""
  199. gitrepo = os.path.join(self.path, "repos", "test.git")
  200. gitrepo2 = os.path.join(
  201. self.path, "repos", "forks", "pingou", "test.git"
  202. )
  203. diff, diff_commits, orig_commit = pagure.lib.git.get_diff_info(
  204. repo_obj=PagureRepo(gitrepo2),
  205. orig_repo=PagureRepo(gitrepo),
  206. branch_from="feature_foo",
  207. branch_to="master",
  208. )
  209. self.assertEqual(len(diff_commits), 2)
  210. self.assertEqual(
  211. diff_commits[0].message,
  212. "Second edit on side branch of the file sources for testing",
  213. )
  214. self.assertEqual(
  215. diff_commits[1].message,
  216. "New edition on side branch of the file sources for testing",
  217. )
  218. self.assertEqual(
  219. orig_commit.message, "Editing the file sources for testing #5"
  220. )
  221. def test_get_pr_info_raises(self):
  222. """Test pagure.ui.fork._get_pr_info"""
  223. gitrepo = os.path.join(self.path, "repos", "test.git")
  224. gitrepo2 = os.path.join(
  225. self.path, "repos", "forks", "pingou", "test.git"
  226. )
  227. self.assertRaises(
  228. pagure.exceptions.BranchNotFoundException,
  229. pagure.lib.git.get_diff_info,
  230. repo_obj=PagureRepo(gitrepo2),
  231. orig_repo=PagureRepo(gitrepo),
  232. branch_from="feature",
  233. branch_to="master",
  234. )
  235. self.assertRaises(
  236. pagure.exceptions.BranchNotFoundException,
  237. pagure.lib.git.get_diff_info,
  238. repo_obj=PagureRepo(gitrepo2),
  239. orig_repo=PagureRepo(gitrepo),
  240. branch_from="feature_foo",
  241. branch_to="bar",
  242. )
  243. def test_diff_pull_request(self):
  244. """Test pagure.lib.git.diff_pull_request"""
  245. gitrepo = os.path.join(self.path, "repos", "test.git")
  246. gitrepo2 = os.path.join(
  247. self.path, "repos", "forks", "pingou", "test.git"
  248. )
  249. request = pagure.lib.query.search_pull_requests(
  250. self.session, requestid=1, project_id=1
  251. )
  252. diff_commits, diff = pagure.lib.git.diff_pull_request(
  253. self.session,
  254. request=request,
  255. repo_obj=PagureRepo(gitrepo2),
  256. orig_repo=PagureRepo(gitrepo),
  257. with_diff=True,
  258. )
  259. self.assertEqual(len(diff_commits), 2)
  260. self.assertEqual(
  261. diff_commits[0].message,
  262. "Second edit on side branch of the file sources for testing",
  263. )
  264. self.assertEqual(
  265. diff_commits[1].message,
  266. "New edition on side branch of the file sources for testing",
  267. )
  268. # Check that the PR has its PR refs
  269. # we don't know the task id but we'll give it 30 sec to finish
  270. cnt = 0
  271. repo = PagureRepo(gitrepo)
  272. self.assertIn("refs/pull/1/head", list(repo.listall_references()))
  273. self.assertTrue(cnt < 60)
  274. pr_ref = repo.lookup_reference("refs/pull/1/head")
  275. commit = pr_ref.peel()
  276. self.assertEqual(commit.oid.hex, diff_commits[0].oid.hex)
  277. @patch.dict(
  278. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  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. )
  288. request = pagure.lib.query.search_pull_requests(
  289. self.session, requestid=1, project_id=1
  290. )
  291. # Get the diff corresponding to the PR and check its ref
  292. diff_commits, diff = pagure.lib.git.diff_pull_request(
  293. self.session,
  294. request=request,
  295. repo_obj=PagureRepo(gitrepo2),
  296. orig_repo=PagureRepo(gitrepo),
  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. self.assertIn("refs/pull/1/head", list(repo.listall_references()))
  305. self.assertTrue(cnt < 60)
  306. pr_ref = repo.lookup_reference("refs/pull/1/head")
  307. commit = pr_ref.peel()
  308. self.assertEqual(commit.oid.hex, diff_commits[0].oid.hex)
  309. # Add a new commit on the fork
  310. repopath = os.path.join(self.path, "pingou_test2")
  311. clone_repo = pygit2.clone_repository(
  312. gitrepo2, repopath, checkout_branch="feature_foo"
  313. )
  314. with open(os.path.join(repopath, "sources"), "w") as stream:
  315. stream.write("foo\n bar\nbaz\nhey there\n")
  316. clone_repo.index.add("sources")
  317. clone_repo.index.write()
  318. last_commit = clone_repo.lookup_branch("feature_foo").peel()
  319. # Commits the files added
  320. tree = clone_repo.index.write_tree()
  321. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  322. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  323. last_commit = clone_repo.create_commit(
  324. "refs/heads/feature_foo", # the name of the reference to update
  325. author,
  326. committer,
  327. "Third edit on side branch of the file sources for testing",
  328. # binary string representing the tree object ID
  329. tree,
  330. # list of binary strings representing parents of the new commit
  331. [last_commit.oid.hex],
  332. )
  333. # Push to the fork repo
  334. ori_remote = clone_repo.remotes[0]
  335. refname = "refs/heads/feature_foo:refs/heads/feature_foo"
  336. PagureRepo.push(ori_remote, refname)
  337. # Get the new diff for that PR and check its new ref
  338. with testing.mock_sends(
  339. pagure_messages.PullRequestUpdatedV1(
  340. topic="pagure.pull-request.updated",
  341. body={
  342. "pullrequest": {
  343. "id": 1,
  344. "uid": ANY,
  345. "full_url": "http://localhost.localdomain/test/pull-request/1",
  346. "title": "test pull-request",
  347. "branch": "master",
  348. "project": {
  349. "id": 1,
  350. "name": "test",
  351. "full_url": "http://localhost.localdomain/test",
  352. "fullname": "test",
  353. "url_path": "test",
  354. "description": "test project #1",
  355. "namespace": None,
  356. "parent": None,
  357. "date_created": ANY,
  358. "date_modified": ANY,
  359. "user": {
  360. "name": "pingou",
  361. "fullname": "PY C",
  362. "url_path": "user/pingou",
  363. "full_url": "http://localhost.localdomain/user/pingou",
  364. },
  365. "access_users": {
  366. "owner": ["pingou"],
  367. "admin": [],
  368. "commit": [],
  369. "collaborator": [],
  370. "ticket": [],
  371. },
  372. "access_groups": {
  373. "admin": [],
  374. "commit": [],
  375. "collaborator": [],
  376. "ticket": [],
  377. },
  378. "tags": [],
  379. "priorities": {},
  380. "custom_keys": [],
  381. "close_status": [
  382. "Invalid",
  383. "Insufficient data",
  384. "Fixed",
  385. "Duplicate",
  386. ],
  387. "milestones": {},
  388. },
  389. "branch_from": "feature_foo",
  390. "repo_from": {
  391. "id": 2,
  392. "name": "test",
  393. "fullname": "forks/pingou/test",
  394. "url_path": "fork/pingou/test",
  395. "full_url": "http://localhost.localdomain/fork/pingou/test",
  396. "description": "test project #1",
  397. "namespace": None,
  398. "parent": {
  399. "id": 1,
  400. "name": "test",
  401. "fullname": "test",
  402. "full_url": "http://localhost.localdomain/test",
  403. "url_path": "test",
  404. "description": "test project #1",
  405. "namespace": None,
  406. "parent": None,
  407. "date_created": ANY,
  408. "date_modified": ANY,
  409. "user": {
  410. "name": "pingou",
  411. "fullname": "PY C",
  412. "url_path": "user/pingou",
  413. "full_url": "http://localhost.localdomain/user/pingou",
  414. },
  415. "access_users": {
  416. "owner": ["pingou"],
  417. "admin": [],
  418. "commit": [],
  419. "collaborator": [],
  420. "ticket": [],
  421. },
  422. "access_groups": {
  423. "admin": [],
  424. "commit": [],
  425. "collaborator": [],
  426. "ticket": [],
  427. },
  428. "tags": [],
  429. "priorities": {},
  430. "custom_keys": [],
  431. "close_status": [
  432. "Invalid",
  433. "Insufficient data",
  434. "Fixed",
  435. "Duplicate",
  436. ],
  437. "milestones": {},
  438. },
  439. "date_created": ANY,
  440. "date_modified": ANY,
  441. "user": {
  442. "name": "pingou",
  443. "fullname": "PY C",
  444. "url_path": "user/pingou",
  445. "full_url": "http://localhost.localdomain/user/pingou",
  446. },
  447. "access_users": {
  448. "owner": ["pingou"],
  449. "admin": [],
  450. "commit": [],
  451. "collaborator": [],
  452. "ticket": [],
  453. },
  454. "access_groups": {
  455. "admin": [],
  456. "commit": [],
  457. "collaborator": [],
  458. "ticket": [],
  459. },
  460. "tags": [],
  461. "priorities": {},
  462. "custom_keys": [],
  463. "close_status": [
  464. "Invalid",
  465. "Insufficient data",
  466. "Fixed",
  467. "Duplicate",
  468. ],
  469. "milestones": {},
  470. },
  471. "remote_git": None,
  472. "date_created": ANY,
  473. "updated_on": ANY,
  474. "last_updated": ANY,
  475. "closed_at": None,
  476. "user": {
  477. "name": "pingou",
  478. "fullname": "PY C",
  479. "url_path": "user/pingou",
  480. "full_url": "http://localhost.localdomain/user/pingou",
  481. },
  482. "assignee": None,
  483. "status": "Open",
  484. "commit_start": ANY,
  485. "commit_stop": ANY,
  486. "closed_by": None,
  487. "initial_comment": None,
  488. "cached_merge_status": "unknown",
  489. "threshold_reached": None,
  490. "tags": [],
  491. "comments": [],
  492. },
  493. "agent": "pagure",
  494. },
  495. ),
  496. pagure_messages.PullRequestCommentAddedV1(
  497. topic="pagure.pull-request.comment.added",
  498. body={
  499. "pullrequest": {
  500. "id": 1,
  501. "full_url": "http://localhost.localdomain/test/pull-request/1",
  502. "uid": ANY,
  503. "title": "test pull-request",
  504. "branch": "master",
  505. "project": {
  506. "id": 1,
  507. "name": "test",
  508. "fullname": "test",
  509. "full_url": "http://localhost.localdomain/test",
  510. "url_path": "test",
  511. "description": "test project #1",
  512. "namespace": None,
  513. "parent": None,
  514. "date_created": ANY,
  515. "date_modified": ANY,
  516. "user": {
  517. "name": "pingou",
  518. "fullname": "PY C",
  519. "url_path": "user/pingou",
  520. "full_url": "http://localhost.localdomain/user/pingou",
  521. },
  522. "access_users": {
  523. "owner": ["pingou"],
  524. "admin": [],
  525. "commit": [],
  526. "collaborator": [],
  527. "ticket": [],
  528. },
  529. "access_groups": {
  530. "admin": [],
  531. "commit": [],
  532. "collaborator": [],
  533. "ticket": [],
  534. },
  535. "tags": [],
  536. "priorities": {},
  537. "custom_keys": [],
  538. "close_status": [
  539. "Invalid",
  540. "Insufficient data",
  541. "Fixed",
  542. "Duplicate",
  543. ],
  544. "milestones": {},
  545. },
  546. "branch_from": "feature_foo",
  547. "repo_from": {
  548. "id": 2,
  549. "name": "test",
  550. "full_url": "http://localhost.localdomain/fork/pingou/test",
  551. "fullname": "forks/pingou/test",
  552. "url_path": "fork/pingou/test",
  553. "description": "test project #1",
  554. "namespace": None,
  555. "parent": {
  556. "id": 1,
  557. "name": "test",
  558. "full_url": "http://localhost.localdomain/test",
  559. "fullname": "test",
  560. "url_path": "test",
  561. "description": "test project #1",
  562. "namespace": None,
  563. "parent": None,
  564. "date_created": ANY,
  565. "date_modified": ANY,
  566. "user": {
  567. "name": "pingou",
  568. "fullname": "PY C",
  569. "url_path": "user/pingou",
  570. "full_url": "http://localhost.localdomain/user/pingou",
  571. },
  572. "access_users": {
  573. "owner": ["pingou"],
  574. "admin": [],
  575. "commit": [],
  576. "collaborator": [],
  577. "ticket": [],
  578. },
  579. "access_groups": {
  580. "admin": [],
  581. "commit": [],
  582. "collaborator": [],
  583. "ticket": [],
  584. },
  585. "tags": [],
  586. "priorities": {},
  587. "custom_keys": [],
  588. "close_status": [
  589. "Invalid",
  590. "Insufficient data",
  591. "Fixed",
  592. "Duplicate",
  593. ],
  594. "milestones": {},
  595. },
  596. "date_created": ANY,
  597. "date_modified": ANY,
  598. "user": {
  599. "name": "pingou",
  600. "fullname": "PY C",
  601. "url_path": "user/pingou",
  602. "full_url": "http://localhost.localdomain/user/pingou",
  603. },
  604. "access_users": {
  605. "owner": ["pingou"],
  606. "admin": [],
  607. "commit": [],
  608. "collaborator": [],
  609. "ticket": [],
  610. },
  611. "access_groups": {
  612. "admin": [],
  613. "commit": [],
  614. "collaborator": [],
  615. "ticket": [],
  616. },
  617. "tags": [],
  618. "priorities": {},
  619. "custom_keys": [],
  620. "close_status": [
  621. "Invalid",
  622. "Insufficient data",
  623. "Fixed",
  624. "Duplicate",
  625. ],
  626. "milestones": {},
  627. },
  628. "remote_git": None,
  629. "date_created": ANY,
  630. "updated_on": ANY,
  631. "last_updated": ANY,
  632. "closed_at": None,
  633. "user": {
  634. "name": "pingou",
  635. "fullname": "PY C",
  636. "url_path": "user/pingou",
  637. "full_url": "http://localhost.localdomain/user/pingou",
  638. },
  639. "assignee": None,
  640. "status": "Open",
  641. "commit_start": ANY,
  642. "commit_stop": ANY,
  643. "closed_by": None,
  644. "initial_comment": None,
  645. "cached_merge_status": "unknown",
  646. "threshold_reached": None,
  647. "tags": [],
  648. "comments": [
  649. {
  650. "id": 1,
  651. "commit": None,
  652. "tree": None,
  653. "filename": None,
  654. "line": None,
  655. "comment": "**1 new commit added**\n\n "
  656. "* ``Third edit on side branch of the file "
  657. "sources for testing``\n",
  658. "parent": None,
  659. "date_created": ANY,
  660. "user": {
  661. "name": "pingou",
  662. "fullname": "PY C",
  663. "url_path": "user/pingou",
  664. "full_url": "http://localhost.localdomain/user/pingou",
  665. },
  666. "edited_on": None,
  667. "editor": None,
  668. "notification": True,
  669. "reactions": {},
  670. }
  671. ],
  672. },
  673. "agent": "pingou",
  674. },
  675. ),
  676. ):
  677. diff_commits, diff = pagure.lib.git.diff_pull_request(
  678. self.session,
  679. request=request,
  680. repo_obj=PagureRepo(gitrepo2),
  681. orig_repo=PagureRepo(gitrepo),
  682. with_diff=True,
  683. )
  684. self.assertEqual(len(diff_commits), 3)
  685. # Check that the PR has its PR refs
  686. # we don't know the task id but we'll give it 30 sec to finish
  687. cnt = 0
  688. repo = PagureRepo(gitrepo)
  689. self.assertIn("refs/pull/1/head", list(repo.listall_references()))
  690. self.assertTrue(cnt < 60)
  691. pr_ref = repo.lookup_reference("refs/pull/1/head")
  692. commit2 = pr_ref.peel()
  693. self.assertEqual(commit2.oid.hex, diff_commits[0].oid.hex)
  694. self.assertNotEqual(commit.oid.hex, commit2.oid.hex)
  695. def test_two_diff_pull_request_sequentially(self):
  696. """Test calling pagure.lib.git.diff_pull_request twice returns
  697. the same data
  698. """
  699. gitrepo = os.path.join(self.path, "repos", "test.git")
  700. gitrepo2 = os.path.join(
  701. self.path, "repos", "forks", "pingou", "test.git"
  702. )
  703. request = pagure.lib.query.search_pull_requests(
  704. self.session, requestid=1, project_id=1
  705. )
  706. # Get the diff corresponding to the PR and check its ref
  707. diff_commits, diff = pagure.lib.git.diff_pull_request(
  708. self.session,
  709. request=request,
  710. repo_obj=PagureRepo(gitrepo2),
  711. orig_repo=PagureRepo(gitrepo),
  712. with_diff=True,
  713. )
  714. self.assertEqual(len(diff_commits), 2)
  715. # Check that the PR has its PR refs
  716. # we don't know the task id but we'll give it 30 sec to finish
  717. cnt = 0
  718. repo = PagureRepo(gitrepo)
  719. self.assertIn("refs/pull/1/head", list(repo.listall_references()))
  720. self.assertTrue(cnt < 60)
  721. pr_ref = repo.lookup_reference("refs/pull/1/head")
  722. commit = pr_ref.peel()
  723. self.assertEqual(commit.oid.hex, diff_commits[0].oid.hex)
  724. # Run diff_pull_request a second time
  725. diff_commits2, diff = pagure.lib.git.diff_pull_request(
  726. self.session,
  727. request=request,
  728. repo_obj=PagureRepo(gitrepo2),
  729. orig_repo=PagureRepo(gitrepo),
  730. with_diff=True,
  731. )
  732. self.assertEqual(len(diff_commits2), 2)
  733. self.assertEqual(
  734. [d.oid.hex for d in diff_commits2],
  735. [d.oid.hex for d in diff_commits],
  736. )
  737. # Check that the PR has its PR refs
  738. # we don't know the task id but we'll give it 30 sec to finish
  739. cnt = 0
  740. repo = PagureRepo(gitrepo)
  741. self.assertIn("refs/pull/1/head", list(repo.listall_references()))
  742. self.assertTrue(cnt < 60)
  743. pr_ref = repo.lookup_reference("refs/pull/1/head")
  744. commit2 = pr_ref.peel()
  745. self.assertEqual(commit2.oid.hex, diff_commits[0].oid.hex)
  746. self.assertEqual(commit.oid.hex, commit2.oid.hex)
  747. if __name__ == "__main__":
  748. unittest.main(verbosity=2)