issue.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/Unknwon/com"
  10. "github.com/go-martini/martini"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/mailer"
  16. "github.com/gogits/gogs/modules/middleware"
  17. )
  18. func Issues(ctx *middleware.Context) {
  19. ctx.Data["Title"] = "Issues"
  20. ctx.Data["IsRepoToolbarIssues"] = true
  21. ctx.Data["IsRepoToolbarIssuesList"] = true
  22. viewType := ctx.Query("type")
  23. types := []string{"assigned", "created_by", "mentioned"}
  24. if !com.IsSliceContainsStr(types, viewType) {
  25. viewType = "all"
  26. }
  27. isShowClosed := ctx.Query("state") == "closed"
  28. if viewType != "all" {
  29. if !ctx.IsSigned {
  30. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  31. ctx.Redirect("/user/login")
  32. return
  33. }
  34. }
  35. var assigneeId, posterId int64
  36. var filterMode int
  37. switch viewType {
  38. case "assigned":
  39. assigneeId = ctx.User.Id
  40. filterMode = models.FM_ASSIGN
  41. case "created_by":
  42. posterId = ctx.User.Id
  43. filterMode = models.FM_CREATE
  44. case "mentioned":
  45. filterMode = models.FM_MENTION
  46. }
  47. mid, _ := base.StrTo(ctx.Query("milestone")).Int64()
  48. page, _ := base.StrTo(ctx.Query("page")).Int()
  49. // Get issues.
  50. issues, err := models.GetIssues(assigneeId, ctx.Repo.Repository.Id, posterId, mid, page,
  51. isShowClosed, ctx.Query("labels"), ctx.Query("sortType"))
  52. if err != nil {
  53. ctx.Handle(500, "issue.Issues(GetIssues): %v", err)
  54. return
  55. }
  56. var pairs []*models.IssseUser
  57. if filterMode == models.FM_MENTION {
  58. // Get issue-user pairs.
  59. pairs, err = models.GetIssueUserPairs(ctx.Repo.Repository.Id, ctx.User.Id, isShowClosed)
  60. if err != nil {
  61. ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err)
  62. return
  63. }
  64. }
  65. // Get posters.
  66. for i := range issues {
  67. if filterMode == models.FM_MENTION && !models.PairsContains(pairs, issues[i].Id) {
  68. continue
  69. }
  70. if err = issues[i].GetPoster(); err != nil {
  71. ctx.Handle(500, "issue.Issues(GetPoster): %v", err)
  72. return
  73. }
  74. }
  75. var uid int64 = -1
  76. if ctx.User != nil {
  77. uid = ctx.User.Id
  78. }
  79. issueStats := models.GetIssueStats(ctx.Repo.Repository.Id, uid, isShowClosed, filterMode)
  80. ctx.Data["IssueStats"] = issueStats
  81. ctx.Data["ViewType"] = viewType
  82. ctx.Data["Issues"] = issues
  83. ctx.Data["IsShowClosed"] = isShowClosed
  84. if isShowClosed {
  85. ctx.Data["State"] = "closed"
  86. ctx.Data["ShowCount"] = issueStats.ClosedCount
  87. } else {
  88. ctx.Data["ShowCount"] = issueStats.OpenCount
  89. }
  90. ctx.HTML(200, "issue/list")
  91. }
  92. func CreateIssue(ctx *middleware.Context, params martini.Params) {
  93. ctx.Data["Title"] = "Create issue"
  94. ctx.Data["IsRepoToolbarIssues"] = true
  95. ctx.Data["IsRepoToolbarIssuesList"] = false
  96. ctx.HTML(200, "issue/create")
  97. }
  98. func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  99. ctx.Data["Title"] = "Create issue"
  100. ctx.Data["IsRepoToolbarIssues"] = true
  101. ctx.Data["IsRepoToolbarIssuesList"] = false
  102. if ctx.HasError() {
  103. ctx.HTML(200, "issue/create")
  104. return
  105. }
  106. issue := &models.Issue{
  107. Index: int64(ctx.Repo.Repository.NumIssues) + 1,
  108. Name: form.IssueName,
  109. RepoId: ctx.Repo.Repository.Id,
  110. PosterId: ctx.User.Id,
  111. MilestoneId: form.MilestoneId,
  112. AssigneeId: form.AssigneeId,
  113. Labels: form.Labels,
  114. Content: form.Content,
  115. }
  116. if err := models.NewIssue(issue); err != nil {
  117. ctx.Handle(500, "issue.CreateIssue(NewIssue)", err)
  118. return
  119. }
  120. // Notify watchers.
  121. if err := models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, ActEmail: ctx.User.Email,
  122. OpType: models.OP_CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name),
  123. RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil {
  124. ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err)
  125. return
  126. }
  127. // Mail watchers and mentions.
  128. if base.Service.NotifyMail {
  129. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  130. if err != nil {
  131. ctx.Handle(500, "issue.CreateIssue(SendIssueNotifyMail)", err)
  132. return
  133. }
  134. tos = append(tos, ctx.User.LowerName)
  135. ms := base.MentionPattern.FindAllString(issue.Content, -1)
  136. newTos := make([]string, 0, len(ms))
  137. for _, m := range ms {
  138. if com.IsSliceContainsStr(tos, m[1:]) {
  139. continue
  140. }
  141. newTos = append(newTos, m[1:])
  142. }
  143. if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
  144. ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
  145. ctx.Handle(500, "issue.CreateIssue(SendIssueMentionMail)", err)
  146. return
  147. }
  148. }
  149. log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
  150. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index))
  151. }
  152. func ViewIssue(ctx *middleware.Context, params martini.Params) {
  153. idx, _ := base.StrTo(params["index"]).Int64()
  154. if idx == 0 {
  155. ctx.Handle(404, "issue.ViewIssue", nil)
  156. return
  157. }
  158. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx)
  159. if err != nil {
  160. if err == models.ErrIssueNotExist {
  161. ctx.Handle(404, "issue.ViewIssue", err)
  162. } else {
  163. ctx.Handle(200, "issue.ViewIssue", err)
  164. }
  165. return
  166. }
  167. // Get poster.
  168. u, err := models.GetUserById(issue.PosterId)
  169. if err != nil {
  170. ctx.Handle(500, "issue.ViewIssue(GetUserById): %v", err)
  171. return
  172. }
  173. issue.Poster = u
  174. issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink))
  175. // Get comments.
  176. comments, err := models.GetIssueComments(issue.Id)
  177. if err != nil {
  178. ctx.Handle(500, "issue.ViewIssue(GetIssueComments): %v", err)
  179. return
  180. }
  181. // Get posters.
  182. for i := range comments {
  183. u, err := models.GetUserById(comments[i].PosterId)
  184. if err != nil {
  185. ctx.Handle(500, "issue.ViewIssue(get poster of comment): %v", err)
  186. return
  187. }
  188. comments[i].Poster = u
  189. comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink))
  190. }
  191. ctx.Data["Title"] = issue.Name
  192. ctx.Data["Issue"] = issue
  193. ctx.Data["Comments"] = comments
  194. ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
  195. ctx.Data["IsRepoToolbarIssues"] = true
  196. ctx.Data["IsRepoToolbarIssuesList"] = false
  197. ctx.HTML(200, "issue/view")
  198. }
  199. func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  200. index, err := base.StrTo(params["index"]).Int()
  201. if err != nil {
  202. ctx.Handle(404, "issue.UpdateIssue", err)
  203. return
  204. }
  205. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  206. if err != nil {
  207. if err == models.ErrIssueNotExist {
  208. ctx.Handle(404, "issue.UpdateIssue", err)
  209. } else {
  210. ctx.Handle(200, "issue.UpdateIssue(get issue)", err)
  211. }
  212. return
  213. }
  214. if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner {
  215. ctx.Handle(404, "issue.UpdateIssue", nil)
  216. return
  217. }
  218. issue.Name = form.IssueName
  219. issue.MilestoneId = form.MilestoneId
  220. issue.AssigneeId = form.AssigneeId
  221. issue.Labels = form.Labels
  222. issue.Content = form.Content
  223. if err = models.UpdateIssue(issue); err != nil {
  224. ctx.Handle(200, "issue.UpdateIssue(update issue)", err)
  225. return
  226. }
  227. ctx.JSON(200, map[string]interface{}{
  228. "ok": true,
  229. "title": issue.Name,
  230. "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)),
  231. })
  232. }
  233. func Comment(ctx *middleware.Context, params martini.Params) {
  234. index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
  235. if err != nil {
  236. ctx.Handle(404, "issue.Comment(get index)", err)
  237. return
  238. }
  239. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index)
  240. if err != nil {
  241. if err == models.ErrIssueNotExist {
  242. ctx.Handle(404, "issue.Comment", err)
  243. } else {
  244. ctx.Handle(200, "issue.Comment(get issue)", err)
  245. }
  246. return
  247. }
  248. // TODO: check collaborators
  249. // Check if issue owner changes the status of issue.
  250. var newStatus string
  251. if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id {
  252. newStatus = ctx.Query("change_status")
  253. }
  254. if len(newStatus) > 0 {
  255. if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) ||
  256. (strings.Contains(newStatus, "Close") && !issue.IsClosed) {
  257. issue.IsClosed = !issue.IsClosed
  258. if err = models.UpdateIssue(issue); err != nil {
  259. ctx.Handle(200, "issue.Comment(update issue status)", err)
  260. return
  261. }
  262. cmtType := models.IT_CLOSE
  263. if !issue.IsClosed {
  264. cmtType = models.IT_REOPEN
  265. }
  266. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, ""); err != nil {
  267. ctx.Handle(200, "issue.Comment(create status change comment)", err)
  268. return
  269. }
  270. log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed)
  271. }
  272. }
  273. content := ctx.Query("content")
  274. if len(content) > 0 {
  275. switch params["action"] {
  276. case "new":
  277. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.IT_PLAIN, content); err != nil {
  278. ctx.Handle(500, "issue.Comment(create comment)", err)
  279. return
  280. }
  281. log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
  282. default:
  283. ctx.Handle(404, "issue.Comment", err)
  284. return
  285. }
  286. }
  287. // Notify watchers.
  288. if err = models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, ActEmail: ctx.User.Email,
  289. OpType: models.OP_COMMENT_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]),
  290. RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil {
  291. ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err)
  292. return
  293. }
  294. // Mail watchers and mentions.
  295. if base.Service.NotifyMail {
  296. issue.Content = content
  297. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  298. if err != nil {
  299. ctx.Handle(500, "issue.Comment(SendIssueNotifyMail)", err)
  300. return
  301. }
  302. tos = append(tos, ctx.User.LowerName)
  303. ms := base.MentionPattern.FindAllString(issue.Content, -1)
  304. newTos := make([]string, 0, len(ms))
  305. for _, m := range ms {
  306. if com.IsSliceContainsStr(tos, m[1:]) {
  307. continue
  308. }
  309. newTos = append(newTos, m[1:])
  310. }
  311. if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
  312. ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
  313. ctx.Handle(500, "issue.Comment(SendIssueMentionMail)", err)
  314. return
  315. }
  316. }
  317. ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index))
  318. }