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

issue.go 34 kB

11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 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
8 years ago
9 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
8 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 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
8 years ago
9 years ago
9 years ago
8 years ago
8 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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 repo
  5. import (
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. "time"
  14. "github.com/Unknwon/com"
  15. "github.com/Unknwon/paginater"
  16. "code.gitea.io/git"
  17. "code.gitea.io/gitea/models"
  18. "code.gitea.io/gitea/modules/auth"
  19. "code.gitea.io/gitea/modules/base"
  20. "code.gitea.io/gitea/modules/context"
  21. "code.gitea.io/gitea/modules/log"
  22. "code.gitea.io/gitea/modules/markdown"
  23. "code.gitea.io/gitea/modules/notification"
  24. "code.gitea.io/gitea/modules/setting"
  25. )
  26. const (
  27. tplIssues base.TplName = "repo/issue/list"
  28. tplIssueNew base.TplName = "repo/issue/new"
  29. tplIssueView base.TplName = "repo/issue/view"
  30. tplLabels base.TplName = "repo/issue/labels"
  31. tplMilestone base.TplName = "repo/issue/milestones"
  32. tplMilestoneNew base.TplName = "repo/issue/milestone_new"
  33. tplMilestoneEdit base.TplName = "repo/issue/milestone_edit"
  34. issueTemplateKey = "IssueTemplate"
  35. )
  36. var (
  37. // ErrFileTypeForbidden not allowed file type error
  38. ErrFileTypeForbidden = errors.New("File type is not allowed")
  39. // ErrTooManyFiles upload too many files
  40. ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
  41. // IssueTemplateCandidates issue templates
  42. IssueTemplateCandidates = []string{
  43. "ISSUE_TEMPLATE.md",
  44. "issue_template.md",
  45. ".gitea/ISSUE_TEMPLATE.md",
  46. ".gitea/issue_template.md",
  47. ".github/ISSUE_TEMPLATE.md",
  48. ".github/issue_template.md",
  49. }
  50. )
  51. // MustEnableIssues check if repository enable internal issues
  52. func MustEnableIssues(ctx *context.Context) {
  53. if !ctx.Repo.Repository.EnableIssues {
  54. ctx.Handle(404, "MustEnableIssues", nil)
  55. return
  56. }
  57. if ctx.Repo.Repository.EnableExternalTracker {
  58. ctx.Redirect(ctx.Repo.Repository.ExternalTrackerURL)
  59. return
  60. }
  61. }
  62. // MustAllowPulls check if repository enable pull requests
  63. func MustAllowPulls(ctx *context.Context) {
  64. if !ctx.Repo.Repository.AllowsPulls() {
  65. ctx.Handle(404, "MustAllowPulls", nil)
  66. return
  67. }
  68. // User can send pull request if owns a forked repository.
  69. if ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID) {
  70. ctx.Repo.PullRequest.Allowed = true
  71. ctx.Repo.PullRequest.HeadInfo = ctx.User.Name + ":" + ctx.Repo.BranchName
  72. }
  73. }
  74. // RetrieveLabels find all the labels of a repository
  75. func RetrieveLabels(ctx *context.Context) {
  76. labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort"))
  77. if err != nil {
  78. ctx.Handle(500, "RetrieveLabels.GetLabels", err)
  79. return
  80. }
  81. for _, l := range labels {
  82. l.CalOpenIssues()
  83. }
  84. ctx.Data["Labels"] = labels
  85. ctx.Data["NumLabels"] = len(labels)
  86. ctx.Data["SortType"] = ctx.Query("sort")
  87. }
  88. // Issues render issues page
  89. func Issues(ctx *context.Context) {
  90. isPullList := ctx.Params(":type") == "pulls"
  91. if isPullList {
  92. MustAllowPulls(ctx)
  93. if ctx.Written() {
  94. return
  95. }
  96. ctx.Data["Title"] = ctx.Tr("repo.pulls")
  97. ctx.Data["PageIsPullList"] = true
  98. } else {
  99. MustEnableIssues(ctx)
  100. if ctx.Written() {
  101. return
  102. }
  103. ctx.Data["Title"] = ctx.Tr("repo.issues")
  104. ctx.Data["PageIsIssueList"] = true
  105. }
  106. viewType := ctx.Query("type")
  107. sortType := ctx.Query("sort")
  108. types := []string{"assigned", "created_by", "mentioned"}
  109. if !com.IsSliceContainsStr(types, viewType) {
  110. viewType = "all"
  111. }
  112. // Must sign in to see issues about you.
  113. if viewType != "all" && !ctx.IsSigned {
  114. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(setting.AppSubURL+ctx.Req.RequestURI), 0, setting.AppSubURL)
  115. ctx.Redirect(setting.AppSubURL + "/user/login")
  116. return
  117. }
  118. var (
  119. assigneeID = ctx.QueryInt64("assignee")
  120. posterID int64
  121. mentionedID int64
  122. forceEmpty bool
  123. )
  124. switch viewType {
  125. case "assigned":
  126. if assigneeID > 0 && ctx.User.ID != assigneeID {
  127. // two different assignees, must be empty
  128. forceEmpty = true
  129. } else {
  130. assigneeID = ctx.User.ID
  131. }
  132. case "created_by":
  133. posterID = ctx.User.ID
  134. case "mentioned":
  135. mentionedID = ctx.User.ID
  136. }
  137. repo := ctx.Repo.Repository
  138. selectLabels := ctx.Query("labels")
  139. milestoneID := ctx.QueryInt64("milestone")
  140. isShowClosed := ctx.Query("state") == "closed"
  141. var issueStats *models.IssueStats
  142. if forceEmpty {
  143. issueStats = &models.IssueStats{}
  144. } else {
  145. issueStats = models.GetIssueStats(&models.IssueStatsOptions{
  146. RepoID: repo.ID,
  147. Labels: selectLabels,
  148. MilestoneID: milestoneID,
  149. AssigneeID: assigneeID,
  150. MentionedID: mentionedID,
  151. IsPull: isPullList,
  152. })
  153. }
  154. page := ctx.QueryInt("page")
  155. if page <= 1 {
  156. page = 1
  157. }
  158. var total int
  159. if !isShowClosed {
  160. total = int(issueStats.OpenCount)
  161. } else {
  162. total = int(issueStats.ClosedCount)
  163. }
  164. pager := paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  165. ctx.Data["Page"] = pager
  166. var issues []*models.Issue
  167. if forceEmpty {
  168. issues = []*models.Issue{}
  169. } else {
  170. var err error
  171. issues, err = models.Issues(&models.IssuesOptions{
  172. AssigneeID: assigneeID,
  173. RepoID: repo.ID,
  174. PosterID: posterID,
  175. MentionedID: mentionedID,
  176. MilestoneID: milestoneID,
  177. Page: pager.Current(),
  178. IsClosed: isShowClosed,
  179. IsPull: isPullList,
  180. Labels: selectLabels,
  181. SortType: sortType,
  182. })
  183. if err != nil {
  184. ctx.Handle(500, "Issues", err)
  185. return
  186. }
  187. }
  188. // Get issue-user relations.
  189. pairs, err := models.GetIssueUsers(repo.ID, posterID, isShowClosed)
  190. if err != nil {
  191. ctx.Handle(500, "GetIssueUsers", err)
  192. return
  193. }
  194. // Get posters.
  195. for i := range issues {
  196. if !ctx.IsSigned {
  197. issues[i].IsRead = true
  198. continue
  199. }
  200. // Check read status.
  201. idx := models.PairsContains(pairs, issues[i].ID, ctx.User.ID)
  202. if idx > -1 {
  203. issues[i].IsRead = pairs[idx].IsRead
  204. } else {
  205. issues[i].IsRead = true
  206. }
  207. }
  208. ctx.Data["Issues"] = issues
  209. // Get milestones.
  210. ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(repo.ID)
  211. if err != nil {
  212. ctx.Handle(500, "GetAllRepoMilestones", err)
  213. return
  214. }
  215. // Get assignees.
  216. ctx.Data["Assignees"], err = repo.GetAssignees()
  217. if err != nil {
  218. ctx.Handle(500, "GetAssignees", err)
  219. return
  220. }
  221. if ctx.QueryInt64("assignee") == 0 {
  222. assigneeID = 0 // Reset ID to prevent unexpected selection of assignee.
  223. }
  224. ctx.Data["IssueStats"] = issueStats
  225. ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
  226. ctx.Data["ViewType"] = viewType
  227. ctx.Data["SortType"] = sortType
  228. ctx.Data["MilestoneID"] = milestoneID
  229. ctx.Data["AssigneeID"] = assigneeID
  230. ctx.Data["IsShowClosed"] = isShowClosed
  231. if isShowClosed {
  232. ctx.Data["State"] = "closed"
  233. } else {
  234. ctx.Data["State"] = "open"
  235. }
  236. ctx.HTML(200, tplIssues)
  237. }
  238. func renderAttachmentSettings(ctx *context.Context) {
  239. ctx.Data["RequireDropzone"] = true
  240. ctx.Data["IsAttachmentEnabled"] = setting.AttachmentEnabled
  241. ctx.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes
  242. ctx.Data["AttachmentMaxSize"] = setting.AttachmentMaxSize
  243. ctx.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles
  244. }
  245. // RetrieveRepoMilestonesAndAssignees find all the milestones and assignees of a repository
  246. func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repository) {
  247. var err error
  248. ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false, "")
  249. if err != nil {
  250. ctx.Handle(500, "GetMilestones", err)
  251. return
  252. }
  253. ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true, "")
  254. if err != nil {
  255. ctx.Handle(500, "GetMilestones", err)
  256. return
  257. }
  258. ctx.Data["Assignees"], err = repo.GetAssignees()
  259. if err != nil {
  260. ctx.Handle(500, "GetAssignees", err)
  261. return
  262. }
  263. }
  264. // RetrieveRepoMetas find all the meta information of a repository
  265. func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.Label {
  266. if !ctx.Repo.IsWriter() {
  267. return nil
  268. }
  269. labels, err := models.GetLabelsByRepoID(repo.ID, "")
  270. if err != nil {
  271. ctx.Handle(500, "GetLabelsByRepoID", err)
  272. return nil
  273. }
  274. ctx.Data["Labels"] = labels
  275. RetrieveRepoMilestonesAndAssignees(ctx, repo)
  276. if ctx.Written() {
  277. return nil
  278. }
  279. return labels
  280. }
  281. func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (string, bool) {
  282. var r io.Reader
  283. var bytes []byte
  284. if ctx.Repo.Commit == nil {
  285. var err error
  286. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
  287. if err != nil {
  288. return "", false
  289. }
  290. }
  291. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(filename)
  292. if err != nil {
  293. return "", false
  294. }
  295. r, err = entry.Blob().Data()
  296. if err != nil {
  297. return "", false
  298. }
  299. bytes, err = ioutil.ReadAll(r)
  300. if err != nil {
  301. return "", false
  302. }
  303. return string(bytes), true
  304. }
  305. func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) {
  306. for _, filename := range possibleFiles {
  307. content, found := getFileContentFromDefaultBranch(ctx, filename)
  308. if found {
  309. ctx.Data[ctxDataKey] = content
  310. return
  311. }
  312. }
  313. }
  314. // NewIssue render createing issue page
  315. func NewIssue(ctx *context.Context) {
  316. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  317. ctx.Data["PageIsIssueList"] = true
  318. ctx.Data["RequireHighlightJS"] = true
  319. ctx.Data["RequireSimpleMDE"] = true
  320. setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
  321. renderAttachmentSettings(ctx)
  322. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  323. if ctx.Written() {
  324. return
  325. }
  326. ctx.HTML(200, tplIssueNew)
  327. }
  328. // ValidateRepoMetas check and returns repository's meta informations
  329. func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm) ([]int64, int64, int64) {
  330. var (
  331. repo = ctx.Repo.Repository
  332. err error
  333. )
  334. labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  335. if ctx.Written() {
  336. return nil, 0, 0
  337. }
  338. if !ctx.Repo.IsWriter() {
  339. return nil, 0, 0
  340. }
  341. // Check labels.
  342. labelIDs, err := base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
  343. if err != nil {
  344. return nil, 0, 0
  345. }
  346. labelIDMark := base.Int64sToMap(labelIDs)
  347. hasSelected := false
  348. for i := range labels {
  349. if labelIDMark[labels[i].ID] {
  350. labels[i].IsChecked = true
  351. hasSelected = true
  352. }
  353. }
  354. ctx.Data["HasSelectedLabel"] = hasSelected
  355. ctx.Data["label_ids"] = form.LabelIDs
  356. ctx.Data["Labels"] = labels
  357. // Check milestone.
  358. milestoneID := form.MilestoneID
  359. if milestoneID > 0 {
  360. ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID)
  361. if err != nil {
  362. ctx.Handle(500, "GetMilestoneByID", err)
  363. return nil, 0, 0
  364. }
  365. ctx.Data["milestone_id"] = milestoneID
  366. }
  367. // Check assignee.
  368. assigneeID := form.AssigneeID
  369. if assigneeID > 0 {
  370. ctx.Data["Assignee"], err = repo.GetAssigneeByID(assigneeID)
  371. if err != nil {
  372. ctx.Handle(500, "GetAssigneeByID", err)
  373. return nil, 0, 0
  374. }
  375. ctx.Data["assignee_id"] = assigneeID
  376. }
  377. return labelIDs, milestoneID, assigneeID
  378. }
  379. // NewIssuePost response for creating new issue
  380. func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
  381. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  382. ctx.Data["PageIsIssueList"] = true
  383. ctx.Data["RequireHighlightJS"] = true
  384. ctx.Data["RequireSimpleMDE"] = true
  385. renderAttachmentSettings(ctx)
  386. var (
  387. repo = ctx.Repo.Repository
  388. attachments []string
  389. )
  390. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  391. if ctx.Written() {
  392. return
  393. }
  394. if setting.AttachmentEnabled {
  395. attachments = form.Files
  396. }
  397. if ctx.HasError() {
  398. ctx.HTML(200, tplIssueNew)
  399. return
  400. }
  401. issue := &models.Issue{
  402. RepoID: repo.ID,
  403. Title: form.Title,
  404. PosterID: ctx.User.ID,
  405. Poster: ctx.User,
  406. MilestoneID: milestoneID,
  407. AssigneeID: assigneeID,
  408. Content: form.Content,
  409. }
  410. if err := models.NewIssue(repo, issue, labelIDs, attachments); err != nil {
  411. ctx.Handle(500, "NewIssue", err)
  412. return
  413. }
  414. notification.Service.NotifyIssue(issue, ctx.User.ID)
  415. log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
  416. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  417. }
  418. // UploadAttachment response for uploading issue's attachment
  419. func UploadAttachment(ctx *context.Context) {
  420. if !setting.AttachmentEnabled {
  421. ctx.Error(404, "attachment is not enabled")
  422. return
  423. }
  424. file, header, err := ctx.Req.FormFile("file")
  425. if err != nil {
  426. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  427. return
  428. }
  429. defer file.Close()
  430. buf := make([]byte, 1024)
  431. n, _ := file.Read(buf)
  432. if n > 0 {
  433. buf = buf[:n]
  434. }
  435. fileType := http.DetectContentType(buf)
  436. allowedTypes := strings.Split(setting.AttachmentAllowedTypes, ",")
  437. allowed := false
  438. for _, t := range allowedTypes {
  439. t := strings.Trim(t, " ")
  440. if t == "*/*" || t == fileType {
  441. allowed = true
  442. break
  443. }
  444. }
  445. if !allowed {
  446. ctx.Error(400, ErrFileTypeForbidden.Error())
  447. return
  448. }
  449. attach, err := models.NewAttachment(header.Filename, buf, file)
  450. if err != nil {
  451. ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
  452. return
  453. }
  454. log.Trace("New attachment uploaded: %s", attach.UUID)
  455. ctx.JSON(200, map[string]string{
  456. "uuid": attach.UUID,
  457. })
  458. }
  459. // ViewIssue render issue view page
  460. func ViewIssue(ctx *context.Context) {
  461. ctx.Data["RequireHighlightJS"] = true
  462. ctx.Data["RequireDropzone"] = true
  463. renderAttachmentSettings(ctx)
  464. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  465. if err != nil {
  466. if models.IsErrIssueNotExist(err) {
  467. ctx.Handle(404, "GetIssueByIndex", err)
  468. } else {
  469. ctx.Handle(500, "GetIssueByIndex", err)
  470. }
  471. return
  472. }
  473. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  474. // Make sure type and URL matches.
  475. if ctx.Params(":type") == "issues" && issue.IsPull {
  476. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  477. return
  478. } else if ctx.Params(":type") == "pulls" && !issue.IsPull {
  479. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  480. return
  481. }
  482. if issue.IsPull {
  483. MustAllowPulls(ctx)
  484. if ctx.Written() {
  485. return
  486. }
  487. ctx.Data["PageIsPullList"] = true
  488. ctx.Data["PageIsPullConversation"] = true
  489. } else {
  490. MustEnableIssues(ctx)
  491. if ctx.Written() {
  492. return
  493. }
  494. ctx.Data["PageIsIssueList"] = true
  495. }
  496. issue.RenderedContent = string(markdown.Render([]byte(issue.Content), ctx.Repo.RepoLink,
  497. ctx.Repo.Repository.ComposeMetas()))
  498. repo := ctx.Repo.Repository
  499. // Get more information if it's a pull request.
  500. if issue.IsPull {
  501. if issue.PullRequest.HasMerged {
  502. ctx.Data["DisableStatusChange"] = issue.PullRequest.HasMerged
  503. PrepareMergedViewPullInfo(ctx, issue)
  504. } else {
  505. PrepareViewPullInfo(ctx, issue)
  506. }
  507. if ctx.Written() {
  508. return
  509. }
  510. }
  511. // Metas.
  512. // Check labels.
  513. labelIDMark := make(map[int64]bool)
  514. for i := range issue.Labels {
  515. labelIDMark[issue.Labels[i].ID] = true
  516. }
  517. labels, err := models.GetLabelsByRepoID(repo.ID, "")
  518. if err != nil {
  519. ctx.Handle(500, "GetLabelsByRepoID", err)
  520. return
  521. }
  522. hasSelected := false
  523. for i := range labels {
  524. if labelIDMark[labels[i].ID] {
  525. labels[i].IsChecked = true
  526. hasSelected = true
  527. }
  528. }
  529. ctx.Data["HasSelectedLabel"] = hasSelected
  530. ctx.Data["Labels"] = labels
  531. // Check milestone and assignee.
  532. if ctx.Repo.IsWriter() {
  533. RetrieveRepoMilestonesAndAssignees(ctx, repo)
  534. if ctx.Written() {
  535. return
  536. }
  537. }
  538. if ctx.IsSigned {
  539. // Update issue-user.
  540. if err = issue.ReadBy(ctx.User.ID); err != nil {
  541. ctx.Handle(500, "ReadBy", err)
  542. return
  543. }
  544. }
  545. var (
  546. tag models.CommentTag
  547. ok bool
  548. marked = make(map[int64]models.CommentTag)
  549. comment *models.Comment
  550. participants = make([]*models.User, 1, 10)
  551. )
  552. // Render comments and and fetch participants.
  553. participants[0] = issue.Poster
  554. for _, comment = range issue.Comments {
  555. if comment.Type == models.CommentTypeComment {
  556. comment.RenderedContent = string(markdown.Render([]byte(comment.Content), ctx.Repo.RepoLink,
  557. ctx.Repo.Repository.ComposeMetas()))
  558. // Check tag.
  559. tag, ok = marked[comment.PosterID]
  560. if ok {
  561. comment.ShowTag = tag
  562. continue
  563. }
  564. if repo.IsOwnedBy(comment.PosterID) ||
  565. (repo.Owner.IsOrganization() && repo.Owner.IsOwnedBy(comment.PosterID)) {
  566. comment.ShowTag = models.CommentTagOwner
  567. } else if comment.Poster.IsWriterOfRepo(repo) {
  568. comment.ShowTag = models.CommentTagWriter
  569. } else if comment.PosterID == issue.PosterID {
  570. comment.ShowTag = models.CommentTagPoster
  571. }
  572. marked[comment.PosterID] = comment.ShowTag
  573. isAdded := false
  574. for j := range participants {
  575. if comment.Poster == participants[j] {
  576. isAdded = true
  577. break
  578. }
  579. }
  580. if !isAdded && !issue.IsPoster(comment.Poster.ID) {
  581. participants = append(participants, comment.Poster)
  582. }
  583. }
  584. }
  585. if issue.IsPull {
  586. pull := issue.PullRequest
  587. canDelete := false
  588. if ctx.IsSigned && pull.HeadBranch != "master" {
  589. if err := pull.GetHeadRepo(); err != nil {
  590. log.Error(4, "GetHeadRepo: %v", err)
  591. } else if ctx.User.IsWriterOfRepo(pull.HeadRepo) {
  592. canDelete = true
  593. deleteBranchURL := pull.HeadRepo.Link() + "/branches/" + pull.HeadBranch + "/delete"
  594. ctx.Data["DeleteBranchLink"] = fmt.Sprintf("%s?commit=%s&redirect_to=%s", deleteBranchURL, pull.MergedCommitID, ctx.Data["Link"])
  595. }
  596. }
  597. ctx.Data["IsPullBranchDeletable"] = canDelete && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch)
  598. }
  599. ctx.Data["Participants"] = participants
  600. ctx.Data["NumParticipants"] = len(participants)
  601. ctx.Data["Issue"] = issue
  602. ctx.Data["IsIssueOwner"] = ctx.Repo.IsWriter() || (ctx.IsSigned && issue.IsPoster(ctx.User.ID))
  603. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login?redirect_to=" + ctx.Data["Link"].(string)
  604. ctx.HTML(200, tplIssueView)
  605. }
  606. func getActionIssue(ctx *context.Context) *models.Issue {
  607. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  608. if err != nil {
  609. if models.IsErrIssueNotExist(err) {
  610. ctx.Error(404, "GetIssueByIndex")
  611. } else {
  612. ctx.Handle(500, "GetIssueByIndex", err)
  613. }
  614. return nil
  615. }
  616. return issue
  617. }
  618. // UpdateIssueTitle change issue's title
  619. func UpdateIssueTitle(ctx *context.Context) {
  620. issue := getActionIssue(ctx)
  621. if ctx.Written() {
  622. return
  623. }
  624. if !ctx.IsSigned || (!issue.IsPoster(ctx.User.ID) && !ctx.Repo.IsWriter()) {
  625. ctx.Error(403)
  626. return
  627. }
  628. title := ctx.QueryTrim("title")
  629. if len(title) == 0 {
  630. ctx.Error(204)
  631. return
  632. }
  633. if err := issue.ChangeTitle(ctx.User, title); err != nil {
  634. ctx.Handle(500, "ChangeTitle", err)
  635. return
  636. }
  637. ctx.JSON(200, map[string]interface{}{
  638. "title": issue.Title,
  639. })
  640. }
  641. // UpdateIssueContent change issue's content
  642. func UpdateIssueContent(ctx *context.Context) {
  643. issue := getActionIssue(ctx)
  644. if ctx.Written() {
  645. return
  646. }
  647. if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.IsWriter()) {
  648. ctx.Error(403)
  649. return
  650. }
  651. content := ctx.Query("content")
  652. if err := issue.ChangeContent(ctx.User, content); err != nil {
  653. ctx.Handle(500, "ChangeContent", err)
  654. return
  655. }
  656. ctx.JSON(200, map[string]interface{}{
  657. "content": string(markdown.Render([]byte(issue.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
  658. })
  659. }
  660. // UpdateIssueLabel change issue's labels
  661. func UpdateIssueLabel(ctx *context.Context) {
  662. issue := getActionIssue(ctx)
  663. if ctx.Written() {
  664. return
  665. }
  666. if ctx.Query("action") == "clear" {
  667. if err := issue.ClearLabels(ctx.User); err != nil {
  668. ctx.Handle(500, "ClearLabels", err)
  669. return
  670. }
  671. } else {
  672. isAttach := ctx.Query("action") == "attach"
  673. label, err := models.GetLabelByID(ctx.QueryInt64("id"))
  674. if err != nil {
  675. if models.IsErrLabelNotExist(err) {
  676. ctx.Error(404, "GetLabelByID")
  677. } else {
  678. ctx.Handle(500, "GetLabelByID", err)
  679. }
  680. return
  681. }
  682. if isAttach && !issue.HasLabel(label.ID) {
  683. if err = issue.AddLabel(ctx.User, label); err != nil {
  684. ctx.Handle(500, "AddLabel", err)
  685. return
  686. }
  687. } else if !isAttach && issue.HasLabel(label.ID) {
  688. if err = issue.RemoveLabel(ctx.User, label); err != nil {
  689. ctx.Handle(500, "RemoveLabel", err)
  690. return
  691. }
  692. }
  693. }
  694. ctx.JSON(200, map[string]interface{}{
  695. "ok": true,
  696. })
  697. }
  698. // UpdateIssueMilestone change issue's milestone
  699. func UpdateIssueMilestone(ctx *context.Context) {
  700. issue := getActionIssue(ctx)
  701. if ctx.Written() {
  702. return
  703. }
  704. oldMilestoneID := issue.MilestoneID
  705. milestoneID := ctx.QueryInt64("id")
  706. if oldMilestoneID == milestoneID {
  707. ctx.JSON(200, map[string]interface{}{
  708. "ok": true,
  709. })
  710. return
  711. }
  712. // Not check for invalid milestone id and give responsibility to owners.
  713. issue.MilestoneID = milestoneID
  714. if err := models.ChangeMilestoneAssign(issue, oldMilestoneID); err != nil {
  715. ctx.Handle(500, "ChangeMilestoneAssign", err)
  716. return
  717. }
  718. ctx.JSON(200, map[string]interface{}{
  719. "ok": true,
  720. })
  721. }
  722. // UpdateIssueAssignee change issue's assignee
  723. func UpdateIssueAssignee(ctx *context.Context) {
  724. issue := getActionIssue(ctx)
  725. if ctx.Written() {
  726. return
  727. }
  728. assigneeID := ctx.QueryInt64("id")
  729. if issue.AssigneeID == assigneeID {
  730. ctx.JSON(200, map[string]interface{}{
  731. "ok": true,
  732. })
  733. return
  734. }
  735. if err := issue.ChangeAssignee(ctx.User, assigneeID); err != nil {
  736. ctx.Handle(500, "ChangeAssignee", err)
  737. return
  738. }
  739. ctx.JSON(200, map[string]interface{}{
  740. "ok": true,
  741. })
  742. }
  743. // NewComment create a comment for issue
  744. func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
  745. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  746. if err != nil {
  747. ctx.NotFoundOrServerError("GetIssueByIndex", models.IsErrIssueNotExist, err)
  748. return
  749. }
  750. var attachments []string
  751. if setting.AttachmentEnabled {
  752. attachments = form.Files
  753. }
  754. if ctx.HasError() {
  755. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  756. ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index))
  757. return
  758. }
  759. var comment *models.Comment
  760. defer func() {
  761. // Check if issue admin/poster changes the status of issue.
  762. if (ctx.Repo.IsWriter() || (ctx.IsSigned && issue.IsPoster(ctx.User.ID))) &&
  763. (form.Status == "reopen" || form.Status == "close") &&
  764. !(issue.IsPull && issue.PullRequest.HasMerged) {
  765. // Duplication and conflict check should apply to reopen pull request.
  766. var pr *models.PullRequest
  767. if form.Status == "reopen" && issue.IsPull {
  768. pull := issue.PullRequest
  769. pr, err = models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch)
  770. if err != nil {
  771. if !models.IsErrPullRequestNotExist(err) {
  772. ctx.Handle(500, "GetUnmergedPullRequest", err)
  773. return
  774. }
  775. }
  776. // Regenerate patch and test conflict.
  777. if pr == nil {
  778. if err = issue.PullRequest.UpdatePatch(); err != nil {
  779. ctx.Handle(500, "UpdatePatch", err)
  780. return
  781. }
  782. issue.PullRequest.AddToTaskQueue()
  783. }
  784. }
  785. if pr != nil {
  786. ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
  787. } else {
  788. if err = issue.ChangeStatus(ctx.User, ctx.Repo.Repository, form.Status == "close"); err != nil {
  789. log.Error(4, "ChangeStatus: %v", err)
  790. } else {
  791. log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
  792. }
  793. }
  794. }
  795. // Redirect to comment hashtag if there is any actual content.
  796. typeName := "issues"
  797. if issue.IsPull {
  798. typeName = "pulls"
  799. }
  800. if comment != nil {
  801. ctx.Redirect(fmt.Sprintf("%s/%s/%d#%s", ctx.Repo.RepoLink, typeName, issue.Index, comment.HashTag()))
  802. } else {
  803. ctx.Redirect(fmt.Sprintf("%s/%s/%d", ctx.Repo.RepoLink, typeName, issue.Index))
  804. }
  805. }()
  806. // Fix #321: Allow empty comments, as long as we have attachments.
  807. if len(form.Content) == 0 && len(attachments) == 0 {
  808. return
  809. }
  810. comment, err = models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
  811. if err != nil {
  812. ctx.Handle(500, "CreateIssueComment", err)
  813. return
  814. }
  815. notification.Service.NotifyIssue(issue, ctx.User.ID)
  816. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  817. }
  818. // UpdateCommentContent change comment of issue's content
  819. func UpdateCommentContent(ctx *context.Context) {
  820. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  821. if err != nil {
  822. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  823. return
  824. }
  825. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  826. ctx.Error(403)
  827. return
  828. } else if comment.Type != models.CommentTypeComment {
  829. ctx.Error(204)
  830. return
  831. }
  832. comment.Content = ctx.Query("content")
  833. if len(comment.Content) == 0 {
  834. ctx.JSON(200, map[string]interface{}{
  835. "content": "",
  836. })
  837. return
  838. }
  839. if err = models.UpdateComment(comment); err != nil {
  840. ctx.Handle(500, "UpdateComment", err)
  841. return
  842. }
  843. ctx.JSON(200, map[string]interface{}{
  844. "content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
  845. })
  846. }
  847. // DeleteComment delete comment of issue
  848. func DeleteComment(ctx *context.Context) {
  849. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  850. if err != nil {
  851. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  852. return
  853. }
  854. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  855. ctx.Error(403)
  856. return
  857. } else if comment.Type != models.CommentTypeComment {
  858. ctx.Error(204)
  859. return
  860. }
  861. if err = models.DeleteCommentByID(comment.ID); err != nil {
  862. ctx.Handle(500, "DeleteCommentByID", err)
  863. return
  864. }
  865. ctx.Status(200)
  866. }
  867. // Labels render issue's labels page
  868. func Labels(ctx *context.Context) {
  869. ctx.Data["Title"] = ctx.Tr("repo.labels")
  870. ctx.Data["PageIsIssueList"] = true
  871. ctx.Data["PageIsLabels"] = true
  872. ctx.Data["RequireMinicolors"] = true
  873. ctx.Data["LabelTemplates"] = models.LabelTemplates
  874. ctx.HTML(200, tplLabels)
  875. }
  876. // InitializeLabels init labels for a repository
  877. func InitializeLabels(ctx *context.Context, form auth.InitializeLabelsForm) {
  878. if ctx.HasError() {
  879. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  880. return
  881. }
  882. list, err := models.GetLabelTemplateFile(form.TemplateName)
  883. if err != nil {
  884. ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, err))
  885. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  886. return
  887. }
  888. labels := make([]*models.Label, len(list))
  889. for i := 0; i < len(list); i++ {
  890. labels[i] = &models.Label{
  891. RepoID: ctx.Repo.Repository.ID,
  892. Name: list[i][0],
  893. Color: list[i][1],
  894. }
  895. }
  896. if err := models.NewLabels(labels...); err != nil {
  897. ctx.Handle(500, "NewLabels", err)
  898. return
  899. }
  900. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  901. }
  902. // NewLabel create new label for repository
  903. func NewLabel(ctx *context.Context, form auth.CreateLabelForm) {
  904. ctx.Data["Title"] = ctx.Tr("repo.labels")
  905. ctx.Data["PageIsLabels"] = true
  906. if ctx.HasError() {
  907. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  908. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  909. return
  910. }
  911. l := &models.Label{
  912. RepoID: ctx.Repo.Repository.ID,
  913. Name: form.Title,
  914. Color: form.Color,
  915. }
  916. if err := models.NewLabels(l); err != nil {
  917. ctx.Handle(500, "NewLabel", err)
  918. return
  919. }
  920. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  921. }
  922. // UpdateLabel update a label's name and color
  923. func UpdateLabel(ctx *context.Context, form auth.CreateLabelForm) {
  924. l, err := models.GetLabelByID(form.ID)
  925. if err != nil {
  926. switch {
  927. case models.IsErrLabelNotExist(err):
  928. ctx.Error(404)
  929. default:
  930. ctx.Handle(500, "UpdateLabel", err)
  931. }
  932. return
  933. }
  934. l.Name = form.Title
  935. l.Color = form.Color
  936. if err := models.UpdateLabel(l); err != nil {
  937. ctx.Handle(500, "UpdateLabel", err)
  938. return
  939. }
  940. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  941. }
  942. // DeleteLabel delete a label
  943. func DeleteLabel(ctx *context.Context) {
  944. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  945. ctx.Flash.Error("DeleteLabel: " + err.Error())
  946. } else {
  947. ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
  948. }
  949. ctx.JSON(200, map[string]interface{}{
  950. "redirect": ctx.Repo.RepoLink + "/labels",
  951. })
  952. return
  953. }
  954. // Milestones render milestones page
  955. func Milestones(ctx *context.Context) {
  956. ctx.Data["Title"] = ctx.Tr("repo.milestones")
  957. ctx.Data["PageIsIssueList"] = true
  958. ctx.Data["PageIsMilestones"] = true
  959. isShowClosed := ctx.Query("state") == "closed"
  960. openCount, closedCount := models.MilestoneStats(ctx.Repo.Repository.ID)
  961. ctx.Data["OpenCount"] = openCount
  962. ctx.Data["ClosedCount"] = closedCount
  963. sortType := ctx.Query("sort")
  964. page := ctx.QueryInt("page")
  965. if page <= 1 {
  966. page = 1
  967. }
  968. var total int
  969. if !isShowClosed {
  970. total = int(openCount)
  971. } else {
  972. total = int(closedCount)
  973. }
  974. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  975. miles, err := models.GetMilestones(ctx.Repo.Repository.ID, page, isShowClosed, sortType)
  976. if err != nil {
  977. ctx.Handle(500, "GetMilestones", err)
  978. return
  979. }
  980. for _, m := range miles {
  981. m.RenderedContent = string(markdown.Render([]byte(m.Content), ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()))
  982. }
  983. ctx.Data["Milestones"] = miles
  984. if isShowClosed {
  985. ctx.Data["State"] = "closed"
  986. } else {
  987. ctx.Data["State"] = "open"
  988. }
  989. ctx.Data["SortType"] = sortType
  990. ctx.Data["IsShowClosed"] = isShowClosed
  991. ctx.HTML(200, tplMilestone)
  992. }
  993. // NewMilestone render creating milestone page
  994. func NewMilestone(ctx *context.Context) {
  995. ctx.Data["Title"] = ctx.Tr("repo.milestones.new")
  996. ctx.Data["PageIsIssueList"] = true
  997. ctx.Data["PageIsMilestones"] = true
  998. ctx.Data["RequireDatetimepicker"] = true
  999. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  1000. ctx.HTML(200, tplMilestoneNew)
  1001. }
  1002. // NewMilestonePost response for creating milestone
  1003. func NewMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) {
  1004. ctx.Data["Title"] = ctx.Tr("repo.milestones.new")
  1005. ctx.Data["PageIsIssueList"] = true
  1006. ctx.Data["PageIsMilestones"] = true
  1007. ctx.Data["RequireDatetimepicker"] = true
  1008. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  1009. if ctx.HasError() {
  1010. ctx.HTML(200, tplMilestoneNew)
  1011. return
  1012. }
  1013. if len(form.Deadline) == 0 {
  1014. form.Deadline = "9999-12-31"
  1015. }
  1016. deadline, err := time.ParseInLocation("2006-01-02", form.Deadline, time.Local)
  1017. if err != nil {
  1018. ctx.Data["Err_Deadline"] = true
  1019. ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), tplMilestoneNew, &form)
  1020. return
  1021. }
  1022. if err = models.NewMilestone(&models.Milestone{
  1023. RepoID: ctx.Repo.Repository.ID,
  1024. Name: form.Title,
  1025. Content: form.Content,
  1026. Deadline: deadline,
  1027. }); err != nil {
  1028. ctx.Handle(500, "NewMilestone", err)
  1029. return
  1030. }
  1031. ctx.Flash.Success(ctx.Tr("repo.milestones.create_success", form.Title))
  1032. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  1033. }
  1034. // EditMilestone render edting milestone page
  1035. func EditMilestone(ctx *context.Context) {
  1036. ctx.Data["Title"] = ctx.Tr("repo.milestones.edit")
  1037. ctx.Data["PageIsMilestones"] = true
  1038. ctx.Data["PageIsEditMilestone"] = true
  1039. ctx.Data["RequireDatetimepicker"] = true
  1040. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  1041. m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  1042. if err != nil {
  1043. if models.IsErrMilestoneNotExist(err) {
  1044. ctx.Handle(404, "", nil)
  1045. } else {
  1046. ctx.Handle(500, "GetMilestoneByRepoID", err)
  1047. }
  1048. return
  1049. }
  1050. ctx.Data["title"] = m.Name
  1051. ctx.Data["content"] = m.Content
  1052. if len(m.DeadlineString) > 0 {
  1053. ctx.Data["deadline"] = m.DeadlineString
  1054. }
  1055. ctx.HTML(200, tplMilestoneNew)
  1056. }
  1057. // EditMilestonePost response for edting milestone
  1058. func EditMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) {
  1059. ctx.Data["Title"] = ctx.Tr("repo.milestones.edit")
  1060. ctx.Data["PageIsMilestones"] = true
  1061. ctx.Data["PageIsEditMilestone"] = true
  1062. ctx.Data["RequireDatetimepicker"] = true
  1063. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  1064. if ctx.HasError() {
  1065. ctx.HTML(200, tplMilestoneNew)
  1066. return
  1067. }
  1068. if len(form.Deadline) == 0 {
  1069. form.Deadline = "9999-12-31"
  1070. }
  1071. deadline, err := time.ParseInLocation("2006-01-02", form.Deadline, time.Local)
  1072. if err != nil {
  1073. ctx.Data["Err_Deadline"] = true
  1074. ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), tplMilestoneNew, &form)
  1075. return
  1076. }
  1077. m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  1078. if err != nil {
  1079. if models.IsErrMilestoneNotExist(err) {
  1080. ctx.Handle(404, "", nil)
  1081. } else {
  1082. ctx.Handle(500, "GetMilestoneByRepoID", err)
  1083. }
  1084. return
  1085. }
  1086. m.Name = form.Title
  1087. m.Content = form.Content
  1088. m.Deadline = deadline
  1089. if err = models.UpdateMilestone(m); err != nil {
  1090. ctx.Handle(500, "UpdateMilestone", err)
  1091. return
  1092. }
  1093. ctx.Flash.Success(ctx.Tr("repo.milestones.edit_success", m.Name))
  1094. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  1095. }
  1096. // ChangeMilestonStatus response for change a milestone's status
  1097. func ChangeMilestonStatus(ctx *context.Context) {
  1098. m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  1099. if err != nil {
  1100. if models.IsErrMilestoneNotExist(err) {
  1101. ctx.Handle(404, "", err)
  1102. } else {
  1103. ctx.Handle(500, "GetMilestoneByRepoID", err)
  1104. }
  1105. return
  1106. }
  1107. switch ctx.Params(":action") {
  1108. case "open":
  1109. if m.IsClosed {
  1110. if err = models.ChangeMilestoneStatus(m, false); err != nil {
  1111. ctx.Handle(500, "ChangeMilestoneStatus", err)
  1112. return
  1113. }
  1114. }
  1115. ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=open")
  1116. case "close":
  1117. if !m.IsClosed {
  1118. m.ClosedDate = time.Now()
  1119. if err = models.ChangeMilestoneStatus(m, true); err != nil {
  1120. ctx.Handle(500, "ChangeMilestoneStatus", err)
  1121. return
  1122. }
  1123. }
  1124. ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=closed")
  1125. default:
  1126. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  1127. }
  1128. }
  1129. // DeleteMilestone delete a milestone
  1130. func DeleteMilestone(ctx *context.Context) {
  1131. if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  1132. ctx.Flash.Error("DeleteMilestoneByRepoID: " + err.Error())
  1133. } else {
  1134. ctx.Flash.Success(ctx.Tr("repo.milestones.deletion_success"))
  1135. }
  1136. ctx.JSON(200, map[string]interface{}{
  1137. "redirect": ctx.Repo.RepoLink + "/milestones",
  1138. })
  1139. }