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

issue.go 31 kB

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