test_pagure_lib_git.py 105 KB

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