test_pagure_lib_git.py 93 KB

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