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

issue.go 30 kB

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