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 7.8 kB

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