issue.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. "fmt"
  7. "net/url"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. "github.com/go-martini/martini"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/mailer"
  16. "github.com/gogits/gogs/modules/middleware"
  17. )
  18. func Issues(ctx *middleware.Context) {
  19. ctx.Data["Title"] = "Issues"
  20. ctx.Data["IsRepoToolbarIssues"] = true
  21. ctx.Data["IsRepoToolbarIssuesList"] = true
  22. ctx.Data["ViewType"] = "all"
  23. milestoneId, _ := base.StrTo(ctx.Query("milestone")).Int()
  24. page, _ := base.StrTo(ctx.Query("page")).Int()
  25. ctx.Data["IssueCreatedCount"] = 0
  26. var posterId int64 = 0
  27. isCreatedBy := ctx.Query("type") == "created_by"
  28. if isCreatedBy {
  29. if !ctx.IsSigned {
  30. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  31. ctx.Redirect("/user/login/", 302)
  32. return
  33. }
  34. ctx.Data["ViewType"] = "created_by"
  35. }
  36. // Get issues.
  37. issues, err := models.GetIssues(0, ctx.Repo.Repository.Id, posterId, int64(milestoneId), page,
  38. ctx.Query("state") == "closed", false, ctx.Query("labels"), ctx.Query("sortType"))
  39. if err != nil {
  40. ctx.Handle(200, "issue.Issues: %v", err)
  41. return
  42. }
  43. if ctx.IsSigned {
  44. posterId = ctx.User.Id
  45. }
  46. var createdByCount int
  47. showIssues := make([]models.Issue, 0, len(issues))
  48. // Get posters.
  49. for i := range issues {
  50. u, err := models.GetUserById(issues[i].PosterId)
  51. if err != nil {
  52. ctx.Handle(200, "issue.Issues(get poster): %v", err)
  53. return
  54. }
  55. if isCreatedBy && u.Id != posterId {
  56. continue
  57. }
  58. if u.Id == posterId {
  59. createdByCount++
  60. }
  61. issues[i].Poster = u
  62. showIssues = append(showIssues, issues[i])
  63. }
  64. ctx.Data["Issues"] = showIssues
  65. ctx.Data["IssueCount"] = ctx.Repo.Repository.NumIssues
  66. ctx.Data["OpenCount"] = ctx.Repo.Repository.NumOpenIssues
  67. ctx.Data["ClosedCount"] = ctx.Repo.Repository.NumClosedIssues
  68. ctx.Data["IssueCreatedCount"] = createdByCount
  69. ctx.Data["IsShowClosed"] = ctx.Query("state") == "closed"
  70. ctx.HTML(200, "issue/list")
  71. }
  72. func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  73. ctx.Data["Title"] = "Create issue"
  74. ctx.Data["IsRepoToolbarIssues"] = true
  75. ctx.Data["IsRepoToolbarIssuesList"] = false
  76. if ctx.Req.Method == "GET" {
  77. ctx.HTML(200, "issue/create")
  78. return
  79. }
  80. if ctx.HasError() {
  81. ctx.HTML(200, "issue/create")
  82. return
  83. }
  84. issue, err := models.CreateIssue(ctx.User.Id, ctx.Repo.Repository.Id, form.MilestoneId, form.AssigneeId,
  85. ctx.Repo.Repository.NumIssues, form.IssueName, form.Labels, form.Content, false)
  86. if err != nil {
  87. ctx.Handle(200, "issue.CreateIssue(CreateIssue)", err)
  88. return
  89. }
  90. // Notify watchers.
  91. if err = models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, ActEmail: ctx.User.Email,
  92. OpType: models.OP_CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name),
  93. RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil {
  94. ctx.Handle(200, "issue.CreateIssue(NotifyWatchers)", err)
  95. return
  96. }
  97. // Mail watchers and mentions.
  98. if base.Service.NotifyMail {
  99. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  100. if err != nil {
  101. ctx.Handle(200, "issue.CreateIssue(SendIssueNotifyMail)", err)
  102. return
  103. }
  104. tos = append(tos, ctx.User.LowerName)
  105. ms := base.MentionPattern.FindAllString(issue.Content, -1)
  106. newTos := make([]string, 0, len(ms))
  107. for _, m := range ms {
  108. if com.IsSliceContainsStr(tos, m[1:]) {
  109. continue
  110. }
  111. newTos = append(newTos, m[1:])
  112. }
  113. if err = mailer.SendIssueMentionMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository,
  114. issue, models.GetUserEmailsByNames(newTos)); err != nil {
  115. ctx.Handle(200, "issue.CreateIssue(SendIssueMentionMail)", err)
  116. return
  117. }
  118. }
  119. log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
  120. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index))
  121. }
  122. func ViewIssue(ctx *middleware.Context, params martini.Params) {
  123. index, err := base.StrTo(params["index"]).Int()
  124. if err != nil {
  125. ctx.Handle(404, "issue.ViewIssue", err)
  126. return
  127. }
  128. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  129. if err != nil {
  130. if err == models.ErrIssueNotExist {
  131. ctx.Handle(404, "issue.ViewIssue", err)
  132. } else {
  133. ctx.Handle(200, "issue.ViewIssue", err)
  134. }
  135. return
  136. }
  137. // Get posters.
  138. u, err := models.GetUserById(issue.PosterId)
  139. if err != nil {
  140. ctx.Handle(200, "issue.ViewIssue(get poster): %v", err)
  141. return
  142. }
  143. issue.Poster = u
  144. issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink))
  145. // Get comments.
  146. comments, err := models.GetIssueComments(issue.Id)
  147. if err != nil {
  148. ctx.Handle(200, "issue.ViewIssue(get comments): %v", err)
  149. return
  150. }
  151. // Get posters.
  152. for i := range comments {
  153. u, err := models.GetUserById(comments[i].PosterId)
  154. if err != nil {
  155. ctx.Handle(200, "issue.ViewIssue(get poster): %v", err)
  156. return
  157. }
  158. comments[i].Poster = u
  159. comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink))
  160. }
  161. ctx.Data["Title"] = issue.Name
  162. ctx.Data["Issue"] = issue
  163. ctx.Data["Comments"] = comments
  164. ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
  165. ctx.Data["IsRepoToolbarIssues"] = true
  166. ctx.Data["IsRepoToolbarIssuesList"] = false
  167. ctx.HTML(200, "issue/view")
  168. }
  169. func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  170. index, err := base.StrTo(params["index"]).Int()
  171. if err != nil {
  172. ctx.Handle(404, "issue.UpdateIssue", err)
  173. return
  174. }
  175. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  176. if err != nil {
  177. if err == models.ErrIssueNotExist {
  178. ctx.Handle(404, "issue.UpdateIssue", err)
  179. } else {
  180. ctx.Handle(200, "issue.UpdateIssue(get issue)", err)
  181. }
  182. return
  183. }
  184. if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner {
  185. ctx.Handle(404, "issue.UpdateIssue", nil)
  186. return
  187. }
  188. issue.Name = form.IssueName
  189. issue.MilestoneId = form.MilestoneId
  190. issue.AssigneeId = form.AssigneeId
  191. issue.Labels = form.Labels
  192. issue.Content = form.Content
  193. if err = models.UpdateIssue(issue); err != nil {
  194. ctx.Handle(200, "issue.UpdateIssue(update issue)", err)
  195. return
  196. }
  197. ctx.JSON(200, map[string]interface{}{
  198. "ok": true,
  199. "title": issue.Name,
  200. "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)),
  201. })
  202. }
  203. func Comment(ctx *middleware.Context, params martini.Params) {
  204. index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
  205. if err != nil {
  206. ctx.Handle(404, "issue.Comment(get index)", err)
  207. return
  208. }
  209. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index)
  210. if err != nil {
  211. if err == models.ErrIssueNotExist {
  212. ctx.Handle(404, "issue.Comment", err)
  213. } else {
  214. ctx.Handle(200, "issue.Comment(get issue)", err)
  215. }
  216. return
  217. }
  218. // Check if issue owner changes the status of issue.
  219. var newStatus string
  220. if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id {
  221. newStatus = ctx.Query("change_status")
  222. }
  223. if len(newStatus) > 0 {
  224. if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) ||
  225. (strings.Contains(newStatus, "Close") && !issue.IsClosed) {
  226. issue.IsClosed = !issue.IsClosed
  227. if err = models.UpdateIssue(issue); err != nil {
  228. ctx.Handle(200, "issue.Comment(update issue status)", err)
  229. return
  230. }
  231. cmtType := models.IT_CLOSE
  232. if !issue.IsClosed {
  233. cmtType = models.IT_REOPEN
  234. }
  235. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, ""); err != nil {
  236. ctx.Handle(200, "issue.Comment(create status change comment)", err)
  237. return
  238. }
  239. log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed)
  240. }
  241. }
  242. content := ctx.Query("content")
  243. if len(content) > 0 {
  244. switch params["action"] {
  245. case "new":
  246. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.IT_PLAIN, content); err != nil {
  247. ctx.Handle(500, "issue.Comment(create comment)", err)
  248. return
  249. }
  250. log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
  251. default:
  252. ctx.Handle(404, "issue.Comment", err)
  253. return
  254. }
  255. }
  256. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))
  257. }