You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 45KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
7 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
7 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Squashed commit of the following: commit 0afcb843d7ffd596991c4885cab768273a6eb42c Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 17:13:29 2016 -0600 Removed Upload stats as the upload table is just a temporary table commit 7ecd73ff5535612d79d471409173ee7f1fcfa157 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:42:41 2016 -0600 Fix for CodeMirror mode commit c29b9ab531e2e7af0fb5db24dc17e51027dd1174 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 08:03:33 2016 -0600 Made tabbing in editor use spaces commit 23af384c53206a8a40e11e45bf49d7a149c4adcd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:56:46 2016 -0600 Fix for data-url commit cfb8a97591cb6fc0a92e49563b7b764c524db0e9 Merge: 7fc8a89 991ce42 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:42:53 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit 7fc8a89cb495478225b02d613e647f99a1489634 Merge: fd3d86c c03d040 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:40:00 2016 -0600 Merge branch 'feature-create-and-edit-repo-file' of github.com:richmahn/gogs into feature-create-and-edit-repo-file commit fd3d86ca6bbc02cfda566a504ffd6b03db4f75ef Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Sun Jul 31 07:39:44 2016 -0600 Code cleanup commit c03d0401c1049eeeccc32ab1f9c3303c130be5ee Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 29 15:38:23 2016 -0600 Code cleanup commit 98e1206ccf9f9a4503c020e3a7830cf9f861dfae Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:36:01 2016 -0600 Code cleanup and fixes commit c2895dc742f25f8412879c9fa15e18f27f42f194 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 18:24:04 2016 -0600 Fixes per Unknwon's requests commit 6aa7e46b21ad4c96e562daa2eac26a8fb408f8ef Merge: 889e9fa ad7ea88 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Thu Jul 28 17:13:43 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go modules/setting/setting.go commit 889e9faf1bd8559a4979c8f46005d488c1a234d4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:09:18 2016 -0600 Fix in gogs.js commit 47603edf223f147b114be65f3bd27bc1e88827a5 Merge: bb57912 cf85e9e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:07:36 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go public/js/gogs.js commit bb5791255867a71c11a77b639db050ad09c597a4 Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 22 14:02:18 2016 -0600 Update for using CodeMirror mode addon commit d10d128c51039be19e2af9c66c63db66a9f2ec6d Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 16:12:57 2016 -0600 Update for Edit commit 34a34982025144e3225e389f7849eb6273c1d576 Merge: fa1b752 1c7dcdd Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Tue Jul 19 11:52:02 2016 -0600 Merge remote-tracking branch 'gogits/develop' into feature-create-and-edit-repo-file Conflicts: modules/bindata/bindata.go commit fa1b752be29cd455c5184ddac2ffe80b3489763e Author: Richard Mahn <richard_mahn@wycliffeassociates.org> Date: Fri Jul 15 18:35:42 2016 -0600 Feature for editing, creating, uploading and deleting files
7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "errors"
  7. "fmt"
  8. "io"
  9. "mime/multipart"
  10. "os"
  11. "path"
  12. "strings"
  13. "time"
  14. "github.com/Unknwon/com"
  15. "github.com/go-xorm/xorm"
  16. api "github.com/gogits/go-gogs-client"
  17. gouuid "github.com/satori/go.uuid"
  18. "github.com/gogits/gogs/modules/base"
  19. "github.com/gogits/gogs/modules/log"
  20. "github.com/gogits/gogs/modules/setting"
  21. )
  22. var (
  23. ErrWrongIssueCounter = errors.New("Invalid number of issues for this milestone")
  24. ErrAttachmentNotLinked = errors.New("Attachment does not belong to this issue")
  25. ErrMissingIssueNumber = errors.New("No issue number specified")
  26. )
  27. // Issue represents an issue or pull request of repository.
  28. type Issue struct {
  29. ID int64 `xorm:"pk autoincr"`
  30. RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
  31. Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
  32. Title string `xorm:"name"`
  33. Repo *Repository `xorm:"-"`
  34. PosterID int64
  35. Poster *User `xorm:"-"`
  36. Labels []*Label `xorm:"-"`
  37. MilestoneID int64
  38. Milestone *Milestone `xorm:"-"`
  39. AssigneeID int64
  40. Assignee *User `xorm:"-"`
  41. IsRead bool `xorm:"-"`
  42. IsPull bool // Indicates whether is a pull request or not.
  43. *PullRequest `xorm:"-"`
  44. IsClosed bool
  45. Content string `xorm:"TEXT"`
  46. RenderedContent string `xorm:"-"`
  47. Priority int
  48. NumComments int
  49. Deadline time.Time `xorm:"-"`
  50. DeadlineUnix int64
  51. Created time.Time `xorm:"-"`
  52. CreatedUnix int64
  53. Updated time.Time `xorm:"-"`
  54. UpdatedUnix int64
  55. Attachments []*Attachment `xorm:"-"`
  56. Comments []*Comment `xorm:"-"`
  57. }
  58. func (i *Issue) BeforeInsert() {
  59. i.CreatedUnix = time.Now().Unix()
  60. i.UpdatedUnix = i.CreatedUnix
  61. }
  62. func (i *Issue) BeforeUpdate() {
  63. i.UpdatedUnix = time.Now().Unix()
  64. i.DeadlineUnix = i.Deadline.Unix()
  65. }
  66. func (i *Issue) AfterSet(colName string, _ xorm.Cell) {
  67. var err error
  68. switch colName {
  69. case "id":
  70. i.Attachments, err = GetAttachmentsByIssueID(i.ID)
  71. if err != nil {
  72. log.Error(3, "GetAttachmentsByIssueID[%d]: %v", i.ID, err)
  73. }
  74. i.Comments, err = GetCommentsByIssueID(i.ID)
  75. if err != nil {
  76. log.Error(3, "GetCommentsByIssueID[%d]: %v", i.ID, err)
  77. }
  78. i.Labels, err = GetLabelsByIssueID(i.ID)
  79. if err != nil {
  80. log.Error(3, "GetLabelsByIssueID[%d]: %v", i.ID, err)
  81. }
  82. case "poster_id":
  83. i.Poster, err = GetUserByID(i.PosterID)
  84. if err != nil {
  85. if IsErrUserNotExist(err) {
  86. i.PosterID = -1
  87. i.Poster = NewGhostUser()
  88. } else {
  89. log.Error(3, "GetUserByID[%d]: %v", i.ID, err)
  90. }
  91. return
  92. }
  93. case "milestone_id":
  94. if i.MilestoneID == 0 {
  95. return
  96. }
  97. i.Milestone, err = GetMilestoneByID(i.MilestoneID)
  98. if err != nil {
  99. log.Error(3, "GetMilestoneById[%d]: %v", i.ID, err)
  100. }
  101. case "assignee_id":
  102. if i.AssigneeID == 0 {
  103. return
  104. }
  105. i.Assignee, err = GetUserByID(i.AssigneeID)
  106. if err != nil {
  107. log.Error(3, "GetUserByID[%d]: %v", i.ID, err)
  108. }
  109. case "deadline_unix":
  110. i.Deadline = time.Unix(i.DeadlineUnix, 0).Local()
  111. case "created_unix":
  112. i.Created = time.Unix(i.CreatedUnix, 0).Local()
  113. case "updated_unix":
  114. i.Updated = time.Unix(i.UpdatedUnix, 0).Local()
  115. }
  116. }
  117. func (issue *Issue) loadAttributes(e Engine) (err error) {
  118. if issue.Repo == nil {
  119. issue.Repo, err = getRepositoryByID(e, issue.RepoID)
  120. if err != nil {
  121. return fmt.Errorf("getRepositoryByID [%d]: %v", issue.RepoID, err)
  122. }
  123. }
  124. if issue.IsPull && issue.PullRequest == nil {
  125. // It is possible pull request is not yet created.
  126. issue.PullRequest, err = getPullRequestByIssueID(e, issue.ID)
  127. if err != nil && !IsErrPullRequestNotExist(err) {
  128. return fmt.Errorf("getPullRequestByIssueID [%d]: %v", issue.ID, err)
  129. }
  130. }
  131. return nil
  132. }
  133. func (issue *Issue) LoadAttributes() error {
  134. return issue.loadAttributes(x)
  135. }
  136. // State returns string representation of issue status.
  137. func (i *Issue) State() api.StateType {
  138. if i.IsClosed {
  139. return api.STATE_CLOSED
  140. }
  141. return api.STATE_OPEN
  142. }
  143. // This method assumes some fields assigned with values:
  144. // Required - Poster, Labels,
  145. // Optional - Milestone, Assignee, PullRequest
  146. func (issue *Issue) APIFormat() *api.Issue {
  147. apiLabels := make([]*api.Label, len(issue.Labels))
  148. for i := range issue.Labels {
  149. apiLabels[i] = issue.Labels[i].APIFormat()
  150. }
  151. apiIssue := &api.Issue{
  152. ID: issue.ID,
  153. Index: issue.Index,
  154. State: issue.State(),
  155. Title: issue.Title,
  156. Body: issue.Content,
  157. User: issue.Poster.APIFormat(),
  158. Labels: apiLabels,
  159. Comments: issue.NumComments,
  160. Created: issue.Created,
  161. Updated: issue.Updated,
  162. }
  163. if issue.Milestone != nil {
  164. apiIssue.Milestone = issue.Milestone.APIFormat()
  165. }
  166. if issue.Assignee != nil {
  167. apiIssue.Assignee = issue.Assignee.APIFormat()
  168. }
  169. if issue.IsPull {
  170. apiIssue.PullRequest = &api.PullRequestMeta{
  171. HasMerged: issue.PullRequest.HasMerged,
  172. }
  173. if issue.PullRequest.HasMerged {
  174. apiIssue.PullRequest.Merged = &issue.PullRequest.Merged
  175. }
  176. }
  177. return apiIssue
  178. }
  179. // HashTag returns unique hash tag for issue.
  180. func (i *Issue) HashTag() string {
  181. return "issue-" + com.ToStr(i.ID)
  182. }
  183. func (issue *Issue) FullLink() string {
  184. var path string
  185. if issue.IsPull {
  186. path = "pulls"
  187. } else {
  188. path = "issues"
  189. }
  190. return fmt.Sprintf("%s/%s/%d", issue.Repo.FullLink(), path, issue.Index)
  191. }
  192. // IsPoster returns true if given user by ID is the poster.
  193. func (i *Issue) IsPoster(uid int64) bool {
  194. return i.PosterID == uid
  195. }
  196. func (i *Issue) hasLabel(e Engine, labelID int64) bool {
  197. return hasIssueLabel(e, i.ID, labelID)
  198. }
  199. // HasLabel returns true if issue has been labeled by given ID.
  200. func (i *Issue) HasLabel(labelID int64) bool {
  201. return i.hasLabel(x, labelID)
  202. }
  203. func (issue *Issue) sendLabelUpdatedWebhook(doer *User) {
  204. var err error
  205. if issue.IsPull {
  206. issue.PullRequest.Issue = issue
  207. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, &api.PullRequestPayload{
  208. Action: api.HOOK_ISSUE_LABEL_UPDATED,
  209. Index: issue.Index,
  210. PullRequest: issue.PullRequest.APIFormat(),
  211. Repository: issue.Repo.APIFormat(nil),
  212. Sender: doer.APIFormat(),
  213. })
  214. }
  215. if err != nil {
  216. log.Error(4, "PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  217. } else {
  218. go HookQueue.Add(issue.RepoID)
  219. }
  220. }
  221. func (i *Issue) addLabel(e *xorm.Session, label *Label) error {
  222. return newIssueLabel(e, i, label)
  223. }
  224. // AddLabel adds a new label to the issue.
  225. func (issue *Issue) AddLabel(doer *User, label *Label) error {
  226. if err := NewIssueLabel(issue, label); err != nil {
  227. return err
  228. }
  229. issue.sendLabelUpdatedWebhook(doer)
  230. return nil
  231. }
  232. func (issue *Issue) addLabels(e *xorm.Session, labels []*Label) error {
  233. return newIssueLabels(e, issue, labels)
  234. }
  235. // AddLabels adds a list of new labels to the issue.
  236. func (issue *Issue) AddLabels(doer *User, labels []*Label) error {
  237. if err := NewIssueLabels(issue, labels); err != nil {
  238. return err
  239. }
  240. issue.sendLabelUpdatedWebhook(doer)
  241. return nil
  242. }
  243. func (issue *Issue) getLabels(e Engine) (err error) {
  244. if len(issue.Labels) > 0 {
  245. return nil
  246. }
  247. issue.Labels, err = getLabelsByIssueID(e, issue.ID)
  248. if err != nil {
  249. return fmt.Errorf("getLabelsByIssueID: %v", err)
  250. }
  251. return nil
  252. }
  253. func (issue *Issue) removeLabel(e *xorm.Session, label *Label) error {
  254. return deleteIssueLabel(e, issue, label)
  255. }
  256. // RemoveLabel removes a label from issue by given ID.
  257. func (issue *Issue) RemoveLabel(doer *User, label *Label) error {
  258. if err := DeleteIssueLabel(issue, label); err != nil {
  259. return err
  260. }
  261. issue.sendLabelUpdatedWebhook(doer)
  262. return nil
  263. }
  264. func (issue *Issue) clearLabels(e *xorm.Session) (err error) {
  265. if err = issue.getLabels(e); err != nil {
  266. return fmt.Errorf("getLabels: %v", err)
  267. }
  268. for i := range issue.Labels {
  269. if err = issue.removeLabel(e, issue.Labels[i]); err != nil {
  270. return fmt.Errorf("removeLabel: %v", err)
  271. }
  272. }
  273. return nil
  274. }
  275. func (issue *Issue) ClearLabels(doer *User) (err error) {
  276. sess := x.NewSession()
  277. defer sessionRelease(sess)
  278. if err = sess.Begin(); err != nil {
  279. return err
  280. }
  281. if err = issue.clearLabels(sess); err != nil {
  282. return err
  283. }
  284. if err = sess.Commit(); err != nil {
  285. return fmt.Errorf("Commit: %v", err)
  286. }
  287. if issue.IsPull {
  288. issue.PullRequest.Issue = issue
  289. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, &api.PullRequestPayload{
  290. Action: api.HOOK_ISSUE_LABEL_CLEARED,
  291. Index: issue.Index,
  292. PullRequest: issue.PullRequest.APIFormat(),
  293. Repository: issue.Repo.APIFormat(nil),
  294. Sender: doer.APIFormat(),
  295. })
  296. }
  297. if err != nil {
  298. log.Error(4, "PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  299. } else {
  300. go HookQueue.Add(issue.RepoID)
  301. }
  302. return nil
  303. }
  304. // ReplaceLabels removes all current labels and add new labels to the issue.
  305. func (issue *Issue) ReplaceLabels(labels []*Label) (err error) {
  306. sess := x.NewSession()
  307. defer sessionRelease(sess)
  308. if err = sess.Begin(); err != nil {
  309. return err
  310. }
  311. if err = issue.clearLabels(sess); err != nil {
  312. return fmt.Errorf("clearLabels: %v", err)
  313. } else if err = issue.addLabels(sess, labels); err != nil {
  314. return fmt.Errorf("addLabels: %v", err)
  315. }
  316. return sess.Commit()
  317. }
  318. func (i *Issue) GetAssignee() (err error) {
  319. if i.AssigneeID == 0 || i.Assignee != nil {
  320. return nil
  321. }
  322. i.Assignee, err = GetUserByID(i.AssigneeID)
  323. if IsErrUserNotExist(err) {
  324. return nil
  325. }
  326. return err
  327. }
  328. // ReadBy sets issue to be read by given user.
  329. func (i *Issue) ReadBy(uid int64) error {
  330. return UpdateIssueUserByRead(uid, i.ID)
  331. }
  332. func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
  333. _, err := e.Id(issue.ID).Cols(cols...).Update(issue)
  334. return err
  335. }
  336. // UpdateIssueCols only updates values of specific columns for given issue.
  337. func UpdateIssueCols(issue *Issue, cols ...string) error {
  338. return updateIssueCols(x, issue, cols...)
  339. }
  340. func (i *Issue) changeStatus(e *xorm.Session, doer *User, repo *Repository, isClosed bool) (err error) {
  341. // Nothing should be performed if current status is same as target status
  342. if i.IsClosed == isClosed {
  343. return nil
  344. }
  345. i.IsClosed = isClosed
  346. if err = updateIssueCols(e, i, "is_closed"); err != nil {
  347. return err
  348. } else if err = updateIssueUsersByStatus(e, i.ID, isClosed); err != nil {
  349. return err
  350. }
  351. // Update issue count of labels
  352. if err = i.getLabels(e); err != nil {
  353. return err
  354. }
  355. for idx := range i.Labels {
  356. if i.IsClosed {
  357. i.Labels[idx].NumClosedIssues++
  358. } else {
  359. i.Labels[idx].NumClosedIssues--
  360. }
  361. if err = updateLabel(e, i.Labels[idx]); err != nil {
  362. return err
  363. }
  364. }
  365. // Update issue count of milestone
  366. if err = changeMilestoneIssueStats(e, i); err != nil {
  367. return err
  368. }
  369. // New action comment
  370. if _, err = createStatusComment(e, doer, repo, i); err != nil {
  371. return err
  372. }
  373. return nil
  374. }
  375. // ChangeStatus changes issue status to open or closed.
  376. func (issue *Issue) ChangeStatus(doer *User, repo *Repository, isClosed bool) (err error) {
  377. sess := x.NewSession()
  378. defer sessionRelease(sess)
  379. if err = sess.Begin(); err != nil {
  380. return err
  381. }
  382. if err = issue.changeStatus(sess, doer, repo, isClosed); err != nil {
  383. return err
  384. }
  385. if err = sess.Commit(); err != nil {
  386. return fmt.Errorf("Commit: %v", err)
  387. }
  388. if issue.IsPull {
  389. // Merge pull request calls issue.changeStatus so we need to handle separately.
  390. issue.PullRequest.Issue = issue
  391. apiPullRequest := &api.PullRequestPayload{
  392. Index: issue.Index,
  393. PullRequest: issue.PullRequest.APIFormat(),
  394. Repository: repo.APIFormat(nil),
  395. Sender: doer.APIFormat(),
  396. }
  397. if isClosed {
  398. apiPullRequest.Action = api.HOOK_ISSUE_CLOSED
  399. } else {
  400. apiPullRequest.Action = api.HOOK_ISSUE_REOPENED
  401. }
  402. err = PrepareWebhooks(repo, HOOK_EVENT_PULL_REQUEST, apiPullRequest)
  403. }
  404. if err != nil {
  405. log.Error(4, "PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
  406. } else {
  407. go HookQueue.Add(repo.ID)
  408. }
  409. return nil
  410. }
  411. func (issue *Issue) ChangeTitle(doer *User, title string) (err error) {
  412. oldTitle := issue.Title
  413. issue.Title = title
  414. if err = UpdateIssueCols(issue, "name"); err != nil {
  415. return fmt.Errorf("UpdateIssueCols: %v", err)
  416. }
  417. if issue.IsPull {
  418. issue.PullRequest.Issue = issue
  419. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, &api.PullRequestPayload{
  420. Action: api.HOOK_ISSUE_EDITED,
  421. Index: issue.Index,
  422. Changes: &api.ChangesPayload{
  423. Title: &api.ChangesFromPayload{
  424. From: oldTitle,
  425. },
  426. },
  427. PullRequest: issue.PullRequest.APIFormat(),
  428. Repository: issue.Repo.APIFormat(nil),
  429. Sender: doer.APIFormat(),
  430. })
  431. }
  432. if err != nil {
  433. log.Error(4, "PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  434. } else {
  435. go HookQueue.Add(issue.RepoID)
  436. }
  437. return nil
  438. }
  439. func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
  440. oldContent := issue.Content
  441. issue.Content = content
  442. if err = UpdateIssueCols(issue, "content"); err != nil {
  443. return fmt.Errorf("UpdateIssueCols: %v", err)
  444. }
  445. if issue.IsPull {
  446. issue.PullRequest.Issue = issue
  447. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, &api.PullRequestPayload{
  448. Action: api.HOOK_ISSUE_EDITED,
  449. Index: issue.Index,
  450. Changes: &api.ChangesPayload{
  451. Body: &api.ChangesFromPayload{
  452. From: oldContent,
  453. },
  454. },
  455. PullRequest: issue.PullRequest.APIFormat(),
  456. Repository: issue.Repo.APIFormat(nil),
  457. Sender: doer.APIFormat(),
  458. })
  459. }
  460. if err != nil {
  461. log.Error(4, "PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  462. } else {
  463. go HookQueue.Add(issue.RepoID)
  464. }
  465. return nil
  466. }
  467. func (issue *Issue) ChangeAssignee(doer *User, assigneeID int64) (err error) {
  468. issue.AssigneeID = assigneeID
  469. if err = UpdateIssueUserByAssignee(issue); err != nil {
  470. return fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
  471. }
  472. issue.Assignee, err = GetUserByID(issue.AssigneeID)
  473. if err != nil && !IsErrUserNotExist(err) {
  474. log.Error(4, "GetUserByID [assignee_id: %v]: %v", issue.AssigneeID, err)
  475. return nil
  476. }
  477. // Error not nil here means user does not exist, which is remove assignee.
  478. isRemoveAssignee := err != nil
  479. if issue.IsPull {
  480. issue.PullRequest.Issue = issue
  481. apiPullRequest := &api.PullRequestPayload{
  482. Index: issue.Index,
  483. PullRequest: issue.PullRequest.APIFormat(),
  484. Repository: issue.Repo.APIFormat(nil),
  485. Sender: doer.APIFormat(),
  486. }
  487. if isRemoveAssignee {
  488. apiPullRequest.Action = api.HOOK_ISSUE_UNASSIGNED
  489. } else {
  490. apiPullRequest.Action = api.HOOK_ISSUE_ASSIGNED
  491. }
  492. err = PrepareWebhooks(issue.Repo, HOOK_EVENT_PULL_REQUEST, apiPullRequest)
  493. }
  494. if err != nil {
  495. log.Error(4, "PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, isRemoveAssignee, err)
  496. } else {
  497. go HookQueue.Add(issue.RepoID)
  498. }
  499. return nil
  500. }
  501. // It's caller's responsibility to create action.
  502. func newIssue(e *xorm.Session, repo *Repository, issue *Issue, labelIDs []int64, uuids []string, isPull bool) (err error) {
  503. issue.Title = strings.TrimSpace(issue.Title)
  504. issue.Index = repo.NextIssueIndex()
  505. if issue.AssigneeID > 0 {
  506. // Silently drop invalid assignee
  507. valid, err := hasAccess(e, &User{ID: issue.AssigneeID}, repo, ACCESS_MODE_WRITE)
  508. if err != nil {
  509. return fmt.Errorf("hasAccess: %v", err)
  510. } else if !valid {
  511. issue.AssigneeID = 0
  512. }
  513. }
  514. if _, err = e.Insert(issue); err != nil {
  515. return err
  516. }
  517. if isPull {
  518. _, err = e.Exec("UPDATE `repository` SET num_pulls=num_pulls+1 WHERE id=?", issue.RepoID)
  519. } else {
  520. _, err = e.Exec("UPDATE `repository` SET num_issues=num_issues+1 WHERE id=?", issue.RepoID)
  521. }
  522. if err != nil {
  523. return err
  524. }
  525. if len(labelIDs) > 0 {
  526. // During the session, SQLite3 dirver cannot handle retrieve objects after update something.
  527. // So we have to get all needed labels first.
  528. labels := make([]*Label, 0, len(labelIDs))
  529. if err = e.In("id", labelIDs).Find(&labels); err != nil {
  530. return fmt.Errorf("find all labels: %v", err)
  531. }
  532. for _, label := range labels {
  533. if label.RepoID != repo.ID {
  534. continue
  535. }
  536. if err = issue.addLabel(e, label); err != nil {
  537. return fmt.Errorf("addLabel: %v", err)
  538. }
  539. }
  540. }
  541. if issue.MilestoneID > 0 {
  542. if err = changeMilestoneAssign(e, 0, issue); err != nil {
  543. return err
  544. }
  545. }
  546. if err = newIssueUsers(e, repo, issue); err != nil {
  547. return err
  548. }
  549. // Check attachments.
  550. for _, uuid := range uuids {
  551. attachment, err := getAttachmentByUUID(e, uuid)
  552. if err != nil {
  553. if IsErrAttachmentNotExist(err) {
  554. continue
  555. }
  556. return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  557. }
  558. attachment.IssueID = issue.ID
  559. // No assign value could be 0, so ignore AllCols().
  560. if _, err = e.Id(attachment.ID).Update(attachment); err != nil {
  561. return fmt.Errorf("update attachment [%d]: %v", attachment.ID, err)
  562. }
  563. }
  564. return issue.loadAttributes(e)
  565. }
  566. // NewIssue creates new issue with labels for repository.
  567. func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
  568. sess := x.NewSession()
  569. defer sessionRelease(sess)
  570. if err = sess.Begin(); err != nil {
  571. return err
  572. }
  573. if err = newIssue(sess, repo, issue, labelIDs, uuids, false); err != nil {
  574. return fmt.Errorf("newIssue: %v", err)
  575. }
  576. if err = sess.Commit(); err != nil {
  577. return fmt.Errorf("Commit: %v", err)
  578. }
  579. // Notify watchers.
  580. act := &Action{
  581. ActUserID: issue.Poster.ID,
  582. ActUserName: issue.Poster.Name,
  583. ActEmail: issue.Poster.Email,
  584. OpType: ACTION_CREATE_ISSUE,
  585. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
  586. RepoID: repo.ID,
  587. RepoUserName: repo.Owner.Name,
  588. RepoName: repo.Name,
  589. IsPrivate: repo.IsPrivate,
  590. }
  591. if err = NotifyWatchers(act); err != nil {
  592. log.Error(4, "NotifyWatchers: %v", err)
  593. } else if err = issue.MailParticipants(); err != nil {
  594. log.Error(4, "MailParticipants: %v", err)
  595. }
  596. return nil
  597. }
  598. // GetIssueByRef returns an Issue specified by a GFM reference.
  599. // See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
  600. func GetIssueByRef(ref string) (*Issue, error) {
  601. n := strings.IndexByte(ref, byte('#'))
  602. if n == -1 {
  603. return nil, ErrMissingIssueNumber
  604. }
  605. index, err := com.StrTo(ref[n+1:]).Int64()
  606. if err != nil {
  607. return nil, err
  608. }
  609. repo, err := GetRepositoryByRef(ref[:n])
  610. if err != nil {
  611. return nil, err
  612. }
  613. issue, err := GetIssueByIndex(repo.ID, index)
  614. if err != nil {
  615. return nil, err
  616. }
  617. return issue, issue.LoadAttributes()
  618. }
  619. // GetIssueByIndex returns issue by given index in repository.
  620. func GetIssueByIndex(repoID, index int64) (*Issue, error) {
  621. issue := &Issue{
  622. RepoID: repoID,
  623. Index: index,
  624. }
  625. has, err := x.Get(issue)
  626. if err != nil {
  627. return nil, err
  628. } else if !has {
  629. return nil, ErrIssueNotExist{0, repoID, index}
  630. }
  631. return issue, issue.LoadAttributes()
  632. }
  633. func getIssueByID(e Engine, id int64) (*Issue, error) {
  634. issue := new(Issue)
  635. has, err := e.Id(id).Get(issue)
  636. if err != nil {
  637. return nil, err
  638. } else if !has {
  639. return nil, ErrIssueNotExist{id, 0, 0}
  640. }
  641. return issue, issue.LoadAttributes()
  642. }
  643. // GetIssueByID returns an issue by given ID.
  644. func GetIssueByID(id int64) (*Issue, error) {
  645. return getIssueByID(x, id)
  646. }
  647. type IssuesOptions struct {
  648. UserID int64
  649. AssigneeID int64
  650. RepoID int64
  651. PosterID int64
  652. MilestoneID int64
  653. RepoIDs []int64
  654. Page int
  655. IsClosed bool
  656. IsMention bool
  657. IsPull bool
  658. Labels string
  659. SortType string
  660. }
  661. // Issues returns a list of issues by given conditions.
  662. func Issues(opts *IssuesOptions) ([]*Issue, error) {
  663. if opts.Page <= 0 {
  664. opts.Page = 1
  665. }
  666. sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
  667. if opts.RepoID > 0 {
  668. sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed)
  669. } else if opts.RepoIDs != nil {
  670. // In case repository IDs are provided but actually no repository has issue.
  671. if len(opts.RepoIDs) == 0 {
  672. return make([]*Issue, 0), nil
  673. }
  674. sess.In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).And("issue.is_closed=?", opts.IsClosed)
  675. } else {
  676. sess.Where("issue.is_closed=?", opts.IsClosed)
  677. }
  678. if opts.AssigneeID > 0 {
  679. sess.And("issue.assignee_id=?", opts.AssigneeID)
  680. } else if opts.PosterID > 0 {
  681. sess.And("issue.poster_id=?", opts.PosterID)
  682. }
  683. if opts.MilestoneID > 0 {
  684. sess.And("issue.milestone_id=?", opts.MilestoneID)
  685. }
  686. sess.And("issue.is_pull=?", opts.IsPull)
  687. switch opts.SortType {
  688. case "oldest":
  689. sess.Asc("issue.created_unix")
  690. case "recentupdate":
  691. sess.Desc("issue.updated_unix")
  692. case "leastupdate":
  693. sess.Asc("issue.updated_unix")
  694. case "mostcomment":
  695. sess.Desc("issue.num_comments")
  696. case "leastcomment":
  697. sess.Asc("issue.num_comments")
  698. case "priority":
  699. sess.Desc("issue.priority")
  700. default:
  701. sess.Desc("issue.created_unix")
  702. }
  703. if len(opts.Labels) > 0 && opts.Labels != "0" {
  704. labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
  705. if len(labelIDs) > 0 {
  706. sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").In("issue_label.label_id", labelIDs)
  707. }
  708. }
  709. if opts.IsMention {
  710. sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").And("issue_user.is_mentioned = ?", true)
  711. if opts.UserID > 0 {
  712. sess.And("issue_user.uid = ?", opts.UserID)
  713. }
  714. }
  715. issues := make([]*Issue, 0, setting.UI.IssuePagingNum)
  716. return issues, sess.Find(&issues)
  717. }
  718. type IssueStatus int
  719. const (
  720. IS_OPEN = iota + 1
  721. IS_CLOSE
  722. )
  723. // GetIssueCountByPoster returns number of issues of repository by poster.
  724. func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
  725. count, _ := x.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
  726. return count
  727. }
  728. // .___ ____ ___
  729. // | | ______ ________ __ ____ | | \______ ___________
  730. // | |/ ___// ___/ | \_/ __ \| | / ___// __ \_ __ \
  731. // | |\___ \ \___ \| | /\ ___/| | /\___ \\ ___/| | \/
  732. // |___/____ >____ >____/ \___ >______//____ >\___ >__|
  733. // \/ \/ \/ \/ \/
  734. // IssueUser represents an issue-user relation.
  735. type IssueUser struct {
  736. ID int64 `xorm:"pk autoincr"`
  737. UID int64 `xorm:"INDEX"` // User ID.
  738. IssueID int64
  739. RepoID int64 `xorm:"INDEX"`
  740. MilestoneID int64
  741. IsRead bool
  742. IsAssigned bool
  743. IsMentioned bool
  744. IsPoster bool
  745. IsClosed bool
  746. }
  747. func newIssueUsers(e *xorm.Session, repo *Repository, issue *Issue) error {
  748. users, err := repo.GetAssignees()
  749. if err != nil {
  750. return err
  751. }
  752. iu := &IssueUser{
  753. IssueID: issue.ID,
  754. RepoID: repo.ID,
  755. }
  756. // Poster can be anyone.
  757. isNeedAddPoster := true
  758. for _, u := range users {
  759. iu.ID = 0
  760. iu.UID = u.ID
  761. iu.IsPoster = iu.UID == issue.PosterID
  762. if isNeedAddPoster && iu.IsPoster {
  763. isNeedAddPoster = false
  764. }
  765. iu.IsAssigned = iu.UID == issue.AssigneeID
  766. if _, err = e.Insert(iu); err != nil {
  767. return err
  768. }
  769. }
  770. if isNeedAddPoster {
  771. iu.ID = 0
  772. iu.UID = issue.PosterID
  773. iu.IsPoster = true
  774. if _, err = e.Insert(iu); err != nil {
  775. return err
  776. }
  777. }
  778. return nil
  779. }
  780. // NewIssueUsers adds new issue-user relations for new issue of repository.
  781. func NewIssueUsers(repo *Repository, issue *Issue) (err error) {
  782. sess := x.NewSession()
  783. defer sessionRelease(sess)
  784. if err = sess.Begin(); err != nil {
  785. return err
  786. }
  787. if err = newIssueUsers(sess, repo, issue); err != nil {
  788. return err
  789. }
  790. return sess.Commit()
  791. }
  792. // PairsContains returns true when pairs list contains given issue.
  793. func PairsContains(ius []*IssueUser, issueId, uid int64) int {
  794. for i := range ius {
  795. if ius[i].IssueID == issueId &&
  796. ius[i].UID == uid {
  797. return i
  798. }
  799. }
  800. return -1
  801. }
  802. // GetIssueUsers returns issue-user pairs by given repository and user.
  803. func GetIssueUsers(rid, uid int64, isClosed bool) ([]*IssueUser, error) {
  804. ius := make([]*IssueUser, 0, 10)
  805. err := x.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoID: rid, UID: uid})
  806. return ius, err
  807. }
  808. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
  809. func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
  810. if len(rids) == 0 {
  811. return []*IssueUser{}, nil
  812. }
  813. ius := make([]*IssueUser, 0, 10)
  814. sess := x.Limit(20, (page-1)*20).Where("is_closed=?", isClosed).In("repo_id", rids)
  815. err := sess.Find(&ius)
  816. return ius, err
  817. }
  818. // GetIssueUserPairsByMode returns issue-user pairs by given repository and user.
  819. func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) {
  820. ius := make([]*IssueUser, 0, 10)
  821. sess := x.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed)
  822. if rid > 0 {
  823. sess.And("repo_id=?", rid)
  824. }
  825. switch filterMode {
  826. case FM_ASSIGN:
  827. sess.And("is_assigned=?", true)
  828. case FM_CREATE:
  829. sess.And("is_poster=?", true)
  830. default:
  831. return ius, nil
  832. }
  833. err := sess.Find(&ius)
  834. return ius, err
  835. }
  836. // UpdateIssueMentions extracts mentioned people from content and
  837. // updates issue-user relations for them.
  838. func UpdateIssueMentions(issueID int64, mentions []string) error {
  839. if len(mentions) == 0 {
  840. return nil
  841. }
  842. for i := range mentions {
  843. mentions[i] = strings.ToLower(mentions[i])
  844. }
  845. users := make([]*User, 0, len(mentions))
  846. if err := x.In("lower_name", mentions).Asc("lower_name").Find(&users); err != nil {
  847. return fmt.Errorf("find mentioned users: %v", err)
  848. }
  849. ids := make([]int64, 0, len(mentions))
  850. for _, user := range users {
  851. ids = append(ids, user.ID)
  852. if !user.IsOrganization() || user.NumMembers == 0 {
  853. continue
  854. }
  855. memberIDs := make([]int64, 0, user.NumMembers)
  856. orgUsers, err := GetOrgUsersByOrgID(user.ID)
  857. if err != nil {
  858. return fmt.Errorf("GetOrgUsersByOrgID [%d]: %v", user.ID, err)
  859. }
  860. for _, orgUser := range orgUsers {
  861. memberIDs = append(memberIDs, orgUser.ID)
  862. }
  863. ids = append(ids, memberIDs...)
  864. }
  865. if err := UpdateIssueUsersByMentions(issueID, ids); err != nil {
  866. return fmt.Errorf("UpdateIssueUsersByMentions: %v", err)
  867. }
  868. return nil
  869. }
  870. // IssueStats represents issue statistic information.
  871. type IssueStats struct {
  872. OpenCount, ClosedCount int64
  873. AllCount int64
  874. AssignCount int64
  875. CreateCount int64
  876. MentionCount int64
  877. }
  878. // Filter modes.
  879. const (
  880. FM_ALL = iota
  881. FM_ASSIGN
  882. FM_CREATE
  883. FM_MENTION
  884. )
  885. func parseCountResult(results []map[string][]byte) int64 {
  886. if len(results) == 0 {
  887. return 0
  888. }
  889. for _, result := range results[0] {
  890. return com.StrTo(string(result)).MustInt64()
  891. }
  892. return 0
  893. }
  894. type IssueStatsOptions struct {
  895. RepoID int64
  896. UserID int64
  897. Labels string
  898. MilestoneID int64
  899. AssigneeID int64
  900. FilterMode int
  901. IsPull bool
  902. }
  903. // GetIssueStats returns issue statistic information by given conditions.
  904. func GetIssueStats(opts *IssueStatsOptions) *IssueStats {
  905. stats := &IssueStats{}
  906. countSession := func(opts *IssueStatsOptions) *xorm.Session {
  907. sess := x.Where("issue.repo_id = ?", opts.RepoID).And("is_pull = ?", opts.IsPull)
  908. if len(opts.Labels) > 0 && opts.Labels != "0" {
  909. labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
  910. if len(labelIDs) > 0 {
  911. sess.Join("INNER", "issue_label", "issue.id = issue_id").In("label_id", labelIDs)
  912. }
  913. }
  914. if opts.MilestoneID > 0 {
  915. sess.And("issue.milestone_id = ?", opts.MilestoneID)
  916. }
  917. if opts.AssigneeID > 0 {
  918. sess.And("assignee_id = ?", opts.AssigneeID)
  919. }
  920. return sess
  921. }
  922. switch opts.FilterMode {
  923. case FM_ALL, FM_ASSIGN:
  924. stats.OpenCount, _ = countSession(opts).
  925. And("is_closed = ?", false).
  926. Count(&Issue{})
  927. stats.ClosedCount, _ = countSession(opts).
  928. And("is_closed = ?", true).
  929. Count(&Issue{})
  930. case FM_CREATE:
  931. stats.OpenCount, _ = countSession(opts).
  932. And("poster_id = ?", opts.UserID).
  933. And("is_closed = ?", false).
  934. Count(&Issue{})
  935. stats.ClosedCount, _ = countSession(opts).
  936. And("poster_id = ?", opts.UserID).
  937. And("is_closed = ?", true).
  938. Count(&Issue{})
  939. case FM_MENTION:
  940. stats.OpenCount, _ = countSession(opts).
  941. Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
  942. And("issue_user.uid = ?", opts.UserID).
  943. And("issue_user.is_mentioned = ?", true).
  944. And("issue.is_closed = ?", false).
  945. Count(&Issue{})
  946. stats.ClosedCount, _ = countSession(opts).
  947. Join("INNER", "issue_user", "issue.id = issue_user.issue_id").
  948. And("issue_user.uid = ?", opts.UserID).
  949. And("issue_user.is_mentioned = ?", true).
  950. And("issue.is_closed = ?", true).
  951. Count(&Issue{})
  952. }
  953. return stats
  954. }
  955. // GetUserIssueStats returns issue statistic information for dashboard by given conditions.
  956. func GetUserIssueStats(repoID, uid int64, repoIDs []int64, filterMode int, isPull bool) *IssueStats {
  957. stats := &IssueStats{}
  958. countSession := func(isClosed, isPull bool, repoID int64, repoIDs []int64) *xorm.Session {
  959. sess := x.Where("issue.is_closed = ?", isClosed).And("issue.is_pull = ?", isPull)
  960. if repoID > 0 || len(repoIDs) == 0 {
  961. sess.And("repo_id = ?", repoID)
  962. } else {
  963. sess.In("repo_id", repoIDs)
  964. }
  965. return sess
  966. }
  967. stats.AssignCount, _ = countSession(false, isPull, repoID, repoIDs).
  968. And("assignee_id = ?", uid).
  969. Count(&Issue{})
  970. stats.CreateCount, _ = countSession(false, isPull, repoID, repoIDs).
  971. And("poster_id = ?", uid).
  972. Count(&Issue{})
  973. openCountSession := countSession(false, isPull, repoID, repoIDs)
  974. closedCountSession := countSession(true, isPull, repoID, repoIDs)
  975. switch filterMode {
  976. case FM_ASSIGN:
  977. openCountSession.And("assignee_id = ?", uid)
  978. closedCountSession.And("assignee_id = ?", uid)
  979. case FM_CREATE:
  980. openCountSession.And("poster_id = ?", uid)
  981. closedCountSession.And("poster_id = ?", uid)
  982. }
  983. stats.OpenCount, _ = openCountSession.Count(&Issue{})
  984. stats.ClosedCount, _ = closedCountSession.Count(&Issue{})
  985. return stats
  986. }
  987. // GetRepoIssueStats returns number of open and closed repository issues by given filter mode.
  988. func GetRepoIssueStats(repoID, uid int64, filterMode int, isPull bool) (numOpen int64, numClosed int64) {
  989. countSession := func(isClosed, isPull bool, repoID int64) *xorm.Session {
  990. sess := x.Where("issue.repo_id = ?", isClosed).
  991. And("is_pull = ?", isPull).
  992. And("repo_id = ?", repoID)
  993. return sess
  994. }
  995. openCountSession := countSession(false, isPull, repoID)
  996. closedCountSession := countSession(true, isPull, repoID)
  997. switch filterMode {
  998. case FM_ASSIGN:
  999. openCountSession.And("assignee_id = ?", uid)
  1000. closedCountSession.And("assignee_id = ?", uid)
  1001. case FM_CREATE:
  1002. openCountSession.And("poster_id = ?", uid)
  1003. closedCountSession.And("poster_id = ?", uid)
  1004. }
  1005. openResult, _ := openCountSession.Count(&Issue{})
  1006. closedResult, _ := closedCountSession.Count(&Issue{})
  1007. return openResult, closedResult
  1008. }
  1009. func updateIssue(e Engine, issue *Issue) error {
  1010. _, err := e.Id(issue.ID).AllCols().Update(issue)
  1011. return err
  1012. }
  1013. // UpdateIssue updates all fields of given issue.
  1014. func UpdateIssue(issue *Issue) error {
  1015. return updateIssue(x, issue)
  1016. }
  1017. func updateIssueUsersByStatus(e Engine, issueID int64, isClosed bool) error {
  1018. _, err := e.Exec("UPDATE `issue_user` SET is_closed=? WHERE issue_id=?", isClosed, issueID)
  1019. return err
  1020. }
  1021. // UpdateIssueUsersByStatus updates issue-user relations by issue status.
  1022. func UpdateIssueUsersByStatus(issueID int64, isClosed bool) error {
  1023. return updateIssueUsersByStatus(x, issueID, isClosed)
  1024. }
  1025. func updateIssueUserByAssignee(e *xorm.Session, issue *Issue) (err error) {
  1026. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?", false, issue.ID); err != nil {
  1027. return err
  1028. }
  1029. // Assignee ID equals to 0 means clear assignee.
  1030. if issue.AssigneeID > 0 {
  1031. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned = ? WHERE uid = ? AND issue_id = ?", true, issue.AssigneeID, issue.ID); err != nil {
  1032. return err
  1033. }
  1034. }
  1035. return updateIssue(e, issue)
  1036. }
  1037. // UpdateIssueUserByAssignee updates issue-user relation for assignee.
  1038. func UpdateIssueUserByAssignee(issue *Issue) (err error) {
  1039. sess := x.NewSession()
  1040. defer sessionRelease(sess)
  1041. if err = sess.Begin(); err != nil {
  1042. return err
  1043. }
  1044. if err = updateIssueUserByAssignee(sess, issue); err != nil {
  1045. return err
  1046. }
  1047. return sess.Commit()
  1048. }
  1049. // UpdateIssueUserByRead updates issue-user relation for reading.
  1050. func UpdateIssueUserByRead(uid, issueID int64) error {
  1051. _, err := x.Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
  1052. return err
  1053. }
  1054. // UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
  1055. func UpdateIssueUsersByMentions(issueID int64, uids []int64) error {
  1056. for _, uid := range uids {
  1057. iu := &IssueUser{
  1058. UID: uid,
  1059. IssueID: issueID,
  1060. }
  1061. has, err := x.Get(iu)
  1062. if err != nil {
  1063. return err
  1064. }
  1065. iu.IsMentioned = true
  1066. if has {
  1067. _, err = x.Id(iu.ID).AllCols().Update(iu)
  1068. } else {
  1069. _, err = x.Insert(iu)
  1070. }
  1071. if err != nil {
  1072. return err
  1073. }
  1074. }
  1075. return nil
  1076. }
  1077. // _____ .__.__ __
  1078. // / \ |__| | ____ _______/ |_ ____ ____ ____
  1079. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  1080. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  1081. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  1082. // \/ \/ \/ \/ \/
  1083. // Milestone represents a milestone of repository.
  1084. type Milestone struct {
  1085. ID int64 `xorm:"pk autoincr"`
  1086. RepoID int64 `xorm:"INDEX"`
  1087. Name string
  1088. Content string `xorm:"TEXT"`
  1089. RenderedContent string `xorm:"-"`
  1090. IsClosed bool
  1091. NumIssues int
  1092. NumClosedIssues int
  1093. NumOpenIssues int `xorm:"-"`
  1094. Completeness int // Percentage(1-100).
  1095. IsOverDue bool `xorm:"-"`
  1096. DeadlineString string `xorm:"-"`
  1097. Deadline time.Time `xorm:"-"`
  1098. DeadlineUnix int64
  1099. ClosedDate time.Time `xorm:"-"`
  1100. ClosedDateUnix int64
  1101. }
  1102. func (m *Milestone) BeforeInsert() {
  1103. m.DeadlineUnix = m.Deadline.Unix()
  1104. }
  1105. func (m *Milestone) BeforeUpdate() {
  1106. if m.NumIssues > 0 {
  1107. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  1108. } else {
  1109. m.Completeness = 0
  1110. }
  1111. m.DeadlineUnix = m.Deadline.Unix()
  1112. m.ClosedDateUnix = m.ClosedDate.Unix()
  1113. }
  1114. func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
  1115. switch colName {
  1116. case "num_closed_issues":
  1117. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  1118. case "deadline_unix":
  1119. m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
  1120. if m.Deadline.Year() == 9999 {
  1121. return
  1122. }
  1123. m.DeadlineString = m.Deadline.Format("2006-01-02")
  1124. if time.Now().Local().After(m.Deadline) {
  1125. m.IsOverDue = true
  1126. }
  1127. case "closed_date_unix":
  1128. m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
  1129. }
  1130. }
  1131. // State returns string representation of milestone status.
  1132. func (m *Milestone) State() api.StateType {
  1133. if m.IsClosed {
  1134. return api.STATE_CLOSED
  1135. }
  1136. return api.STATE_OPEN
  1137. }
  1138. func (m *Milestone) APIFormat() *api.Milestone {
  1139. apiMilestone := &api.Milestone{
  1140. ID: m.ID,
  1141. State: m.State(),
  1142. Title: m.Name,
  1143. Description: m.Content,
  1144. OpenIssues: m.NumOpenIssues,
  1145. ClosedIssues: m.NumClosedIssues,
  1146. }
  1147. if m.IsClosed {
  1148. apiMilestone.Closed = &m.ClosedDate
  1149. }
  1150. if m.Deadline.Year() < 9999 {
  1151. apiMilestone.Deadline = &m.Deadline
  1152. }
  1153. return apiMilestone
  1154. }
  1155. // NewMilestone creates new milestone of repository.
  1156. func NewMilestone(m *Milestone) (err error) {
  1157. sess := x.NewSession()
  1158. defer sessionRelease(sess)
  1159. if err = sess.Begin(); err != nil {
  1160. return err
  1161. }
  1162. if _, err = sess.Insert(m); err != nil {
  1163. return err
  1164. }
  1165. if _, err = sess.Exec("UPDATE `repository` SET num_milestones=num_milestones+1 WHERE id=?", m.RepoID); err != nil {
  1166. return err
  1167. }
  1168. return sess.Commit()
  1169. }
  1170. func getMilestoneByID(e Engine, id int64) (*Milestone, error) {
  1171. m := &Milestone{ID: id}
  1172. has, err := e.Get(m)
  1173. if err != nil {
  1174. return nil, err
  1175. } else if !has {
  1176. return nil, ErrMilestoneNotExist{id, 0}
  1177. }
  1178. return m, nil
  1179. }
  1180. // GetMilestoneByID returns the milestone of given ID.
  1181. func GetMilestoneByID(id int64) (*Milestone, error) {
  1182. return getMilestoneByID(x, id)
  1183. }
  1184. // GetRepoMilestoneByID returns the milestone of given ID and repository.
  1185. func GetRepoMilestoneByID(repoID, milestoneID int64) (*Milestone, error) {
  1186. m := &Milestone{ID: milestoneID, RepoID: repoID}
  1187. has, err := x.Get(m)
  1188. if err != nil {
  1189. return nil, err
  1190. } else if !has {
  1191. return nil, ErrMilestoneNotExist{milestoneID, repoID}
  1192. }
  1193. return m, nil
  1194. }
  1195. // GetAllRepoMilestones returns all milestones of given repository.
  1196. func GetAllRepoMilestones(repoID int64) ([]*Milestone, error) {
  1197. miles := make([]*Milestone, 0, 10)
  1198. return miles, x.Where("repo_id=?", repoID).Find(&miles)
  1199. }
  1200. // GetMilestones returns a list of milestones of given repository and status.
  1201. func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) {
  1202. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  1203. sess := x.Where("repo_id=? AND is_closed=?", repoID, isClosed)
  1204. if page > 0 {
  1205. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  1206. }
  1207. return miles, sess.Find(&miles)
  1208. }
  1209. func updateMilestone(e Engine, m *Milestone) error {
  1210. _, err := e.Id(m.ID).AllCols().Update(m)
  1211. return err
  1212. }
  1213. // UpdateMilestone updates information of given milestone.
  1214. func UpdateMilestone(m *Milestone) error {
  1215. return updateMilestone(x, m)
  1216. }
  1217. func countRepoMilestones(e Engine, repoID int64) int64 {
  1218. count, _ := e.Where("repo_id=?", repoID).Count(new(Milestone))
  1219. return count
  1220. }
  1221. // CountRepoMilestones returns number of milestones in given repository.
  1222. func CountRepoMilestones(repoID int64) int64 {
  1223. return countRepoMilestones(x, repoID)
  1224. }
  1225. func countRepoClosedMilestones(e Engine, repoID int64) int64 {
  1226. closed, _ := e.Where("repo_id=? AND is_closed=?", repoID, true).Count(new(Milestone))
  1227. return closed
  1228. }
  1229. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  1230. func CountRepoClosedMilestones(repoID int64) int64 {
  1231. return countRepoClosedMilestones(x, repoID)
  1232. }
  1233. // MilestoneStats returns number of open and closed milestones of given repository.
  1234. func MilestoneStats(repoID int64) (open int64, closed int64) {
  1235. open, _ = x.Where("repo_id=? AND is_closed=?", repoID, false).Count(new(Milestone))
  1236. return open, CountRepoClosedMilestones(repoID)
  1237. }
  1238. // ChangeMilestoneStatus changes the milestone open/closed status.
  1239. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  1240. repo, err := GetRepositoryByID(m.RepoID)
  1241. if err != nil {
  1242. return err
  1243. }
  1244. sess := x.NewSession()
  1245. defer sessionRelease(sess)
  1246. if err = sess.Begin(); err != nil {
  1247. return err
  1248. }
  1249. m.IsClosed = isClosed
  1250. if err = updateMilestone(sess, m); err != nil {
  1251. return err
  1252. }
  1253. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  1254. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  1255. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  1256. return err
  1257. }
  1258. return sess.Commit()
  1259. }
  1260. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  1261. if issue.MilestoneID == 0 {
  1262. return nil
  1263. }
  1264. m, err := getMilestoneByID(e, issue.MilestoneID)
  1265. if err != nil {
  1266. return err
  1267. }
  1268. if issue.IsClosed {
  1269. m.NumOpenIssues--
  1270. m.NumClosedIssues++
  1271. } else {
  1272. m.NumOpenIssues++
  1273. m.NumClosedIssues--
  1274. }
  1275. return updateMilestone(e, m)
  1276. }
  1277. // ChangeMilestoneIssueStats updates the open/closed issues counter and progress
  1278. // for the milestone associated with the given issue.
  1279. func ChangeMilestoneIssueStats(issue *Issue) (err error) {
  1280. sess := x.NewSession()
  1281. defer sessionRelease(sess)
  1282. if err = sess.Begin(); err != nil {
  1283. return err
  1284. }
  1285. if err = changeMilestoneIssueStats(sess, issue); err != nil {
  1286. return err
  1287. }
  1288. return sess.Commit()
  1289. }
  1290. func changeMilestoneAssign(e *xorm.Session, oldMid int64, issue *Issue) error {
  1291. if oldMid > 0 {
  1292. m, err := getMilestoneByID(e, oldMid)
  1293. if err != nil {
  1294. return err
  1295. }
  1296. m.NumIssues--
  1297. if issue.IsClosed {
  1298. m.NumClosedIssues--
  1299. }
  1300. if err = updateMilestone(e, m); err != nil {
  1301. return err
  1302. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id=0 WHERE issue_id=?", issue.ID); err != nil {
  1303. return err
  1304. }
  1305. }
  1306. if issue.MilestoneID > 0 {
  1307. m, err := getMilestoneByID(e, issue.MilestoneID)
  1308. if err != nil {
  1309. return err
  1310. }
  1311. m.NumIssues++
  1312. if issue.IsClosed {
  1313. m.NumClosedIssues++
  1314. }
  1315. if m.NumIssues == 0 {
  1316. return ErrWrongIssueCounter
  1317. }
  1318. if err = updateMilestone(e, m); err != nil {
  1319. return err
  1320. } else if _, err = e.Exec("UPDATE `issue_user` SET milestone_id=? WHERE issue_id=?", m.ID, issue.ID); err != nil {
  1321. return err
  1322. }
  1323. }
  1324. return updateIssue(e, issue)
  1325. }
  1326. // ChangeMilestoneAssign changes assignment of milestone for issue.
  1327. func ChangeMilestoneAssign(oldMid int64, issue *Issue) (err error) {
  1328. sess := x.NewSession()
  1329. defer sess.Close()
  1330. if err = sess.Begin(); err != nil {
  1331. return err
  1332. }
  1333. if err = changeMilestoneAssign(sess, oldMid, issue); err != nil {
  1334. return err
  1335. }
  1336. return sess.Commit()
  1337. }
  1338. // DeleteMilestoneByID deletes a milestone by given ID.
  1339. func DeleteMilestoneByID(id int64) error {
  1340. m, err := GetMilestoneByID(id)
  1341. if err != nil {
  1342. if IsErrMilestoneNotExist(err) {
  1343. return nil
  1344. }
  1345. return err
  1346. }
  1347. repo, err := GetRepositoryByID(m.RepoID)
  1348. if err != nil {
  1349. return err
  1350. }
  1351. sess := x.NewSession()
  1352. defer sessionRelease(sess)
  1353. if err = sess.Begin(); err != nil {
  1354. return err
  1355. }
  1356. if _, err = sess.Id(m.ID).Delete(new(Milestone)); err != nil {
  1357. return err
  1358. }
  1359. repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
  1360. repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
  1361. if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil {
  1362. return err
  1363. }
  1364. if _, err = sess.Exec("UPDATE `issue` SET milestone_id=0 WHERE milestone_id=?", m.ID); err != nil {
  1365. return err
  1366. } else if _, err = sess.Exec("UPDATE `issue_user` SET milestone_id=0 WHERE milestone_id=?", m.ID); err != nil {
  1367. return err
  1368. }
  1369. return sess.Commit()
  1370. }
  1371. // Attachment represent a attachment of issue/comment/release.
  1372. type Attachment struct {
  1373. ID int64 `xorm:"pk autoincr"`
  1374. UUID string `xorm:"uuid UNIQUE"`
  1375. IssueID int64 `xorm:"INDEX"`
  1376. CommentID int64
  1377. ReleaseID int64 `xorm:"INDEX"`
  1378. Name string
  1379. Created time.Time `xorm:"-"`
  1380. CreatedUnix int64
  1381. }
  1382. func (a *Attachment) BeforeInsert() {
  1383. a.CreatedUnix = time.Now().Unix()
  1384. }
  1385. func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
  1386. switch colName {
  1387. case "created_unix":
  1388. a.Created = time.Unix(a.CreatedUnix, 0).Local()
  1389. }
  1390. }
  1391. // AttachmentLocalPath returns where attachment is stored in local file system based on given UUID.
  1392. func AttachmentLocalPath(uuid string) string {
  1393. return path.Join(setting.AttachmentPath, uuid[0:1], uuid[1:2], uuid)
  1394. }
  1395. // LocalPath returns where attachment is stored in local file system.
  1396. func (attach *Attachment) LocalPath() string {
  1397. return AttachmentLocalPath(attach.UUID)
  1398. }
  1399. // NewAttachment creates a new attachment object.
  1400. func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
  1401. attach := &Attachment{
  1402. UUID: gouuid.NewV4().String(),
  1403. Name: name,
  1404. }
  1405. if err = os.MkdirAll(path.Dir(attach.LocalPath()), os.ModePerm); err != nil {
  1406. return nil, fmt.Errorf("MkdirAll: %v", err)
  1407. }
  1408. fw, err := os.Create(attach.LocalPath())
  1409. if err != nil {
  1410. return nil, fmt.Errorf("Create: %v", err)
  1411. }
  1412. defer fw.Close()
  1413. if _, err = fw.Write(buf); err != nil {
  1414. return nil, fmt.Errorf("Write: %v", err)
  1415. } else if _, err = io.Copy(fw, file); err != nil {
  1416. return nil, fmt.Errorf("Copy: %v", err)
  1417. }
  1418. sess := x.NewSession()
  1419. defer sessionRelease(sess)
  1420. if err := sess.Begin(); err != nil {
  1421. return nil, err
  1422. }
  1423. if _, err := sess.Insert(attach); err != nil {
  1424. return nil, err
  1425. }
  1426. return attach, sess.Commit()
  1427. }
  1428. func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
  1429. attach := &Attachment{UUID: uuid}
  1430. has, err := x.Get(attach)
  1431. if err != nil {
  1432. return nil, err
  1433. } else if !has {
  1434. return nil, ErrAttachmentNotExist{0, uuid}
  1435. }
  1436. return attach, nil
  1437. }
  1438. // GetAttachmentByUUID returns attachment by given UUID.
  1439. func GetAttachmentByUUID(uuid string) (*Attachment, error) {
  1440. return getAttachmentByUUID(x, uuid)
  1441. }
  1442. // GetAttachmentsByIssueID returns all attachments for given issue by ID.
  1443. func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {
  1444. attachments := make([]*Attachment, 0, 10)
  1445. return attachments, x.Where("issue_id=? AND comment_id=0", issueID).Find(&attachments)
  1446. }
  1447. // GetAttachmentsByCommentID returns all attachments if comment by given ID.
  1448. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
  1449. attachments := make([]*Attachment, 0, 10)
  1450. return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
  1451. }
  1452. // DeleteAttachment deletes the given attachment and optionally the associated file.
  1453. func DeleteAttachment(a *Attachment, remove bool) error {
  1454. _, err := DeleteAttachments([]*Attachment{a}, remove)
  1455. return err
  1456. }
  1457. // DeleteAttachments deletes the given attachments and optionally the associated files.
  1458. func DeleteAttachments(attachments []*Attachment, remove bool) (int, error) {
  1459. for i, a := range attachments {
  1460. if remove {
  1461. if err := os.Remove(a.LocalPath()); err != nil {
  1462. return i, err
  1463. }
  1464. }
  1465. if _, err := x.Delete(a); err != nil {
  1466. return i, err
  1467. }
  1468. }
  1469. return len(attachments), nil
  1470. }
  1471. // DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
  1472. func DeleteAttachmentsByIssue(issueId int64, remove bool) (int, error) {
  1473. attachments, err := GetAttachmentsByIssueID(issueId)
  1474. if err != nil {
  1475. return 0, err
  1476. }
  1477. return DeleteAttachments(attachments, remove)
  1478. }
  1479. // DeleteAttachmentsByComment deletes all attachments associated with the given comment.
  1480. func DeleteAttachmentsByComment(commentId int64, remove bool) (int, error) {
  1481. attachments, err := GetAttachmentsByCommentID(commentId)
  1482. if err != nil {
  1483. return 0, err
  1484. }
  1485. return DeleteAttachments(attachments, remove)
  1486. }