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

issue.go 30 kB

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