issue.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. "github.com/codegangsta/martini"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/middleware"
  15. )
  16. func Issues(ctx *middleware.Context) {
  17. if !ctx.Repo.IsValid {
  18. ctx.Handle(404, "issue.Issues(invalid repo):", nil)
  19. }
  20. ctx.Data["Title"] = "Issues"
  21. ctx.Data["IsRepoToolbarIssues"] = true
  22. ctx.Data["IsRepoToolbarIssuesList"] = true
  23. ctx.Data["ViewType"] = "all"
  24. milestoneId, _ := base.StrTo(ctx.Query("milestone")).Int()
  25. page, _ := base.StrTo(ctx.Query("page")).Int()
  26. ctx.Data["IssueCreatedCount"] = 0
  27. var posterId int64 = 0
  28. if ctx.Query("type") == "created_by" {
  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. // 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. issues[i].Poster = u
  55. if u.Id == posterId {
  56. createdByCount++
  57. }
  58. }
  59. ctx.Data["Issues"] = issues
  60. ctx.Data["IssueCount"] = ctx.Repo.Repository.NumIssues
  61. ctx.Data["OpenCount"] = ctx.Repo.Repository.NumIssues - ctx.Repo.Repository.NumClosedIssues
  62. ctx.Data["ClosedCount"] = ctx.Repo.Repository.NumClosedIssues
  63. ctx.Data["IssueCreatedCount"] = createdByCount
  64. ctx.Data["IsShowClosed"] = ctx.Query("state") == "closed"
  65. ctx.HTML(200, "issue/list")
  66. }
  67. func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  68. if !ctx.Repo.IsValid {
  69. ctx.Handle(404, "issue.CreateIssue(invalid repo):", nil)
  70. }
  71. ctx.Data["Title"] = "Create issue"
  72. ctx.Data["IsRepoToolbarIssues"] = true
  73. ctx.Data["IsRepoToolbarIssuesList"] = false
  74. if ctx.Req.Method == "GET" {
  75. ctx.HTML(200, "issue/create")
  76. return
  77. }
  78. if ctx.HasError() {
  79. ctx.HTML(200, "issue/create")
  80. return
  81. }
  82. issue, err := models.CreateIssue(ctx.User.Id, ctx.Repo.Repository.Id, form.MilestoneId, form.AssigneeId,
  83. ctx.Repo.Repository.NumIssues, form.IssueName, form.Labels, form.Content, false)
  84. if err != nil {
  85. ctx.Handle(200, "issue.CreateIssue", err)
  86. return
  87. }
  88. // Notify watchers.
  89. if err = models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name,
  90. OpType: models.OP_CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name),
  91. RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil {
  92. ctx.Handle(200, "issue.CreateIssue", err)
  93. return
  94. }
  95. // Mail watchers.
  96. if base.Service.NotifyMail {
  97. if err = mailer.SendNotifyMail(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.Name, ctx.Repo.Repository.Name, issue.Name, issue.Content); err != nil {
  98. ctx.Handle(200, "issue.CreateIssue", err)
  99. return
  100. }
  101. }
  102. log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
  103. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index))
  104. }
  105. func ViewIssue(ctx *middleware.Context, params martini.Params) {
  106. if !ctx.Repo.IsValid {
  107. ctx.Handle(404, "issue.ViewIssue(invalid repo):", nil)
  108. }
  109. index, err := base.StrTo(params["index"]).Int()
  110. if err != nil {
  111. ctx.Handle(404, "issue.ViewIssue", err)
  112. return
  113. }
  114. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  115. if err != nil {
  116. if err == models.ErrIssueNotExist {
  117. ctx.Handle(404, "issue.ViewIssue", err)
  118. } else {
  119. ctx.Handle(200, "issue.ViewIssue", err)
  120. }
  121. return
  122. }
  123. // Get posters.
  124. u, err := models.GetUserById(issue.PosterId)
  125. if err != nil {
  126. ctx.Handle(200, "issue.ViewIssue(get poster): %v", err)
  127. return
  128. }
  129. issue.Poster = u
  130. issue.Content = string(base.RenderMarkdown([]byte(issue.Content), ""))
  131. // Get comments.
  132. comments, err := models.GetIssueComments(issue.Id)
  133. if err != nil {
  134. ctx.Handle(200, "issue.ViewIssue(get comments): %v", err)
  135. return
  136. }
  137. // Get posters.
  138. for i := range comments {
  139. u, err := models.GetUserById(comments[i].PosterId)
  140. if err != nil {
  141. ctx.Handle(200, "issue.ViewIssue(get poster): %v", err)
  142. return
  143. }
  144. comments[i].Poster = u
  145. comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ""))
  146. }
  147. ctx.Data["Title"] = issue.Name
  148. ctx.Data["Issue"] = issue
  149. ctx.Data["Comments"] = comments
  150. ctx.Data["IsRepoToolbarIssues"] = true
  151. ctx.Data["IsRepoToolbarIssuesList"] = false
  152. ctx.HTML(200, "issue/view")
  153. }
  154. func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  155. if !ctx.Repo.IsValid {
  156. ctx.Handle(404, "issue.UpdateIssue(invalid repo):", nil)
  157. }
  158. index, err := base.StrTo(params["index"]).Int()
  159. if err != nil {
  160. ctx.Handle(404, "issue.UpdateIssue", err)
  161. return
  162. }
  163. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  164. if err != nil {
  165. if err == models.ErrIssueNotExist {
  166. ctx.Handle(404, "issue.UpdateIssue", err)
  167. } else {
  168. ctx.Handle(200, "issue.UpdateIssue(get issue)", err)
  169. }
  170. return
  171. }
  172. if ctx.User.Id != issue.PosterId {
  173. ctx.Handle(404, "issue.UpdateIssue", nil)
  174. return
  175. }
  176. issue.Name = form.IssueName
  177. issue.MilestoneId = form.MilestoneId
  178. issue.AssigneeId = form.AssigneeId
  179. issue.Labels = form.Labels
  180. issue.Content = form.Content
  181. if err = models.UpdateIssue(issue); err != nil {
  182. ctx.Handle(200, "issue.UpdateIssue(update issue)", err)
  183. return
  184. }
  185. ctx.Data["Title"] = issue.Name
  186. ctx.Data["Issue"] = issue
  187. }
  188. func Comment(ctx *middleware.Context, params martini.Params) {
  189. fmt.Println(ctx.Query("change_status"))
  190. if !ctx.Repo.IsValid {
  191. ctx.Handle(404, "issue.Comment(invalid repo):", nil)
  192. }
  193. index, err := base.StrTo(ctx.Query("issueIndex")).Int()
  194. if err != nil {
  195. ctx.Handle(404, "issue.Comment", err)
  196. return
  197. }
  198. content := ctx.Query("content")
  199. if len(content) == 0 {
  200. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))
  201. return
  202. }
  203. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  204. if err != nil {
  205. if err == models.ErrIssueNotExist {
  206. ctx.Handle(404, "issue.Comment", err)
  207. } else {
  208. ctx.Handle(200, "issue.Comment(get issue)", err)
  209. }
  210. return
  211. }
  212. switch params["action"] {
  213. case "new":
  214. if err = models.CreateComment(ctx.User.Id, issue.Id, 0, 0, content); err != nil {
  215. ctx.Handle(500, "issue.Comment(create comment)", err)
  216. return
  217. }
  218. log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
  219. default:
  220. ctx.Handle(404, "issue.Comment", err)
  221. return
  222. }
  223. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))
  224. }