issue.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, ActEmail: ctx.User.Email,
  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.RenderedContent = 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["IsIssueOwner"] = issue.PosterId == ctx.User.Id
  151. ctx.Data["IsRepoToolbarIssues"] = true
  152. ctx.Data["IsRepoToolbarIssuesList"] = false
  153. ctx.HTML(200, "issue/view")
  154. }
  155. func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  156. if !ctx.Repo.IsValid {
  157. ctx.Handle(404, "issue.UpdateIssue(invalid repo):", nil)
  158. }
  159. index, err := base.StrTo(params["index"]).Int()
  160. if err != nil {
  161. ctx.Handle(404, "issue.UpdateIssue", err)
  162. return
  163. }
  164. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  165. if err != nil {
  166. if err == models.ErrIssueNotExist {
  167. ctx.Handle(404, "issue.UpdateIssue", err)
  168. } else {
  169. ctx.Handle(200, "issue.UpdateIssue(get issue)", err)
  170. }
  171. return
  172. }
  173. if ctx.User.Id != issue.PosterId {
  174. ctx.Handle(404, "issue.UpdateIssue", nil)
  175. return
  176. }
  177. issue.Name = form.IssueName
  178. issue.MilestoneId = form.MilestoneId
  179. issue.AssigneeId = form.AssigneeId
  180. issue.Labels = form.Labels
  181. issue.Content = form.Content
  182. if err = models.UpdateIssue(issue); err != nil {
  183. ctx.Handle(200, "issue.UpdateIssue(update issue)", err)
  184. return
  185. }
  186. ctx.JSON(200, map[string]interface{}{
  187. "ok": true,
  188. "title": issue.Name,
  189. "content": string(base.RenderMarkdown([]byte(issue.Content), "")),
  190. })
  191. }
  192. func Comment(ctx *middleware.Context, params martini.Params) {
  193. fmt.Println(ctx.Query("change_status"))
  194. if !ctx.Repo.IsValid {
  195. ctx.Handle(404, "issue.Comment(invalid repo):", nil)
  196. }
  197. index, err := base.StrTo(ctx.Query("issueIndex")).Int()
  198. if err != nil {
  199. ctx.Handle(404, "issue.Comment", err)
  200. return
  201. }
  202. content := ctx.Query("content")
  203. if len(content) == 0 {
  204. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))
  205. return
  206. }
  207. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  208. if err != nil {
  209. if err == models.ErrIssueNotExist {
  210. ctx.Handle(404, "issue.Comment", err)
  211. } else {
  212. ctx.Handle(200, "issue.Comment(get issue)", err)
  213. }
  214. return
  215. }
  216. switch params["action"] {
  217. case "new":
  218. if err = models.CreateComment(ctx.User.Id, issue.Id, 0, 0, content); err != nil {
  219. ctx.Handle(500, "issue.Comment(create comment)", err)
  220. return
  221. }
  222. log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
  223. default:
  224. ctx.Handle(404, "issue.Comment", err)
  225. return
  226. }
  227. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", ctx.User.Name, ctx.Repo.Repository.Name, index))
  228. }