test_pagure_lib_git.py 106 KB

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