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 30 kB

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