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