test_pagure_lib_git.py 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678
  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,150 @@
  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. + "commit": [],
  1379. + "ticket": []
  1380. + },
  1381. + "access_users": {
  1382. + "admin": [],
  1383. + "commit": [],
  1384. + "owner": [
  1385. + "pingou"
  1386. + ],
  1387. + "ticket": []
  1388. + },
  1389. + "close_status": [],
  1390. + "custom_keys": [],
  1391. + "date_created": null,
  1392. + "date_modified": null,
  1393. + "description": "test project for ticket",
  1394. + "fullname": "test_ticket_repo",
  1395. + "id": 1,
  1396. + "milestones": {},
  1397. + "name": "test_ticket_repo",
  1398. + "namespace": null,
  1399. + "parent": null,
  1400. + "priorities": {},
  1401. + "settings": {
  1402. + "Enforce_signed-off_commits_in_pull-request": false,
  1403. + "Minimum_score_to_merge_pull-request": -1,
  1404. + "Only_assignee_can_merge_pull-request": false,
  1405. + "Web-hooks": null,
  1406. + "always_merge": false,
  1407. + "disable_non_fast-forward_merges": false,
  1408. + "fedmsg_notifications": true,
  1409. + "issue_tracker": true,
  1410. + "issue_tracker_read_only": false,
  1411. + "issues_default_to_private": false,
  1412. + "mqtt_notifications": true,
  1413. + "notify_on_commit_flag": false,
  1414. + "notify_on_pull-request_flag": false,
  1415. + "open_metadata_access_to_all": false,
  1416. + "project_documentation": false,
  1417. + "pull_request_access_only": false,
  1418. + "pull_requests": true,
  1419. + "stomp_notifications": true
  1420. + },
  1421. + "tags": [],
  1422. + "url_path": "test_ticket_repo",
  1423. + "user": {
  1424. + "default_email": "bar@pingou.com",
  1425. + "emails": [
  1426. + "bar@pingou.com",
  1427. + "foo@pingou.com"
  1428. + ],
  1429. + "fullname": "PY C",
  1430. + "name": "pingou",
  1431. + "url_path": "user/pingou"
  1432. + }
  1433. + },
  1434. + "remote_git": null,
  1435. + "repo_from": {
  1436. + "access_groups": {
  1437. + "admin": [],
  1438. + "commit": [],
  1439. + "ticket": []
  1440. + },
  1441. + "access_users": {
  1442. + "admin": [],
  1443. + "commit": [],
  1444. + "owner": [
  1445. + "pingou"
  1446. + ],
  1447. + "ticket": []
  1448. + },
  1449. + "close_status": [],
  1450. + "custom_keys": [],
  1451. + "date_created": null,
  1452. + "date_modified": null,
  1453. + "description": "test project for ticket",
  1454. + "fullname": "test_ticket_repo",
  1455. + "id": 1,
  1456. + "milestones": {},
  1457. + "name": "test_ticket_repo",
  1458. + "namespace": null,
  1459. + "parent": null,
  1460. + "priorities": {},
  1461. + "settings": {
  1462. + "Enforce_signed-off_commits_in_pull-request": false,
  1463. + "Minimum_score_to_merge_pull-request": -1,
  1464. + "Only_assignee_can_merge_pull-request": false,
  1465. + "Web-hooks": null,
  1466. + "always_merge": false,
  1467. + "disable_non_fast-forward_merges": false,
  1468. + "fedmsg_notifications": true,
  1469. + "issue_tracker": true,
  1470. + "issue_tracker_read_only": false,
  1471. + "issues_default_to_private": false,
  1472. + "mqtt_notifications": true,
  1473. + "notify_on_commit_flag": false,
  1474. + "notify_on_pull-request_flag": false,
  1475. + "open_metadata_access_to_all": false,
  1476. + "project_documentation": false,
  1477. + "pull_request_access_only": false,
  1478. + "pull_requests": true,
  1479. + "stomp_notifications": true
  1480. + },
  1481. + "tags": [],
  1482. + "url_path": "test_ticket_repo",
  1483. + "user": {
  1484. + "default_email": "bar@pingou.com",
  1485. + "emails": [
  1486. + "bar@pingou.com",
  1487. + "foo@pingou.com"
  1488. + ],
  1489. + "fullname": "PY C",
  1490. + "name": "pingou",
  1491. + "url_path": "user/pingou"
  1492. + }
  1493. + },
  1494. + "status": "Open",
  1495. + "tags": [],
  1496. + "threshold_reached": null,
  1497. + "title": "test PR",
  1498. + "uid": "foobar",
  1499. + "updated_on": null,
  1500. + "user": {
  1501. + "default_email": "bar@pingou.com",
  1502. + "emails": [
  1503. + "bar@pingou.com",
  1504. + "foo@pingou.com"
  1505. + ],
  1506. + "fullname": "PY C",
  1507. + "name": "pingou",
  1508. + "url_path": "user/pingou"
  1509. + }
  1510. +}
  1511. \ No newline at end of file
  1512. """
  1513. npatch = []
  1514. for row in patch.split("\n"):
  1515. if row.startswith("Date:"):
  1516. continue
  1517. elif row.startswith("From "):
  1518. row = row.split(" ", 2)[2]
  1519. elif row.startswith("diff --git "):
  1520. row = row.split(" ")
  1521. row[2] = "a/123"
  1522. row[3] = "b/456"
  1523. row = " ".join(row)
  1524. elif "Updated pull-request" in row:
  1525. row = row.split()
  1526. row[3] = "<hash>:"
  1527. row = " ".join(row)
  1528. elif "date_created" in row:
  1529. t = row.split(": ")[0]
  1530. row = "%s: null," % t
  1531. elif "date_modified" in row:
  1532. t = row.split(": ")[0]
  1533. row = "%s: null," % t
  1534. elif "last_updated" in row:
  1535. t = row.split(": ")[0]
  1536. row = "%s: null," % t
  1537. elif "updated_on" in row:
  1538. t = row.split(": ")[0]
  1539. row = "%s: null," % t
  1540. elif row.startswith("index 00"):
  1541. row = "index 0000000..60f7480"
  1542. elif row.startswith("+++ b/"):
  1543. row = "+++ b/456"
  1544. npatch.append(row)
  1545. patch = "\n".join(npatch)
  1546. print(patch)
  1547. self.assertEqual(patch, exp)
  1548. def test_update_ticket_from_git_no_priority(self):
  1549. """ Test the update_ticket_from_git method from pagure.lib.git. """
  1550. tests.create_projects(self.session)
  1551. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1552. # Before
  1553. self.assertEqual(len(repo.issues), 0)
  1554. self.assertEqual(repo.issues, [])
  1555. data = {
  1556. "status": "Open",
  1557. "title": "foo",
  1558. "comments": [],
  1559. "content": "bar",
  1560. "date_created": "1426500263",
  1561. "user": {"name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1562. "milestone": "Next Release",
  1563. "priority": 1,
  1564. }
  1565. # Invalid project
  1566. self.assertRaises(
  1567. pagure.exceptions.PagureException,
  1568. pagure.lib.git.update_ticket_from_git,
  1569. self.session,
  1570. reponame="foobar",
  1571. namespace=None,
  1572. username=None,
  1573. issue_uid="foobar",
  1574. json_data=data,
  1575. agent="pingou",
  1576. )
  1577. # Create the issue
  1578. data = {
  1579. "status": "Open",
  1580. "title": "foo",
  1581. "comments": [],
  1582. "content": "bar",
  1583. "date_created": "1426500263",
  1584. "user": {"name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1585. "milestone": "Next Release",
  1586. }
  1587. pagure.lib.git.update_ticket_from_git(
  1588. self.session,
  1589. reponame="test",
  1590. namespace=None,
  1591. username=None,
  1592. issue_uid="foobar",
  1593. json_data=data,
  1594. agent="pingou",
  1595. )
  1596. self.session.commit()
  1597. # Edit the issue
  1598. data = {
  1599. "status": "Open",
  1600. "title": "foo",
  1601. "comments": [],
  1602. "content": "bar",
  1603. "date_created": "1426500263",
  1604. "user": {"name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1605. "milestone": "Next Release",
  1606. "priority": 1,
  1607. }
  1608. pagure.lib.git.update_ticket_from_git(
  1609. self.session,
  1610. reponame="test",
  1611. namespace=None,
  1612. username=None,
  1613. issue_uid="foobar",
  1614. json_data=data,
  1615. agent="pingou",
  1616. )
  1617. self.session.commit()
  1618. # Data contained a priority but not the project, so bailing
  1619. self.assertEqual(len(repo.issues), 1)
  1620. self.assertEqual(repo.issues[0].id, 1)
  1621. self.assertEqual(repo.issues[0].uid, "foobar")
  1622. self.assertEqual(repo.issues[0].title, "foo")
  1623. self.assertEqual(repo.issues[0].depending_text, [])
  1624. self.assertEqual(repo.issues[0].blocking_text, [])
  1625. self.assertEqual(repo.issues[0].milestone, "Next Release")
  1626. self.assertEqual(repo.issues[0].priority, None)
  1627. self.assertEqual(
  1628. repo.milestones, {"Next Release": {"active": True, "date": None}}
  1629. )
  1630. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1631. def test_update_ticket_from_git_close_ticket(self):
  1632. """ Test the update_ticket_from_git method from pagure.lib.git. """
  1633. tests.create_projects(self.session)
  1634. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1635. # Before
  1636. self.assertEqual(len(repo.issues), 0)
  1637. self.assertEqual(repo.issues, [])
  1638. # Create the issue
  1639. data = {
  1640. "status": "Open",
  1641. "title": "foo",
  1642. "comments": [],
  1643. "content": "bar",
  1644. "date_created": "1426500263",
  1645. "user": {"name": "foo", "emails": ["foo@fedoraproject.org"]},
  1646. "milestone": "Next Release",
  1647. }
  1648. pagure.lib.git.update_ticket_from_git(
  1649. self.session,
  1650. reponame="test",
  1651. namespace=None,
  1652. username=None,
  1653. issue_uid="foobar",
  1654. json_data=data,
  1655. agent="pingou",
  1656. )
  1657. self.session.commit()
  1658. # Edit the issue
  1659. data = {
  1660. "status": "Closed",
  1661. "close_status": "Fixed",
  1662. "title": "foo",
  1663. "comments": [],
  1664. "content": "bar",
  1665. "date_created": "1426500263",
  1666. "user": {"name": "foo", "emails": ["foo@fedoraproject.org"]},
  1667. "milestone": "Next Release",
  1668. }
  1669. pagure.lib.git.update_ticket_from_git(
  1670. self.session,
  1671. reponame="test",
  1672. namespace=None,
  1673. username=None,
  1674. issue_uid="foobar",
  1675. json_data=data,
  1676. agent="pingou",
  1677. )
  1678. self.session.commit()
  1679. self.assertEqual(len(repo.issues), 1)
  1680. self.assertEqual(repo.issues[0].id, 1)
  1681. self.assertEqual(repo.issues[0].uid, "foobar")
  1682. self.assertEqual(repo.issues[0].title, "foo")
  1683. self.assertEqual(repo.issues[0].depending_text, [])
  1684. self.assertEqual(repo.issues[0].blocking_text, [])
  1685. self.assertEqual(repo.issues[0].milestone, "Next Release")
  1686. self.assertEqual(repo.issues[0].priority, None)
  1687. self.assertEqual(
  1688. repo.milestones, {"Next Release": {"active": True, "date": None}}
  1689. )
  1690. self.assertEqual(repo.issues[0].status, "Closed")
  1691. self.assertEqual(repo.issues[0].close_status, "Fixed")
  1692. self.assertIsNotNone(repo.issues[0].closed_at)
  1693. self.assertEqual(
  1694. repo.issues[0].comments[-1].comment,
  1695. "**Metadata Update from @pingou**:\n"
  1696. "- Issue close_status updated to: Fixed\n"
  1697. "- Issue status updated to: Closed (was: Open)",
  1698. )
  1699. def test_update_ticket_from_git(self):
  1700. """ Test the update_ticket_from_git method from pagure.lib.git. """
  1701. tests.create_projects(self.session)
  1702. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1703. # Set some priorities to the project
  1704. repo.priorities = {"1": "High", "2": "Normal"}
  1705. self.session.add(repo)
  1706. self.session.commit()
  1707. # Before
  1708. self.assertEqual(len(repo.issues), 0)
  1709. self.assertEqual(repo.issues, [])
  1710. data = {
  1711. "status": "Open",
  1712. "title": "foo",
  1713. "comments": [],
  1714. "content": "bar",
  1715. "date_created": "1426500263",
  1716. "user": {"name": "pingou", "emails": ["pingou@fedoraproject.org"]},
  1717. "milestone": "Next Release",
  1718. "priority": 1,
  1719. }
  1720. self.assertRaises(
  1721. pagure.exceptions.PagureException,
  1722. pagure.lib.git.update_ticket_from_git,
  1723. self.session,
  1724. reponame="foobar",
  1725. namespace=None,
  1726. username=None,
  1727. issue_uid="foobar",
  1728. json_data=data,
  1729. agent="pingou",
  1730. )
  1731. pagure.lib.git.update_ticket_from_git(
  1732. self.session,
  1733. reponame="test",
  1734. namespace=None,
  1735. username=None,
  1736. issue_uid="foobar",
  1737. json_data=data,
  1738. agent="pingou",
  1739. )
  1740. self.session.commit()
  1741. # After 1 insertion
  1742. self.assertEqual(len(repo.issues), 1)
  1743. self.assertEqual(repo.issues[0].id, 1)
  1744. self.assertEqual(repo.issues[0].uid, "foobar")
  1745. self.assertEqual(repo.issues[0].title, "foo")
  1746. self.assertEqual(repo.issues[0].depending_text, [])
  1747. self.assertEqual(repo.issues[0].blocking_text, [])
  1748. self.assertEqual(repo.issues[0].milestone, "Next Release")
  1749. self.assertEqual(repo.issues[0].priority, 1)
  1750. self.assertIsNone(repo.issues[0].closed_at)
  1751. self.assertEqual(
  1752. repo.milestones, {"Next Release": {"active": True, "date": None}}
  1753. )
  1754. data["title"] = "fake issue for tests"
  1755. pagure.lib.git.update_ticket_from_git(
  1756. self.session,
  1757. reponame="test",
  1758. namespace=None,
  1759. username=None,
  1760. issue_uid="foobar",
  1761. json_data=data,
  1762. agent="pingou",
  1763. )
  1764. self.session.commit()
  1765. # After edit
  1766. self.assertEqual(len(repo.issues), 1)
  1767. self.assertEqual(repo.issues[0].id, 1)
  1768. self.assertEqual(repo.issues[0].uid, "foobar")
  1769. self.assertEqual(repo.issues[0].title, "fake issue for tests")
  1770. self.assertEqual(repo.issues[0].depending_text, [])
  1771. self.assertEqual(repo.issues[0].blocking_text, [])
  1772. self.assertEqual(repo.issues[0].priority, 1)
  1773. self.assertIsNone(repo.issues[0].closed_at)
  1774. data = {
  1775. "status": "Closed",
  1776. "close_status": "Fixed",
  1777. "closed_at": "1426595225",
  1778. "title": "Rename pagure",
  1779. "private": False,
  1780. "content": "This is too much of a conflict with the book",
  1781. "user": {
  1782. "fullname": "Pierre-YvesChibon",
  1783. "name": "pingou",
  1784. "default_email": "pingou@fedoraproject.org",
  1785. "emails": ["pingou@fedoraproject.org"],
  1786. },
  1787. "id": 20,
  1788. "blocks": [1],
  1789. "depends": [3, 4],
  1790. "date_created": "1426595224",
  1791. "milestone": "Future",
  1792. "priority": 1,
  1793. "comments": [
  1794. {
  1795. "comment": "Nirik:\r\n\r\n- sourceforge++ \r\n- "
  1796. "gitmaker\r\n- mastergit \r\n- hostomatic\r\n- "
  1797. "gitcorp\r\n- git-keiretsu \r\n- gitbuffet\r\n- "
  1798. "cogitator\r\n- cogitate\r\n\r\nrandomuser:\r\n\r\n- "
  1799. "COLLABORATRON5000\r\n- git-sm\\u00f6rg\\u00e5sbord\r\n- "
  1800. "thislittlegittywenttomarket\r\n- git-o-rama\r\n- "
  1801. "gitsundheit",
  1802. "date_created": "1426595224",
  1803. "id": 250,
  1804. "parent": None,
  1805. "user": {
  1806. "fullname": "Pierre-YvesChibon",
  1807. "name": "pingou",
  1808. "default_email": "pingou@fedoraproject.org",
  1809. "emails": ["pingou@fedoraproject.org"],
  1810. },
  1811. },
  1812. {
  1813. "comment": "Nirik:\r\n\r\n- sourceforge++ \r\n- "
  1814. "gitmaker\r\n- mastergit \r\n- hostomatic\r\n- "
  1815. "gitcorp\r\n- git-keiretsu \r\n- gitbuffet\r\n- "
  1816. "cogitator\r\n- cogitate\r\n\r\nrandomuser:\r\n\r\n- "
  1817. "COLLABORATRON5000\r\n- git-sm\\u00f6rg\\u00e5sbord\r\n- "
  1818. "thislittlegittywenttomarket\r\n- git-o-rama\r\n- "
  1819. "gitsundheit",
  1820. "date_created": "1426595340",
  1821. "id": 324,
  1822. "parent": None,
  1823. "user": {
  1824. "fullname": "Ralph Bean",
  1825. "name": "ralph",
  1826. "default_email": "ralph@fedoraproject.org",
  1827. "emails": ["ralph@fedoraproject.org"],
  1828. },
  1829. },
  1830. ],
  1831. }
  1832. pagure.lib.git.update_ticket_from_git(
  1833. self.session,
  1834. reponame="test",
  1835. namespace=None,
  1836. username=None,
  1837. issue_uid="foobar2",
  1838. json_data=data,
  1839. agent="pingou",
  1840. )
  1841. # After second insertion
  1842. self.assertEqual(len(repo.issues), 2)
  1843. self.assertEqual(repo.issues[0].uid, "foobar")
  1844. self.assertEqual(repo.issues[0].title, "fake issue for tests")
  1845. self.assertEqual(repo.issues[0].depending_text, [20])
  1846. self.assertEqual(repo.issues[0].blocking_text, [])
  1847. self.assertEqual(repo.issues[0].status, "Open")
  1848. self.assertIsNone(repo.issues[0].close_status)
  1849. self.assertIsNone(repo.issues[0].closed_at)
  1850. # New one
  1851. self.assertEqual(repo.issues[1].uid, "foobar2")
  1852. self.assertEqual(repo.issues[1].title, "Rename pagure")
  1853. self.assertEqual(repo.issues[1].depending_text, [])
  1854. self.assertEqual(repo.issues[1].blocking_text, [1])
  1855. self.assertEqual(repo.issues[1].milestone, "Future")
  1856. self.assertEqual(repo.issues[1].status, "Closed")
  1857. self.assertEqual(repo.issues[1].close_status, "Fixed")
  1858. self.assertIsNotNone(repo.issues[1].closed_at)
  1859. self.assertDictEqual(
  1860. repo.milestones,
  1861. {
  1862. "Future": {"active": True, "date": None},
  1863. "Next Release": {"active": True, "date": None},
  1864. },
  1865. )
  1866. def test_update_request_from_git(self):
  1867. """ Test the update_request_from_git method from pagure.lib.git. """
  1868. tests.create_projects(self.session)
  1869. tests.create_projects_git(os.path.join(self.path, "repos"))
  1870. repo = pagure.lib.query._get_project(self.session, "test")
  1871. namespaced_repo = pagure.lib.query._get_project(
  1872. self.session, "test3", namespace="somenamespace"
  1873. )
  1874. # Before
  1875. self.assertEqual(len(repo.requests), 0)
  1876. self.assertEqual(repo.requests, [])
  1877. self.assertEqual(len(namespaced_repo.requests), 0)
  1878. self.assertEqual(namespaced_repo.requests, [])
  1879. data = {
  1880. "status": True,
  1881. "uid": "d4182a2ac2d541d884742d3037c26e56",
  1882. "project": {
  1883. "custom_keys": [],
  1884. "parent": None,
  1885. "settings": {
  1886. "issue_tracker": True,
  1887. "project_documentation": True,
  1888. "pull_requests": True,
  1889. },
  1890. "name": "test",
  1891. "date_created": "1426500194",
  1892. "tags": [],
  1893. "user": {
  1894. "fullname": "Pierre-YvesChibon",
  1895. "name": "pingou",
  1896. "default_email": "pingou@fedoraproject.org",
  1897. "emails": ["pingou@fedoraproject.org"],
  1898. },
  1899. "id": 1,
  1900. "description": "test project",
  1901. },
  1902. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  1903. "user": {
  1904. "fullname": "Pierre-YvesChibon",
  1905. "name": "pingou",
  1906. "default_email": "pingou@fedoraproject.org",
  1907. "emails": ["pingou@fedoraproject.org"],
  1908. },
  1909. "id": 7,
  1910. "comments": [
  1911. {
  1912. "comment": "really?",
  1913. "user": {
  1914. "fullname": "Pierre-YvesChibon",
  1915. "name": "pingou",
  1916. "default_email": "pingou@fedoraproject.org",
  1917. "emails": ["pingou@fedoraproject.org"],
  1918. },
  1919. "parent": None,
  1920. "date_created": "1426843778",
  1921. "commit": "fa72f315373ec5f98f2b08c8ffae3645c97aaad2",
  1922. "line": 5,
  1923. "id": 1,
  1924. "filename": "test",
  1925. },
  1926. {
  1927. "comment": "Again ?",
  1928. "user": {
  1929. "fullname": "Pierre-YvesChibon",
  1930. "name": "pingou",
  1931. "default_email": "pingou@fedoraproject.org",
  1932. "emails": ["pingou@fedoraproject.org"],
  1933. },
  1934. "parent": None,
  1935. "date_created": "1426866781",
  1936. "commit": "94ebaf900161394059478fd88aec30e59092a1d7",
  1937. "line": 5,
  1938. "id": 2,
  1939. "filename": "test2",
  1940. },
  1941. {
  1942. "comment": "Should be fine in fact",
  1943. "user": {
  1944. "fullname": "Pierre-YvesChibon",
  1945. "name": "pingou",
  1946. "default_email": "pingou@fedoraproject.org",
  1947. "emails": ["pingou@fedoraproject.org"],
  1948. },
  1949. "parent": None,
  1950. "date_created": "1426866950",
  1951. "commit": "94ebaf900161394059478fd88aec30e59092a1d7",
  1952. "line": 5,
  1953. "id": 3,
  1954. "filename": "test2",
  1955. },
  1956. ],
  1957. "branch_from": "master",
  1958. "title": "test request",
  1959. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  1960. "repo_from": {
  1961. "parent": {
  1962. "custom_keys": [],
  1963. "parent": None,
  1964. "name": "test",
  1965. "date_created": "1426500194",
  1966. "tags": [],
  1967. "user": {
  1968. "fullname": "Pierre-YvesChibon",
  1969. "name": "pingou",
  1970. "default_email": "pingou@fedoraproject.org",
  1971. "emails": ["pingou@fedoraproject.org"],
  1972. },
  1973. "settings": {
  1974. "issue_tracker": True,
  1975. "project_documentation": True,
  1976. "pull_requests": True,
  1977. },
  1978. "id": 1,
  1979. "description": "test project",
  1980. },
  1981. "settings": {
  1982. "issue_tracker": True,
  1983. "project_documentation": True,
  1984. "pull_requests": True,
  1985. },
  1986. "name": "test",
  1987. "date_created": "1426843440",
  1988. "custom_keys": [],
  1989. "tags": [],
  1990. "user": {
  1991. "fullname": "fake user",
  1992. "name": "fake",
  1993. "default_email": "fake@fedoraproject.org",
  1994. "emails": ["fake@fedoraproject.org"],
  1995. },
  1996. "id": 6,
  1997. "description": "test project",
  1998. },
  1999. "branch": "master",
  2000. "date_created": "1426843732",
  2001. }
  2002. self.assertRaises(
  2003. pagure.exceptions.PagureException,
  2004. pagure.lib.git.update_request_from_git,
  2005. self.session,
  2006. reponame="foobar",
  2007. namespace=None,
  2008. username=None,
  2009. request_uid="d4182a2ac2d541d884742d3037c26e56",
  2010. json_data=data,
  2011. )
  2012. pagure.lib.git.update_request_from_git(
  2013. self.session,
  2014. reponame="test",
  2015. namespace=None,
  2016. username=None,
  2017. request_uid="d4182a2ac2d541d884742d3037c26e56",
  2018. json_data=data,
  2019. )
  2020. self.session.commit()
  2021. # After 1 st insertion
  2022. self.assertEqual(len(repo.requests), 1)
  2023. self.assertEqual(repo.requests[0].id, 7)
  2024. self.assertEqual(
  2025. repo.requests[0].uid, "d4182a2ac2d541d884742d3037c26e56"
  2026. )
  2027. self.assertEqual(repo.requests[0].title, "test request")
  2028. self.assertEqual(len(repo.requests[0].comments), 3)
  2029. data = {
  2030. "status": True,
  2031. "uid": "d4182a2ac2d541d884742d3037c26e57",
  2032. "project": {
  2033. "parent": None,
  2034. "name": "test",
  2035. "custom_keys": [],
  2036. "date_created": "1426500194",
  2037. "tags": [],
  2038. "user": {
  2039. "fullname": "Pierre-YvesChibon",
  2040. "name": "pingou",
  2041. "default_email": "pingou@fedoraproject.org",
  2042. "emails": ["pingou@fedoraproject.org"],
  2043. },
  2044. "settings": {
  2045. "issue_tracker": True,
  2046. "project_documentation": True,
  2047. "pull_requests": True,
  2048. },
  2049. "id": 1,
  2050. "description": "test project",
  2051. },
  2052. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  2053. "user": {
  2054. "fullname": "Pierre-YvesChibon",
  2055. "name": "pingou",
  2056. "default_email": "pingou@fedoraproject.org",
  2057. "emails": ["pingou@fedoraproject.org"],
  2058. },
  2059. "id": 4,
  2060. "comments": [],
  2061. "branch_from": "master",
  2062. "title": "test request #2",
  2063. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  2064. "repo_from": {
  2065. "parent": {
  2066. "parent": None,
  2067. "name": "test",
  2068. "custom_keys": [],
  2069. "date_created": "1426500194",
  2070. "tags": [],
  2071. "user": {
  2072. "fullname": "Pierre-YvesChibon",
  2073. "name": "pingou",
  2074. "default_email": "pingou@fedoraproject.org",
  2075. "emails": ["pingou@fedoraproject.org"],
  2076. },
  2077. "settings": {
  2078. "issue_tracker": True,
  2079. "project_documentation": True,
  2080. "pull_requests": True,
  2081. },
  2082. "id": 1,
  2083. "description": "test project",
  2084. },
  2085. "settings": {
  2086. "issue_tracker": True,
  2087. "project_documentation": True,
  2088. "pull_requests": True,
  2089. },
  2090. "name": "test",
  2091. "date_created": "1426843440",
  2092. "custom_keys": [],
  2093. "tags": [],
  2094. "user": {
  2095. "fullname": "fake user",
  2096. "name": "fake",
  2097. "default_email": "fake@fedoraproject.org",
  2098. "emails": ["fake@fedoraproject.org"],
  2099. },
  2100. "project_docs": True,
  2101. "id": 6,
  2102. "description": "test project",
  2103. },
  2104. "branch": "master",
  2105. "date_created": "1426843745",
  2106. }
  2107. pagure.lib.git.update_request_from_git(
  2108. self.session,
  2109. reponame="test",
  2110. namespace=None,
  2111. username=None,
  2112. request_uid="d4182a2ac2d541d884742d3037c26e57",
  2113. json_data=data,
  2114. )
  2115. self.session.commit()
  2116. # After 2 nd insertion
  2117. self.assertEqual(len(repo.requests), 2)
  2118. self.assertEqual(repo.requests[0].id, 7)
  2119. self.assertEqual(
  2120. repo.requests[0].uid, "d4182a2ac2d541d884742d3037c26e56"
  2121. )
  2122. self.assertEqual(repo.requests[0].title, "test request")
  2123. self.assertEqual(len(repo.requests[0].comments), 3)
  2124. # 2 entry
  2125. self.assertEqual(repo.requests[1].id, 4)
  2126. self.assertEqual(
  2127. repo.requests[1].uid, "d4182a2ac2d541d884742d3037c26e57"
  2128. )
  2129. self.assertEqual(repo.requests[1].title, "test request #2")
  2130. self.assertEqual(len(repo.requests[1].comments), 0)
  2131. data = {
  2132. "status": True,
  2133. "uid": "d4182a2ac2d541d884742d3037c26e58",
  2134. "project": {
  2135. "parent": None,
  2136. "name": "test3",
  2137. "custom_keys": [],
  2138. "namespace": "somenamespace",
  2139. "date_created": "1426500194",
  2140. "tags": [],
  2141. "user": {
  2142. "fullname": "Pierre-YvesChibon",
  2143. "name": "pingou",
  2144. "default_email": "pingou@fedoraproject.org",
  2145. "emails": ["pingou@fedoraproject.org"],
  2146. },
  2147. "settings": {
  2148. "issue_tracker": True,
  2149. "project_documentation": True,
  2150. "pull_requests": True,
  2151. },
  2152. "id": 3,
  2153. "description": "namespaced test project",
  2154. },
  2155. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  2156. "user": {
  2157. "fullname": "Pierre-YvesChibon",
  2158. "name": "pingou",
  2159. "default_email": "pingou@fedoraproject.org",
  2160. "emails": ["pingou@fedoraproject.org"],
  2161. },
  2162. "id": 5,
  2163. "comments": [],
  2164. "branch_from": "master",
  2165. "title": "test request to namespaced repo",
  2166. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  2167. "repo_from": {
  2168. "parent": {
  2169. "parent": None,
  2170. "name": "test",
  2171. "custom_keys": [],
  2172. "date_created": "1426500194",
  2173. "tags": [],
  2174. "user": {
  2175. "fullname": "Pierre-YvesChibon",
  2176. "name": "pingou",
  2177. "default_email": "pingou@fedoraproject.org",
  2178. "emails": ["pingou@fedoraproject.org"],
  2179. },
  2180. "settings": {
  2181. "issue_tracker": True,
  2182. "project_documentation": True,
  2183. "pull_requests": True,
  2184. },
  2185. "id": 1,
  2186. "description": "test project",
  2187. },
  2188. "settings": {
  2189. "issue_tracker": True,
  2190. "project_documentation": True,
  2191. "pull_requests": True,
  2192. },
  2193. "name": "test",
  2194. "date_created": "1426843440",
  2195. "custom_keys": [],
  2196. "tags": [],
  2197. "user": {
  2198. "fullname": "fake user",
  2199. "name": "fake",
  2200. "default_email": "fake@fedoraproject.org",
  2201. "emails": ["fake@fedoraproject.org"],
  2202. },
  2203. "project_docs": True,
  2204. "id": 6,
  2205. "description": "test project",
  2206. },
  2207. "branch": "master",
  2208. "date_created": "1426843745",
  2209. }
  2210. pagure.lib.git.update_request_from_git(
  2211. self.session,
  2212. reponame="test3",
  2213. namespace="somenamespace",
  2214. username=None,
  2215. request_uid="d4182a2ac2d541d884742d3037c26e58",
  2216. json_data=data,
  2217. )
  2218. self.session.commit()
  2219. self.assertEqual(len(namespaced_repo.requests), 1)
  2220. self.assertEqual(namespaced_repo.requests[0].id, 5)
  2221. self.assertEqual(
  2222. namespaced_repo.requests[0].uid, "d4182a2ac2d541d884742d3037c26e58"
  2223. )
  2224. self.assertEqual(
  2225. namespaced_repo.requests[0].title,
  2226. "test request to namespaced repo",
  2227. )
  2228. def test_update_request_from_git(self):
  2229. """ Test the update_request_from_git method from pagure.lib.git. """
  2230. tests.create_projects(self.session)
  2231. tests.create_projects_git(os.path.join(self.path, "repos"))
  2232. repo = pagure.lib.query._get_project(self.session, "test")
  2233. namespaced_repo = pagure.lib.query._get_project(
  2234. self.session, "test3", namespace="somenamespace"
  2235. )
  2236. # Before
  2237. self.assertEqual(len(repo.requests), 0)
  2238. self.assertEqual(repo.requests, [])
  2239. self.assertEqual(len(namespaced_repo.requests), 0)
  2240. self.assertEqual(namespaced_repo.requests, [])
  2241. data = {
  2242. "status": True,
  2243. "uid": "d4182a2ac2d541d884742d3037c26e57",
  2244. "project": {
  2245. "parent": None,
  2246. "name": "test",
  2247. "custom_keys": [],
  2248. "date_created": "1426500194",
  2249. "tags": [],
  2250. "user": {
  2251. "fullname": "Pierre-YvesChibon",
  2252. "name": "pingou",
  2253. "default_email": "pingou@fedoraproject.org",
  2254. "emails": ["pingou@fedoraproject.org"],
  2255. },
  2256. "settings": {
  2257. "issue_tracker": True,
  2258. "project_documentation": True,
  2259. "pull_requests": True,
  2260. },
  2261. "id": 1,
  2262. "description": "test project",
  2263. },
  2264. "commit_stop": "eface8e13bc2a08a3fb22af9a72a8c90e36b8b89",
  2265. "user": {
  2266. "fullname": "Pierre-YvesChibon",
  2267. "name": "pingou",
  2268. "default_email": "pingou@fedoraproject.org",
  2269. "emails": ["pingou@fedoraproject.org"],
  2270. },
  2271. "id": 4,
  2272. "comments": [],
  2273. "branch_from": "master",
  2274. "title": "test request #2",
  2275. "commit_start": "788efeaaf86bde8618f594a8181abb402e1dd904",
  2276. "repo_from": {
  2277. "parent": {
  2278. "parent": None,
  2279. "name": "test",
  2280. "custom_keys": [],
  2281. "date_created": "1426500194",
  2282. "tags": [],
  2283. "user": {
  2284. "fullname": "Pierre-YvesChibon",
  2285. "name": "pingou",
  2286. "default_email": "pingou@fedoraproject.org",
  2287. "emails": ["pingou@fedoraproject.org"],
  2288. },
  2289. "settings": {
  2290. "issue_tracker": True,
  2291. "project_documentation": True,
  2292. "pull_requests": True,
  2293. },
  2294. "id": 1,
  2295. "description": "test project",
  2296. },
  2297. "settings": {
  2298. "issue_tracker": True,
  2299. "project_documentation": True,
  2300. "pull_requests": True,
  2301. },
  2302. "name": "test",
  2303. "date_created": "1426843440",
  2304. "custom_keys": [],
  2305. "tags": [],
  2306. "user": {
  2307. "fullname": "fake user",
  2308. "name": "fake",
  2309. "default_email": "fake@fedoraproject.org",
  2310. "emails": ["fake@fedoraproject.org"],
  2311. },
  2312. "project_docs": True,
  2313. "id": 6,
  2314. "description": "test project",
  2315. },
  2316. "branch": "master",
  2317. "date_created": "1426843745",
  2318. "tags": ["WIP", "core"],
  2319. }
  2320. pagure.lib.git.update_request_from_git(
  2321. self.session,
  2322. reponame="test",
  2323. namespace=None,
  2324. username=None,
  2325. request_uid="d4182a2ac2d541d884742d3037c26e57",
  2326. json_data=data,
  2327. )
  2328. self.session.commit()
  2329. # After insertion
  2330. self.assertEqual(repo.requests[0].id, 4)
  2331. self.assertEqual(
  2332. repo.requests[0].uid, "d4182a2ac2d541d884742d3037c26e57"
  2333. )
  2334. self.assertEqual(repo.requests[0].title, "test request #2")
  2335. self.assertEqual(len(repo.requests[0].comments), 0)
  2336. self.assertEqual(repo.requests[0].tags_text, ["WIP", "core"])
  2337. def test_read_git_lines(self):
  2338. """ Test the read_git_lines method of pagure.lib.git. """
  2339. self.test_update_git()
  2340. gitrepo = os.path.join(
  2341. self.path, "repos", "tickets", "test_ticket_repo.git"
  2342. )
  2343. output = pagure.lib.git.read_git_lines(
  2344. ["log", "-1", "--pretty='%s'"], gitrepo
  2345. )
  2346. self.assertEqual(len(output), 1)
  2347. self.assertTrue(output[0].startswith("'Updated issue "))
  2348. self.assertTrue(output[0].endswith(": Test issue'"))
  2349. # Keeping the new line symbol
  2350. output = pagure.lib.git.read_git_lines(
  2351. ["log", "-1", "--pretty='%s'"], gitrepo, keepends=True
  2352. )
  2353. self.assertEqual(len(output), 1)
  2354. self.assertTrue(output[0].endswith(": Test issue'\n"))
  2355. def test_get_revs_between(self):
  2356. """ Test the get_revs_between method of pagure.lib.git. """
  2357. self.test_update_git()
  2358. gitrepo = os.path.join(
  2359. self.path, "repos", "tickets", "test_ticket_repo.git"
  2360. )
  2361. output = pagure.lib.git.read_git_lines(
  2362. ["log", "-3", "--pretty='%H'"], gitrepo
  2363. )
  2364. self.assertEqual(len(output), 2)
  2365. from_hash = output[1].replace("'", "")
  2366. # Case 1, repo BASE is null and HEAD is equal to from_hash
  2367. to_hash = "0"
  2368. output1 = pagure.lib.git.get_revs_between(
  2369. to_hash, from_hash, gitrepo, "refs/heads/master"
  2370. )
  2371. self.assertEqual(output1, [from_hash])
  2372. # Case 2, get revs between two commits (to_hash, from_hash)
  2373. to_hash = output[0].replace("'", "")
  2374. output2 = pagure.lib.git.get_revs_between(
  2375. to_hash, from_hash, gitrepo, "refs/heads/master"
  2376. )
  2377. self.assertEqual(output2, [to_hash])
  2378. # Case 3, get revs between two commits (from_hash, to_hash)
  2379. output3 = pagure.lib.git.get_revs_between(
  2380. from_hash, to_hash, gitrepo, "refs/heads/master"
  2381. )
  2382. self.assertEqual(output3, [to_hash])
  2383. # Case 4, get revs between two commits on two different branches
  2384. newgitrepo = tempfile.mkdtemp(prefix="pagure-")
  2385. newrepo = pygit2.clone_repository(gitrepo, newgitrepo)
  2386. newrepo.create_branch("feature", newrepo.head.peel())
  2387. with open(os.path.join(newgitrepo, "sources"), "w") as stream:
  2388. stream.write("foo\n bar")
  2389. newrepo.index.add("sources")
  2390. newrepo.index.write()
  2391. # Commits the files added
  2392. tree = newrepo.index.write_tree()
  2393. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  2394. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  2395. newrepo.create_commit(
  2396. "refs/heads/feature", # the name of the reference to update
  2397. author,
  2398. committer,
  2399. "Add sources file for testing",
  2400. # binary string representing the tree object ID
  2401. tree,
  2402. # list of binary strings representing parents of the new commit
  2403. [to_hash],
  2404. )
  2405. branch_commit = newrepo.revparse_single("refs/heads/feature")
  2406. # Push to origin
  2407. ori_remote = newrepo.remotes[0]
  2408. PagureRepo.push(ori_remote, "refs/heads/feature")
  2409. # Remove the clone
  2410. shutil.rmtree(newgitrepo)
  2411. output4 = pagure.lib.git.get_revs_between(
  2412. "0", branch_commit.oid.hex, gitrepo, "refs/heads/feature"
  2413. )
  2414. self.assertEqual(output4, [branch_commit.oid.hex])
  2415. def test_get_author(self):
  2416. """ Test the get_author method of pagure.lib.git. """
  2417. self.test_update_git()
  2418. gitrepo = os.path.join(
  2419. self.path, "repos", "tickets", "test_ticket_repo.git"
  2420. )
  2421. output = pagure.lib.git.read_git_lines(
  2422. ["log", "-3", "--pretty='%H'"], gitrepo
  2423. )
  2424. self.assertEqual(len(output), 2)
  2425. for githash in output:
  2426. githash = githash.replace("'", "")
  2427. output = pagure.lib.git.get_author(githash, gitrepo)
  2428. self.assertEqual(output, "pagure")
  2429. def get_author_email(self):
  2430. """ Test the get_author_email method of pagure.lib.git. """
  2431. self.test_update_git()
  2432. gitrepo = os.path.join(self.path, "tickets", "test_ticket_repo.git")
  2433. output = pagure.lib.git.read_git_lines(
  2434. ["log", "-3", "--pretty='%H'"], gitrepo
  2435. )
  2436. self.assertEqual(len(output), 2)
  2437. for githash in output:
  2438. githash = githash.replace("'", "")
  2439. output = pagure.lib.git.get_author_email(githash, gitrepo)
  2440. self.assertEqual(output, "pagure")
  2441. def test_get_repo_name(self):
  2442. """ Test the get_repo_name method of pagure.lib.git. """
  2443. def runtest(reponame, *path):
  2444. gitrepo = os.path.join(self.path, "repos", *path)
  2445. os.makedirs(gitrepo)
  2446. repo_name = pagure.lib.git.get_repo_name(gitrepo)
  2447. self.assertEqual(repo_name, reponame)
  2448. runtest("test_ticket_repo", "tickets", "test_ticket_repo.git")
  2449. runtest("test", "test.git")
  2450. runtest("foo.test", "foo.test.git")
  2451. def test_get_username(self):
  2452. """ Test the get_username method of pagure.lib.git. """
  2453. def runtest(username, *path):
  2454. gitrepo = os.path.join(self.path, "repos", *path)
  2455. os.makedirs(gitrepo)
  2456. repo_username = pagure.lib.git.get_username(gitrepo)
  2457. self.assertEqual(repo_username, username)
  2458. runtest(None, "tickets", "test_ticket_repo.git")
  2459. runtest(None, "test.git")
  2460. runtest(None, "foo.test.git")
  2461. runtest("pingou", "forks", "pingou", "foo.test.git")
  2462. runtest("pingou", "forks", "pingou", "bar/foo.test.git")
  2463. def test_get_repo_namespace(self):
  2464. """ Test the get_repo_namespace method of pagure.lib.git. """
  2465. def runtest(namespace, *path):
  2466. gitrepo = os.path.join(self.path, "repos", *path)
  2467. if not os.path.exists(gitrepo):
  2468. os.makedirs(gitrepo)
  2469. repo_namespace = pagure.lib.git.get_repo_namespace(gitrepo)
  2470. self.assertEqual(repo_namespace, namespace)
  2471. runtest(None, "test_ticket_repo.git")
  2472. runtest("foo/bar/baz", "foo", "bar", "baz", "test.git")
  2473. runtest(None, "foo.test.git")
  2474. runtest(None, "forks", "user", "foo.test.git")
  2475. runtest("bar", "forks", "user", "bar", "foo.test.git")
  2476. runtest("ns/bar", "forks", "user", "ns", "bar", "foo.test.git")
  2477. runtest("bar", "forks", "user", "bar", "foo.test.git")
  2478. def test_update_custom_fields_from_json(self):
  2479. """ Test the update_custom_fields_from_json method of lib.git """
  2480. tests.create_projects(self.session)
  2481. repo = pagure.lib.query._get_project(self.session, "test")
  2482. # Create issues to play with
  2483. pagure.lib.query.new_issue(
  2484. session=self.session,
  2485. repo=repo,
  2486. title="Test issue",
  2487. content="We should work on this",
  2488. user="pingou",
  2489. issue_uid="someuid",
  2490. )
  2491. self.session.commit()
  2492. issue = pagure.lib.query.get_issue_by_uid(self.session, "someuid")
  2493. # Fake json data, currently without custom_fields
  2494. # This should bring no new custom_fields to the issue
  2495. json_data = {
  2496. "status": "Open",
  2497. "title": "Test issue",
  2498. "private": False,
  2499. "content": "We should work on this",
  2500. "user": {
  2501. "fullname": "PY C",
  2502. "name": "pingou",
  2503. "default_email": "bar@pingou.com",
  2504. "emails": ["bar@pingou.com"],
  2505. },
  2506. "id": 1,
  2507. "blocks": [],
  2508. "depends": [],
  2509. "date_created": "1234567",
  2510. "comments": [],
  2511. }
  2512. pagure.lib.git.update_custom_field_from_json(
  2513. self.session, repo, issue, json_data
  2514. )
  2515. updated_issue = pagure.lib.query.get_issue_by_uid(
  2516. self.session, "someuid"
  2517. )
  2518. self.assertEqual(updated_issue.to_json().get("custom_fields"), [])
  2519. custom_fields = [
  2520. {
  2521. "name": "custom1",
  2522. "key_type": "text",
  2523. "value": "value1",
  2524. "key_data": None,
  2525. },
  2526. {
  2527. "name": "custom2",
  2528. "key_type": "text",
  2529. "value": "value2",
  2530. "key_data": None,
  2531. },
  2532. ]
  2533. # Again, Fake the json data but, with custom_fields in it
  2534. # The updated issue should have the custom_fields as
  2535. # was in the json_data
  2536. json_data = {
  2537. "status": "Open",
  2538. "title": "Test issue",
  2539. "private": False,
  2540. "content": "We should work on this",
  2541. "user": {
  2542. "fullname": "PY C",
  2543. "name": "pingou",
  2544. "default_email": "bar@pingou.com",
  2545. "emails": ["bar@pingou.com"],
  2546. },
  2547. "id": 1,
  2548. "blocks": [],
  2549. "depends": [],
  2550. "date_created": "1234567",
  2551. "comments": [],
  2552. "custom_fields": custom_fields,
  2553. }
  2554. pagure.lib.git.update_custom_field_from_json(
  2555. self.session, repo, issue, json_data
  2556. )
  2557. updated_issue = pagure.lib.query.get_issue_by_uid(
  2558. self.session, "someuid"
  2559. )
  2560. custom_fields_of_issue = updated_issue.to_json().get("custom_fields")
  2561. self.assertEqual(custom_fields_of_issue, custom_fields)
  2562. @patch("pagure.lib.notify.send_email")
  2563. @patch("pagure.lib.git.update_git")
  2564. def test_merge_pull_request_no_master(self, email_f, up_git):
  2565. """ Test the merge_pull_request function when there are no master
  2566. branch in the repo. """
  2567. email_f.return_value = True
  2568. up_git.return_value = True
  2569. gitfolder = os.path.join(self.path, "repos")
  2570. docfolder = os.path.join(self.path, "docs")
  2571. ticketfolder = os.path.join(self.path, "tickets")
  2572. requestfolder = os.path.join(self.path, "requests")
  2573. # Create project
  2574. item = pagure.lib.model.Project(
  2575. user_id=1, # pingou
  2576. name="test",
  2577. description="test project",
  2578. hook_token="aaabbbwww",
  2579. )
  2580. self.session.add(item)
  2581. self.session.commit()
  2582. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2583. gitrepo = os.path.join(gitfolder, repo.path)
  2584. docrepo = os.path.join(docfolder, repo.path)
  2585. ticketrepo = os.path.join(ticketfolder, repo.path)
  2586. requestrepo = os.path.join(requestfolder, repo.path)
  2587. os.makedirs(os.path.join(self.path, "repos", "forks", "foo"))
  2588. self.gitrepo = os.path.join(self.path, "repos", "test.git")
  2589. os.makedirs(self.gitrepo)
  2590. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  2591. # Fork the project
  2592. task = pagure.lib.query.fork_project(
  2593. session=self.session, user="foo", repo=repo
  2594. )
  2595. self.session.commit()
  2596. self.assertEqual(
  2597. task.get(),
  2598. {
  2599. "endpoint": "ui_ns.view_repo",
  2600. "repo": "test",
  2601. "username": "foo",
  2602. "namespace": None,
  2603. },
  2604. )
  2605. # Create repo, with some content
  2606. self.gitrepo = os.path.join(
  2607. self.path, "repos", "forks", "foo", "test.git"
  2608. )
  2609. tests.add_content_git_repo(self.gitrepo, branch="feature")
  2610. fork_repo = pagure.lib.query.get_authorized_project(
  2611. self.session, "test", user="foo"
  2612. )
  2613. # Create a PR to play with
  2614. req = pagure.lib.query.new_pull_request(
  2615. session=self.session,
  2616. repo_from=fork_repo,
  2617. branch_from="feature",
  2618. repo_to=repo,
  2619. branch_to="master",
  2620. title="test PR",
  2621. user="pingou",
  2622. requestuid="foobar",
  2623. requestid=None,
  2624. status="Open",
  2625. notify=True,
  2626. )
  2627. self.assertEqual(req.id, 1)
  2628. self.assertEqual(req.title, "test PR")
  2629. # `master` branch not found
  2630. msg = pagure.lib.git.merge_pull_request(
  2631. self.session, request=req, username="pingou", domerge=False
  2632. )
  2633. self.assertEqual(msg, "FFORWARD")
  2634. @patch("pagure.lib.notify.send_email")
  2635. @patch("pagure.lib.git.update_git")
  2636. def test_merge_pull_request_closed(self, email_f, up_git):
  2637. """ Test the merge_pull_request function when the PR was already
  2638. closed/merged. """
  2639. email_f.return_value = True
  2640. up_git.return_value = True
  2641. gitfolder = os.path.join(self.path, "repos")
  2642. docfolder = os.path.join(self.path, "docs")
  2643. ticketfolder = os.path.join(self.path, "tickets")
  2644. requestfolder = os.path.join(self.path, "requests")
  2645. # Create project
  2646. item = pagure.lib.model.Project(
  2647. user_id=1, # pingou
  2648. name="test",
  2649. description="test project",
  2650. hook_token="aaabbbwww",
  2651. )
  2652. self.session.add(item)
  2653. self.session.commit()
  2654. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2655. os.makedirs(os.path.join(self.path, "repos", "forks", "foo"))
  2656. self.gitrepo = os.path.join(self.path, "repos", "test.git")
  2657. os.makedirs(self.gitrepo)
  2658. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  2659. tests.add_content_git_repo(self.gitrepo, branch="master")
  2660. # Fork the project
  2661. task = pagure.lib.query.fork_project(
  2662. session=self.session, user="foo", repo=repo
  2663. )
  2664. self.session.commit()
  2665. self.assertEqual(
  2666. task.get(),
  2667. {
  2668. "endpoint": "ui_ns.view_repo",
  2669. "repo": "test",
  2670. "username": "foo",
  2671. "namespace": None,
  2672. },
  2673. )
  2674. # Create repo, with some content
  2675. self.gitrepo = os.path.join(
  2676. self.path, "repos", "forks", "foo", "test.git"
  2677. )
  2678. tests.add_content_git_repo(self.gitrepo, branch="feature")
  2679. fork_repo = pagure.lib.query.get_authorized_project(
  2680. self.session, "test", user="foo"
  2681. )
  2682. # Create a PR to play with
  2683. req = pagure.lib.query.new_pull_request(
  2684. session=self.session,
  2685. repo_from=fork_repo,
  2686. branch_from="feature",
  2687. repo_to=repo,
  2688. branch_to="master",
  2689. title="test PR",
  2690. user="pingou",
  2691. requestuid="foobar",
  2692. requestid=None,
  2693. status="Open",
  2694. notify=True,
  2695. )
  2696. self.assertEqual(req.id, 1)
  2697. self.assertEqual(req.title, "test PR")
  2698. # Close the PR before we ask to merge it
  2699. req.status = "Closed"
  2700. req.closed_by_id = 2 # id:2 == foo
  2701. req.closed_at = datetime.datetime(2017, 10, 20, 12, 32, 10)
  2702. self.session.add(req)
  2703. self.session.commit()
  2704. # PR already closed
  2705. six.assertRaisesRegex(
  2706. self,
  2707. pagure.exceptions.PagureException,
  2708. "This pull-request was merged or closed by foo",
  2709. pagure.lib.git.merge_pull_request,
  2710. self.session,
  2711. request=req,
  2712. username="pingou",
  2713. domerge=False,
  2714. )
  2715. @patch("subprocess.Popen")
  2716. def test_generate_gitolite_acls(self, popen):
  2717. """ Test calling generate_gitolite_acls. """
  2718. pagure.SESSION = self.session
  2719. pagure.lib.git.SESSION = self.session
  2720. pagure.config.config["GITOLITE_HOME"] = "/tmp"
  2721. proc = MagicMock()
  2722. proc.communicate.return_value = (1, 2)
  2723. proc.returncode = 0
  2724. popen.return_value = proc
  2725. helper = pagure.lib.git_auth.get_git_auth_helper("gitolite3")
  2726. helper.generate_acls(project=None)
  2727. popen.assert_called_with(
  2728. "HOME=/tmp gitolite compile && "
  2729. "HOME=/tmp gitolite trigger POST_COMPILE",
  2730. cwd="/tmp",
  2731. shell=True,
  2732. stderr=-1,
  2733. stdout=-1,
  2734. )
  2735. def test_is_forced_push_new_branch(self):
  2736. self.assertFalse(
  2737. pagure.lib.git.is_forced_push(
  2738. "0000000000000000000000000000000000000000",
  2739. "^0e6e0b6c931d65ee22f67205a53933d841c6eeff",
  2740. "path/is/not/important",
  2741. )
  2742. )
  2743. def test_read_output(self):
  2744. here = os.path.dirname(os.path.realpath(__file__))
  2745. # This should't block
  2746. self.assertTrue(
  2747. pagure.lib.git.read_output(["git", "rev-list", "--all"], here)
  2748. )
  2749. @patch("pagure.utils.get_repo_path")
  2750. def test_update_pull_ref(self, get_repo_path):
  2751. fake_pr = MagicMock()
  2752. fake_pr.uid = "1234567"
  2753. fake_pr.branch_from = "master"
  2754. fake_pr.id = 6
  2755. fake_pr.user = MagicMock()
  2756. fake_pr.user.user = "pingou"
  2757. fake_pr.project = "doesnt_matter_mocked_out"
  2758. projects = tests.create_projects_git(
  2759. os.path.join(self.path, "repos"), bare=True
  2760. )
  2761. tests.add_content_git_repo(projects[0])
  2762. tests.add_content_git_repo(projects[1])
  2763. orig = pygit2.Repository(projects[0])
  2764. fork = pygit2.Repository(projects[1])
  2765. get_repo_path.return_value = projects[0]
  2766. # make sure that creating works the first time
  2767. pagure.lib.git.update_pull_ref(fake_pr, fork)
  2768. oldhex = fork.references["refs/heads/master"].peel().hex
  2769. self.assertEqual(
  2770. orig.references["refs/pull/6/head"].peel().hex, oldhex
  2771. )
  2772. # make sure that updating works correctly
  2773. tests.add_content_git_repo(projects[1], append="foobar")
  2774. newhex = fork.references["refs/heads/master"].peel().hex
  2775. self.assertNotEqual(oldhex, newhex)
  2776. pagure.lib.git.update_pull_ref(fake_pr, fork)
  2777. self.assertEqual(
  2778. orig.references["refs/pull/6/head"].peel().hex, newhex
  2779. )
  2780. # make sure the function works fine even if there's a leftover
  2781. # ref from previous failed run of the function
  2782. with patch("pygit2.remote.RemoteCollection.delete"):
  2783. pagure.lib.git.update_pull_ref(fake_pr, fork)
  2784. self.assertIsNotNone(fork.remotes["pingou_1234567"])
  2785. tests.add_content_git_repo(projects[1], append="foobarbaz")
  2786. newesthex = fork.references["refs/heads/master"].peel().hex
  2787. self.assertNotEqual(newhex, newesthex)
  2788. pagure.lib.git.update_pull_ref(fake_pr, fork)
  2789. self.assertEqual(
  2790. orig.references["refs/pull/6/head"].peel().hex, newesthex
  2791. )
  2792. class PagureLibGitCommitToPatchtests(tests.Modeltests):
  2793. """ Tests for pagure.lib.git """
  2794. maxDiff = None
  2795. def setUp(self):
  2796. """ Set up the environment for the tests. """
  2797. super(PagureLibGitCommitToPatchtests, self).setUp()
  2798. # Create a git repo to play with
  2799. self.gitrepo = os.path.join(self.path, "repos", "test_repo.git")
  2800. os.makedirs(self.gitrepo)
  2801. repo = pygit2.init_repository(self.gitrepo)
  2802. # Create a file in that git repo
  2803. with open(os.path.join(self.gitrepo, "sources"), "w") as stream:
  2804. stream.write("foo\n bar")
  2805. repo.index.add("sources")
  2806. repo.index.write()
  2807. # Commits the files added
  2808. tree = repo.index.write_tree()
  2809. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  2810. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  2811. repo.create_commit(
  2812. "refs/heads/master", # the name of the reference to update
  2813. author,
  2814. committer,
  2815. "Add sources file for testing",
  2816. # binary string representing the tree object ID
  2817. tree,
  2818. # list of binary strings representing parents of the new commit
  2819. [],
  2820. )
  2821. self.first_commit = repo.revparse_single("HEAD")
  2822. # Edit the sources file again
  2823. with open(os.path.join(self.gitrepo, "sources"), "w") as stream:
  2824. stream.write("foo\n bar\nbaz\n boose")
  2825. repo.index.add("sources")
  2826. repo.index.write()
  2827. # Commits the files added
  2828. tree = repo.index.write_tree()
  2829. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  2830. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  2831. repo.create_commit(
  2832. "refs/heads/master", # the name of the reference to update
  2833. author,
  2834. committer,
  2835. "Add baz and boose to the sources\n\n There are more objects to "
  2836. "consider",
  2837. # binary string representing the tree object ID
  2838. tree,
  2839. # list of binary strings representing parents of the new commit
  2840. [self.first_commit.oid.hex],
  2841. )
  2842. self.second_commit = repo.revparse_single("HEAD")
  2843. # Add empty commit
  2844. repo.create_commit(
  2845. "refs/heads/master", # the name of the reference to update
  2846. author,
  2847. committer,
  2848. "Empty commit",
  2849. # binary string representing the tree object ID
  2850. tree,
  2851. # list of binary strings representing parents of the new commit
  2852. [self.second_commit.oid.hex],
  2853. )
  2854. self.third_commit = repo.revparse_single("HEAD")
  2855. def test_commit_to_patch_first_commit(self):
  2856. """ Test the commit_to_patch function of pagure.lib.git. """
  2857. repo = pygit2.init_repository(self.gitrepo)
  2858. patch = pagure.lib.git.commit_to_patch(repo, self.first_commit)
  2859. exp = r"""Mon Sep 17 00:00:00 2001
  2860. From: Alice Author <alice@authors.tld>
  2861. Subject: Add sources file for testing
  2862. ---
  2863. diff --git a/sources b/sources
  2864. new file mode 100644
  2865. index 0000000..9f44358
  2866. --- /dev/null
  2867. +++ b/sources
  2868. @@ -0,0 +1,2 @@
  2869. +foo
  2870. + bar
  2871. \ No newline at end of file
  2872. """
  2873. npatch = []
  2874. for row in patch.split("\n"):
  2875. if row.startswith("Date:"):
  2876. continue
  2877. if row.startswith("From "):
  2878. row = row.split(" ", 2)[2]
  2879. npatch.append(row)
  2880. patch = "\n".join(npatch)
  2881. self.assertEqual(patch, exp)
  2882. def test_commit_to_patch_single_commit(self):
  2883. """ Test the commit_to_patch function of pagure.lib.git. """
  2884. repo = pygit2.init_repository(self.gitrepo)
  2885. patch = pagure.lib.git.commit_to_patch(repo, self.second_commit)
  2886. exp = r"""Mon Sep 17 00:00:00 2001
  2887. From: Alice Author <alice@authors.tld>
  2888. Subject: Add baz and boose to the sources
  2889. There are more objects to consider
  2890. ---
  2891. diff --git a/sources b/sources
  2892. index 9f44358..2a552bb 100644
  2893. --- a/sources
  2894. +++ b/sources
  2895. @@ -1,2 +1,4 @@
  2896. foo
  2897. - bar
  2898. \ No newline at end of file
  2899. + bar
  2900. +baz
  2901. + boose
  2902. \ No newline at end of file
  2903. """
  2904. npatch = []
  2905. for row in patch.split("\n"):
  2906. if row.startswith("Date:"):
  2907. continue
  2908. if row.startswith("From "):
  2909. row = row.split(" ", 2)[2]
  2910. npatch.append(row)
  2911. patch = "\n".join(npatch)
  2912. self.assertEqual(patch, exp)
  2913. def test_commit_to_patch_2_commits(self):
  2914. """ Test the commit_to_patch function of pagure.lib.git. """
  2915. repo = pygit2.init_repository(self.gitrepo)
  2916. patch = pagure.lib.git.commit_to_patch(
  2917. repo, [self.first_commit, self.second_commit]
  2918. )
  2919. exp = r"""Mon Sep 17 00:00:00 2001
  2920. From: Alice Author <alice@authors.tld>
  2921. Subject: [PATCH 1/2] Add sources file for testing
  2922. ---
  2923. diff --git a/sources b/sources
  2924. new file mode 100644
  2925. index 0000000..9f44358
  2926. --- /dev/null
  2927. +++ b/sources
  2928. @@ -0,0 +1,2 @@
  2929. +foo
  2930. + bar
  2931. \ No newline at end of file
  2932. Mon Sep 17 00:00:00 2001
  2933. From: Alice Author <alice@authors.tld>
  2934. Subject: [PATCH 2/2] Add baz and boose to the sources
  2935. There are more objects to consider
  2936. ---
  2937. diff --git a/sources b/sources
  2938. index 9f44358..2a552bb 100644
  2939. --- a/sources
  2940. +++ b/sources
  2941. @@ -1,2 +1,4 @@
  2942. foo
  2943. - bar
  2944. \ No newline at end of file
  2945. + bar
  2946. +baz
  2947. + boose
  2948. \ No newline at end of file
  2949. """
  2950. npatch = []
  2951. for row in patch.split("\n"):
  2952. if row.startswith("Date:"):
  2953. continue
  2954. if row.startswith("From "):
  2955. row = row.split(" ", 2)[2]
  2956. npatch.append(row)
  2957. patch = "\n".join(npatch)
  2958. self.assertEqual(patch, exp)
  2959. def test_commit_to_patch_first_commit_diff(self):
  2960. """ Test the commit_to_patch function of pagure.lib.git. """
  2961. repo = pygit2.init_repository(self.gitrepo)
  2962. patch = pagure.lib.git.commit_to_patch(
  2963. repo, self.first_commit, diff_view=True
  2964. )
  2965. exp = r"""diff --git a/sources b/sources
  2966. new file mode 100644
  2967. index 0000000..9f44358
  2968. --- /dev/null
  2969. +++ b/sources
  2970. @@ -0,0 +1,2 @@
  2971. +foo
  2972. + bar
  2973. \ No newline at end of file
  2974. """
  2975. npatch = []
  2976. for row in patch.split("\n"):
  2977. if row.startswith("Date:"):
  2978. continue
  2979. if row.startswith("From "):
  2980. row = row.split(" ", 2)[2]
  2981. npatch.append(row)
  2982. patch = "\n".join(npatch)
  2983. self.assertEqual(patch, exp)
  2984. def test_commit_to_patch_single_commit_diff(self):
  2985. """ Test the commit_to_patch function of pagure.lib.git. """
  2986. repo = pygit2.init_repository(self.gitrepo)
  2987. patch = pagure.lib.git.commit_to_patch(
  2988. repo, self.second_commit, diff_view=True
  2989. )
  2990. exp = r"""diff --git a/sources b/sources
  2991. index 9f44358..2a552bb 100644
  2992. --- a/sources
  2993. +++ b/sources
  2994. @@ -1,2 +1,4 @@
  2995. foo
  2996. - bar
  2997. \ No newline at end of file
  2998. + bar
  2999. +baz
  3000. + boose
  3001. \ No newline at end of file
  3002. """
  3003. npatch = []
  3004. for row in patch.split("\n"):
  3005. if row.startswith("Date:"):
  3006. continue
  3007. if row.startswith("From "):
  3008. row = row.split(" ", 2)[2]
  3009. npatch.append(row)
  3010. patch = "\n".join(npatch)
  3011. self.assertEqual(patch, exp)
  3012. def test_commit_to_patch_two_commits_diff(self):
  3013. """ Test the commit_to_patch function of pagure.lib.git. """
  3014. repo = pygit2.init_repository(self.gitrepo)
  3015. patch = pagure.lib.git.commit_to_patch(
  3016. repo, [self.first_commit, self.second_commit], diff_view=True
  3017. )
  3018. exp = r"""diff --git a/sources b/sources
  3019. new file mode 100644
  3020. index 0000000..9f44358
  3021. --- /dev/null
  3022. +++ b/sources
  3023. @@ -0,0 +1,2 @@
  3024. +foo
  3025. + bar
  3026. \ No newline at end of file
  3027. diff --git a/sources b/sources
  3028. index 9f44358..2a552bb 100644
  3029. --- a/sources
  3030. +++ b/sources
  3031. @@ -1,2 +1,4 @@
  3032. foo
  3033. - bar
  3034. \ No newline at end of file
  3035. + bar
  3036. +baz
  3037. + boose
  3038. \ No newline at end of file
  3039. """
  3040. npatch = []
  3041. for row in patch.split("\n"):
  3042. if row.startswith("Date:"):
  3043. continue
  3044. if row.startswith("From "):
  3045. row = row.split(" ", 2)[2]
  3046. npatch.append(row)
  3047. patch = "\n".join(npatch)
  3048. self.assertEqual(patch, exp)
  3049. def test_commit_to_patch_first_commit_diff_separated(self):
  3050. """ Test the commit_to_patch function of pagure.lib.git. """
  3051. repo = pygit2.init_repository(self.gitrepo)
  3052. patches = pagure.lib.git.commit_to_patch(
  3053. repo, self.first_commit, diff_view=True, separated=True
  3054. )
  3055. exp = r"""diff --git a/sources b/sources
  3056. new file mode 100644
  3057. index 0000000..9f44358
  3058. --- /dev/null
  3059. +++ b/sources
  3060. @@ -0,0 +1,2 @@
  3061. +foo
  3062. + bar
  3063. \ No newline at end of file
  3064. """
  3065. output = []
  3066. for patch in patches:
  3067. npatch = []
  3068. for row in patch.split("\n"):
  3069. if row.startswith("Date:"):
  3070. continue
  3071. if row.startswith("From "):
  3072. row = row.split(" ", 2)[2]
  3073. npatch.append(row)
  3074. patch = "\n".join(npatch)
  3075. output.append(patch)
  3076. self.assertEqual(output, [exp])
  3077. def test_commit_to_patch_single_commit_diff_separated(self):
  3078. """ Test the commit_to_patch function of pagure.lib.git. """
  3079. repo = pygit2.init_repository(self.gitrepo)
  3080. patches = pagure.lib.git.commit_to_patch(
  3081. repo, self.second_commit, diff_view=True, separated=True
  3082. )
  3083. exp = r"""diff --git a/sources b/sources
  3084. index 9f44358..2a552bb 100644
  3085. --- a/sources
  3086. +++ b/sources
  3087. @@ -1,2 +1,4 @@
  3088. foo
  3089. - bar
  3090. \ No newline at end of file
  3091. + bar
  3092. +baz
  3093. + boose
  3094. \ No newline at end of file
  3095. """
  3096. output = []
  3097. for patch in patches:
  3098. npatch = []
  3099. for row in patch.split("\n"):
  3100. if row.startswith("Date:"):
  3101. continue
  3102. if row.startswith("From "):
  3103. row = row.split(" ", 2)[2]
  3104. npatch.append(row)
  3105. patch = "\n".join(npatch)
  3106. output.append(patch)
  3107. self.assertEqual(output, [exp])
  3108. def test_commit_to_patch_two_commits_diff_separated(self):
  3109. """ Test the commit_to_patch function of pagure.lib.git. """
  3110. repo = pygit2.init_repository(self.gitrepo)
  3111. patches = pagure.lib.git.commit_to_patch(
  3112. repo,
  3113. [self.first_commit, self.second_commit],
  3114. diff_view=True,
  3115. separated=True,
  3116. )
  3117. exp = [
  3118. r"""diff --git a/sources b/sources
  3119. new file mode 100644
  3120. index 0000000..9f44358
  3121. --- /dev/null
  3122. +++ b/sources
  3123. @@ -0,0 +1,2 @@
  3124. +foo
  3125. + bar
  3126. \ No newline at end of file
  3127. """,
  3128. r"""diff --git a/sources b/sources
  3129. index 9f44358..2a552bb 100644
  3130. --- a/sources
  3131. +++ b/sources
  3132. @@ -1,2 +1,4 @@
  3133. foo
  3134. - bar
  3135. \ No newline at end of file
  3136. + bar
  3137. +baz
  3138. + boose
  3139. \ No newline at end of file
  3140. """,
  3141. ]
  3142. output = []
  3143. for patch in patches:
  3144. npatch = []
  3145. for row in patch.split("\n"):
  3146. if row.startswith("Date:"):
  3147. continue
  3148. if row.startswith("From "):
  3149. row = row.split(" ", 2)[2]
  3150. npatch.append(row)
  3151. patch = "\n".join(npatch)
  3152. output.append(patch)
  3153. self.assertEqual(output, exp)
  3154. def test_commit_to_patch_empty_commit(self):
  3155. """ Test the commit_to_path function of pagure.lib.git. """
  3156. repo = pygit2.init_repository(self.gitrepo)
  3157. patch = pagure.lib.git.commit_to_patch(repo, self.third_commit)
  3158. exp = ""
  3159. self.assertEqual(patch, exp)
  3160. def test_commit_to_patch_empty_commit_diff(self):
  3161. """ Test the commit_to_patch function of pagure.lib.git. """
  3162. repo = pygit2.init_repository(self.gitrepo)
  3163. patch = pagure.lib.git.commit_to_patch(
  3164. repo, self.third_commit, diff_view=True
  3165. )
  3166. exp = ""
  3167. self.assertEqual(patch, exp)
  3168. if __name__ == "__main__":
  3169. unittest.main(verbosity=2)