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

issue.go 31 kB

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