test_pagure_lib_git.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import tempfile
  15. import pygit2
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib.git
  20. import tests
  21. from pagure.lib.repo import PagureRepo
  22. class PagureLibGittests(tests.Modeltests):
  23. """ Tests for pagure.lib.git """
  24. def setUp(self):
  25. """ Set up the environnment, ran before every tests. """
  26. super(PagureLibGittests, self).setUp()
  27. pagure.lib.git.SESSION = self.session
  28. pagure.APP.config['GIT_FOLDER'] = os.path.join(
  29. self.path, 'repos')
  30. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  31. self.path, 'forks')
  32. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  33. self.path, 'tickets')
  34. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  35. self.path, 'docs')
  36. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  37. self.path, 'requests')
  38. def test_write_gitolite_acls(self):
  39. """ Test the write_gitolite_acls function of pagure.lib.git. """
  40. tests.create_projects(self.session)
  41. repo = pagure.lib.get_project(self.session, 'test')
  42. # Add an user to a project
  43. msg = pagure.lib.add_user_to_project(
  44. session=self.session,
  45. project=repo,
  46. new_user='foo',
  47. user='pingou',
  48. )
  49. self.session.commit()
  50. self.assertEqual(msg, 'User added')
  51. # Add a forked project
  52. item = pagure.lib.model.Project(
  53. user_id=1, # pingou
  54. name='test3',
  55. description='test project #2',
  56. is_fork=True,
  57. parent_id=1,
  58. hook_token='aaabbbvvv',
  59. )
  60. self.session.add(item)
  61. self.session.commit()
  62. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  63. pagure.lib.git.write_gitolite_acls(self.session, outputconf)
  64. self.assertTrue(os.path.exists(outputconf))
  65. with open(outputconf) as stream:
  66. data = stream.read()
  67. exp = """
  68. repo test
  69. R = @all
  70. RW+ = pingou
  71. RW+ = foo
  72. repo docs/test
  73. R = @all
  74. RW+ = pingou
  75. RW+ = foo
  76. repo tickets/test
  77. RW+ = pingou
  78. RW+ = foo
  79. repo requests/test
  80. RW+ = pingou
  81. RW+ = foo
  82. repo test2
  83. R = @all
  84. RW+ = pingou
  85. repo docs/test2
  86. R = @all
  87. RW+ = pingou
  88. repo tickets/test2
  89. RW+ = pingou
  90. repo requests/test2
  91. RW+ = pingou
  92. repo forks/pingou/test3
  93. R = @all
  94. RW+ = pingou
  95. repo docs/forks/pingou/test3
  96. R = @all
  97. RW+ = pingou
  98. repo tickets/forks/pingou/test3
  99. RW+ = pingou
  100. repo requests/forks/pingou/test3
  101. RW+ = pingou
  102. """
  103. #print data
  104. self.assertEqual(data, exp)
  105. os.unlink(outputconf)
  106. self.assertFalse(os.path.exists(outputconf))
  107. def test_write_gitolite_acls_groups(self):
  108. """ Test the write_gitolite_acls function of pagure.lib.git with
  109. groups.
  110. """
  111. tests.create_projects(self.session)
  112. repo = pagure.lib.get_project(self.session, 'test')
  113. # Add a couple of groups
  114. msg = pagure.lib.add_group(
  115. self.session,
  116. group_name='sysadmin',
  117. display_name='sysadmin group',
  118. description=None,
  119. group_type='user',
  120. user='pingou',
  121. is_admin=False,
  122. blacklist=[],
  123. )
  124. self.session.commit()
  125. self.assertEqual(msg, 'User `pingou` added to the group `sysadmin`.')
  126. msg = pagure.lib.add_group(
  127. self.session,
  128. group_name='devs',
  129. display_name='devs group',
  130. description=None,
  131. group_type='user',
  132. user='pingou',
  133. is_admin=False,
  134. blacklist=[],
  135. )
  136. self.session.commit()
  137. self.assertEqual(msg, 'User `pingou` added to the group `devs`.')
  138. # Associate these groups to a project
  139. msg = pagure.lib.add_group_to_project(
  140. session=self.session,
  141. project=repo,
  142. new_group='sysadmin',
  143. user='pingou',
  144. )
  145. self.session.commit()
  146. self.assertEqual(msg, 'Group added')
  147. msg = pagure.lib.add_group_to_project(
  148. session=self.session,
  149. project=repo,
  150. new_group='devs',
  151. user='pingou',
  152. )
  153. self.session.commit()
  154. self.assertEqual(msg, 'Group added')
  155. # Add an user to a project
  156. msg = pagure.lib.add_user_to_project(
  157. session=self.session,
  158. project=repo,
  159. new_user='foo',
  160. user='pingou',
  161. )
  162. self.session.commit()
  163. self.assertEqual(msg, 'User added')
  164. # Add a forked project
  165. item = pagure.lib.model.Project(
  166. user_id=1, # pingou
  167. name='test2',
  168. description='test project #2',
  169. is_fork=True,
  170. parent_id=1,
  171. hook_token='aaabbbvvv',
  172. )
  173. self.session.add(item)
  174. self.session.commit()
  175. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  176. pagure.lib.git.write_gitolite_acls(self.session, outputconf)
  177. self.assertTrue(os.path.exists(outputconf))
  178. with open(outputconf) as stream:
  179. data = stream.read()
  180. exp = """@sysadmin = pingou
  181. @devs = pingou
  182. repo test
  183. R = @all
  184. RW+ = @sysadmin @devs
  185. RW+ = pingou
  186. RW+ = foo
  187. repo docs/test
  188. R = @all
  189. RW+ = @sysadmin @devs
  190. RW+ = pingou
  191. RW+ = foo
  192. repo tickets/test
  193. RW+ = @sysadmin @devs
  194. RW+ = pingou
  195. RW+ = foo
  196. repo requests/test
  197. RW+ = @sysadmin @devs
  198. RW+ = pingou
  199. RW+ = foo
  200. repo test2
  201. R = @all
  202. RW+ = pingou
  203. repo docs/test2
  204. R = @all
  205. RW+ = pingou
  206. repo tickets/test2
  207. RW+ = pingou
  208. repo requests/test2
  209. RW+ = pingou
  210. repo forks/pingou/test2
  211. R = @all
  212. RW+ = pingou
  213. repo docs/forks/pingou/test2
  214. R = @all
  215. RW+ = pingou
  216. repo tickets/forks/pingou/test2
  217. RW+ = pingou
  218. repo requests/forks/pingou/test2
  219. RW+ = pingou
  220. """
  221. #print data
  222. self.assertEqual(data.split('\n'), exp.split('\n'))
  223. os.unlink(outputconf)
  224. self.assertFalse(os.path.exists(outputconf))
  225. def test_commit_to_patch(self):
  226. """ Test the commit_to_patch function of pagure.lib.git. """
  227. # Create a git repo to play with
  228. self.gitrepo = os.path.join(self.path, 'test_repo.git')
  229. os.makedirs(self.gitrepo)
  230. repo = pygit2.init_repository(self.gitrepo)
  231. # Create a file in that git repo
  232. with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream:
  233. stream.write('foo\n bar')
  234. repo.index.add('sources')
  235. repo.index.write()
  236. # Commits the files added
  237. tree = repo.index.write_tree()
  238. author = pygit2.Signature(
  239. 'Alice Author', 'alice@authors.tld')
  240. committer = pygit2.Signature(
  241. 'Cecil Committer', 'cecil@committers.tld')
  242. repo.create_commit(
  243. 'refs/heads/master', # the name of the reference to update
  244. author,
  245. committer,
  246. 'Add sources file for testing',
  247. # binary string representing the tree object ID
  248. tree,
  249. # list of binary strings representing parents of the new commit
  250. []
  251. )
  252. first_commit = repo.revparse_single('HEAD')
  253. # Edit the sources file again
  254. with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream:
  255. stream.write('foo\n bar\nbaz\n boose')
  256. repo.index.add('sources')
  257. repo.index.write()
  258. # Commits the files added
  259. tree = repo.index.write_tree()
  260. author = pygit2.Signature(
  261. 'Alice Author', 'alice@authors.tld')
  262. committer = pygit2.Signature(
  263. 'Cecil Committer', 'cecil@committers.tld')
  264. repo.create_commit(
  265. 'refs/heads/master', # the name of the reference to update
  266. author,
  267. committer,
  268. 'Add baz and boose to the sources\n\n There are more objects to '
  269. 'consider',
  270. # binary string representing the tree object ID
  271. tree,
  272. # list of binary strings representing parents of the new commit
  273. [first_commit.oid.hex]
  274. )
  275. second_commit = repo.revparse_single('HEAD')
  276. # Generate a patch for 2 commits
  277. patch = pagure.lib.git.commit_to_patch(
  278. repo, [first_commit, second_commit])
  279. exp = """Mon Sep 17 00:00:00 2001
  280. From: Alice Author <alice@authors.tld>
  281. Subject: [PATCH 1/2] Add sources file for testing
  282. ---
  283. diff --git a/sources b/sources
  284. new file mode 100644
  285. index 0000000..9f44358
  286. --- /dev/null
  287. +++ b/sources
  288. @@ -0,0 +1,2 @@
  289. +foo
  290. + bar
  291. \ No newline at end of file
  292. Mon Sep 17 00:00:00 2001
  293. From: Alice Author <alice@authors.tld>
  294. Subject: [PATCH 2/2] Add baz and boose to the sources
  295. There are more objects to consider
  296. ---
  297. diff --git a/sources b/sources
  298. index 9f44358..2a552bb 100644
  299. --- a/sources
  300. +++ b/sources
  301. @@ -1,2 +1,4 @@
  302. foo
  303. - bar
  304. \ No newline at end of file
  305. + bar
  306. +baz
  307. + boose
  308. \ No newline at end of file
  309. """
  310. npatch = []
  311. for row in patch.split('\n'):
  312. if row.startswith('Date:'):
  313. continue
  314. if row.startswith('From '):
  315. row = row.split(' ', 2)[2]
  316. npatch.append(row)
  317. patch = '\n'.join(npatch)
  318. self.assertEqual(patch, exp)
  319. # Generate a patch for a single commit
  320. patch = pagure.lib.git.commit_to_patch(repo, second_commit)
  321. exp = """Mon Sep 17 00:00:00 2001
  322. From: Alice Author <alice@authors.tld>
  323. Subject: Add baz and boose to the sources
  324. There are more objects to consider
  325. ---
  326. diff --git a/sources b/sources
  327. index 9f44358..2a552bb 100644
  328. --- a/sources
  329. +++ b/sources
  330. @@ -1,2 +1,4 @@
  331. foo
  332. - bar
  333. \ No newline at end of file
  334. + bar
  335. +baz
  336. + boose
  337. \ No newline at end of file
  338. """
  339. npatch = []
  340. for row in patch.split('\n'):
  341. if row.startswith('Date:'):
  342. continue
  343. if row.startswith('From '):
  344. row = row.split(' ', 2)[2]
  345. npatch.append(row)
  346. patch = '\n'.join(npatch)
  347. self.assertEqual(patch, exp)
  348. @patch('pagure.lib.notify.send_email')
  349. def test_update_git(self, email_f):
  350. """ Test the update_git of pagure.lib.git. """
  351. email_f.return_value = True
  352. # Create project
  353. item = pagure.lib.model.Project(
  354. user_id=1, # pingou
  355. name='test_ticket_repo',
  356. description='test project for ticket',
  357. hook_token='aaabbbwww',
  358. )
  359. self.session.add(item)
  360. self.session.commit()
  361. # Create repo
  362. self.gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  363. os.makedirs(self.gitrepo)
  364. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  365. repo = pagure.lib.get_project(self.session, 'test_ticket_repo')
  366. # Create an issue to play with
  367. msg = pagure.lib.new_issue(
  368. session=self.session,
  369. repo=repo,
  370. title='Test issue',
  371. content='We should work on this',
  372. user='pingou',
  373. ticketfolder=self.path
  374. )
  375. self.assertEqual(msg.title, 'Test issue')
  376. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  377. pagure.lib.git.update_git(issue, repo, self.path)
  378. repo = pygit2.Repository(self.gitrepo)
  379. commit = repo.revparse_single('HEAD')
  380. # Use patch to validate the repo
  381. patch = pagure.lib.git.commit_to_patch(repo, commit)
  382. exp = """Mon Sep 17 00:00:00 2001
  383. From: pagure <pagure>
  384. Subject: Updated issue <hash>: Test issue
  385. ---
  386. diff --git a/123 b/456
  387. new file mode 100644
  388. index 0000000..60f7480
  389. --- /dev/null
  390. +++ b/456
  391. @@ -0,0 +1,25 @@
  392. +{
  393. + "assignee": null,
  394. + "blocks": [],
  395. + "closed_at": null,
  396. + "comments": [],
  397. + "content": "We should work on this",
  398. + "date_created": null,
  399. + "depends": [],
  400. + "id": 1,
  401. + "milestone": null,
  402. + "priority": null,
  403. + "private": false,
  404. + "status": "Open",
  405. + "tags": [],
  406. + "title": "Test issue",
  407. + "user": {
  408. + "default_email": "bar@pingou.com",
  409. + "emails": [
  410. + "bar@pingou.com",
  411. + "foo@pingou.com"
  412. + ],
  413. + "fullname": "PY C",
  414. + "name": "pingou"
  415. + }
  416. +}
  417. \ No newline at end of file
  418. """
  419. npatch = []
  420. for row in patch.split('\n'):
  421. if row.startswith('Date:'):
  422. continue
  423. elif row.startswith('From '):
  424. row = row.split(' ', 2)[2]
  425. elif row.startswith('diff --git '):
  426. row = row.split(' ')
  427. row[2] = 'a/123'
  428. row[3] = 'b/456'
  429. row = ' '.join(row)
  430. elif 'Updated issue' in row:
  431. row = row.split()
  432. row[3] = '<hash>:'
  433. row = ' '.join(row)
  434. elif 'date_created' in row:
  435. t = row.split(': ')[0]
  436. row = '%s: null,' % t
  437. elif 'closed_at' in row:
  438. t = row.split(': ')[0]
  439. row = '%s: null,' % t
  440. elif row.startswith('index 00'):
  441. row = 'index 0000000..60f7480'
  442. elif row.startswith('+++ b/'):
  443. row = '+++ b/456'
  444. npatch.append(row)
  445. patch = '\n'.join(npatch)
  446. #print patch
  447. self.assertEqual(patch, exp)
  448. # Test again after adding a comment
  449. msg = pagure.lib.add_issue_comment(
  450. session=self.session,
  451. issue=issue,
  452. comment='Hey look a comment!',
  453. user='foo',
  454. ticketfolder=self.path
  455. )
  456. self.session.commit()
  457. self.assertEqual(msg, 'Comment added')
  458. # Use patch to validate the repo
  459. repo = pygit2.Repository(self.gitrepo)
  460. commit = repo.revparse_single('HEAD')
  461. patch = pagure.lib.git.commit_to_patch(repo, commit)
  462. exp = """Mon Sep 17 00:00:00 2001
  463. From: pagure <pagure>
  464. Subject: Updated issue <hash>: Test issue
  465. ---
  466. diff --git a/123 b/456
  467. index 458821a..77674a8
  468. --- a/123
  469. +++ b/456
  470. @@ -2,7 +2,25 @@
  471. "assignee": null,
  472. "blocks": [],
  473. "closed_at": null,
  474. - "comments": [],
  475. + "comments": [
  476. + {
  477. + "comment": "Hey look a comment!",
  478. + "date_created": null,
  479. + "edited_on": null,
  480. + "editor": null,
  481. + "id": 1,
  482. + "notification": false,
  483. + "parent": null,
  484. + "user": {
  485. + "default_email": "foo@bar.com",
  486. + "emails": [
  487. + "foo@bar.com"
  488. + ],
  489. + "fullname": "foo bar",
  490. + "name": "foo"
  491. + }
  492. + }
  493. + ],
  494. "content": "We should work on this",
  495. "date_created": null,
  496. "depends": [],
  497. """
  498. npatch = []
  499. for row in patch.split('\n'):
  500. if row.startswith('Date:'):
  501. continue
  502. elif row.startswith('From '):
  503. row = row.split(' ', 2)[2]
  504. elif row.startswith('diff --git '):
  505. row = row.split(' ')
  506. row[2] = 'a/123'
  507. row[3] = 'b/456'
  508. row = ' '.join(row)
  509. elif 'Updated issue' in row:
  510. row = row.split()
  511. row[3] = '<hash>:'
  512. row = ' '.join(row)
  513. elif 'date_created' in row:
  514. t = row.split(': ')[0]
  515. row = '%s: null,' % t
  516. elif 'closed_at' in row:
  517. t = row.split(': ')[0]
  518. row = '%s: null,' % t
  519. elif row.startswith('index'):
  520. row = 'index 458821a..77674a8'
  521. elif row.startswith('--- a/'):
  522. row = '--- a/123'
  523. elif row.startswith('+++ b/'):
  524. row = '+++ b/456'
  525. npatch.append(row)
  526. patch = '\n'.join(npatch)
  527. #print patch
  528. self.assertEqual(patch, exp)
  529. def test_clean_git(self):
  530. """ Test the clean_git method of pagure.lib.git. """
  531. pagure.lib.git.clean_git(None, None, None)
  532. self.test_update_git()
  533. gitpath = os.path.join(self.path, 'test_ticket_repo.git')
  534. gitrepo = pygit2.init_repository(gitpath, bare=True)
  535. # Get the uid of the ticket created
  536. commit = gitrepo.revparse_single('HEAD')
  537. patch = pagure.lib.git.commit_to_patch(gitrepo, commit)
  538. hash_file = None
  539. for row in patch.split('\n'):
  540. if row.startswith('+++ b/'):
  541. hash_file = row.split('+++ b/')[-1]
  542. break
  543. # The only file in git is the one of that ticket
  544. files = [entry.name for entry in commit.tree]
  545. self.assertEqual(files, [hash_file])
  546. repo = pagure.lib.get_project(self.session, 'test_ticket_repo')
  547. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  548. pagure.lib.git.clean_git(issue, repo, self.path)
  549. # No more files in the git repo
  550. commit = gitrepo.revparse_single('HEAD')
  551. files = [entry.name for entry in commit.tree]
  552. self.assertEqual(files, [])
  553. @patch('pagure.lib.notify.send_email')
  554. def test_update_git_requests(self, email_f):
  555. """ Test the update_git of pagure.lib.git for pull-requests. """
  556. email_f.return_value = True
  557. # Create project
  558. item = pagure.lib.model.Project(
  559. user_id=1, # pingou
  560. name='test_ticket_repo',
  561. description='test project for ticket',
  562. hook_token='aaabbbxxx',
  563. )
  564. self.session.add(item)
  565. self.session.commit()
  566. # Create repo
  567. self.gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  568. os.makedirs(self.gitrepo)
  569. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  570. repo = pagure.lib.get_project(self.session, 'test_ticket_repo')
  571. # Create an issue to play with
  572. req = pagure.lib.new_pull_request(
  573. session=self.session,
  574. repo_from=repo,
  575. branch_from='feature',
  576. repo_to=repo,
  577. branch_to='master',
  578. title='test PR',
  579. user='pingou',
  580. requestfolder=self.path,
  581. requestuid='foobar',
  582. requestid=None,
  583. status='Open',
  584. notify=True
  585. )
  586. self.assertEqual(req.id, 1)
  587. self.assertEqual(req.title, 'test PR')
  588. request = repo.requests[0]
  589. self.assertEqual(request.title, 'test PR')
  590. pagure.lib.git.update_git(request, request.project, self.path)
  591. repo = pygit2.Repository(self.gitrepo)
  592. commit = repo.revparse_single('HEAD')
  593. # Use patch to validate the repo
  594. patch = pagure.lib.git.commit_to_patch(repo, commit)
  595. exp = """Mon Sep 17 00:00:00 2001
  596. From: pagure <pagure>
  597. Subject: Updated pull-request <hash>: test PR
  598. ---
  599. diff --git a/123 b/456
  600. new file mode 100644
  601. index 0000000..60f7480
  602. --- /dev/null
  603. +++ b/456
  604. @@ -0,0 +1,89 @@
  605. +{
  606. + "assignee": null,
  607. + "branch": "master",
  608. + "branch_from": "feature",
  609. + "closed_at": null,
  610. + "closed_by": null,
  611. + "comments": [],
  612. + "commit_start": null,
  613. + "commit_stop": null,
  614. + "date_created": null,
  615. + "id": 1,
  616. + "initial_comment": null,
  617. + "project": {
  618. + "date_created": null,
  619. + "description": "test project for ticket",
  620. + "id": 1,
  621. + "name": "test_ticket_repo",
  622. + "namespace": null,
  623. + "parent": null,
  624. + "priorities": {},
  625. + "settings": {
  626. + "Enforce_signed-off_commits_in_pull-request": false,
  627. + "Minimum_score_to_merge_pull-request": -1,
  628. + "Only_assignee_can_merge_pull-request": false,
  629. + "Web-hooks": null,
  630. + "always_merge": false,
  631. + "fedmsg_notifications": true,
  632. + "issue_tracker": true,
  633. + "issues_default_to_private": false,
  634. + "project_documentation": false,
  635. + "pull_requests": true
  636. + },
  637. + "tags": [],
  638. + "user": {
  639. + "default_email": "bar@pingou.com",
  640. + "emails": [
  641. + "bar@pingou.com",
  642. + "foo@pingou.com"
  643. + ],
  644. + "fullname": "PY C",
  645. + "name": "pingou"
  646. + }
  647. + },
  648. + "remote_git": null,
  649. + "repo_from": {
  650. + "date_created": null,
  651. + "description": "test project for ticket",
  652. + "id": 1,
  653. + "name": "test_ticket_repo",
  654. + "namespace": null,
  655. + "parent": null,
  656. + "priorities": {},
  657. + "settings": {
  658. + "Enforce_signed-off_commits_in_pull-request": false,
  659. + "Minimum_score_to_merge_pull-request": -1,
  660. + "Only_assignee_can_merge_pull-request": false,
  661. + "Web-hooks": null,
  662. + "always_merge": false,
  663. + "fedmsg_notifications": true,
  664. + "issue_tracker": true,
  665. + "issues_default_to_private": false,
  666. + "project_documentation": false,
  667. + "pull_requests": true
  668. + },
  669. + "tags": [],
  670. + "user": {
  671. + "default_email": "bar@pingou.com",
  672. + "emails": [
  673. + "bar@pingou.com",
  674. + "foo@pingou.com"
  675. + ],
  676. + "fullname": "PY C",
  677. + "name": "pingou"
  678. + }
  679. + },
  680. + "status": "Open",
  681. + "title": "test PR",
  682. + "uid": "foobar",
  683. + "updated_on": null,
  684. + "user": {
  685. + "default_email": "bar@pingou.com",
  686. + "emails": [
  687. + "bar@pingou.com",
  688. + "foo@pingou.com"
  689. + ],
  690. + "fullname": "PY C",
  691. + "name": "pingou"
  692. + }
  693. +}
  694. \ No newline at end of file
  695. """
  696. npatch = []
  697. for row in patch.split('\n'):
  698. if row.startswith('Date:'):
  699. continue
  700. elif row.startswith('From '):
  701. row = row.split(' ', 2)[2]
  702. elif row.startswith('diff --git '):
  703. row = row.split(' ')
  704. row[2] = 'a/123'
  705. row[3] = 'b/456'
  706. row = ' '.join(row)
  707. elif 'Updated pull-request' in row:
  708. row = row.split()
  709. row[3] = '<hash>:'
  710. row = ' '.join(row)
  711. elif 'date_created' in row:
  712. t = row.split(': ')[0]
  713. row = '%s: null,' % t
  714. elif 'updated_on' in row:
  715. t = row.split(': ')[0]
  716. row = '%s: null,' % t
  717. elif row.startswith('index 00'):
  718. row = 'index 0000000..60f7480'
  719. elif row.startswith('+++ b/'):
  720. row = '+++ b/456'
  721. npatch.append(row)
  722. patch = '\n'.join(npatch)
  723. #print patch
  724. self.assertEqual(patch, exp)
  725. def test_update_ticket_from_git(self):
  726. """ Test the update_ticket_from_git method from pagure.lib.git. """
  727. tests.create_projects(self.session)
  728. repo = pagure.lib.get_project(self.session, 'test')
  729. # Before
  730. self.assertEqual(len(repo.issues), 0)
  731. self.assertEqual(repo.issues, [])
  732. data = {
  733. "status": "Open", "title": "foo", "comments": [],
  734. "content": "bar", "date_created": "1426500263",
  735. "user": {
  736. "name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  737. }
  738. self.assertRaises(
  739. pagure.exceptions.PagureException,
  740. pagure.lib.git.update_ticket_from_git,
  741. self.session,
  742. reponame='foobar',
  743. namespace=None,
  744. username=None,
  745. issue_uid='foobar',
  746. json_data=data
  747. )
  748. pagure.lib.git.update_ticket_from_git(
  749. self.session, reponame='test', namespace=None, username=None,
  750. issue_uid='foobar', json_data=data
  751. )
  752. self.session.commit()
  753. # After 1 insertion
  754. self.assertEqual(len(repo.issues), 1)
  755. self.assertEqual(repo.issues[0].id, 1)
  756. self.assertEqual(repo.issues[0].uid, 'foobar')
  757. self.assertEqual(repo.issues[0].title, 'foo')
  758. self.assertEqual(repo.issues[0].depends_text, [])
  759. self.assertEqual(repo.issues[0].blocks_text, [])
  760. data["title"] = "fake issue for tests"
  761. pagure.lib.git.update_ticket_from_git(
  762. self.session, reponame='test', namespace=None, username=None,
  763. issue_uid='foobar', json_data=data
  764. )
  765. self.session.commit()
  766. # After edit
  767. self.assertEqual(len(repo.issues), 1)
  768. self.assertEqual(repo.issues[0].id, 1)
  769. self.assertEqual(repo.issues[0].uid, 'foobar')
  770. self.assertEqual(repo.issues[0].title, 'fake issue for tests')
  771. self.assertEqual(repo.issues[0].depends_text, [])
  772. self.assertEqual(repo.issues[0].blocks_text, [])
  773. data = {
  774. "status": "Open", "title": "Rename pagure", "private": False,
  775. "content": "This is too much of a conflict with the book",
  776. "user": {
  777. "fullname": "Pierre-YvesChibon",
  778. "name": "pingou",
  779. "default_email": "pingou@fedoraproject.org",
  780. "emails": ["pingou@fedoraproject.org"]
  781. },
  782. "id": 20,
  783. "blocks": [1],
  784. "depends": [3, 4],
  785. "date_created": "1426595224",
  786. "comments": [
  787. {
  788. "comment": "Nirik:\r\n\r\n- sourceforge++ \r\n- "
  789. "gitmaker\r\n- mastergit \r\n- hostomatic\r\n- "
  790. "gitcorp\r\n- git-keiretsu \r\n- gitbuffet\r\n- "
  791. "cogitator\r\n- cogitate\r\n\r\nrandomuser:\r\n\r\n- "
  792. "COLLABORATRON5000\r\n- git-sm\u00f6rg\u00e5sbord\r\n- "
  793. "thislittlegittywenttomarket\r\n- git-o-rama\r\n- "
  794. "gitsundheit",
  795. "date_created": "1426595224", "id": 250, "parent": None,
  796. "user": {
  797. "fullname": "Pierre-YvesChibon",
  798. "name": "pingou",
  799. "default_email": "pingou@fedoraproject.org",
  800. "emails": ["pingou@fedoraproject.org"]
  801. }
  802. },
  803. {
  804. "comment": "Nirik:\r\n\r\n- sourceforge++ \r\n- "
  805. "gitmaker\r\n- mastergit \r\n- hostomatic\r\n- "
  806. "gitcorp\r\n- git-keiretsu \r\n- gitbuffet\r\n- "
  807. "cogitator\r\n- cogitate\r\n\r\nrandomuser:\r\n\r\n- "
  808. "COLLABORATRON5000\r\n- git-sm\u00f6rg\u00e5sbord\r\n- "
  809. "thislittlegittywenttomarket\r\n- git-o-rama\r\n- "
  810. "gitsundheit",
  811. "date_created": "1426595340", "id": 324, "parent": None,
  812. "user": {
  813. "fullname": "Ralph Bean",
  814. "name": "ralph",
  815. "default_email": "ralph@fedoraproject.org",
  816. "emails": ["ralph@fedoraproject.org"]
  817. }
  818. }
  819. ]
  820. }
  821. pagure.lib.git.update_ticket_from_git(
  822. self.session, reponame='test', namespace=None, username=None,
  823. issue_uid='foobar2', json_data=data
  824. )
  825. # After second insertion
  826. self.assertEqual(len(repo.issues), 2)
  827. self.assertEqual(repo.issues[0].uid, 'foobar')
  828. self.assertEqual(repo.issues[0].title, 'fake issue for tests')
  829. self.assertEqual(repo.issues[0].depends_text, [20])
  830. self.assertEqual(repo.issues[0].blocks_text, [])
  831. # New one
  832. self.assertEqual(repo.issues[1].uid, 'foobar2')
  833. self.assertEqual(repo.issues[1].title, 'Rename pagure')
  834. self.assertEqual(repo.issues[1].depends_text, [])
  835. self.assertEqual(repo.issues[1].blocks_text, [1])
  836. def test_update_request_from_git(self):
  837. """ Test the update_request_from_git method from pagure.lib.git. """
  838. tests.create_projects(self.session)
  839. tests.create_projects_git(os.path.join(self.path, 'repos'))
  840. repo = pagure.lib.get_project(self.session, 'test')
  841. # Before
  842. self.assertEqual(len(repo.requests), 0)
  843. self.assertEqual(repo.requests, [])
  844. data = {
  845. "status": True,
  846. "uid": "d4182a2ac2d541d884742d3037c26e56",
  847. "project": {
  848. "parent": None,
  849. "settings": {
  850. "issue_tracker": True,
  851. "project_documentation": True,
  852. "pull_requests": True,
  853. },
  854. "name": "test",
  855. "date_created": "1426500194",
  856. "tags": [],
  857. "user": {
  858. "fullname": "Pierre-YvesChibon",
  859. "name": "pingou",
  860. "default_email": "pingou@fedoraproject.org",
  861. "emails": [
  862. "pingou@fedoraproject.org"
  863. ]
  864. },
  865. "id": 1,
  866. "description": "test project"
  867. },
  868. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  869. "user": {
  870. "fullname": "Pierre-YvesChibon",
  871. "name": "pingou",
  872. "default_email": "pingou@fedoraproject.org",
  873. "emails": ["pingou@fedoraproject.org"]
  874. },
  875. "id": 7,
  876. "comments": [
  877. {
  878. "comment": "really?",
  879. "user": {
  880. "fullname": "Pierre-YvesChibon",
  881. "name": "pingou",
  882. "default_email": "pingou@fedoraproject.org",
  883. "emails": ["pingou@fedoraproject.org"]
  884. },
  885. "parent": None,
  886. "date_created": "1426843778",
  887. "commit": "fa72f315373ec5f98f2b08c8ffae3645c97aaad2",
  888. "line": 5,
  889. "id": 1,
  890. "filename": "test"
  891. },
  892. {
  893. "comment": "Again ?",
  894. "user": {
  895. "fullname": "Pierre-YvesChibon",
  896. "name": "pingou",
  897. "default_email": "pingou@fedoraproject.org",
  898. "emails": [
  899. "pingou@fedoraproject.org"
  900. ]
  901. },
  902. "parent": None,
  903. "date_created": "1426866781",
  904. "commit": "94ebaf900161394059478fd88aec30e59092a1d7",
  905. "line": 5,
  906. "id": 2,
  907. "filename": "test2"
  908. },
  909. {
  910. "comment": "Should be fine in fact",
  911. "user": {
  912. "fullname": "Pierre-YvesChibon",
  913. "name": "pingou",
  914. "default_email": "pingou@fedoraproject.org",
  915. "emails": [
  916. "pingou@fedoraproject.org"
  917. ]
  918. },
  919. "parent": None,
  920. "date_created": "1426866950",
  921. "commit": "94ebaf900161394059478fd88aec30e59092a1d7",
  922. "line": 5,
  923. "id": 3,
  924. "filename": "test2"
  925. }
  926. ],
  927. "branch_from": "master",
  928. "title": "test request",
  929. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  930. "repo_from": {
  931. "parent": {
  932. "parent": None,
  933. "name": "test",
  934. "date_created": "1426500194",
  935. "tags": [],
  936. "user": {
  937. "fullname": "Pierre-YvesChibon",
  938. "name": "pingou",
  939. "default_email": "pingou@fedoraproject.org",
  940. "emails": [
  941. "pingou@fedoraproject.org"
  942. ]
  943. },
  944. "settings": {
  945. "issue_tracker": True,
  946. "project_documentation": True,
  947. "pull_requests": True,
  948. },
  949. "id": 1,
  950. "description": "test project"
  951. },
  952. "settings": {
  953. "issue_tracker": True,
  954. "project_documentation": True,
  955. "pull_requests": True,
  956. },
  957. "name": "test",
  958. "date_created": "1426843440",
  959. "tags": [],
  960. "user": {
  961. "fullname": "fake user",
  962. "name": "fake",
  963. "default_email": "fake@fedoraproject.org",
  964. "emails": [
  965. "fake@fedoraproject.org"
  966. ]
  967. },
  968. "id": 6,
  969. "description": "test project"
  970. },
  971. "branch": "master",
  972. "date_created": "1426843732"
  973. }
  974. self.assertRaises(
  975. pagure.exceptions.PagureException,
  976. pagure.lib.git.update_request_from_git,
  977. self.session,
  978. reponame='foobar',
  979. namespace=None,
  980. username=None,
  981. request_uid='d4182a2ac2d541d884742d3037c26e56',
  982. json_data=data,
  983. gitfolder=self.path,
  984. docfolder=os.path.join(self.path, 'docs'),
  985. ticketfolder=os.path.join(self.path, 'tickets'),
  986. requestfolder=os.path.join(self.path, 'requests')
  987. )
  988. pagure.lib.git.update_request_from_git(
  989. self.session,
  990. reponame='test',
  991. namespace=None,
  992. username=None,
  993. request_uid='d4182a2ac2d541d884742d3037c26e56',
  994. json_data=data,
  995. gitfolder=self.path,
  996. docfolder=os.path.join(self.path, 'docs'),
  997. ticketfolder=os.path.join(self.path, 'tickets'),
  998. requestfolder=os.path.join(self.path, 'requests')
  999. )
  1000. self.session.commit()
  1001. # After 1 st insertion
  1002. self.assertEqual(len(repo.requests), 1)
  1003. self.assertEqual(repo.requests[0].id, 7)
  1004. self.assertEqual(
  1005. repo.requests[0].uid, 'd4182a2ac2d541d884742d3037c26e56')
  1006. self.assertEqual(repo.requests[0].title, 'test request')
  1007. self.assertEqual(len(repo.requests[0].comments), 3)
  1008. data = {
  1009. "status": True,
  1010. "uid": "d4182a2ac2d541d884742d3037c26e57",
  1011. "project": {
  1012. "parent": None,
  1013. "name": "test",
  1014. "date_created": "1426500194",
  1015. "tags": [],
  1016. "user": {
  1017. "fullname": "Pierre-YvesChibon",
  1018. "name": "pingou",
  1019. "default_email": "pingou@fedoraproject.org",
  1020. "emails": [
  1021. "pingou@fedoraproject.org"
  1022. ]
  1023. },
  1024. "settings": {
  1025. "issue_tracker": True,
  1026. "project_documentation": True,
  1027. "pull_requests": True,
  1028. },
  1029. "id": 1,
  1030. "description": "test project"
  1031. },
  1032. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  1033. "user": {
  1034. "fullname": "Pierre-YvesChibon",
  1035. "name": "pingou",
  1036. "default_email": "pingou@fedoraproject.org",
  1037. "emails": ["pingou@fedoraproject.org"]
  1038. },
  1039. "id": 4,
  1040. "comments": [],
  1041. "branch_from": "master",
  1042. "title": "test request #2",
  1043. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  1044. "repo_from": {
  1045. "parent": {
  1046. "parent": None,
  1047. "name": "test",
  1048. "date_created": "1426500194",
  1049. "tags": [],
  1050. "user": {
  1051. "fullname": "Pierre-YvesChibon",
  1052. "name": "pingou",
  1053. "default_email": "pingou@fedoraproject.org",
  1054. "emails": [
  1055. "pingou@fedoraproject.org"
  1056. ]
  1057. },
  1058. "settings": {
  1059. "issue_tracker": True,
  1060. "project_documentation": True,
  1061. "pull_requests": True,
  1062. },
  1063. "id": 1,
  1064. "description": "test project"
  1065. },
  1066. "settings": {
  1067. "issue_tracker": True,
  1068. "project_documentation": True,
  1069. "pull_requests": True,
  1070. },
  1071. "name": "test",
  1072. "date_created": "1426843440",
  1073. "tags": [],
  1074. "user": {
  1075. "fullname": "fake user",
  1076. "name": "fake",
  1077. "default_email": "fake@fedoraproject.org",
  1078. "emails": [
  1079. "fake@fedoraproject.org"
  1080. ]
  1081. },
  1082. "project_docs": True,
  1083. "id": 6,
  1084. "description": "test project"
  1085. },
  1086. "branch": "master",
  1087. "date_created": "1426843745"
  1088. }
  1089. pagure.lib.git.update_request_from_git(
  1090. self.session,
  1091. reponame='test',
  1092. namespace=None,
  1093. username=None,
  1094. request_uid='d4182a2ac2d541d884742d3037c26e57',
  1095. json_data=data,
  1096. gitfolder=self.path,
  1097. docfolder=os.path.join(self.path, 'docs'),
  1098. ticketfolder=os.path.join(self.path, 'tickets'),
  1099. requestfolder=os.path.join(self.path, 'requests')
  1100. )
  1101. self.session.commit()
  1102. # After 2 nd insertion
  1103. self.assertEqual(len(repo.requests), 2)
  1104. self.assertEqual(repo.requests[0].id, 7)
  1105. self.assertEqual(
  1106. repo.requests[0].uid, 'd4182a2ac2d541d884742d3037c26e56')
  1107. self.assertEqual(repo.requests[0].title, 'test request')
  1108. self.assertEqual(len(repo.requests[0].comments), 3)
  1109. # 2 entry
  1110. self.assertEqual(repo.requests[1].id, 4)
  1111. self.assertEqual(
  1112. repo.requests[1].uid, 'd4182a2ac2d541d884742d3037c26e57')
  1113. self.assertEqual(repo.requests[1].title, 'test request #2')
  1114. self.assertEqual(len(repo.requests[1].comments), 0)
  1115. def test_read_git_lines(self):
  1116. """ Test the read_git_lines method of pagure.lib.git. """
  1117. self.test_update_git()
  1118. gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  1119. output = pagure.lib.git.read_git_lines(
  1120. ['log', '-1', "--pretty='%s'"], gitrepo)
  1121. self.assertEqual(len(output), 1)
  1122. self.assertTrue(
  1123. output[0].startswith("'Updated issue ")
  1124. )
  1125. self.assertTrue(
  1126. output[0].endswith(": Test issue'")
  1127. )
  1128. # Keeping the new line symbol
  1129. output = pagure.lib.git.read_git_lines(
  1130. ['log', '-1', "--pretty='%s'"], gitrepo, keepends=True)
  1131. self.assertEqual(len(output), 1)
  1132. self.assertTrue(
  1133. output[0].endswith(": Test issue'\n")
  1134. )
  1135. def test_get_revs_between(self):
  1136. """ Test the get_revs_between method of pagure.lib.git. """
  1137. self.test_update_git()
  1138. gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  1139. output = pagure.lib.git.read_git_lines(
  1140. ['log', '-3', "--pretty='%H'"], gitrepo)
  1141. self.assertEqual(len(output), 2)
  1142. from_hash = output[1].replace("'", '')
  1143. # Case 1, repo BASE is null and HEAD is equal to from_hash
  1144. to_hash = '0'
  1145. output1 = pagure.lib.git.get_revs_between(
  1146. to_hash, from_hash, gitrepo, 'refs/heads/master')
  1147. self.assertEqual(output1, [from_hash])
  1148. # Case 2, get revs between two commits (to_hash, from_hash)
  1149. to_hash = output[0].replace("'", '')
  1150. output2 = pagure.lib.git.get_revs_between(
  1151. to_hash, from_hash, gitrepo, 'refs/heads/master')
  1152. self.assertEqual(output2, [to_hash])
  1153. # Case 3, get revs between two commits (from_hash, to_hash)
  1154. output3 = pagure.lib.git.get_revs_between(
  1155. from_hash, to_hash, gitrepo, 'refs/heads/master')
  1156. self.assertEqual(output3, [to_hash])
  1157. # Case 4, get revs between two commits on two different branches
  1158. newgitrepo = tempfile.mkdtemp(prefix='pagure-')
  1159. newrepo = pygit2.clone_repository(gitrepo, newgitrepo)
  1160. newrepo.create_branch('feature', newrepo.head.get_object())
  1161. with open(os.path.join(newgitrepo, 'sources'), 'w') as stream:
  1162. stream.write('foo\n bar')
  1163. newrepo.index.add('sources')
  1164. newrepo.index.write()
  1165. # Commits the files added
  1166. tree = newrepo.index.write_tree()
  1167. author = pygit2.Signature(
  1168. 'Alice Author', 'alice@authors.tld')
  1169. committer = pygit2.Signature(
  1170. 'Cecil Committer', 'cecil@committers.tld')
  1171. newrepo.create_commit(
  1172. 'refs/heads/feature', # the name of the reference to update
  1173. author,
  1174. committer,
  1175. 'Add sources file for testing',
  1176. # binary string representing the tree object ID
  1177. tree,
  1178. # list of binary strings representing parents of the new commit
  1179. [to_hash]
  1180. )
  1181. branch_commit = newrepo.revparse_single('refs/heads/feature')
  1182. # Push to origin
  1183. ori_remote = newrepo.remotes[0]
  1184. PagureRepo.push(ori_remote, 'refs/heads/feature')
  1185. # Remove the clone
  1186. shutil.rmtree(newgitrepo)
  1187. output4 = pagure.lib.git.get_revs_between(
  1188. '0', branch_commit.oid.hex, gitrepo, 'refs/heads/feature')
  1189. self.assertEqual(output4, [branch_commit.oid.hex])
  1190. def test_get_author(self):
  1191. """ Test the get_author method of pagure.lib.git. """
  1192. self.test_update_git()
  1193. gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  1194. output = pagure.lib.git.read_git_lines(
  1195. ['log', '-3', "--pretty='%H'"], gitrepo)
  1196. self.assertEqual(len(output), 2)
  1197. for githash in output:
  1198. githash = githash.replace("'", '')
  1199. output = pagure.lib.git.get_author(githash, gitrepo)
  1200. self.assertEqual(output, 'pagure')
  1201. def get_author_email(self):
  1202. """ Test the get_author_email method of pagure.lib.git. """
  1203. self.test_update_git()
  1204. gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  1205. output = pagure.lib.git.read_git_lines(
  1206. ['log', '-3', "--pretty='%H'"], gitrepo)
  1207. self.assertEqual(len(output), 2)
  1208. for githash in output:
  1209. githash = githash.replace("'", '')
  1210. output = pagure.lib.git.get_author_email(githash, gitrepo)
  1211. self.assertEqual(output, 'pagure')
  1212. def test_get_repo_name(self):
  1213. """ Test the get_repo_name method of pagure.lib.git. """
  1214. gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  1215. repo_name = pagure.lib.git.get_repo_name(gitrepo)
  1216. self.assertEqual(repo_name, 'test_ticket_repo')
  1217. repo_name = pagure.lib.git.get_repo_name('foo/bar/baz/test.git')
  1218. self.assertEqual(repo_name, 'test')
  1219. repo_name = pagure.lib.git.get_repo_name('foo.test.git')
  1220. self.assertEqual(repo_name, 'foo.test')
  1221. def test_get_username(self):
  1222. """ Test the get_username method of pagure.lib.git. """
  1223. gitrepo = os.path.join(self.path, 'test_ticket_repo.git')
  1224. repo_name = pagure.lib.git.get_username(gitrepo)
  1225. self.assertEqual(repo_name, None)
  1226. repo_name = pagure.lib.git.get_username('foo/bar/baz/test.git')
  1227. self.assertEqual(repo_name, None)
  1228. repo_name = pagure.lib.git.get_username('foo.test.git')
  1229. self.assertEqual(repo_name, None)
  1230. repo_name = pagure.lib.git.get_username(
  1231. os.path.join(self.path, 'forks', 'pingou', 'foo.test.git'))
  1232. self.assertEqual(repo_name, 'pingou')
  1233. repo_name = pagure.lib.git.get_username(
  1234. os.path.join(self.path, 'forks', 'pingou', 'bar/foo.test.git'))
  1235. self.assertEqual(repo_name, 'pingou')
  1236. repo_name = pagure.lib.git.get_username(os.path.join(
  1237. self.path, 'forks', 'pingou', 'fooo/bar/foo.test.git'))
  1238. self.assertEqual(repo_name, 'pingou')
  1239. def test_get_repo_namespace(self):
  1240. """ Test the get_repo_namespace method of pagure.lib.git. """
  1241. repo_name = pagure.lib.git.get_repo_namespace(
  1242. os.path.join(self.path, 'repos', 'test_ticket_repo.git'))
  1243. self.assertEqual(repo_name, None)
  1244. repo_name = pagure.lib.git.get_repo_namespace(
  1245. os.path.join(self.path, 'repos', 'foo/bar/baz/test.git'))
  1246. self.assertEqual(repo_name, 'foo/bar/baz')
  1247. repo_name = pagure.lib.git.get_repo_namespace(
  1248. os.path.join(self.path, 'repos', 'foo.test.git'))
  1249. self.assertEqual(repo_name, None)
  1250. repo_name = pagure.lib.git.get_repo_namespace(os.path.join(
  1251. self.path, 'repos', 'forks', 'user', 'foo.test.git'))
  1252. self.assertEqual(repo_name, None)
  1253. repo_name = pagure.lib.git.get_repo_namespace(os.path.join(
  1254. self.path, 'repos', 'forks', 'user', 'bar/foo.test.git'))
  1255. self.assertEqual(repo_name, 'bar')
  1256. repo_name = pagure.lib.git.get_repo_namespace(os.path.join(
  1257. self.path, 'repos', 'forks', 'user', 'ns/bar/foo.test.git'))
  1258. self.assertEqual(repo_name, 'ns/bar')
  1259. repo_name = pagure.lib.git.get_repo_namespace(os.path.join(
  1260. self.path, 'repos', 'forks', 'user', '/bar/foo.test.git'))
  1261. self.assertEqual(repo_name, 'bar')
  1262. if __name__ == '__main__':
  1263. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibGittests)
  1264. unittest.TextTestRunner(verbosity=2).run(SUITE)