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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. // Get issue-user pairs.
  57. pairs, err := models.GetIssueUserPairs(ctx.Repo.Repository.Id, ctx.User.Id, isShowClosed)
  58. if err != nil {
  59. ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err)
  60. return
  61. }
  62. // Get posters.
  63. for i := range issues {
  64. idx := models.PairsContains(pairs, issues[i].Id)
  65. if filterMode == models.FM_MENTION && (idx == -1 || !pairs[idx].IsMentioned) {
  66. continue
  67. }
  68. if idx > -1 {
  69. issues[i].IsRead = pairs[idx].IsRead
  70. } else {
  71. issues[i].IsRead = true
  72. }
  73. if err = issues[i].GetPoster(); err != nil {
  74. ctx.Handle(500, "issue.Issues(GetPoster): %v", err)
  75. return
  76. }
  77. }
  78. var uid int64 = -1
  79. if ctx.User != nil {
  80. uid = ctx.User.Id
  81. }
  82. issueStats := models.GetIssueStats(ctx.Repo.Repository.Id, uid, isShowClosed, filterMode)
  83. ctx.Data["IssueStats"] = issueStats
  84. ctx.Data["ViewType"] = viewType
  85. ctx.Data["Issues"] = issues
  86. ctx.Data["IsShowClosed"] = isShowClosed
  87. if isShowClosed {
  88. ctx.Data["State"] = "closed"
  89. ctx.Data["ShowCount"] = issueStats.ClosedCount
  90. } else {
  91. ctx.Data["ShowCount"] = issueStats.OpenCount
  92. }
  93. ctx.HTML(200, "issue/list")
  94. }
  95. func CreateIssue(ctx *middleware.Context, params martini.Params) {
  96. ctx.Data["Title"] = "Create issue"
  97. ctx.Data["IsRepoToolbarIssues"] = true
  98. ctx.Data["IsRepoToolbarIssuesList"] = false
  99. ctx.HTML(200, "issue/create")
  100. }
  101. func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  102. ctx.Data["Title"] = "Create issue"
  103. ctx.Data["IsRepoToolbarIssues"] = true
  104. ctx.Data["IsRepoToolbarIssuesList"] = false
  105. if ctx.HasError() {
  106. ctx.HTML(200, "issue/create")
  107. return
  108. }
  109. issue := &models.Issue{
  110. Index: int64(ctx.Repo.Repository.NumIssues) + 1,
  111. Name: form.IssueName,
  112. RepoId: ctx.Repo.Repository.Id,
  113. PosterId: ctx.User.Id,
  114. MilestoneId: form.MilestoneId,
  115. AssigneeId: form.AssigneeId,
  116. Labels: form.Labels,
  117. Content: form.Content,
  118. }
  119. if err := models.NewIssue(issue); err != nil {
  120. ctx.Handle(500, "issue.CreateIssue(NewIssue)", err)
  121. return
  122. } else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id,
  123. ctx.Repo.Owner.Id, ctx.User.Id, form.AssigneeId); err != nil {
  124. ctx.Handle(500, "issue.CreateIssue(NewIssueUserPairs)", err)
  125. return
  126. }
  127. // Update mentions.
  128. ms := base.MentionPattern.FindAllString(issue.Content, -1)
  129. if len(ms) > 0 {
  130. for i := range ms {
  131. ms[i] = ms[i][1:]
  132. }
  133. ids := models.GetUserIdsByNames(ms)
  134. if err := models.UpdateIssueUserPairsByMentions(ids, issue.Id); err != nil {
  135. ctx.Handle(500, "issue.CreateIssue(UpdateIssueUserPairsByMentions)", err)
  136. return
  137. }
  138. }
  139. // Notify watchers.
  140. if err := models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, ActEmail: ctx.User.Email,
  141. OpType: models.OP_CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name),
  142. RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil {
  143. ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err)
  144. return
  145. }
  146. // Mail watchers and mentions.
  147. if base.Service.NotifyMail {
  148. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  149. if err != nil {
  150. ctx.Handle(500, "issue.CreateIssue(SendIssueNotifyMail)", err)
  151. return
  152. }
  153. tos = append(tos, ctx.User.LowerName)
  154. newTos := make([]string, 0, len(ms))
  155. for _, m := range ms {
  156. if com.IsSliceContainsStr(tos, m) {
  157. continue
  158. }
  159. newTos = append(newTos, m)
  160. }
  161. if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
  162. ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
  163. ctx.Handle(500, "issue.CreateIssue(SendIssueMentionMail)", err)
  164. return
  165. }
  166. }
  167. log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
  168. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index))
  169. }
  170. func ViewIssue(ctx *middleware.Context, params martini.Params) {
  171. idx, _ := base.StrTo(params["index"]).Int64()
  172. if idx == 0 {
  173. ctx.Handle(404, "issue.ViewIssue", nil)
  174. return
  175. }
  176. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx)
  177. if err != nil {
  178. if err == models.ErrIssueNotExist {
  179. ctx.Handle(404, "issue.ViewIssue", err)
  180. } else {
  181. ctx.Handle(200, "issue.ViewIssue", err)
  182. }
  183. return
  184. }
  185. // Update issue-user.
  186. if err = models.UpdateIssueUserPairByRead(ctx.User.Id, issue.Id); err != nil {
  187. ctx.Handle(500, "issue.ViewIssue(UpdateIssueUserPairByRead): %v", err)
  188. return
  189. }
  190. // Get poster.
  191. u, err := models.GetUserById(issue.PosterId)
  192. if err != nil {
  193. ctx.Handle(500, "issue.ViewIssue(GetUserById): %v", err)
  194. return
  195. }
  196. issue.Poster = u
  197. issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink))
  198. // Get comments.
  199. comments, err := models.GetIssueComments(issue.Id)
  200. if err != nil {
  201. ctx.Handle(500, "issue.ViewIssue(GetIssueComments): %v", err)
  202. return
  203. }
  204. // Get posters.
  205. for i := range comments {
  206. u, err := models.GetUserById(comments[i].PosterId)
  207. if err != nil {
  208. ctx.Handle(500, "issue.ViewIssue(get poster of comment): %v", err)
  209. return
  210. }
  211. comments[i].Poster = u
  212. comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink))
  213. }
  214. ctx.Data["Title"] = issue.Name
  215. ctx.Data["Issue"] = issue
  216. ctx.Data["Comments"] = comments
  217. ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
  218. ctx.Data["IsRepoToolbarIssues"] = true
  219. ctx.Data["IsRepoToolbarIssuesList"] = false
  220. ctx.HTML(200, "issue/view")
  221. }
  222. func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  223. index, err := base.StrTo(params["index"]).Int()
  224. if err != nil {
  225. ctx.Handle(404, "issue.UpdateIssue", err)
  226. return
  227. }
  228. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
  229. if err != nil {
  230. if err == models.ErrIssueNotExist {
  231. ctx.Handle(404, "issue.UpdateIssue", err)
  232. } else {
  233. ctx.Handle(200, "issue.UpdateIssue(get issue)", err)
  234. }
  235. return
  236. }
  237. if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner {
  238. ctx.Handle(404, "issue.UpdateIssue", nil)
  239. return
  240. }
  241. issue.Name = form.IssueName
  242. issue.MilestoneId = form.MilestoneId
  243. issue.AssigneeId = form.AssigneeId
  244. issue.Labels = form.Labels
  245. issue.Content = form.Content
  246. if err = models.UpdateIssue(issue); err != nil {
  247. ctx.Handle(200, "issue.UpdateIssue(update issue)", err)
  248. return
  249. }
  250. ctx.JSON(200, map[string]interface{}{
  251. "ok": true,
  252. "title": issue.Name,
  253. "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)),
  254. })
  255. }
  256. func Comment(ctx *middleware.Context, params martini.Params) {
  257. index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
  258. if err != nil {
  259. ctx.Handle(404, "issue.Comment(get index)", err)
  260. return
  261. }
  262. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index)
  263. if err != nil {
  264. if err == models.ErrIssueNotExist {
  265. ctx.Handle(404, "issue.Comment", err)
  266. } else {
  267. ctx.Handle(200, "issue.Comment(get issue)", err)
  268. }
  269. return
  270. }
  271. // TODO: check collaborators
  272. // Check if issue owner changes the status of issue.
  273. var newStatus string
  274. if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id {
  275. newStatus = ctx.Query("change_status")
  276. }
  277. if len(newStatus) > 0 {
  278. if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) ||
  279. (strings.Contains(newStatus, "Close") && !issue.IsClosed) {
  280. issue.IsClosed = !issue.IsClosed
  281. if err = models.UpdateIssue(issue); err != nil {
  282. ctx.Handle(500, "issue.Comment(UpdateIssue)", err)
  283. return
  284. } else if err = models.UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil {
  285. ctx.Handle(500, "issue.Comment(UpdateIssueUserPairsByStatus)", err)
  286. return
  287. }
  288. cmtType := models.IT_CLOSE
  289. if !issue.IsClosed {
  290. cmtType = models.IT_REOPEN
  291. }
  292. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, ""); err != nil {
  293. ctx.Handle(200, "issue.Comment(create status change comment)", err)
  294. return
  295. }
  296. log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed)
  297. }
  298. }
  299. var ms []string
  300. content := ctx.Query("content")
  301. if len(content) > 0 {
  302. switch params["action"] {
  303. case "new":
  304. if err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.IT_PLAIN, content); err != nil {
  305. ctx.Handle(500, "issue.Comment(create comment)", err)
  306. return
  307. }
  308. // Update mentions.
  309. ms = base.MentionPattern.FindAllString(issue.Content, -1)
  310. if len(ms) > 0 {
  311. for i := range ms {
  312. ms[i] = ms[i][1:]
  313. }
  314. ids := models.GetUserIdsByNames(ms)
  315. if err := models.UpdateIssueUserPairsByMentions(ids, issue.Id); err != nil {
  316. ctx.Handle(500, "issue.CreateIssue(UpdateIssueUserPairsByMentions)", err)
  317. return
  318. }
  319. }
  320. log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
  321. default:
  322. ctx.Handle(404, "issue.Comment", err)
  323. return
  324. }
  325. }
  326. // Notify watchers.
  327. if err = models.NotifyWatchers(&models.Action{ActUserId: ctx.User.Id, ActUserName: ctx.User.Name, ActEmail: ctx.User.Email,
  328. OpType: models.OP_COMMENT_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]),
  329. RepoId: ctx.Repo.Repository.Id, RepoName: ctx.Repo.Repository.Name, RefName: ""}); err != nil {
  330. ctx.Handle(500, "issue.CreateIssue(NotifyWatchers)", err)
  331. return
  332. }
  333. // Mail watchers and mentions.
  334. if base.Service.NotifyMail {
  335. issue.Content = content
  336. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  337. if err != nil {
  338. ctx.Handle(500, "issue.Comment(SendIssueNotifyMail)", err)
  339. return
  340. }
  341. tos = append(tos, ctx.User.LowerName)
  342. newTos := make([]string, 0, len(ms))
  343. for _, m := range ms {
  344. if com.IsSliceContainsStr(tos, m) {
  345. continue
  346. }
  347. newTos = append(newTos, m)
  348. }
  349. if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
  350. ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
  351. ctx.Handle(500, "issue.Comment(SendIssueMentionMail)", err)
  352. return
  353. }
  354. }
  355. ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index))
  356. }
  357. func Milestones(ctx *middleware.Context) {
  358. ctx.Data["Title"] = "Milestones"
  359. ctx.Data["IsRepoToolbarIssues"] = true
  360. ctx.Data["IsRepoToolbarIssuesList"] = true
  361. ctx.HTML(200, "issue/milestone")
  362. }