issue.go 7.8 KB

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