test_pagure_lib_git.py 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - 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
  10. import datetime
  11. import os
  12. import shutil
  13. import sys
  14. import tempfile
  15. import time
  16. import unittest
  17. import pygit2
  18. import six
  19. from mock import patch, MagicMock
  20. sys.path.insert(0, os.path.join(os.path.dirname(
  21. os.path.abspath(__file__)), '..'))
  22. import pagure
  23. import pagure.lib.git
  24. import tests
  25. from pagure.lib.repo import PagureRepo
  26. class PagureLibGittests(tests.Modeltests):
  27. """ Tests for pagure.lib.git """
  28. def test_write_gitolite_acls(self):
  29. """ Test the write_gitolite_acls function of pagure.lib.git.
  30. when the new uesr is an made an admin """
  31. tests.create_projects(self.session)
  32. repo = pagure.lib.get_authorized_project(self.session, 'test')
  33. # Add an user to a project
  34. # The user will be an admin of the project
  35. msg = pagure.lib.add_user_to_project(
  36. session=self.session,
  37. project=repo,
  38. new_user='foo',
  39. user='pingou',
  40. )
  41. self.session.commit()
  42. self.assertEqual(msg, 'User added')
  43. # Add a forked project
  44. item = pagure.lib.model.Project(
  45. user_id=1, # pingou
  46. name='test3',
  47. description='test project #2',
  48. is_fork=True,
  49. parent_id=1,
  50. hook_token='aaabbbvvv',
  51. )
  52. self.session.add(item)
  53. self.session.commit()
  54. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  55. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  56. helper.write_gitolite_acls(
  57. self.session, configfile=outputconf, project=-1)
  58. self.assertTrue(os.path.exists(outputconf))
  59. with open(outputconf) as stream:
  60. data = stream.read()
  61. exp = """repo test
  62. R = @all
  63. RW+ = pingou
  64. RW+ = foo
  65. repo docs/test
  66. R = @all
  67. RW+ = pingou
  68. RW+ = foo
  69. repo tickets/test
  70. RW+ = pingou
  71. RW+ = foo
  72. repo requests/test
  73. RW+ = pingou
  74. RW+ = foo
  75. repo test2
  76. R = @all
  77. RW+ = pingou
  78. repo docs/test2
  79. R = @all
  80. RW+ = pingou
  81. repo tickets/test2
  82. RW+ = pingou
  83. repo requests/test2
  84. RW+ = pingou
  85. repo somenamespace/test3
  86. R = @all
  87. RW+ = pingou
  88. repo docs/somenamespace/test3
  89. R = @all
  90. RW+ = pingou
  91. repo tickets/somenamespace/test3
  92. RW+ = pingou
  93. repo requests/somenamespace/test3
  94. RW+ = pingou
  95. repo forks/pingou/test3
  96. R = @all
  97. RW+ = pingou
  98. repo docs/forks/pingou/test3
  99. R = @all
  100. RW+ = pingou
  101. repo tickets/forks/pingou/test3
  102. RW+ = pingou
  103. repo requests/forks/pingou/test3
  104. RW+ = pingou
  105. # end of body
  106. """
  107. #print data
  108. self.assertEqual(data, exp)
  109. os.unlink(outputconf)
  110. self.assertFalse(os.path.exists(outputconf))
  111. def test_write_gitolite_acls_preconf(self):
  112. """ Test the write_gitolite_acls function of pagure.lib.git with
  113. a preconf set """
  114. tests.create_projects(self.session)
  115. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  116. preconf = os.path.join(self.path, 'header_gitolite')
  117. with open(preconf, 'w') as stream:
  118. stream.write('# this is a header that is manually added')
  119. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  120. helper.write_gitolite_acls(
  121. self.session,
  122. outputconf,
  123. project=-1,
  124. preconf=preconf
  125. )
  126. self.assertTrue(os.path.exists(outputconf))
  127. with open(outputconf) as stream:
  128. data = stream.read()
  129. exp = """# this is a header that is manually added
  130. # end of header
  131. repo test
  132. R = @all
  133. RW+ = pingou
  134. repo docs/test
  135. R = @all
  136. RW+ = pingou
  137. repo tickets/test
  138. RW+ = pingou
  139. repo requests/test
  140. RW+ = pingou
  141. repo test2
  142. R = @all
  143. RW+ = pingou
  144. repo docs/test2
  145. R = @all
  146. RW+ = pingou
  147. repo tickets/test2
  148. RW+ = pingou
  149. repo requests/test2
  150. RW+ = pingou
  151. repo somenamespace/test3
  152. R = @all
  153. RW+ = pingou
  154. repo docs/somenamespace/test3
  155. R = @all
  156. RW+ = pingou
  157. repo tickets/somenamespace/test3
  158. RW+ = pingou
  159. repo requests/somenamespace/test3
  160. RW+ = pingou
  161. # end of body
  162. """
  163. #print data
  164. self.assertEqual(data.split('\n'), exp.split('\n'))
  165. os.unlink(outputconf)
  166. self.assertFalse(os.path.exists(outputconf))
  167. def test_write_gitolite_acls_preconf_postconf(self):
  168. """ Test the write_gitolite_acls function of pagure.lib.git with
  169. a postconf set """
  170. tests.create_projects(self.session)
  171. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  172. preconf = os.path.join(self.path, 'header_gitolite')
  173. with open(preconf, 'w') as stream:
  174. stream.write('# this is a header that is manually added')
  175. postconf = os.path.join(self.path, 'footer_gitolite')
  176. with open(postconf, 'w') as stream:
  177. stream.write('# end of generated configuration')
  178. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  179. helper.write_gitolite_acls(
  180. self.session,
  181. outputconf,
  182. project=-1,
  183. preconf=preconf,
  184. postconf=postconf
  185. )
  186. self.assertTrue(os.path.exists(outputconf))
  187. with open(outputconf) as stream:
  188. data = stream.read()
  189. exp = """# this is a header that is manually added
  190. # end of header
  191. repo test
  192. R = @all
  193. RW+ = pingou
  194. repo docs/test
  195. R = @all
  196. RW+ = pingou
  197. repo tickets/test
  198. RW+ = pingou
  199. repo requests/test
  200. RW+ = pingou
  201. repo test2
  202. R = @all
  203. RW+ = pingou
  204. repo docs/test2
  205. R = @all
  206. RW+ = pingou
  207. repo tickets/test2
  208. RW+ = pingou
  209. repo requests/test2
  210. RW+ = pingou
  211. repo somenamespace/test3
  212. R = @all
  213. RW+ = pingou
  214. repo docs/somenamespace/test3
  215. R = @all
  216. RW+ = pingou
  217. repo tickets/somenamespace/test3
  218. RW+ = pingou
  219. repo requests/somenamespace/test3
  220. RW+ = pingou
  221. # end of body
  222. # end of generated configuration
  223. """
  224. #print data
  225. self.assertEqual(data, exp)
  226. os.unlink(outputconf)
  227. self.assertFalse(os.path.exists(outputconf))
  228. def test_write_gitolite_acls_postconf(self):
  229. """ Test the write_gitolite_acls function of pagure.lib.git with
  230. a preconf and a postconf set """
  231. tests.create_projects(self.session)
  232. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  233. postconf = os.path.join(self.path, 'footer_gitolite')
  234. with open(postconf, 'w') as stream:
  235. stream.write('# end of generated configuration')
  236. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  237. helper.write_gitolite_acls(
  238. self.session,
  239. outputconf,
  240. project=-1,
  241. postconf=postconf
  242. )
  243. self.assertTrue(os.path.exists(outputconf))
  244. with open(outputconf) as stream:
  245. data = stream.read()
  246. exp = """repo test
  247. R = @all
  248. RW+ = pingou
  249. repo docs/test
  250. R = @all
  251. RW+ = pingou
  252. repo tickets/test
  253. RW+ = pingou
  254. repo requests/test
  255. RW+ = pingou
  256. repo test2
  257. R = @all
  258. RW+ = pingou
  259. repo docs/test2
  260. R = @all
  261. RW+ = pingou
  262. repo tickets/test2
  263. RW+ = pingou
  264. repo requests/test2
  265. RW+ = pingou
  266. repo somenamespace/test3
  267. R = @all
  268. RW+ = pingou
  269. repo docs/somenamespace/test3
  270. R = @all
  271. RW+ = pingou
  272. repo tickets/somenamespace/test3
  273. RW+ = pingou
  274. repo requests/somenamespace/test3
  275. RW+ = pingou
  276. # end of body
  277. # end of generated configuration
  278. """
  279. #print data
  280. self.assertEqual(data, exp)
  281. os.unlink(outputconf)
  282. self.assertFalse(os.path.exists(outputconf))
  283. def test_write_gitolite_acls_deploykeys(self):
  284. """ Test write_gitolite_acls function to add deploy keys. """
  285. tests.create_projects(self.session)
  286. repo = pagure.lib.get_authorized_project(self.session, 'test')
  287. # Add two deploy keys (one readonly one push)
  288. pingou = pagure.lib.get_user(self.session, 'pingou')
  289. msg1 = pagure.lib.add_sshkey_to_project_or_user(
  290. session=self.session,
  291. project=repo,
  292. ssh_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==',
  293. pushaccess=False,
  294. creator=pingou
  295. )
  296. msg2 = pagure.lib.add_sshkey_to_project_or_user(
  297. session=self.session,
  298. project=repo,
  299. ssh_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC9Xwc2RDzPBhlEDARfHldGjudIVoa04tqT1JVKGQmyllTFz7Rb8CngQL3e7zyNzotnhwYKHdoiLlPkVEiDee4dWMUe48ilqId+FJZQGhyv8fu4BoFdE1AJUVylzmltbLg14VqG5gjTpXgtlrEva9arKwBMHJjRYc8ScaSn3OgyQw==',
  300. pushaccess=True,
  301. creator=pingou
  302. )
  303. self.session.commit()
  304. self.assertEqual(msg1, 'SSH key added')
  305. self.assertEqual(msg2, 'SSH key added')
  306. # Add a forked project
  307. item = pagure.lib.model.Project(
  308. user_id=1, # pingou
  309. name='test3',
  310. description='test project #2',
  311. is_fork=True,
  312. parent_id=1,
  313. hook_token='aaabbbvvv',
  314. )
  315. self.session.add(item)
  316. self.session.commit()
  317. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  318. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  319. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  320. self.assertTrue(os.path.exists(outputconf))
  321. with open(outputconf) as stream:
  322. data = stream.read()
  323. exp = """repo test
  324. R = @all
  325. RW+ = pingou
  326. R = deploykey_test_1
  327. RW+ = deploykey_test_2
  328. repo docs/test
  329. R = @all
  330. RW+ = pingou
  331. R = deploykey_test_1
  332. RW+ = deploykey_test_2
  333. repo tickets/test
  334. RW+ = pingou
  335. R = deploykey_test_1
  336. RW+ = deploykey_test_2
  337. repo requests/test
  338. RW+ = pingou
  339. R = deploykey_test_1
  340. RW+ = deploykey_test_2
  341. repo test2
  342. R = @all
  343. RW+ = pingou
  344. repo docs/test2
  345. R = @all
  346. RW+ = pingou
  347. repo tickets/test2
  348. RW+ = pingou
  349. repo requests/test2
  350. RW+ = pingou
  351. repo somenamespace/test3
  352. R = @all
  353. RW+ = pingou
  354. repo docs/somenamespace/test3
  355. R = @all
  356. RW+ = pingou
  357. repo tickets/somenamespace/test3
  358. RW+ = pingou
  359. repo requests/somenamespace/test3
  360. RW+ = pingou
  361. repo forks/pingou/test3
  362. R = @all
  363. RW+ = pingou
  364. repo docs/forks/pingou/test3
  365. R = @all
  366. RW+ = pingou
  367. repo tickets/forks/pingou/test3
  368. RW+ = pingou
  369. repo requests/forks/pingou/test3
  370. RW+ = pingou
  371. # end of body
  372. """
  373. #print data
  374. self.assertEqual(data, exp)
  375. os.unlink(outputconf)
  376. self.assertFalse(os.path.exists(outputconf))
  377. def test_write_gitolite_acls_ticket(self):
  378. """ Test the write_gitolite_acls function of pagure.lib.git.
  379. when the new uesr is just a ticketer """
  380. tests.create_projects(self.session)
  381. repo = pagure.lib.get_authorized_project(self.session, 'test')
  382. # Add an user to a project
  383. # The user will be an admin of the project
  384. msg = pagure.lib.add_user_to_project(
  385. session=self.session,
  386. project=repo,
  387. new_user='foo',
  388. user='pingou',
  389. access='ticket'
  390. )
  391. self.session.commit()
  392. self.assertEqual(msg, 'User added')
  393. # Add a forked project
  394. item = pagure.lib.model.Project(
  395. user_id=1, # pingou
  396. name='test3',
  397. description='test project #2',
  398. is_fork=True,
  399. parent_id=1,
  400. hook_token='aaabbbvvv',
  401. )
  402. self.session.add(item)
  403. self.session.commit()
  404. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  405. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  406. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  407. self.assertTrue(os.path.exists(outputconf))
  408. with open(outputconf) as stream:
  409. data = stream.read()
  410. exp = """repo test
  411. R = @all
  412. RW+ = pingou
  413. repo docs/test
  414. R = @all
  415. RW+ = pingou
  416. repo tickets/test
  417. RW+ = pingou
  418. repo requests/test
  419. RW+ = pingou
  420. repo test2
  421. R = @all
  422. RW+ = pingou
  423. repo docs/test2
  424. R = @all
  425. RW+ = pingou
  426. repo tickets/test2
  427. RW+ = pingou
  428. repo requests/test2
  429. RW+ = pingou
  430. repo somenamespace/test3
  431. R = @all
  432. RW+ = pingou
  433. repo docs/somenamespace/test3
  434. R = @all
  435. RW+ = pingou
  436. repo tickets/somenamespace/test3
  437. RW+ = pingou
  438. repo requests/somenamespace/test3
  439. RW+ = pingou
  440. repo forks/pingou/test3
  441. R = @all
  442. RW+ = pingou
  443. repo docs/forks/pingou/test3
  444. R = @all
  445. RW+ = pingou
  446. repo tickets/forks/pingou/test3
  447. RW+ = pingou
  448. repo requests/forks/pingou/test3
  449. RW+ = pingou
  450. # end of body
  451. """
  452. #print data
  453. self.assertEqual(data, exp)
  454. os.unlink(outputconf)
  455. self.assertFalse(os.path.exists(outputconf))
  456. def test_write_gitolite_acls_commit(self):
  457. """ Test the write_gitolite_acls function of pagure.lib.git.
  458. when the new uesr is just a committer """
  459. tests.create_projects(self.session)
  460. repo = pagure.lib.get_authorized_project(self.session, 'test')
  461. # Add an user to a project
  462. # The user will be an admin of the project
  463. msg = pagure.lib.add_user_to_project(
  464. session=self.session,
  465. project=repo,
  466. new_user='foo',
  467. user='pingou',
  468. access='commit'
  469. )
  470. self.session.commit()
  471. self.assertEqual(msg, 'User added')
  472. # Add a forked project
  473. item = pagure.lib.model.Project(
  474. user_id=1, # pingou
  475. name='test3',
  476. description='test project #2',
  477. is_fork=True,
  478. parent_id=1,
  479. hook_token='aaabbbvvv',
  480. )
  481. self.session.add(item)
  482. self.session.commit()
  483. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  484. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  485. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  486. self.assertTrue(os.path.exists(outputconf))
  487. with open(outputconf) as stream:
  488. data = stream.read()
  489. exp = """repo test
  490. R = @all
  491. RW+ = pingou
  492. RW+ = foo
  493. repo docs/test
  494. R = @all
  495. RW+ = pingou
  496. RW+ = foo
  497. repo tickets/test
  498. RW+ = pingou
  499. RW+ = foo
  500. repo requests/test
  501. RW+ = pingou
  502. RW+ = foo
  503. repo test2
  504. R = @all
  505. RW+ = pingou
  506. repo docs/test2
  507. R = @all
  508. RW+ = pingou
  509. repo tickets/test2
  510. RW+ = pingou
  511. repo requests/test2
  512. RW+ = pingou
  513. repo somenamespace/test3
  514. R = @all
  515. RW+ = pingou
  516. repo docs/somenamespace/test3
  517. R = @all
  518. RW+ = pingou
  519. repo tickets/somenamespace/test3
  520. RW+ = pingou
  521. repo requests/somenamespace/test3
  522. RW+ = pingou
  523. repo forks/pingou/test3
  524. R = @all
  525. RW+ = pingou
  526. repo docs/forks/pingou/test3
  527. R = @all
  528. RW+ = pingou
  529. repo tickets/forks/pingou/test3
  530. RW+ = pingou
  531. repo requests/forks/pingou/test3
  532. RW+ = pingou
  533. # end of body
  534. """
  535. #print data
  536. self.assertEqual(data, exp)
  537. os.unlink(outputconf)
  538. self.assertFalse(os.path.exists(outputconf))
  539. def test_write_gitolite_acls_groups(self):
  540. """ Test the write_gitolite_acls function of pagure.lib.git with
  541. groups as admin
  542. """
  543. tests.create_projects(self.session)
  544. repo = pagure.lib.get_authorized_project(self.session, 'test')
  545. # Add a couple of groups
  546. # They would be admins
  547. msg = pagure.lib.add_group(
  548. self.session,
  549. group_name='sysadmin',
  550. display_name='sysadmin group',
  551. description=None,
  552. group_type='user',
  553. user='pingou',
  554. is_admin=False,
  555. blacklist=[],
  556. )
  557. self.session.commit()
  558. self.assertEqual(msg, 'User `pingou` added to the group `sysadmin`.')
  559. msg = pagure.lib.add_group(
  560. self.session,
  561. group_name='devs',
  562. display_name='devs group',
  563. description=None,
  564. group_type='user',
  565. user='pingou',
  566. is_admin=False,
  567. blacklist=[],
  568. )
  569. self.session.commit()
  570. self.assertEqual(msg, 'User `pingou` added to the group `devs`.')
  571. # Associate these groups to a project
  572. msg = pagure.lib.add_group_to_project(
  573. session=self.session,
  574. project=repo,
  575. new_group='sysadmin',
  576. user='pingou',
  577. )
  578. self.session.commit()
  579. self.assertEqual(msg, 'Group added')
  580. msg = pagure.lib.add_group_to_project(
  581. session=self.session,
  582. project=repo,
  583. new_group='devs',
  584. user='pingou',
  585. )
  586. self.session.commit()
  587. self.assertEqual(msg, 'Group added')
  588. # Add an user to a project
  589. msg = pagure.lib.add_user_to_project(
  590. session=self.session,
  591. project=repo,
  592. new_user='foo',
  593. user='pingou',
  594. )
  595. self.session.commit()
  596. self.assertEqual(msg, 'User added')
  597. # Add a forked project
  598. item = pagure.lib.model.Project(
  599. user_id=1, # pingou
  600. name='test2',
  601. description='test project #2',
  602. is_fork=True,
  603. parent_id=1,
  604. hook_token='aaabbbvvv',
  605. )
  606. self.session.add(item)
  607. self.session.commit()
  608. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  609. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  610. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  611. self.assertTrue(os.path.exists(outputconf))
  612. with open(outputconf) as stream:
  613. data = stream.read()
  614. exp = """@devs = pingou
  615. @sysadmin = pingou
  616. # end of groups
  617. repo test
  618. R = @all
  619. RW+ = @devs @sysadmin
  620. RW+ = pingou
  621. RW+ = foo
  622. repo docs/test
  623. R = @all
  624. RW+ = @devs @sysadmin
  625. RW+ = pingou
  626. RW+ = foo
  627. repo tickets/test
  628. RW+ = @devs @sysadmin
  629. RW+ = pingou
  630. RW+ = foo
  631. repo requests/test
  632. RW+ = @devs @sysadmin
  633. RW+ = pingou
  634. RW+ = foo
  635. repo test2
  636. R = @all
  637. RW+ = pingou
  638. repo docs/test2
  639. R = @all
  640. RW+ = pingou
  641. repo tickets/test2
  642. RW+ = pingou
  643. repo requests/test2
  644. RW+ = pingou
  645. repo somenamespace/test3
  646. R = @all
  647. RW+ = pingou
  648. repo docs/somenamespace/test3
  649. R = @all
  650. RW+ = pingou
  651. repo tickets/somenamespace/test3
  652. RW+ = pingou
  653. repo requests/somenamespace/test3
  654. RW+ = pingou
  655. repo forks/pingou/test2
  656. R = @all
  657. RW+ = pingou
  658. repo docs/forks/pingou/test2
  659. R = @all
  660. RW+ = pingou
  661. repo tickets/forks/pingou/test2
  662. RW+ = pingou
  663. repo requests/forks/pingou/test2
  664. RW+ = pingou
  665. # end of body
  666. """
  667. #print data
  668. self.assertEqual(data.split('\n'), exp.split('\n'))
  669. os.unlink(outputconf)
  670. self.assertFalse(os.path.exists(outputconf))
  671. def test_write_gitolite_acls_groups_ticket(self):
  672. """ Test the write_gitolite_acls function of pagure.lib.git with
  673. groups as ticketers
  674. """
  675. tests.create_projects(self.session)
  676. repo = pagure.lib.get_authorized_project(self.session, 'test')
  677. # Add a couple of groups
  678. # They would be ticketers
  679. msg = pagure.lib.add_group(
  680. self.session,
  681. group_name='sysadmin',
  682. display_name='sysadmin group',
  683. description=None,
  684. group_type='user',
  685. user='pingou',
  686. is_admin=False,
  687. blacklist=[],
  688. )
  689. self.session.commit()
  690. self.assertEqual(msg, 'User `pingou` added to the group `sysadmin`.')
  691. msg = pagure.lib.add_group(
  692. self.session,
  693. group_name='devs',
  694. display_name='devs group',
  695. description=None,
  696. group_type='user',
  697. user='pingou',
  698. is_admin=False,
  699. blacklist=[],
  700. )
  701. self.session.commit()
  702. self.assertEqual(msg, 'User `pingou` added to the group `devs`.')
  703. # Associate these groups to a project
  704. msg = pagure.lib.add_group_to_project(
  705. session=self.session,
  706. project=repo,
  707. new_group='sysadmin',
  708. user='pingou',
  709. access='ticket',
  710. )
  711. self.session.commit()
  712. self.assertEqual(msg, 'Group added')
  713. msg = pagure.lib.add_group_to_project(
  714. session=self.session,
  715. project=repo,
  716. new_group='devs',
  717. user='pingou',
  718. access='ticket'
  719. )
  720. self.session.commit()
  721. self.assertEqual(msg, 'Group added')
  722. # Add an user to a project
  723. msg = pagure.lib.add_user_to_project(
  724. session=self.session,
  725. project=repo,
  726. new_user='foo',
  727. user='pingou',
  728. )
  729. self.session.commit()
  730. self.assertEqual(msg, 'User added')
  731. # Add a forked project
  732. item = pagure.lib.model.Project(
  733. user_id=1, # pingou
  734. name='test2',
  735. description='test project #2',
  736. is_fork=True,
  737. parent_id=1,
  738. hook_token='aaabbbvvv',
  739. )
  740. self.session.add(item)
  741. self.session.commit()
  742. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  743. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  744. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  745. self.assertTrue(os.path.exists(outputconf))
  746. with open(outputconf) as stream:
  747. data = stream.read()
  748. exp = """@devs = pingou
  749. @sysadmin = pingou
  750. # end of groups
  751. repo test
  752. R = @all
  753. RW+ = pingou
  754. RW+ = foo
  755. repo docs/test
  756. R = @all
  757. RW+ = pingou
  758. RW+ = foo
  759. repo tickets/test
  760. RW+ = pingou
  761. RW+ = foo
  762. repo requests/test
  763. RW+ = pingou
  764. RW+ = foo
  765. repo test2
  766. R = @all
  767. RW+ = pingou
  768. repo docs/test2
  769. R = @all
  770. RW+ = pingou
  771. repo tickets/test2
  772. RW+ = pingou
  773. repo requests/test2
  774. RW+ = pingou
  775. repo somenamespace/test3
  776. R = @all
  777. RW+ = pingou
  778. repo docs/somenamespace/test3
  779. R = @all
  780. RW+ = pingou
  781. repo tickets/somenamespace/test3
  782. RW+ = pingou
  783. repo requests/somenamespace/test3
  784. RW+ = pingou
  785. repo forks/pingou/test2
  786. R = @all
  787. RW+ = pingou
  788. repo docs/forks/pingou/test2
  789. R = @all
  790. RW+ = pingou
  791. repo tickets/forks/pingou/test2
  792. RW+ = pingou
  793. repo requests/forks/pingou/test2
  794. RW+ = pingou
  795. # end of body
  796. """
  797. #print data
  798. self.assertEqual(data.split('\n'), exp.split('\n'))
  799. os.unlink(outputconf)
  800. self.assertFalse(os.path.exists(outputconf))
  801. def test_write_gitolite_acls_groups_commit(self):
  802. """ Test the write_gitolite_acls function of pagure.lib.git with
  803. groups as committers
  804. """
  805. tests.create_projects(self.session)
  806. repo = pagure.lib.get_authorized_project(self.session, 'test')
  807. # Add a couple of groups
  808. # They would be committers
  809. msg = pagure.lib.add_group(
  810. self.session,
  811. group_name='sysadmin',
  812. display_name='sysadmin group',
  813. description=None,
  814. group_type='user',
  815. user='pingou',
  816. is_admin=False,
  817. blacklist=[],
  818. )
  819. self.session.commit()
  820. self.assertEqual(msg, 'User `pingou` added to the group `sysadmin`.')
  821. msg = pagure.lib.add_group(
  822. self.session,
  823. group_name='devs',
  824. display_name='devs group',
  825. description=None,
  826. group_type='user',
  827. user='pingou',
  828. is_admin=False,
  829. blacklist=[],
  830. )
  831. self.session.commit()
  832. self.assertEqual(msg, 'User `pingou` added to the group `devs`.')
  833. # Associate these groups to a project
  834. msg = pagure.lib.add_group_to_project(
  835. session=self.session,
  836. project=repo,
  837. new_group='sysadmin',
  838. user='pingou',
  839. access='commit'
  840. )
  841. self.session.commit()
  842. self.assertEqual(msg, 'Group added')
  843. msg = pagure.lib.add_group_to_project(
  844. session=self.session,
  845. project=repo,
  846. new_group='devs',
  847. user='pingou',
  848. access='commit'
  849. )
  850. self.session.commit()
  851. self.assertEqual(msg, 'Group added')
  852. # Add an user to a project
  853. msg = pagure.lib.add_user_to_project(
  854. session=self.session,
  855. project=repo,
  856. new_user='foo',
  857. user='pingou',
  858. )
  859. self.session.commit()
  860. self.assertEqual(msg, 'User added')
  861. # Add a forked project
  862. item = pagure.lib.model.Project(
  863. user_id=1, # pingou
  864. name='test2',
  865. description='test project #2',
  866. is_fork=True,
  867. parent_id=1,
  868. hook_token='aaabbbvvv',
  869. )
  870. self.session.add(item)
  871. self.session.commit()
  872. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  873. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  874. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  875. self.assertTrue(os.path.exists(outputconf))
  876. with open(outputconf) as stream:
  877. data = stream.read()
  878. exp = """@devs = pingou
  879. @sysadmin = pingou
  880. # end of groups
  881. repo test
  882. R = @all
  883. RW+ = @devs @sysadmin
  884. RW+ = pingou
  885. RW+ = foo
  886. repo docs/test
  887. R = @all
  888. RW+ = @devs @sysadmin
  889. RW+ = pingou
  890. RW+ = foo
  891. repo tickets/test
  892. RW+ = @devs @sysadmin
  893. RW+ = pingou
  894. RW+ = foo
  895. repo requests/test
  896. RW+ = @devs @sysadmin
  897. RW+ = pingou
  898. RW+ = foo
  899. repo test2
  900. R = @all
  901. RW+ = pingou
  902. repo docs/test2
  903. R = @all
  904. RW+ = pingou
  905. repo tickets/test2
  906. RW+ = pingou
  907. repo requests/test2
  908. RW+ = pingou
  909. repo somenamespace/test3
  910. R = @all
  911. RW+ = pingou
  912. repo docs/somenamespace/test3
  913. R = @all
  914. RW+ = pingou
  915. repo tickets/somenamespace/test3
  916. RW+ = pingou
  917. repo requests/somenamespace/test3
  918. RW+ = pingou
  919. repo forks/pingou/test2
  920. R = @all
  921. RW+ = pingou
  922. repo docs/forks/pingou/test2
  923. R = @all
  924. RW+ = pingou
  925. repo tickets/forks/pingou/test2
  926. RW+ = pingou
  927. repo requests/forks/pingou/test2
  928. RW+ = pingou
  929. # end of body
  930. """
  931. #print data
  932. self.assertEqual(data.split('\n'), exp.split('\n'))
  933. os.unlink(outputconf)
  934. self.assertFalse(os.path.exists(outputconf))
  935. def test_write_gitolite_project_pr_only(self):
  936. """ Test the write_gitolite_acls function of pagure.lib.git.
  937. when the project enforces the PR approach.
  938. """
  939. tests.create_projects(self.session)
  940. repo = pagure.lib._get_project(self.session, 'test')
  941. # Make the project enforce the PR workflow
  942. settings = repo.settings
  943. settings['pull_request_access_only'] = True
  944. repo.settings = settings
  945. self.session.add(repo)
  946. self.session.commit()
  947. # Add an user to a project
  948. # The user will be an admin of the project
  949. msg = pagure.lib.add_user_to_project(
  950. session=self.session,
  951. project=repo,
  952. new_user='foo',
  953. user='pingou',
  954. )
  955. self.session.commit()
  956. self.assertEqual(msg, 'User added')
  957. # Add a forked project
  958. item = pagure.lib.model.Project(
  959. user_id=1, # pingou
  960. name='test3',
  961. description='test project #2',
  962. is_fork=True,
  963. parent_id=1,
  964. hook_token='aaabbbvvv',
  965. )
  966. self.session.add(item)
  967. self.session.commit()
  968. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  969. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  970. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  971. self.assertTrue(os.path.exists(outputconf))
  972. with open(outputconf) as stream:
  973. data = stream.read()
  974. exp = """repo docs/test
  975. R = @all
  976. RW+ = pingou
  977. RW+ = foo
  978. repo tickets/test
  979. RW+ = pingou
  980. RW+ = foo
  981. repo requests/test
  982. RW+ = pingou
  983. RW+ = foo
  984. repo test2
  985. R = @all
  986. RW+ = pingou
  987. repo docs/test2
  988. R = @all
  989. RW+ = pingou
  990. repo tickets/test2
  991. RW+ = pingou
  992. repo requests/test2
  993. RW+ = pingou
  994. repo somenamespace/test3
  995. R = @all
  996. RW+ = pingou
  997. repo docs/somenamespace/test3
  998. R = @all
  999. RW+ = pingou
  1000. repo tickets/somenamespace/test3
  1001. RW+ = pingou
  1002. repo requests/somenamespace/test3
  1003. RW+ = pingou
  1004. repo forks/pingou/test3
  1005. R = @all
  1006. RW+ = pingou
  1007. repo docs/forks/pingou/test3
  1008. R = @all
  1009. RW+ = pingou
  1010. repo tickets/forks/pingou/test3
  1011. RW+ = pingou
  1012. repo requests/forks/pingou/test3
  1013. RW+ = pingou
  1014. # end of body
  1015. """
  1016. #print data
  1017. self.assertEqual(data, exp)
  1018. os.unlink(outputconf)
  1019. self.assertFalse(os.path.exists(outputconf))
  1020. @patch.dict('pagure.config.config', {'PR_ONLY': True})
  1021. def test_write_gitolite_global_pr_only(self):
  1022. """ Test the write_gitolite_acls function of pagure.lib.git.
  1023. when the pagure instance enforces the PR approach.
  1024. """
  1025. tests.create_projects(self.session)
  1026. repo = pagure.lib._get_project(self.session, 'test')
  1027. self.assertFalse(repo.settings['pull_request_access_only'])
  1028. # Add an user to a project
  1029. # The user will be an admin of the project
  1030. msg = pagure.lib.add_user_to_project(
  1031. session=self.session,
  1032. project=repo,
  1033. new_user='foo',
  1034. user='pingou',
  1035. )
  1036. self.session.commit()
  1037. self.assertEqual(msg, 'User added')
  1038. # Add a forked project
  1039. item = pagure.lib.model.Project(
  1040. user_id=1, # pingou
  1041. name='test3',
  1042. description='test project #2',
  1043. is_fork=True,
  1044. parent_id=1,
  1045. hook_token='aaabbbvvv',
  1046. )
  1047. self.session.add(item)
  1048. self.session.commit()
  1049. outputconf = os.path.join(self.path, 'test_gitolite.conf')
  1050. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  1051. helper.write_gitolite_acls(self.session, outputconf, project=-1)
  1052. self.assertTrue(os.path.exists(outputconf))
  1053. with open(outputconf) as stream:
  1054. data = stream.read()
  1055. exp = """repo docs/test
  1056. R = @all
  1057. RW+ = pingou
  1058. RW+ = foo
  1059. repo tickets/test
  1060. RW+ = pingou
  1061. RW+ = foo
  1062. repo requests/test
  1063. RW+ = pingou
  1064. RW+ = foo
  1065. repo docs/test2
  1066. R = @all
  1067. RW+ = pingou
  1068. repo tickets/test2
  1069. RW+ = pingou
  1070. repo requests/test2
  1071. RW+ = pingou
  1072. repo docs/somenamespace/test3
  1073. R = @all
  1074. RW+ = pingou
  1075. repo tickets/somenamespace/test3
  1076. RW+ = pingou
  1077. repo requests/somenamespace/test3
  1078. RW+ = pingou
  1079. repo forks/pingou/test3
  1080. R = @all
  1081. RW+ = pingou
  1082. repo docs/forks/pingou/test3
  1083. R = @all
  1084. RW+ = pingou
  1085. repo tickets/forks/pingou/test3
  1086. RW+ = pingou
  1087. repo requests/forks/pingou/test3
  1088. RW+ = pingou
  1089. # end of body
  1090. """
  1091. #print data
  1092. self.assertEqual(data, exp)
  1093. os.unlink(outputconf)
  1094. self.assertFalse(os.path.exists(outputconf))
  1095. @patch('pagure.lib.notify.send_email')
  1096. def test_update_git(self, email_f):
  1097. """ Test the update_git of pagure.lib.git. """
  1098. email_f.return_value = True
  1099. # Create project
  1100. item = pagure.lib.model.Project(
  1101. user_id=1, # pingou
  1102. name='test_ticket_repo',
  1103. description='test project for ticket',
  1104. hook_token='aaabbbwww',
  1105. )
  1106. self.session.add(item)
  1107. self.session.commit()
  1108. # Create repo
  1109. self.gitrepo = os.path.join(self.path, 'repos', 'tickets',
  1110. 'test_ticket_repo.git')
  1111. pygit2.init_repository(self.gitrepo, bare=True)
  1112. repo = pagure.lib.get_authorized_project(self.session, 'test_ticket_repo')
  1113. # Create an issue to play with
  1114. msg = pagure.lib.new_issue(
  1115. session=self.session,
  1116. repo=repo,
  1117. title='Test issue',
  1118. content='We should work on this',
  1119. user='pingou',
  1120. )
  1121. self.assertEqual(msg.title, 'Test issue')
  1122. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1123. pagure.lib.git.update_git(issue, repo).get()
  1124. repo = pygit2.Repository(self.gitrepo)
  1125. commit = repo.revparse_single('HEAD')
  1126. # Use patch to validate the repo
  1127. commit_patch = pagure.lib.git.commit_to_patch(repo, commit)
  1128. exp = """Mon Sep 17 00:00:00 2001
  1129. From: pagure <pagure>
  1130. Subject: Updated issue <hash>: Test issue
  1131. ---
  1132. diff --git a/123 b/456
  1133. new file mode 100644
  1134. index 0000000..60f7480
  1135. --- /dev/null
  1136. +++ b/456
  1137. @@ -0,0 +1,28 @@
  1138. +{
  1139. + "assignee": null,
  1140. + "blocks": [],
  1141. + "close_status": null,
  1142. + "closed_at": null,
  1143. + "comments": [],
  1144. + "content": "We should work on this",
  1145. + "custom_fields": [],
  1146. + "date_created": null,
  1147. + "depends": [],
  1148. + "id": 1,
  1149. + "last_updated": null,
  1150. + "milestone": null,
  1151. + "priority": null,
  1152. + "private": false,
  1153. + "status": "Open",
  1154. + "tags": [],
  1155. + "title": "Test issue",
  1156. + "user": {
  1157. + "default_email": "bar@pingou.com",
  1158. + "emails": [
  1159. + "bar@pingou.com",
  1160. + "foo@pingou.com"
  1161. + ],
  1162. + "fullname": "PY C",
  1163. + "name": "pingou"
  1164. + }
  1165. +}
  1166. \ No newline at end of file
  1167. """
  1168. npatch = []
  1169. for row in commit_patch.split('\n'):
  1170. if row.startswith('Date:'):
  1171. continue
  1172. elif row.startswith('From '):
  1173. row = row.split(' ', 2)[2]
  1174. elif row.startswith('diff --git '):
  1175. row = row.split(' ')
  1176. row[2] = 'a/123'
  1177. row[3] = 'b/456'
  1178. row = ' '.join(row)
  1179. elif 'Updated issue' in row:
  1180. row = row.split()
  1181. row[3] = '<hash>:'
  1182. row = ' '.join(row)
  1183. elif 'date_created' in row:
  1184. t = row.split(': ')[0]
  1185. row = '%s: null,' % t
  1186. elif 'last_updated' in row:
  1187. t = row.split(': ')[0]
  1188. row = '%s: null,' % t
  1189. elif 'closed_at' in row:
  1190. t = row.split(': ')[0]
  1191. row = '%s: null,' % t
  1192. elif row.startswith('index 00'):
  1193. row = 'index 0000000..60f7480'
  1194. elif row.startswith('+++ b/'):
  1195. row = '+++ b/456'
  1196. npatch.append(row)
  1197. commit_patch = '\n'.join(npatch)
  1198. #print commit_patch
  1199. self.assertEqual(commit_patch, exp)
  1200. # Enforce having a different last_updated field
  1201. # This is required as the test run fine and fast with sqlite but is
  1202. # much slower with postgresql so we end-up with an updated
  1203. # last_updated in postgresql but not with sqlite
  1204. time.sleep(1)
  1205. # Test again after adding a comment
  1206. # We need to make sure we wait for worker to commit the comment
  1207. with patch('pagure.lib.git._maybe_wait', tests.definitely_wait):
  1208. msg = pagure.lib.add_issue_comment(
  1209. session=self.session,
  1210. issue=issue,
  1211. comment='Hey look a comment!',
  1212. user='foo',
  1213. )
  1214. self.session.commit()
  1215. self.assertEqual(msg, 'Comment added')
  1216. # Use patch to validate the repo
  1217. repo = pygit2.Repository(self.gitrepo)
  1218. commit = repo.revparse_single('HEAD')
  1219. commit_patch = pagure.lib.git.commit_to_patch(repo, commit)
  1220. exp = """Mon Sep 17 00:00:00 2001
  1221. From: pagure <pagure>
  1222. Subject: Updated issue <hash>: Test issue
  1223. ---
  1224. diff --git a/123 b/456
  1225. index 458821a..77674a8
  1226. --- a/123
  1227. +++ b/456
  1228. @@ -3,13 +3,32 @@
  1229. "blocks": [],
  1230. "close_status": null,
  1231. "closed_at": null,
  1232. - "comments": [],
  1233. + "comments": [
  1234. + {
  1235. + "comment": "Hey look a comment!",
  1236. + "date_created": null,
  1237. + "edited_on": null,
  1238. + "editor": null,
  1239. + "id": 1,
  1240. + "notification": false,
  1241. + "parent": null,
  1242. + "reactions": {},
  1243. + "user": {
  1244. + "default_email": "foo@bar.com",
  1245. + "emails": [
  1246. + "foo@bar.com"
  1247. + ],
  1248. + "fullname": "foo bar",
  1249. + "name": "foo"
  1250. + }
  1251. + }
  1252. + ],
  1253. "content": "We should work on this",
  1254. "custom_fields": [],
  1255. "date_created": null,
  1256. "depends": [],
  1257. "id": 1,
  1258. - "last_updated": "<date>",
  1259. + "last_updated": "<date>",
  1260. "milestone": null,
  1261. "priority": null,
  1262. "private": false,
  1263. """
  1264. npatch = []
  1265. for row in commit_patch.split('\n'):
  1266. if row.startswith('Date:'):
  1267. continue
  1268. elif row.startswith('From '):
  1269. row = row.split(' ', 2)[2]
  1270. elif row.startswith('diff --git '):
  1271. row = row.split(' ')
  1272. row[2] = 'a/123'
  1273. row[3] = 'b/456'
  1274. row = ' '.join(row)
  1275. elif 'Updated issue' in row:
  1276. row = row.split()
  1277. row[3] = '<hash>:'
  1278. row = ' '.join(row)
  1279. elif 'date_created' in row:
  1280. t = row.split(': ')[0]
  1281. row = '%s: null,' % t
  1282. elif 'closed_at' in row:
  1283. t = row.split(': ')[0]
  1284. row = '%s: null,' % t
  1285. elif row.startswith('index'):
  1286. row = 'index 458821a..77674a8'
  1287. elif row.startswith('--- a/'):
  1288. row = '--- a/123'
  1289. elif row.startswith('+++ b/'):
  1290. row = '+++ b/456'
  1291. elif 'last_updated' in row:
  1292. t = row.split(': ')[0]
  1293. row = '%s: "<date>",' % t
  1294. npatch.append(row)
  1295. commit_patch = '\n'.join(npatch)
  1296. # print commit_patch
  1297. self.assertEqual(commit_patch, exp)
  1298. def test_clean_git(self):
  1299. """ Test the clean_git method of pagure.lib.git. """
  1300. self.test_update_git()
  1301. gitpath = os.path.join(self.path, 'repos', 'tickets',
  1302. 'test_ticket_repo.git')
  1303. gitrepo = pygit2.init_repository(gitpath, bare=True)
  1304. # Get the uid of the ticket created
  1305. commit = gitrepo.revparse_single('HEAD')
  1306. patch = pagure.lib.git.commit_to_patch(gitrepo, commit)
  1307. hash_file = None
  1308. for row in patch.split('\n'):
  1309. if row.startswith('+++ b/'):
  1310. hash_file = row.split('+++ b/')[-1]
  1311. break
  1312. # The only file in git is the one of that ticket
  1313. files = [entry.name for entry in commit.tree]
  1314. self.assertEqual(files, [hash_file])
  1315. repo = pagure.lib.get_authorized_project(self.session, 'test_ticket_repo')
  1316. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1317. pagure.lib.git.clean_git(repo, issue.repotype, issue.uid).get()
  1318. # No more files in the git repo
  1319. commit = gitrepo.revparse_single('HEAD')
  1320. files = [entry.name for entry in commit.tree]
  1321. self.assertEqual(files, [])
  1322. @patch('pagure.lib.notify.send_email')
  1323. def test_update_git_requests(self, email_f):
  1324. """ Test the update_git of pagure.lib.git for pull-requests. """
  1325. email_f.return_value = True
  1326. # Create project
  1327. item = pagure.lib.model.Project(
  1328. user_id=1, # pingou
  1329. name='test_ticket_repo',
  1330. description='test project for ticket',
  1331. hook_token='aaabbbxxx',
  1332. )
  1333. self.session.add(item)
  1334. self.session.commit()
  1335. # Create repo
  1336. self.gitrepo = os.path.join(self.path, 'repos', 'requests',
  1337. 'test_ticket_repo.git')
  1338. pygit2.init_repository(self.gitrepo, bare=True)
  1339. # Create a PR to play with
  1340. repo = pagure.lib.get_authorized_project(self.session, 'test_ticket_repo')
  1341. # Create an issue to play with
  1342. req = pagure.lib.new_pull_request(
  1343. session=self.session,
  1344. repo_from=repo,
  1345. branch_from='feature',
  1346. repo_to=repo,
  1347. branch_to='master',
  1348. title='test PR',
  1349. user='pingou',
  1350. requestuid='foobar',
  1351. requestid=None,
  1352. status='Open',
  1353. notify=True
  1354. )
  1355. self.assertEqual(req.id, 1)
  1356. self.assertEqual(req.title, 'test PR')
  1357. request = repo.requests[0]
  1358. self.assertEqual(request.title, 'test PR')
  1359. pagure.lib.git.update_git(request, request.project).get()
  1360. repo = pygit2.Repository(self.gitrepo)
  1361. commit = repo.revparse_single('HEAD')
  1362. # Use patch to validate the repo
  1363. patch = pagure.lib.git.commit_to_patch(repo, commit)
  1364. exp = """Mon Sep 17 00:00:00 2001
  1365. From: pagure <pagure>
  1366. Subject: Updated pull-request <hash>: test PR
  1367. ---
  1368. diff --git a/123 b/456
  1369. new file mode 100644
  1370. index 0000000..60f7480
  1371. --- /dev/null
  1372. +++ b/456
  1373. @@ -0,0 +1,143 @@
  1374. +{
  1375. + "assignee": null,
  1376. + "branch": "master",
  1377. + "branch_from": "feature",
  1378. + "cached_merge_status": "unknown",
  1379. + "closed_at": null,
  1380. + "closed_by": null,
  1381. + "comments": [],
  1382. + "commit_start": null,
  1383. + "commit_stop": null,
  1384. + "date_created": null,
  1385. + "id": 1,
  1386. + "initial_comment": null,
  1387. + "last_updated": null,
  1388. + "project": {
  1389. + "access_groups": {
  1390. + "admin": [],
  1391. + "commit": [],
  1392. + "ticket": []
  1393. + },
  1394. + "access_users": {
  1395. + "admin": [],
  1396. + "commit": [],
  1397. + "owner": [
  1398. + "pingou"
  1399. + ],
  1400. + "ticket": []
  1401. + },
  1402. + "close_status": [],
  1403. + "custom_keys": [],
  1404. + "date_created": null,
  1405. + "date_modified": null,
  1406. + "description": "test project for ticket",
  1407. + "fullname": "test_ticket_repo",
  1408. + "id": 1,
  1409. + "milestones": {},
  1410. + "name": "test_ticket_repo",
  1411. + "namespace": null,
  1412. + "parent": null,
  1413. + "priorities": {},
  1414. + "settings": {
  1415. + "Enforce_signed-off_commits_in_pull-request": false,
  1416. + "Minimum_score_to_merge_pull-request": -1,
  1417. + "Only_assignee_can_merge_pull-request": false,
  1418. + "Web-hooks": null,
  1419. + "always_merge": false,
  1420. + "disable_non_fast-forward_merges": false,
  1421. + "fedmsg_notifications": true,
  1422. + "issue_tracker": true,
  1423. + "issue_tracker_read_only": false,
  1424. + "issues_default_to_private": false,
  1425. + "notify_on_commit_flag": false,
  1426. + "notify_on_pull-request_flag": false,
  1427. + "open_metadata_access_to_all": false,
  1428. + "project_documentation": false,
  1429. + "pull_request_access_only": false,
  1430. + "pull_requests": true,
  1431. + "stomp_notifications": true
  1432. + },
  1433. + "tags": [],
  1434. + "url_path": "test_ticket_repo",
  1435. + "user": {
  1436. + "default_email": "bar@pingou.com",
  1437. + "emails": [
  1438. + "bar@pingou.com",
  1439. + "foo@pingou.com"
  1440. + ],
  1441. + "fullname": "PY C",
  1442. + "name": "pingou"
  1443. + }
  1444. + },
  1445. + "remote_git": null,
  1446. + "repo_from": {
  1447. + "access_groups": {
  1448. + "admin": [],
  1449. + "commit": [],
  1450. + "ticket": []
  1451. + },
  1452. + "access_users": {
  1453. + "admin": [],
  1454. + "commit": [],
  1455. + "owner": [
  1456. + "pingou"
  1457. + ],
  1458. + "ticket": []
  1459. + },
  1460. + "close_status": [],
  1461. + "custom_keys": [],
  1462. + "date_created": null,
  1463. + "date_modified": null,
  1464. + "description": "test project for ticket",
  1465. + "fullname": "test_ticket_repo",
  1466. + "id": 1,
  1467. + "milestones": {},
  1468. + "name": "test_ticket_repo",
  1469. + "namespace": null,
  1470. + "parent": null,
  1471. + "priorities": {},
  1472. + "settings": {
  1473. + "Enforce_signed-off_commits_in_pull-request": false,
  1474. + "Minimum_score_to_merge_pull-request": -1,
  1475. + "Only_assignee_can_merge_pull-request": false,
  1476. + "Web-hooks": null,
  1477. + "always_merge": false,
  1478. + "disable_non_fast-forward_merges": false,
  1479. + "fedmsg_notifications": true,
  1480. + "issue_tracker": true,
  1481. + "issue_tracker_read_only": false,
  1482. + "issues_default_to_private": false,
  1483. + "notify_on_commit_flag": false,
  1484. + "notify_on_pull-request_flag": false,
  1485. + "open_metadata_access_to_all": false,
  1486. + "project_documentation": false,
  1487. + "pull_request_access_only": false,
  1488. + "pull_requests": true,
  1489. + "stomp_notifications": true
  1490. + },
  1491. + "tags": [],
  1492. + "url_path": "test_ticket_repo",
  1493. + "user": {
  1494. + "default_email": "bar@pingou.com",
  1495. + "emails": [
  1496. + "bar@pingou.com",
  1497. + "foo@pingou.com"
  1498. + ],
  1499. + "fullname": "PY C",
  1500. + "name": "pingou"
  1501. + }
  1502. + },
  1503. + "status": "Open",
  1504. + "title": "test PR",
  1505. + "uid": "foobar",
  1506. + "updated_on": null,
  1507. + "user": {
  1508. + "default_email": "bar@pingou.com",
  1509. + "emails": [
  1510. + "bar@pingou.com",
  1511. + "foo@pingou.com"
  1512. + ],
  1513. + "fullname": "PY C",
  1514. + "name": "pingou"
  1515. + }
  1516. +}
  1517. \ No newline at end of file
  1518. """
  1519. npatch = []
  1520. for row in patch.split('\n'):
  1521. if row.startswith('Date:'):
  1522. continue
  1523. elif row.startswith('From '):
  1524. row = row.split(' ', 2)[2]
  1525. elif row.startswith('diff --git '):
  1526. row = row.split(' ')
  1527. row[2] = 'a/123'
  1528. row[3] = 'b/456'
  1529. row = ' '.join(row)
  1530. elif 'Updated pull-request' in row:
  1531. row = row.split()
  1532. row[3] = '<hash>:'
  1533. row = ' '.join(row)
  1534. elif 'date_created' in row:
  1535. t = row.split(': ')[0]
  1536. row = '%s: null,' % t
  1537. elif 'date_modified' in row:
  1538. t = row.split(': ')[0]
  1539. row = '%s: null,' % t
  1540. elif 'last_updated' in row:
  1541. t = row.split(': ')[0]
  1542. row = '%s: null,' % t
  1543. elif 'updated_on' in row:
  1544. t = row.split(': ')[0]
  1545. row = '%s: null,' % t
  1546. elif row.startswith('index 00'):
  1547. row = 'index 0000000..60f7480'
  1548. elif row.startswith('+++ b/'):
  1549. row = '+++ b/456'
  1550. npatch.append(row)
  1551. patch = '\n'.join(npatch)
  1552. print(patch)
  1553. self.assertEqual(patch, exp)
  1554. def test_update_ticket_from_git_no_priority(self):
  1555. """ Test the update_ticket_from_git method from pagure.lib.git. """
  1556. tests.create_projects(self.session)
  1557. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1558. # Before
  1559. self.assertEqual(len(repo.issues), 0)
  1560. self.assertEqual(repo.issues, [])
  1561. data = {
  1562. "status": "Open", "title": "foo", "comments": [],
  1563. "content": "bar", "date_created": "1426500263",
  1564. "user": {
  1565. "name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1566. "milestone": "Next Release",
  1567. "priority": 1,
  1568. }
  1569. # Invalid project
  1570. self.assertRaises(
  1571. pagure.exceptions.PagureException,
  1572. pagure.lib.git.update_ticket_from_git,
  1573. self.session,
  1574. reponame='foobar',
  1575. namespace=None,
  1576. username=None,
  1577. issue_uid='foobar',
  1578. json_data=data,
  1579. agent='pingou',
  1580. )
  1581. # Create the issue
  1582. data = {
  1583. "status": "Open", "title": "foo", "comments": [],
  1584. "content": "bar", "date_created": "1426500263",
  1585. "user": {
  1586. "name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1587. "milestone": "Next Release",
  1588. }
  1589. pagure.lib.git.update_ticket_from_git(
  1590. self.session, reponame='test', namespace=None, username=None,
  1591. issue_uid='foobar', json_data=data, agent='pingou',
  1592. )
  1593. self.session.commit()
  1594. # Edit the issue
  1595. data = {
  1596. "status": "Open", "title": "foo", "comments": [],
  1597. "content": "bar", "date_created": "1426500263",
  1598. "user": {
  1599. "name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1600. "milestone": "Next Release",
  1601. "priority": 1,
  1602. }
  1603. pagure.lib.git.update_ticket_from_git(
  1604. self.session, reponame='test', namespace=None, username=None,
  1605. issue_uid='foobar', json_data=data, agent='pingou',
  1606. )
  1607. self.session.commit()
  1608. # Data contained a priority but not the project, so bailing
  1609. self.assertEqual(len(repo.issues), 1)
  1610. self.assertEqual(repo.issues[0].id, 1)
  1611. self.assertEqual(repo.issues[0].uid, 'foobar')
  1612. self.assertEqual(repo.issues[0].title, 'foo')
  1613. self.assertEqual(repo.issues[0].depending_text, [])
  1614. self.assertEqual(repo.issues[0].blocking_text, [])
  1615. self.assertEqual(repo.issues[0].milestone, 'Next Release')
  1616. self.assertEqual(repo.issues[0].priority, None)
  1617. self.assertEqual(
  1618. repo.milestones,
  1619. {'Next Release': {'active': True, 'date': None}})
  1620. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  1621. def test_update_ticket_from_git_close_ticket(self):
  1622. """ Test the update_ticket_from_git method from pagure.lib.git. """
  1623. tests.create_projects(self.session)
  1624. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1625. # Before
  1626. self.assertEqual(len(repo.issues), 0)
  1627. self.assertEqual(repo.issues, [])
  1628. # Create the issue
  1629. data = {
  1630. "status": "Open",
  1631. "title": "foo",
  1632. "comments": [],
  1633. "content": "bar",
  1634. "date_created": "1426500263",
  1635. "user": {
  1636. "name": "foo",
  1637. "emails": ["foo@fedoraproject.org"]
  1638. },
  1639. "milestone": "Next Release",
  1640. }
  1641. pagure.lib.git.update_ticket_from_git(
  1642. self.session, reponame='test', namespace=None, username=None,
  1643. issue_uid='foobar', json_data=data, agent='pingou',
  1644. )
  1645. self.session.commit()
  1646. # Edit the issue
  1647. data = {
  1648. "status": "Closed",
  1649. "close_status": "Fixed",
  1650. "title": "foo",
  1651. "comments": [],
  1652. "content": "bar",
  1653. "date_created": "1426500263",
  1654. "user": {
  1655. "name": "foo",
  1656. "emails": ["foo@fedoraproject.org"]
  1657. },
  1658. "milestone": "Next Release",
  1659. }
  1660. pagure.lib.git.update_ticket_from_git(
  1661. self.session, reponame='test', namespace=None, username=None,
  1662. issue_uid='foobar', json_data=data, agent='pingou',
  1663. )
  1664. self.session.commit()
  1665. self.assertEqual(len(repo.issues), 1)
  1666. self.assertEqual(repo.issues[0].id, 1)
  1667. self.assertEqual(repo.issues[0].uid, 'foobar')
  1668. self.assertEqual(repo.issues[0].title, 'foo')
  1669. self.assertEqual(repo.issues[0].depending_text, [])
  1670. self.assertEqual(repo.issues[0].blocking_text, [])
  1671. self.assertEqual(repo.issues[0].milestone, 'Next Release')
  1672. self.assertEqual(repo.issues[0].priority, None)
  1673. self.assertEqual(
  1674. repo.milestones,
  1675. {'Next Release': {'active': True, 'date': None}})
  1676. self.assertEqual(repo.issues[0].status, 'Closed')
  1677. self.assertEqual(repo.issues[0].close_status, 'Fixed')
  1678. self.assertIsNotNone(repo.issues[0].closed_at)
  1679. self.assertEqual(
  1680. repo.issues[0].comments[-1].comment,
  1681. '**Metadata Update from @pingou**:\n'
  1682. '- Issue close_status updated to: Fixed\n'
  1683. '- Issue status updated to: Closed (was: Open)'
  1684. )
  1685. def test_update_ticket_from_git(self):
  1686. """ Test the update_ticket_from_git method from pagure.lib.git. """
  1687. tests.create_projects(self.session)
  1688. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1689. # Set some priorities to the project
  1690. repo.priorities = {'1': 'High', '2': 'Normal'}
  1691. self.session.add(repo)
  1692. self.session.commit()
  1693. # Before
  1694. self.assertEqual(len(repo.issues), 0)
  1695. self.assertEqual(repo.issues, [])
  1696. data = {
  1697. "status": "Open", "title": "foo", "comments": [],
  1698. "content": "bar", "date_created": "1426500263",
  1699. "user": {
  1700. "name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1701. "milestone": "Next Release",
  1702. "priority": 1,
  1703. }
  1704. self.assertRaises(
  1705. pagure.exceptions.PagureException,
  1706. pagure.lib.git.update_ticket_from_git,
  1707. self.session,
  1708. reponame='foobar',
  1709. namespace=None,
  1710. username=None,
  1711. issue_uid='foobar',
  1712. json_data=data,
  1713. agent='pingou',
  1714. )
  1715. pagure.lib.git.update_ticket_from_git(
  1716. self.session, reponame='test', namespace=None, username=None,
  1717. issue_uid='foobar', json_data=data, agent='pingou',
  1718. )
  1719. self.session.commit()
  1720. # After 1 insertion
  1721. self.assertEqual(len(repo.issues), 1)
  1722. self.assertEqual(repo.issues[0].id, 1)
  1723. self.assertEqual(repo.issues[0].uid, 'foobar')
  1724. self.assertEqual(repo.issues[0].title, 'foo')
  1725. self.assertEqual(repo.issues[0].depending_text, [])
  1726. self.assertEqual(repo.issues[0].blocking_text, [])
  1727. self.assertEqual(repo.issues[0].milestone, 'Next Release')
  1728. self.assertEqual(repo.issues[0].priority, 1)
  1729. self.assertIsNone(repo.issues[0].closed_at)
  1730. self.assertEqual(
  1731. repo.milestones,
  1732. {'Next Release': {'active': True, 'date': None}})
  1733. data["title"] = "fake issue for tests"
  1734. pagure.lib.git.update_ticket_from_git(
  1735. self.session, reponame='test', namespace=None, username=None,
  1736. issue_uid='foobar', json_data=data, agent='pingou',
  1737. )
  1738. self.session.commit()
  1739. # After edit
  1740. self.assertEqual(len(repo.issues), 1)
  1741. self.assertEqual(repo.issues[0].id, 1)
  1742. self.assertEqual(repo.issues[0].uid, 'foobar')
  1743. self.assertEqual(repo.issues[0].title, 'fake issue for tests')
  1744. self.assertEqual(repo.issues[0].depending_text, [])
  1745. self.assertEqual(repo.issues[0].blocking_text, [])
  1746. self.assertEqual(repo.issues[0].priority, 1)
  1747. self.assertIsNone(repo.issues[0].closed_at)
  1748. data = {
  1749. "status": "Open", "title": "Rename pagure", "private": False,
  1750. "content": "This is too much of a conflict with the book",
  1751. "user": {
  1752. "fullname": "Pierre-YvesChibon",
  1753. "name": "pingou",
  1754. "default_email": "pingou@fedoraproject.org",
  1755. "emails": ["pingou@fedoraproject.org"]
  1756. },
  1757. "id": 20,
  1758. "blocks": [1],
  1759. "depends": [3, 4],
  1760. "date_created": "1426595224",
  1761. "milestone": "Future",
  1762. "priority": 1,
  1763. "comments": [
  1764. {
  1765. "comment": "Nirik:\r\n\r\n- sourceforge++ \r\n- "
  1766. "gitmaker\r\n- mastergit \r\n- hostomatic\r\n- "
  1767. "gitcorp\r\n- git-keiretsu \r\n- gitbuffet\r\n- "
  1768. "cogitator\r\n- cogitate\r\n\r\nrandomuser:\r\n\r\n- "
  1769. "COLLABORATRON5000\r\n- git-sm\\u00f6rg\\u00e5sbord\r\n- "
  1770. "thislittlegittywenttomarket\r\n- git-o-rama\r\n- "
  1771. "gitsundheit",
  1772. "date_created": "1426595224", "id": 250, "parent": None,
  1773. "user": {
  1774. "fullname": "Pierre-YvesChibon",
  1775. "name": "pingou",
  1776. "default_email": "pingou@fedoraproject.org",
  1777. "emails": ["pingou@fedoraproject.org"]
  1778. }
  1779. },
  1780. {
  1781. "comment": "Nirik:\r\n\r\n- sourceforge++ \r\n- "
  1782. "gitmaker\r\n- mastergit \r\n- hostomatic\r\n- "
  1783. "gitcorp\r\n- git-keiretsu \r\n- gitbuffet\r\n- "
  1784. "cogitator\r\n- cogitate\r\n\r\nrandomuser:\r\n\r\n- "
  1785. "COLLABORATRON5000\r\n- git-sm\\u00f6rg\\u00e5sbord\r\n- "
  1786. "thislittlegittywenttomarket\r\n- git-o-rama\r\n- "
  1787. "gitsundheit",
  1788. "date_created": "1426595340", "id": 324, "parent": None,
  1789. "user": {
  1790. "fullname": "Ralph Bean",
  1791. "name": "ralph",
  1792. "default_email": "ralph@fedoraproject.org",
  1793. "emails": ["ralph@fedoraproject.org"]
  1794. }
  1795. }
  1796. ]
  1797. }
  1798. pagure.lib.git.update_ticket_from_git(
  1799. self.session, reponame='test', namespace=None, username=None,
  1800. issue_uid='foobar2', json_data=data, agent='pingou',
  1801. )
  1802. # After second insertion
  1803. self.assertEqual(len(repo.issues), 2)
  1804. self.assertEqual(repo.issues[0].uid, 'foobar')
  1805. self.assertEqual(repo.issues[0].title, 'fake issue for tests')
  1806. self.assertEqual(repo.issues[0].depending_text, [20])
  1807. self.assertEqual(repo.issues[0].blocking_text, [])
  1808. self.assertIsNone(repo.issues[0].closed_at)
  1809. # New one
  1810. self.assertEqual(repo.issues[1].uid, 'foobar2')
  1811. self.assertEqual(repo.issues[1].title, 'Rename pagure')
  1812. self.assertEqual(repo.issues[1].depending_text, [])
  1813. self.assertEqual(repo.issues[1].blocking_text, [1])
  1814. self.assertEqual(repo.issues[1].milestone, 'Future')
  1815. self.assertIsNone(repo.issues[1].closed_at)
  1816. self.assertDictEqual(
  1817. repo.milestones,
  1818. {
  1819. 'Future': {'active': True, 'date': None},
  1820. 'Next Release': {'active': True, 'date': None}
  1821. }
  1822. )
  1823. def test_update_request_from_git(self):
  1824. """ Test the update_request_from_git method from pagure.lib.git. """
  1825. tests.create_projects(self.session)
  1826. tests.create_projects_git(os.path.join(self.path, 'repos'))
  1827. repo = pagure.lib._get_project(self.session, 'test')
  1828. namespaced_repo = pagure.lib._get_project(self.session, 'test3', namespace='somenamespace')
  1829. # Before
  1830. self.assertEqual(len(repo.requests), 0)
  1831. self.assertEqual(repo.requests, [])
  1832. self.assertEqual(len(namespaced_repo.requests), 0)
  1833. self.assertEqual(namespaced_repo.requests, [])
  1834. data = {
  1835. "status": True,
  1836. "uid": "d4182a2ac2d541d884742d3037c26e56",
  1837. "project": {
  1838. "custom_keys": [],
  1839. "parent": None,
  1840. "settings": {
  1841. "issue_tracker": True,
  1842. "project_documentation": True,
  1843. "pull_requests": True,
  1844. },
  1845. "name": "test",
  1846. "date_created": "1426500194",
  1847. "tags": [],
  1848. "user": {
  1849. "fullname": "Pierre-YvesChibon",
  1850. "name": "pingou",
  1851. "default_email": "pingou@fedoraproject.org",
  1852. "emails": [
  1853. "pingou@fedoraproject.org"
  1854. ]
  1855. },
  1856. "id": 1,
  1857. "description": "test project"
  1858. },
  1859. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  1860. "user": {
  1861. "fullname": "Pierre-YvesChibon",
  1862. "name": "pingou",
  1863. "default_email": "pingou@fedoraproject.org",
  1864. "emails": ["pingou@fedoraproject.org"]
  1865. },
  1866. "id": 7,
  1867. "comments": [
  1868. {
  1869. "comment": "really?",
  1870. "user": {
  1871. "fullname": "Pierre-YvesChibon",
  1872. "name": "pingou",
  1873. "default_email": "pingou@fedoraproject.org",
  1874. "emails": ["pingou@fedoraproject.org"]
  1875. },
  1876. "parent": None,
  1877. "date_created": "1426843778",
  1878. "commit": "fa72f315373ec5f98f2b08c8ffae3645c97aaad2",
  1879. "line": 5,
  1880. "id": 1,
  1881. "filename": "test"
  1882. },
  1883. {
  1884. "comment": "Again ?",
  1885. "user": {
  1886. "fullname": "Pierre-YvesChibon",
  1887. "name": "pingou",
  1888. "default_email": "pingou@fedoraproject.org",
  1889. "emails": [
  1890. "pingou@fedoraproject.org"
  1891. ]
  1892. },
  1893. "parent": None,
  1894. "date_created": "1426866781",
  1895. "commit": "94ebaf900161394059478fd88aec30e59092a1d7",
  1896. "line": 5,
  1897. "id": 2,
  1898. "filename": "test2"
  1899. },
  1900. {
  1901. "comment": "Should be fine in fact",
  1902. "user": {
  1903. "fullname": "Pierre-YvesChibon",
  1904. "name": "pingou",
  1905. "default_email": "pingou@fedoraproject.org",
  1906. "emails": [
  1907. "pingou@fedoraproject.org"
  1908. ]
  1909. },
  1910. "parent": None,
  1911. "date_created": "1426866950",
  1912. "commit": "94ebaf900161394059478fd88aec30e59092a1d7",
  1913. "line": 5,
  1914. "id": 3,
  1915. "filename": "test2"
  1916. }
  1917. ],
  1918. "branch_from": "master",
  1919. "title": "test request",
  1920. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  1921. "repo_from": {
  1922. "parent": {
  1923. "custom_keys": [],
  1924. "parent": None,
  1925. "name": "test",
  1926. "date_created": "1426500194",
  1927. "tags": [],
  1928. "user": {
  1929. "fullname": "Pierre-YvesChibon",
  1930. "name": "pingou",
  1931. "default_email": "pingou@fedoraproject.org",
  1932. "emails": [
  1933. "pingou@fedoraproject.org"
  1934. ]
  1935. },
  1936. "settings": {
  1937. "issue_tracker": True,
  1938. "project_documentation": True,
  1939. "pull_requests": True,
  1940. },
  1941. "id": 1,
  1942. "description": "test project"
  1943. },
  1944. "settings": {
  1945. "issue_tracker": True,
  1946. "project_documentation": True,
  1947. "pull_requests": True,
  1948. },
  1949. "name": "test",
  1950. "date_created": "1426843440",
  1951. "custom_keys": [],
  1952. "tags": [],
  1953. "user": {
  1954. "fullname": "fake user",
  1955. "name": "fake",
  1956. "default_email": "fake@fedoraproject.org",
  1957. "emails": [
  1958. "fake@fedoraproject.org"
  1959. ]
  1960. },
  1961. "id": 6,
  1962. "description": "test project"
  1963. },
  1964. "branch": "master",
  1965. "date_created": "1426843732"
  1966. }
  1967. self.assertRaises(
  1968. pagure.exceptions.PagureException,
  1969. pagure.lib.git.update_request_from_git,
  1970. self.session,
  1971. reponame='foobar',
  1972. namespace=None,
  1973. username=None,
  1974. request_uid='d4182a2ac2d541d884742d3037c26e56',
  1975. json_data=data,
  1976. )
  1977. pagure.lib.git.update_request_from_git(
  1978. self.session,
  1979. reponame='test',
  1980. namespace=None,
  1981. username=None,
  1982. request_uid='d4182a2ac2d541d884742d3037c26e56',
  1983. json_data=data,
  1984. )
  1985. self.session.commit()
  1986. # After 1 st insertion
  1987. self.assertEqual(len(repo.requests), 1)
  1988. self.assertEqual(repo.requests[0].id, 7)
  1989. self.assertEqual(
  1990. repo.requests[0].uid, 'd4182a2ac2d541d884742d3037c26e56')
  1991. self.assertEqual(repo.requests[0].title, 'test request')
  1992. self.assertEqual(len(repo.requests[0].comments), 3)
  1993. data = {
  1994. "status": True,
  1995. "uid": "d4182a2ac2d541d884742d3037c26e57",
  1996. "project": {
  1997. "parent": None,
  1998. "name": "test",
  1999. "custom_keys": [],
  2000. "date_created": "1426500194",
  2001. "tags": [],
  2002. "user": {
  2003. "fullname": "Pierre-YvesChibon",
  2004. "name": "pingou",
  2005. "default_email": "pingou@fedoraproject.org",
  2006. "emails": [
  2007. "pingou@fedoraproject.org"
  2008. ]
  2009. },
  2010. "settings": {
  2011. "issue_tracker": True,
  2012. "project_documentation": True,
  2013. "pull_requests": True,
  2014. },
  2015. "id": 1,
  2016. "description": "test project"
  2017. },
  2018. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  2019. "user": {
  2020. "fullname": "Pierre-YvesChibon",
  2021. "name": "pingou",
  2022. "default_email": "pingou@fedoraproject.org",
  2023. "emails": ["pingou@fedoraproject.org"]
  2024. },
  2025. "id": 4,
  2026. "comments": [],
  2027. "branch_from": "master",
  2028. "title": "test request #2",
  2029. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  2030. "repo_from": {
  2031. "parent": {
  2032. "parent": None,
  2033. "name": "test",
  2034. "custom_keys": [],
  2035. "date_created": "1426500194",
  2036. "tags": [],
  2037. "user": {
  2038. "fullname": "Pierre-YvesChibon",
  2039. "name": "pingou",
  2040. "default_email": "pingou@fedoraproject.org",
  2041. "emails": [
  2042. "pingou@fedoraproject.org"
  2043. ]
  2044. },
  2045. "settings": {
  2046. "issue_tracker": True,
  2047. "project_documentation": True,
  2048. "pull_requests": True,
  2049. },
  2050. "id": 1,
  2051. "description": "test project"
  2052. },
  2053. "settings": {
  2054. "issue_tracker": True,
  2055. "project_documentation": True,
  2056. "pull_requests": True,
  2057. },
  2058. "name": "test",
  2059. "date_created": "1426843440",
  2060. "custom_keys": [],
  2061. "tags": [],
  2062. "user": {
  2063. "fullname": "fake user",
  2064. "name": "fake",
  2065. "default_email": "fake@fedoraproject.org",
  2066. "emails": [
  2067. "fake@fedoraproject.org"
  2068. ]
  2069. },
  2070. "project_docs": True,
  2071. "id": 6,
  2072. "description": "test project"
  2073. },
  2074. "branch": "master",
  2075. "date_created": "1426843745"
  2076. }
  2077. pagure.lib.git.update_request_from_git(
  2078. self.session,
  2079. reponame='test',
  2080. namespace=None,
  2081. username=None,
  2082. request_uid='d4182a2ac2d541d884742d3037c26e57',
  2083. json_data=data,
  2084. )
  2085. self.session.commit()
  2086. # After 2 nd insertion
  2087. self.assertEqual(len(repo.requests), 2)
  2088. self.assertEqual(repo.requests[0].id, 7)
  2089. self.assertEqual(
  2090. repo.requests[0].uid, 'd4182a2ac2d541d884742d3037c26e56')
  2091. self.assertEqual(repo.requests[0].title, 'test request')
  2092. self.assertEqual(len(repo.requests[0].comments), 3)
  2093. # 2 entry
  2094. self.assertEqual(repo.requests[1].id, 4)
  2095. self.assertEqual(
  2096. repo.requests[1].uid, 'd4182a2ac2d541d884742d3037c26e57')
  2097. self.assertEqual(repo.requests[1].title, 'test request #2')
  2098. self.assertEqual(len(repo.requests[1].comments), 0)
  2099. data = {
  2100. "status": True,
  2101. "uid": "d4182a2ac2d541d884742d3037c26e58",
  2102. "project": {
  2103. "parent": None,
  2104. "name": "test3",
  2105. "custom_keys": [],
  2106. "namespace": "somenamespace",
  2107. "date_created": "1426500194",
  2108. "tags": [],
  2109. "user": {
  2110. "fullname": "Pierre-YvesChibon",
  2111. "name": "pingou",
  2112. "default_email": "pingou@fedoraproject.org",
  2113. "emails": [
  2114. "pingou@fedoraproject.org"
  2115. ]
  2116. },
  2117. "settings": {
  2118. "issue_tracker": True,
  2119. "project_documentation": True,
  2120. "pull_requests": True,
  2121. },
  2122. "id": 3,
  2123. "description": "namespaced test project"
  2124. },
  2125. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  2126. "user": {
  2127. "fullname": "Pierre-YvesChibon",
  2128. "name": "pingou",
  2129. "default_email": "pingou@fedoraproject.org",
  2130. "emails": ["pingou@fedoraproject.org"]
  2131. },
  2132. "id": 5,
  2133. "comments": [],
  2134. "branch_from": "master",
  2135. "title": "test request to namespaced repo",
  2136. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  2137. "repo_from": {
  2138. "parent": {
  2139. "parent": None,
  2140. "name": "test",
  2141. "custom_keys": [],
  2142. "date_created": "1426500194",
  2143. "tags": [],
  2144. "user": {
  2145. "fullname": "Pierre-YvesChibon",
  2146. "name": "pingou",
  2147. "default_email": "pingou@fedoraproject.org",
  2148. "emails": [
  2149. "pingou@fedoraproject.org"
  2150. ]
  2151. },
  2152. "settings": {
  2153. "issue_tracker": True,
  2154. "project_documentation": True,
  2155. "pull_requests": True,
  2156. },
  2157. "id": 1,
  2158. "description": "test project"
  2159. },
  2160. "settings": {
  2161. "issue_tracker": True,
  2162. "project_documentation": True,
  2163. "pull_requests": True,
  2164. },
  2165. "name": "test",
  2166. "date_created": "1426843440",
  2167. "custom_keys": [],
  2168. "tags": [],
  2169. "user": {
  2170. "fullname": "fake user",
  2171. "name": "fake",
  2172. "default_email": "fake@fedoraproject.org",
  2173. "emails": [
  2174. "fake@fedoraproject.org"
  2175. ]
  2176. },
  2177. "project_docs": True,
  2178. "id": 6,
  2179. "description": "test project"
  2180. },
  2181. "branch": "master",
  2182. "date_created": "1426843745"
  2183. }
  2184. pagure.lib.git.update_request_from_git(
  2185. self.session,
  2186. reponame='test3',
  2187. namespace='somenamespace',
  2188. username=None,
  2189. request_uid='d4182a2ac2d541d884742d3037c26e58',
  2190. json_data=data,
  2191. )
  2192. self.session.commit()
  2193. self.assertEqual(len(namespaced_repo.requests), 1)
  2194. self.assertEqual(namespaced_repo.requests[0].id, 5)
  2195. self.assertEqual(
  2196. namespaced_repo.requests[0].uid,
  2197. 'd4182a2ac2d541d884742d3037c26e58'
  2198. )
  2199. self.assertEqual(
  2200. namespaced_repo.requests[0].title,
  2201. 'test request to namespaced repo'
  2202. )
  2203. def test_read_git_lines(self):
  2204. """ Test the read_git_lines method of pagure.lib.git. """
  2205. self.test_update_git()
  2206. gitrepo = os.path.join(self.path, 'repos', 'tickets', 'test_ticket_repo.git')
  2207. output = pagure.lib.git.read_git_lines(
  2208. ['log', '-1', "--pretty='%s'"], gitrepo)
  2209. self.assertEqual(len(output), 1)
  2210. self.assertTrue(
  2211. output[0].startswith("'Updated issue ")
  2212. )
  2213. self.assertTrue(
  2214. output[0].endswith(": Test issue'")
  2215. )
  2216. # Keeping the new line symbol
  2217. output = pagure.lib.git.read_git_lines(
  2218. ['log', '-1', "--pretty='%s'"], gitrepo, keepends=True)
  2219. self.assertEqual(len(output), 1)
  2220. self.assertTrue(
  2221. output[0].endswith(": Test issue'\n")
  2222. )
  2223. def test_get_revs_between(self):
  2224. """ Test the get_revs_between method of pagure.lib.git. """
  2225. self.test_update_git()
  2226. gitrepo = os.path.join(self.path, 'repos', 'tickets', 'test_ticket_repo.git')
  2227. output = pagure.lib.git.read_git_lines(
  2228. ['log', '-3', "--pretty='%H'"], gitrepo)
  2229. self.assertEqual(len(output), 2)
  2230. from_hash = output[1].replace("'", '')
  2231. # Case 1, repo BASE is null and HEAD is equal to from_hash
  2232. to_hash = '0'
  2233. output1 = pagure.lib.git.get_revs_between(
  2234. to_hash, from_hash, gitrepo, 'refs/heads/master')
  2235. self.assertEqual(output1, [from_hash])
  2236. # Case 2, get revs between two commits (to_hash, from_hash)
  2237. to_hash = output[0].replace("'", '')
  2238. output2 = pagure.lib.git.get_revs_between(
  2239. to_hash, from_hash, gitrepo, 'refs/heads/master')
  2240. self.assertEqual(output2, [to_hash])
  2241. # Case 3, get revs between two commits (from_hash, to_hash)
  2242. output3 = pagure.lib.git.get_revs_between(
  2243. from_hash, to_hash, gitrepo, 'refs/heads/master')
  2244. self.assertEqual(output3, [to_hash])
  2245. # Case 4, get revs between two commits on two different branches
  2246. newgitrepo = tempfile.mkdtemp(prefix='pagure-')
  2247. newrepo = pygit2.clone_repository(gitrepo, newgitrepo)
  2248. newrepo.create_branch('feature', newrepo.head.get_object())
  2249. with open(os.path.join(newgitrepo, 'sources'), 'w') as stream:
  2250. stream.write('foo\n bar')
  2251. newrepo.index.add('sources')
  2252. newrepo.index.write()
  2253. # Commits the files added
  2254. tree = newrepo.index.write_tree()
  2255. author = pygit2.Signature(
  2256. 'Alice Author', 'alice@authors.tld')
  2257. committer = pygit2.Signature(
  2258. 'Cecil Committer', 'cecil@committers.tld')
  2259. newrepo.create_commit(
  2260. 'refs/heads/feature', # the name of the reference to update
  2261. author,
  2262. committer,
  2263. 'Add sources file for testing',
  2264. # binary string representing the tree object ID
  2265. tree,
  2266. # list of binary strings representing parents of the new commit
  2267. [to_hash]
  2268. )
  2269. branch_commit = newrepo.revparse_single('refs/heads/feature')
  2270. # Push to origin
  2271. ori_remote = newrepo.remotes[0]
  2272. PagureRepo.push(ori_remote, 'refs/heads/feature')
  2273. # Remove the clone
  2274. shutil.rmtree(newgitrepo)
  2275. output4 = pagure.lib.git.get_revs_between(
  2276. '0', branch_commit.oid.hex, gitrepo, 'refs/heads/feature')
  2277. self.assertEqual(output4, [branch_commit.oid.hex])
  2278. def test_get_author(self):
  2279. """ Test the get_author method of pagure.lib.git. """
  2280. self.test_update_git()
  2281. gitrepo = os.path.join(self.path, 'repos', 'tickets', 'test_ticket_repo.git')
  2282. output = pagure.lib.git.read_git_lines(
  2283. ['log', '-3', "--pretty='%H'"], gitrepo)
  2284. self.assertEqual(len(output), 2)
  2285. for githash in output:
  2286. githash = githash.replace("'", '')
  2287. output = pagure.lib.git.get_author(githash, gitrepo)
  2288. self.assertEqual(output, 'pagure')
  2289. def get_author_email(self):
  2290. """ Test the get_author_email method of pagure.lib.git. """
  2291. self.test_update_git()
  2292. gitrepo = os.path.join(self.path, 'tickets', 'test_ticket_repo.git')
  2293. output = pagure.lib.git.read_git_lines(
  2294. ['log', '-3', "--pretty='%H'"], gitrepo)
  2295. self.assertEqual(len(output), 2)
  2296. for githash in output:
  2297. githash = githash.replace("'", '')
  2298. output = pagure.lib.git.get_author_email(githash, gitrepo)
  2299. self.assertEqual(output, 'pagure')
  2300. def test_get_repo_name(self):
  2301. """ Test the get_repo_name method of pagure.lib.git. """
  2302. def runtest(reponame, *path):
  2303. gitrepo = os.path.join(self.path, 'repos', *path)
  2304. os.makedirs(gitrepo)
  2305. repo_name = pagure.lib.git.get_repo_name(gitrepo)
  2306. self.assertEqual(repo_name, reponame)
  2307. runtest('test_ticket_repo', 'tickets', 'test_ticket_repo.git')
  2308. runtest('test', 'test.git')
  2309. runtest('foo.test', 'foo.test.git')
  2310. def test_get_username(self):
  2311. """ Test the get_username method of pagure.lib.git. """
  2312. def runtest(username, *path):
  2313. gitrepo = os.path.join(self.path, 'repos', *path)
  2314. os.makedirs(gitrepo)
  2315. repo_username = pagure.lib.git.get_username(gitrepo)
  2316. self.assertEqual(repo_username, username)
  2317. runtest(None, 'tickets', 'test_ticket_repo.git')
  2318. runtest(None, 'test.git')
  2319. runtest(None, 'foo.test.git')
  2320. runtest('pingou', 'forks', 'pingou', 'foo.test.git')
  2321. runtest('pingou', 'forks', 'pingou', 'bar/foo.test.git')
  2322. def test_get_repo_namespace(self):
  2323. """ Test the get_repo_namespace method of pagure.lib.git. """
  2324. def runtest(namespace, *path):
  2325. gitrepo = os.path.join(self.path, 'repos', *path)
  2326. if not os.path.exists(gitrepo):
  2327. os.makedirs(gitrepo)
  2328. repo_namespace = pagure.lib.git.get_repo_namespace(gitrepo)
  2329. self.assertEqual(repo_namespace, namespace)
  2330. runtest(None, 'test_ticket_repo.git')
  2331. runtest('foo/bar/baz', 'foo', 'bar', 'baz', 'test.git')
  2332. runtest(None, 'foo.test.git')
  2333. runtest(None, 'forks', 'user', 'foo.test.git')
  2334. runtest('bar', 'forks', 'user', 'bar', 'foo.test.git')
  2335. runtest('ns/bar', 'forks', 'user', 'ns', 'bar', 'foo.test.git')
  2336. runtest('bar', 'forks', 'user', 'bar', 'foo.test.git')
  2337. def test_update_custom_fields_from_json(self):
  2338. """ Test the update_custom_fields_from_json method of lib.git """
  2339. tests.create_projects(self.session)
  2340. repo = pagure.lib._get_project(self.session, 'test')
  2341. # Create issues to play with
  2342. pagure.lib.new_issue(
  2343. session=self.session,
  2344. repo=repo,
  2345. title='Test issue',
  2346. content='We should work on this',
  2347. user='pingou',
  2348. issue_uid='someuid'
  2349. )
  2350. self.session.commit()
  2351. issue = pagure.lib.get_issue_by_uid(self.session, 'someuid')
  2352. # Fake json data, currently without custom_fields
  2353. # This should bring no new custom_fields to the issue
  2354. json_data = {
  2355. "status": "Open",
  2356. "title": "Test issue",
  2357. "private": False,
  2358. "content": "We should work on this",
  2359. "user": {
  2360. "fullname": "PY C",
  2361. "name": "pingou",
  2362. "default_email": "bar@pingou.com",
  2363. "emails": ["bar@pingou.com"]
  2364. },
  2365. "id": 1,
  2366. "blocks": [],
  2367. "depends": [],
  2368. "date_created": "1234567",
  2369. "comments": [],
  2370. }
  2371. pagure.lib.git.update_custom_field_from_json(
  2372. self.session, repo, issue, json_data)
  2373. updated_issue = pagure.lib.get_issue_by_uid(self.session, 'someuid')
  2374. self.assertEqual(updated_issue.to_json().get('custom_fields'), [])
  2375. custom_fields = [
  2376. {
  2377. "name": "custom1",
  2378. "key_type": "text",
  2379. "value": "value1",
  2380. "key_data": None,
  2381. },
  2382. {
  2383. "name": "custom2",
  2384. "key_type": "text",
  2385. "value": "value2",
  2386. "key_data": None,
  2387. }
  2388. ]
  2389. # Again, Fake the json data but, with custom_fields in it
  2390. # The updated issue should have the custom_fields as
  2391. # was in the json_data
  2392. json_data = {
  2393. "status": "Open",
  2394. "title": "Test issue",
  2395. "private": False,
  2396. "content": "We should work on this",
  2397. "user": {
  2398. "fullname": "PY C",
  2399. "name": "pingou",
  2400. "default_email": "bar@pingou.com",
  2401. "emails": ["bar@pingou.com"]
  2402. },
  2403. "id": 1,
  2404. "blocks": [],
  2405. "depends": [],
  2406. "date_created": "1234567",
  2407. "comments": [],
  2408. "custom_fields": custom_fields,
  2409. }
  2410. pagure.lib.git.update_custom_field_from_json(
  2411. self.session, repo, issue, json_data)
  2412. updated_issue = pagure.lib.get_issue_by_uid(self.session, 'someuid')
  2413. custom_fields_of_issue = updated_issue.to_json().get('custom_fields')
  2414. self.assertEqual(custom_fields_of_issue, custom_fields)
  2415. @patch('pagure.lib.notify.send_email')
  2416. @patch('pagure.lib.git.update_git')
  2417. def test_merge_pull_request_no_master(self, email_f, up_git):
  2418. """ Test the merge_pull_request function when there are no master
  2419. branch in the repo. """
  2420. email_f.return_value = True
  2421. up_git.return_value = True
  2422. gitfolder = os.path.join(self.path, 'repos')
  2423. docfolder = os.path.join(self.path, 'docs')
  2424. ticketfolder = os.path.join(self.path, 'tickets')
  2425. requestfolder = os.path.join(self.path, 'requests')
  2426. # Create project
  2427. item = pagure.lib.model.Project(
  2428. user_id=1, # pingou
  2429. name='test',
  2430. description='test project',
  2431. hook_token='aaabbbwww',
  2432. )
  2433. self.session.add(item)
  2434. self.session.commit()
  2435. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2436. gitrepo = os.path.join(gitfolder, repo.path)
  2437. docrepo = os.path.join(docfolder, repo.path)
  2438. ticketrepo = os.path.join(ticketfolder, repo.path)
  2439. requestrepo = os.path.join(requestfolder, repo.path)
  2440. os.makedirs(os.path.join(self.path, 'repos', 'forks', 'foo'))
  2441. self.gitrepo = os.path.join(self.path, 'repos', 'test.git')
  2442. os.makedirs(self.gitrepo)
  2443. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  2444. # Fork the project
  2445. task = pagure.lib.fork_project(
  2446. session=self.session,
  2447. user='foo',
  2448. repo=repo,
  2449. )
  2450. self.session.commit()
  2451. self.assertEqual(task.get(),
  2452. {'endpoint': 'ui_ns.view_repo',
  2453. 'repo': 'test',
  2454. 'username': 'foo',
  2455. 'namespace': None})
  2456. # Create repo, with some content
  2457. self.gitrepo = os.path.join(
  2458. self.path, 'repos', 'forks', 'foo', 'test.git')
  2459. tests.add_content_git_repo(self.gitrepo, branch='feature')
  2460. fork_repo = pagure.lib.get_authorized_project(self.session, 'test', user='foo')
  2461. # Create a PR to play with
  2462. req = pagure.lib.new_pull_request(
  2463. session=self.session,
  2464. repo_from=fork_repo,
  2465. branch_from='feature',
  2466. repo_to=repo,
  2467. branch_to='master',
  2468. title='test PR',
  2469. user='pingou',
  2470. requestuid='foobar',
  2471. requestid=None,
  2472. status='Open',
  2473. notify=True
  2474. )
  2475. self.assertEqual(req.id, 1)
  2476. self.assertEqual(req.title, 'test PR')
  2477. # `master` branch not found
  2478. msg = pagure.lib.git.merge_pull_request(
  2479. self.session,
  2480. request=req,
  2481. username='pingou',
  2482. domerge=False
  2483. )
  2484. self.assertEqual(msg, 'FFORWARD')
  2485. @patch('pagure.lib.notify.send_email')
  2486. @patch('pagure.lib.git.update_git')
  2487. def test_merge_pull_request_closed(self, email_f, up_git):
  2488. """ Test the merge_pull_request function when the PR was already
  2489. closed/merged. """
  2490. email_f.return_value = True
  2491. up_git.return_value = True
  2492. gitfolder = os.path.join(self.path, 'repos')
  2493. docfolder = os.path.join(self.path, 'docs')
  2494. ticketfolder = os.path.join(self.path, 'tickets')
  2495. requestfolder = os.path.join(self.path, 'requests')
  2496. # Create project
  2497. item = pagure.lib.model.Project(
  2498. user_id=1, # pingou
  2499. name='test',
  2500. description='test project',
  2501. hook_token='aaabbbwww',
  2502. )
  2503. self.session.add(item)
  2504. self.session.commit()
  2505. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2506. os.makedirs(os.path.join(self.path, 'repos', 'forks', 'foo'))
  2507. self.gitrepo = os.path.join(self.path, 'repos', 'test.git')
  2508. os.makedirs(self.gitrepo)
  2509. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  2510. tests.add_content_git_repo(self.gitrepo, branch='master')
  2511. # Fork the project
  2512. task = pagure.lib.fork_project(
  2513. session=self.session,
  2514. user='foo',
  2515. repo=repo,
  2516. )
  2517. self.session.commit()
  2518. self.assertEqual(task.get(),
  2519. {'endpoint': 'ui_ns.view_repo',
  2520. 'repo': 'test',
  2521. 'username': 'foo',
  2522. 'namespace': None})
  2523. # Create repo, with some content
  2524. self.gitrepo = os.path.join(
  2525. self.path, 'repos', 'forks', 'foo', 'test.git')
  2526. tests.add_content_git_repo(self.gitrepo, branch='feature')
  2527. fork_repo = pagure.lib.get_authorized_project(self.session, 'test', user='foo')
  2528. # Create a PR to play with
  2529. req = pagure.lib.new_pull_request(
  2530. session=self.session,
  2531. repo_from=fork_repo,
  2532. branch_from='feature',
  2533. repo_to=repo,
  2534. branch_to='master',
  2535. title='test PR',
  2536. user='pingou',
  2537. requestuid='foobar',
  2538. requestid=None,
  2539. status='Open',
  2540. notify=True
  2541. )
  2542. self.assertEqual(req.id, 1)
  2543. self.assertEqual(req.title, 'test PR')
  2544. # Close the PR before we ask to merge it
  2545. req.status = 'Closed'
  2546. req.closed_by_id = 2 # id:2 == foo
  2547. req.closed_at = datetime.datetime(2017, 10, 20, 12, 32, 10)
  2548. self.session.add(req)
  2549. self.session.commit()
  2550. # PR already closed
  2551. six.assertRaisesRegex(
  2552. self,
  2553. pagure.exceptions.PagureException,
  2554. 'This pull-request was merged or closed by foo',
  2555. pagure.lib.git.merge_pull_request,
  2556. self.session,
  2557. request=req,
  2558. username='pingou',
  2559. domerge=False
  2560. )
  2561. @patch('subprocess.Popen')
  2562. def test_generate_gitolite_acls(self, popen):
  2563. """ Test calling generate_gitolite_acls. """
  2564. pagure.SESSION = self.session
  2565. pagure.lib.git.SESSION = self.session
  2566. pagure.config.config['GITOLITE_HOME'] = '/tmp'
  2567. proc = MagicMock()
  2568. proc.communicate.return_value = (1, 2)
  2569. proc.returncode = 0
  2570. popen.return_value = proc
  2571. helper = pagure.lib.git_auth.get_git_auth_helper('gitolite3')
  2572. helper.generate_acls(project=None)
  2573. popen.assert_called_with(
  2574. 'HOME=/tmp gitolite compile && '
  2575. 'HOME=/tmp gitolite trigger POST_COMPILE',
  2576. cwd='/tmp', shell=True, stderr=-1, stdout=-1
  2577. )
  2578. def test_is_forced_push_new_branch(self):
  2579. self.assertFalse(
  2580. pagure.lib.git.is_forced_push(
  2581. '0000000000000000000000000000000000000000',
  2582. '^0e6e0b6c931d65ee22f67205a53933d841c6eeff',
  2583. 'path/is/not/important'
  2584. )
  2585. )
  2586. class PagureLibGitCommitToPatchtests(tests.Modeltests):
  2587. """ Tests for pagure.lib.git """
  2588. maxDiff = None
  2589. def setUp(self):
  2590. """ Set up the environment for the tests. """
  2591. super(PagureLibGitCommitToPatchtests, self).setUp()
  2592. # Create a git repo to play with
  2593. self.gitrepo = os.path.join(self.path, 'repos', 'test_repo.git')
  2594. os.makedirs(self.gitrepo)
  2595. repo = pygit2.init_repository(self.gitrepo)
  2596. # Create a file in that git repo
  2597. with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream:
  2598. stream.write('foo\n bar')
  2599. repo.index.add('sources')
  2600. repo.index.write()
  2601. # Commits the files added
  2602. tree = repo.index.write_tree()
  2603. author = pygit2.Signature(
  2604. 'Alice Author', 'alice@authors.tld')
  2605. committer = pygit2.Signature(
  2606. 'Cecil Committer', 'cecil@committers.tld')
  2607. repo.create_commit(
  2608. 'refs/heads/master', # the name of the reference to update
  2609. author,
  2610. committer,
  2611. 'Add sources file for testing',
  2612. # binary string representing the tree object ID
  2613. tree,
  2614. # list of binary strings representing parents of the new commit
  2615. []
  2616. )
  2617. self.first_commit = repo.revparse_single('HEAD')
  2618. # Edit the sources file again
  2619. with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream:
  2620. stream.write('foo\n bar\nbaz\n boose')
  2621. repo.index.add('sources')
  2622. repo.index.write()
  2623. # Commits the files added
  2624. tree = repo.index.write_tree()
  2625. author = pygit2.Signature(
  2626. 'Alice Author', 'alice@authors.tld')
  2627. committer = pygit2.Signature(
  2628. 'Cecil Committer', 'cecil@committers.tld')
  2629. repo.create_commit(
  2630. 'refs/heads/master', # the name of the reference to update
  2631. author,
  2632. committer,
  2633. 'Add baz and boose to the sources\n\n There are more objects to '
  2634. 'consider',
  2635. # binary string representing the tree object ID
  2636. tree,
  2637. # list of binary strings representing parents of the new commit
  2638. [self.first_commit.oid.hex]
  2639. )
  2640. self.second_commit = repo.revparse_single('HEAD')
  2641. def test_commit_to_patch_first_commit(self):
  2642. """ Test the commit_to_patch function of pagure.lib.git. """
  2643. repo = pygit2.init_repository(self.gitrepo)
  2644. patch = pagure.lib.git.commit_to_patch(repo, self.first_commit)
  2645. exp = """Mon Sep 17 00:00:00 2001
  2646. From: Alice Author <alice@authors.tld>
  2647. Subject: Add sources file for testing
  2648. ---
  2649. diff --git a/sources b/sources
  2650. new file mode 100644
  2651. index 0000000..9f44358
  2652. --- /dev/null
  2653. +++ b/sources
  2654. @@ -0,0 +1,2 @@
  2655. +foo
  2656. + bar
  2657. \ No newline at end of file
  2658. """
  2659. npatch = []
  2660. for row in patch.split('\n'):
  2661. if row.startswith('Date:'):
  2662. continue
  2663. if row.startswith('From '):
  2664. row = row.split(' ', 2)[2]
  2665. npatch.append(row)
  2666. patch = '\n'.join(npatch)
  2667. self.assertEqual(patch, exp)
  2668. def test_commit_to_patch_single_commit(self):
  2669. """ Test the commit_to_patch function of pagure.lib.git. """
  2670. repo = pygit2.init_repository(self.gitrepo)
  2671. patch = pagure.lib.git.commit_to_patch(repo, self.second_commit)
  2672. exp = """Mon Sep 17 00:00:00 2001
  2673. From: Alice Author <alice@authors.tld>
  2674. Subject: Add baz and boose to the sources
  2675. There are more objects to consider
  2676. ---
  2677. diff --git a/sources b/sources
  2678. index 9f44358..2a552bb 100644
  2679. --- a/sources
  2680. +++ b/sources
  2681. @@ -1,2 +1,4 @@
  2682. foo
  2683. - bar
  2684. \ No newline at end of file
  2685. + bar
  2686. +baz
  2687. + boose
  2688. \ No newline at end of file
  2689. """
  2690. npatch = []
  2691. for row in patch.split('\n'):
  2692. if row.startswith('Date:'):
  2693. continue
  2694. if row.startswith('From '):
  2695. row = row.split(' ', 2)[2]
  2696. npatch.append(row)
  2697. patch = '\n'.join(npatch)
  2698. self.assertEqual(patch, exp)
  2699. def test_commit_to_patch_2_commits(self):
  2700. """ Test the commit_to_patch function of pagure.lib.git. """
  2701. repo = pygit2.init_repository(self.gitrepo)
  2702. patch = pagure.lib.git.commit_to_patch(
  2703. repo, [self.first_commit, self.second_commit])
  2704. exp = """Mon Sep 17 00:00:00 2001
  2705. From: Alice Author <alice@authors.tld>
  2706. Subject: [PATCH 1/2] Add sources file for testing
  2707. ---
  2708. diff --git a/sources b/sources
  2709. new file mode 100644
  2710. index 0000000..9f44358
  2711. --- /dev/null
  2712. +++ b/sources
  2713. @@ -0,0 +1,2 @@
  2714. +foo
  2715. + bar
  2716. \ No newline at end of file
  2717. Mon Sep 17 00:00:00 2001
  2718. From: Alice Author <alice@authors.tld>
  2719. Subject: [PATCH 2/2] Add baz and boose to the sources
  2720. There are more objects to consider
  2721. ---
  2722. diff --git a/sources b/sources
  2723. index 9f44358..2a552bb 100644
  2724. --- a/sources
  2725. +++ b/sources
  2726. @@ -1,2 +1,4 @@
  2727. foo
  2728. - bar
  2729. \ No newline at end of file
  2730. + bar
  2731. +baz
  2732. + boose
  2733. \ No newline at end of file
  2734. """
  2735. npatch = []
  2736. for row in patch.split('\n'):
  2737. if row.startswith('Date:'):
  2738. continue
  2739. if row.startswith('From '):
  2740. row = row.split(' ', 2)[2]
  2741. npatch.append(row)
  2742. patch = '\n'.join(npatch)
  2743. self.assertEqual(patch, exp)
  2744. def test_commit_to_patch_first_commit_diff(self):
  2745. """ Test the commit_to_patch function of pagure.lib.git. """
  2746. repo = pygit2.init_repository(self.gitrepo)
  2747. patch = pagure.lib.git.commit_to_patch(
  2748. repo, self.first_commit, diff_view=True)
  2749. exp = """diff --git a/sources b/sources
  2750. new file mode 100644
  2751. index 0000000..9f44358
  2752. --- /dev/null
  2753. +++ b/sources
  2754. @@ -0,0 +1,2 @@
  2755. +foo
  2756. + bar
  2757. \ No newline at end of file
  2758. """
  2759. npatch = []
  2760. for row in patch.split('\n'):
  2761. if row.startswith('Date:'):
  2762. continue
  2763. if row.startswith('From '):
  2764. row = row.split(' ', 2)[2]
  2765. npatch.append(row)
  2766. patch = '\n'.join(npatch)
  2767. self.assertEqual(patch, exp)
  2768. def test_commit_to_patch_single_commit_diff(self):
  2769. """ Test the commit_to_patch function of pagure.lib.git. """
  2770. repo = pygit2.init_repository(self.gitrepo)
  2771. patch = pagure.lib.git.commit_to_patch(
  2772. repo, self.second_commit, diff_view=True)
  2773. exp = """diff --git a/sources b/sources
  2774. index 9f44358..2a552bb 100644
  2775. --- a/sources
  2776. +++ b/sources
  2777. @@ -1,2 +1,4 @@
  2778. foo
  2779. - bar
  2780. \ No newline at end of file
  2781. + bar
  2782. +baz
  2783. + boose
  2784. \ No newline at end of file
  2785. """
  2786. npatch = []
  2787. for row in patch.split('\n'):
  2788. if row.startswith('Date:'):
  2789. continue
  2790. if row.startswith('From '):
  2791. row = row.split(' ', 2)[2]
  2792. npatch.append(row)
  2793. patch = '\n'.join(npatch)
  2794. self.assertEqual(patch, exp)
  2795. def test_commit_to_patch_two_commits_diff(self):
  2796. """ Test the commit_to_patch function of pagure.lib.git. """
  2797. repo = pygit2.init_repository(self.gitrepo)
  2798. patch = pagure.lib.git.commit_to_patch(
  2799. repo, [self.first_commit, self.second_commit], diff_view=True)
  2800. exp = """diff --git a/sources b/sources
  2801. new file mode 100644
  2802. index 0000000..9f44358
  2803. --- /dev/null
  2804. +++ b/sources
  2805. @@ -0,0 +1,2 @@
  2806. +foo
  2807. + bar
  2808. \ No newline at end of file
  2809. diff --git a/sources b/sources
  2810. index 9f44358..2a552bb 100644
  2811. --- a/sources
  2812. +++ b/sources
  2813. @@ -1,2 +1,4 @@
  2814. foo
  2815. - bar
  2816. \ No newline at end of file
  2817. + bar
  2818. +baz
  2819. + boose
  2820. \ No newline at end of file
  2821. """
  2822. npatch = []
  2823. for row in patch.split('\n'):
  2824. if row.startswith('Date:'):
  2825. continue
  2826. if row.startswith('From '):
  2827. row = row.split(' ', 2)[2]
  2828. npatch.append(row)
  2829. patch = '\n'.join(npatch)
  2830. self.assertEqual(patch, exp)
  2831. def test_commit_to_patch_first_commit_diff_separated(self):
  2832. """ Test the commit_to_patch function of pagure.lib.git. """
  2833. repo = pygit2.init_repository(self.gitrepo)
  2834. patches = pagure.lib.git.commit_to_patch(
  2835. repo, self.first_commit, diff_view=True, separated=True)
  2836. exp = """diff --git a/sources b/sources
  2837. new file mode 100644
  2838. index 0000000..9f44358
  2839. --- /dev/null
  2840. +++ b/sources
  2841. @@ -0,0 +1,2 @@
  2842. +foo
  2843. + bar
  2844. \ No newline at end of file
  2845. """
  2846. output = []
  2847. for patch in patches:
  2848. npatch = []
  2849. for row in patch.split('\n'):
  2850. if row.startswith('Date:'):
  2851. continue
  2852. if row.startswith('From '):
  2853. row = row.split(' ', 2)[2]
  2854. npatch.append(row)
  2855. patch = '\n'.join(npatch)
  2856. output.append(patch)
  2857. self.assertEqual(output, [exp])
  2858. def test_commit_to_patch_single_commit_diff_separated(self):
  2859. """ Test the commit_to_patch function of pagure.lib.git. """
  2860. repo = pygit2.init_repository(self.gitrepo)
  2861. patches = pagure.lib.git.commit_to_patch(
  2862. repo, self.second_commit, diff_view=True, separated=True)
  2863. exp = """diff --git a/sources b/sources
  2864. index 9f44358..2a552bb 100644
  2865. --- a/sources
  2866. +++ b/sources
  2867. @@ -1,2 +1,4 @@
  2868. foo
  2869. - bar
  2870. \ No newline at end of file
  2871. + bar
  2872. +baz
  2873. + boose
  2874. \ No newline at end of file
  2875. """
  2876. output = []
  2877. for patch in patches:
  2878. npatch = []
  2879. for row in patch.split('\n'):
  2880. if row.startswith('Date:'):
  2881. continue
  2882. if row.startswith('From '):
  2883. row = row.split(' ', 2)[2]
  2884. npatch.append(row)
  2885. patch = '\n'.join(npatch)
  2886. output.append(patch)
  2887. self.assertEqual(output, [exp])
  2888. def test_commit_to_patch_two_commits_diff_separated(self):
  2889. """ Test the commit_to_patch function of pagure.lib.git. """
  2890. repo = pygit2.init_repository(self.gitrepo)
  2891. patches = pagure.lib.git.commit_to_patch(
  2892. repo, [self.first_commit, self.second_commit], diff_view=True,
  2893. separated=True)
  2894. exp = ["""diff --git a/sources b/sources
  2895. new file mode 100644
  2896. index 0000000..9f44358
  2897. --- /dev/null
  2898. +++ b/sources
  2899. @@ -0,0 +1,2 @@
  2900. +foo
  2901. + bar
  2902. \ No newline at end of file
  2903. """,
  2904. """diff --git a/sources b/sources
  2905. index 9f44358..2a552bb 100644
  2906. --- a/sources
  2907. +++ b/sources
  2908. @@ -1,2 +1,4 @@
  2909. foo
  2910. - bar
  2911. \ No newline at end of file
  2912. + bar
  2913. +baz
  2914. + boose
  2915. \ No newline at end of file
  2916. """]
  2917. output = []
  2918. for patch in patches:
  2919. npatch = []
  2920. for row in patch.split('\n'):
  2921. if row.startswith('Date:'):
  2922. continue
  2923. if row.startswith('From '):
  2924. row = row.split(' ', 2)[2]
  2925. npatch.append(row)
  2926. patch = '\n'.join(npatch)
  2927. output.append(patch)
  2928. self.assertEqual(output, exp)
  2929. if __name__ == '__main__':
  2930. unittest.main(verbosity=2)