test_pagure_lib_git.py 117 KB

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