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

issue.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/codegangsta/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. if !ctx.Repo.IsValid {
  19. ctx.Handle(404, "issue.Issues(invalid repo):", nil)
  20. }
  21. ctx.Data["Title"] = "Issues"
  22. ctx.Data["IsRepoToolbarIssues"] = true
  23. ctx.Data["IsRepoToolbarIssuesList"] = true
  24. ctx.Data["ViewType"] = "all"
  25. milestoneId, _ := base.StrTo(ctx.Query("milestone")).Int()
  26. page, _ := base.StrTo(ctx.Query("page")).Int()
  27. ctx.Data["IssueCreatedCount"] = 0
  28. var posterId int64 = 0
  29. if ctx.Query("type") == "created_by" {
  30. if !ctx.IsSigned {
  31. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  32. ctx.Redirect("/user/login/", 302)
  33. return
  34. }
  35. ctx.Data["ViewType"] = "created_by"
  36. }
  37. // Get issues.
  38. issues, err := models.GetIssues(0, ctx.Repo.Repository.Id, posterId, int64(milestoneId), page,
  39. ctx.Query("state") == "closed", false, ctx.Query("labels"), ctx.Query("sortType"))
  40. if err != nil {
  41. ctx.Handle(200, "issue.Issues: %v", err)
  42. return
  43. }
  44. if ctx.IsSigned {
  45. posterId = ctx.User.Id
  46. }
  47. var createdByCount int
  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. issues[i].Poster = u
  56. if u.Id == posterId {
  57. createdByCount++
  58. }
  59. }
  60. ctx.Data["Issues"] = issues
  61. ctx.Data["IssueCount"] = ctx.Repo.Repository.NumIssues
  62. ctx.Data["OpenCount"] = ctx.Repo.Repository.NumIssues - ctx.Repo.Repository.NumClosedIssues
  63. ctx.Data["ClosedCount"] = ctx.Repo.Repository.NumClosedIssues
  64. ctx.Data["IssueCreatedCount"] = createdByCount
  65. ctx.Data["IsShowClosed"] = ctx.Query("state") == "closed"
  66. ctx.HTML(200, "issue/list")
  67. }
  68. func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  69. if !ctx.Repo.IsValid {
  70. ctx.Handle(404, "issue.CreateIssue(invalid repo):", nil)
  71. }
  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.Id, ctx.Repo.Repository.Id, ctx.User.Name, ctx.Repo.Repository.Name, issue.Name, issue.Content); 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. if !ctx.Repo.IsValid {
  108. ctx.Handle(404, "issue.ViewIssue(invalid repo):", nil)
  109. }
  110. index, err := base.StrTo(params["index"]).Int()
  111. if err != nil {
  112. ctx.Handle(404, "issue.ViewIssue", err)
  113. return
  114. }
  115. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  116. if err != nil {
  117. if err == models.ErrIssueNotExist {
  118. ctx.Handle(404, "issue.ViewIssue", err)
  119. } else {
  120. ctx.Handle(200, "issue.ViewIssue", err)
  121. }
  122. return
  123. }
  124. // Get posters.
  125. u, err := models.GetUserById(issue.PosterId)
  126. if err != nil {
  127. ctx.Handle(200, "issue.ViewIssue(get poster): %v", err)
  128. return
  129. }
  130. issue.Poster = u
  131. issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ""))
  132. // Get comments.
  133. comments, err := models.GetIssueComments(issue.Id)
  134. if err != nil {
  135. ctx.Handle(200, "issue.ViewIssue(get comments): %v", err)
  136. return
  137. }
  138. // Get posters.
  139. for i := range comments {
  140. u, err := models.GetUserById(comments[i].PosterId)
  141. if err != nil {
  142. ctx.Handle(200, "issue.ViewIssue(get poster): %v", err)
  143. return
  144. }
  145. comments[i].Poster = u
  146. comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ""))
  147. }
  148. ctx.Data["Title"] = issue.Name
  149. ctx.Data["Issue"] = issue
  150. ctx.Data["Comments"] = comments
  151. ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id
  152. ctx.Data["IsRepoToolbarIssues"] = true
  153. ctx.Data["IsRepoToolbarIssuesList"] = false
  154. ctx.HTML(200, "issue/view")
  155. }
  156. func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  157. if !ctx.Repo.IsValid {
  158. ctx.Handle(404, "issue.UpdateIssue(invalid repo):", nil)
  159. }
  160. index, err := base.StrTo(params["index"]).Int()
  161. if err != nil {
  162. ctx.Handle(404, "issue.UpdateIssue", err)
  163. return
  164. }
  165. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  166. if err != nil {
  167. if err == models.ErrIssueNotExist {
  168. ctx.Handle(404, "issue.UpdateIssue", err)
  169. } else {
  170. ctx.Handle(200, "issue.UpdateIssue(get issue)", err)
  171. }
  172. return
  173. }
  174. if ctx.User.Id != issue.PosterId {
  175. ctx.Handle(404, "issue.UpdateIssue", nil)
  176. return
  177. }
  178. issue.Name = form.IssueName
  179. issue.MilestoneId = form.MilestoneId
  180. issue.AssigneeId = form.AssigneeId
  181. issue.Labels = form.Labels
  182. issue.Content = form.Content
  183. if err = models.UpdateIssue(issue); err != nil {
  184. ctx.Handle(200, "issue.UpdateIssue(update issue)", err)
  185. return
  186. }
  187. ctx.JSON(200, map[string]interface{}{
  188. "ok": true,
  189. "title": issue.Name,
  190. "content": string(base.RenderMarkdown([]byte(issue.Content), "")),
  191. })
  192. }
  193. func Comment(ctx *middleware.Context, params martini.Params) {
  194. if !ctx.Repo.IsValid {
  195. ctx.Handle(404, "issue.Comment(invalid repo):", nil)
  196. }
  197. index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
  198. if err != nil {
  199. ctx.Handle(404, "issue.Comment(get index)", err)
  200. return
  201. }
  202. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index)
  203. if err != nil {
  204. if err == models.ErrIssueNotExist {
  205. ctx.Handle(404, "issue.Comment", err)
  206. } else {
  207. ctx.Handle(200, "issue.Comment(get issue)", err)
  208. }
  209. return
  210. }
  211. // Check if issue owner changes the status of issue.
  212. var newStatus string
  213. if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id {
  214. newStatus = ctx.Query("change_status")
  215. }
  216. if len(newStatus) > 0 {
  217. if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) ||
  218. (strings.Contains(newStatus, "Close") && !issue.IsClosed) {
  219. issue.IsClosed = !issue.IsClosed
  220. if err = models.UpdateIssue(issue); err != nil {
  221. ctx.Handle(200, "issue.Comment(update issue status)", err)
  222. return
  223. }
  224. cmtType := models.IT_CLOSE
  225. if !issue.IsClosed {
  226. cmtType = models.IT_REOPEN
  227. }
  228. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, ""); err != nil {
  229. ctx.Handle(200, "issue.Comment(create status change comment)", err)
  230. return
  231. }
  232. log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed)
  233. }
  234. }
  235. content := ctx.Query("content")
  236. if len(content) > 0 {
  237. switch params["action"] {
  238. case "new":
  239. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.IT_PLAIN, content); err != nil {
  240. ctx.Handle(500, "issue.Comment(create comment)", err)
  241. return
  242. }
  243. log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
  244. default:
  245. ctx.Handle(404, "issue.Comment", err)
  246. return
  247. }
  248. }
  249. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))
  250. }