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