issue.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "mime"
  11. "net/url"
  12. "strings"
  13. "time"
  14. "github.com/Unknwon/com"
  15. "github.com/go-martini/martini"
  16. "github.com/gogits/gogs/models"
  17. "github.com/gogits/gogs/modules/auth"
  18. "github.com/gogits/gogs/modules/base"
  19. "github.com/gogits/gogs/modules/log"
  20. "github.com/gogits/gogs/modules/mailer"
  21. "github.com/gogits/gogs/modules/middleware"
  22. "github.com/gogits/gogs/modules/setting"
  23. )
  24. const (
  25. ISSUES base.TplName = "repo/issue/list"
  26. ISSUE_CREATE base.TplName = "repo/issue/create"
  27. ISSUE_VIEW base.TplName = "repo/issue/view"
  28. MILESTONE base.TplName = "repo/issue/milestone"
  29. MILESTONE_NEW base.TplName = "repo/issue/milestone_new"
  30. MILESTONE_EDIT base.TplName = "repo/issue/milestone_edit"
  31. )
  32. var (
  33. ErrFileTypeForbidden = errors.New("File type is not allowed")
  34. ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
  35. )
  36. func Issues(ctx *middleware.Context) {
  37. ctx.Data["Title"] = "Issues"
  38. ctx.Data["IsRepoToolbarIssues"] = true
  39. ctx.Data["IsRepoToolbarIssuesList"] = true
  40. viewType := ctx.Query("type")
  41. types := []string{"assigned", "created_by", "mentioned"}
  42. if !com.IsSliceContainsStr(types, viewType) {
  43. viewType = "all"
  44. }
  45. isShowClosed := ctx.Query("state") == "closed"
  46. if viewType != "all" && !ctx.IsSigned {
  47. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
  48. ctx.Redirect("/user/login")
  49. return
  50. }
  51. var assigneeId, posterId int64
  52. var filterMode int
  53. switch viewType {
  54. case "assigned":
  55. assigneeId = ctx.User.Id
  56. filterMode = models.FM_ASSIGN
  57. case "created_by":
  58. posterId = ctx.User.Id
  59. filterMode = models.FM_CREATE
  60. case "mentioned":
  61. filterMode = models.FM_MENTION
  62. }
  63. var mid int64
  64. midx, _ := base.StrTo(ctx.Query("milestone")).Int64()
  65. if midx > 0 {
  66. mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, midx)
  67. if err != nil {
  68. ctx.Handle(500, "issue.Issues(GetMilestoneByIndex): %v", err)
  69. return
  70. }
  71. mid = mile.Id
  72. }
  73. selectLabels := ctx.Query("labels")
  74. labels, err := models.GetLabels(ctx.Repo.Repository.Id)
  75. if err != nil {
  76. ctx.Handle(500, "issue.Issues(GetLabels): %v", err)
  77. return
  78. }
  79. for _, l := range labels {
  80. l.CalOpenIssues()
  81. }
  82. ctx.Data["Labels"] = labels
  83. page, _ := base.StrTo(ctx.Query("page")).Int()
  84. // Get issues.
  85. issues, err := models.GetIssues(assigneeId, ctx.Repo.Repository.Id, posterId, mid, page,
  86. isShowClosed, selectLabels, ctx.Query("sortType"))
  87. if err != nil {
  88. ctx.Handle(500, "issue.Issues(GetIssues): %v", err)
  89. return
  90. }
  91. // Get issue-user pairs.
  92. pairs, err := models.GetIssueUserPairs(ctx.Repo.Repository.Id, posterId, isShowClosed)
  93. if err != nil {
  94. ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err)
  95. return
  96. }
  97. // Get posters.
  98. for i := range issues {
  99. if err = issues[i].GetLabels(); err != nil {
  100. ctx.Handle(500, "issue.Issues(GetLabels)", fmt.Errorf("[#%d]%v", issues[i].Id, err))
  101. return
  102. }
  103. idx := models.PairsContains(pairs, issues[i].Id)
  104. if filterMode == models.FM_MENTION && (idx == -1 || !pairs[idx].IsMentioned) {
  105. continue
  106. }
  107. if idx > -1 {
  108. issues[i].IsRead = pairs[idx].IsRead
  109. } else {
  110. issues[i].IsRead = true
  111. }
  112. if err = issues[i].GetPoster(); err != nil {
  113. ctx.Handle(500, "issue.Issues(GetPoster)", fmt.Errorf("[#%d]%v", issues[i].Id, err))
  114. return
  115. }
  116. }
  117. var uid int64 = -1
  118. if ctx.User != nil {
  119. uid = ctx.User.Id
  120. }
  121. issueStats := models.GetIssueStats(ctx.Repo.Repository.Id, uid, isShowClosed, filterMode)
  122. ctx.Data["IssueStats"] = issueStats
  123. ctx.Data["SelectLabels"], _ = base.StrTo(selectLabels).Int64()
  124. ctx.Data["ViewType"] = viewType
  125. ctx.Data["Issues"] = issues
  126. ctx.Data["IsShowClosed"] = isShowClosed
  127. if isShowClosed {
  128. ctx.Data["State"] = "closed"
  129. ctx.Data["ShowCount"] = issueStats.ClosedCount
  130. } else {
  131. ctx.Data["ShowCount"] = issueStats.OpenCount
  132. }
  133. ctx.HTML(200, ISSUES)
  134. }
  135. func CreateIssue(ctx *middleware.Context, params martini.Params) {
  136. ctx.Data["Title"] = "Create issue"
  137. ctx.Data["IsRepoToolbarIssues"] = true
  138. ctx.Data["IsRepoToolbarIssuesList"] = false
  139. ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled
  140. var err error
  141. // Get all milestones.
  142. ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false)
  143. if err != nil {
  144. ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err)
  145. return
  146. }
  147. ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true)
  148. if err != nil {
  149. ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err)
  150. return
  151. }
  152. us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
  153. if err != nil {
  154. ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err)
  155. return
  156. }
  157. ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes
  158. ctx.Data["Collaborators"] = us
  159. ctx.HTML(200, ISSUE_CREATE)
  160. }
  161. func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  162. send := func(status int, data interface{}, err error) {
  163. log.Error("issue.Comment(?): %s", err)
  164. if err != nil {
  165. ctx.JSON(status, map[string]interface{}{
  166. "ok": false,
  167. "status": status,
  168. "error": err.Error(),
  169. })
  170. } else {
  171. ctx.JSON(status, map[string]interface{}{
  172. "ok": true,
  173. "status": status,
  174. "data": data,
  175. })
  176. }
  177. }
  178. var err error
  179. // Get all milestones.
  180. _, err = models.GetMilestones(ctx.Repo.Repository.Id, false)
  181. if err != nil {
  182. send(500, nil, err)
  183. return
  184. }
  185. _, err = models.GetMilestones(ctx.Repo.Repository.Id, true)
  186. if err != nil {
  187. send(500, nil, err)
  188. return
  189. }
  190. _, err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
  191. if err != nil {
  192. send(500, nil, err)
  193. return
  194. }
  195. if ctx.HasError() {
  196. send(400, nil, errors.New(ctx.Flash.ErrorMsg))
  197. return
  198. }
  199. // Only collaborators can assign.
  200. if !ctx.Repo.IsOwner {
  201. form.AssigneeId = 0
  202. }
  203. issue := &models.Issue{
  204. RepoId: ctx.Repo.Repository.Id,
  205. Index: int64(ctx.Repo.Repository.NumIssues) + 1,
  206. Name: form.IssueName,
  207. PosterId: ctx.User.Id,
  208. MilestoneId: form.MilestoneId,
  209. AssigneeId: form.AssigneeId,
  210. LabelIds: form.Labels,
  211. Content: form.Content,
  212. }
  213. if err := models.NewIssue(issue); err != nil {
  214. send(500, nil, err)
  215. return
  216. } else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id, ctx.Repo.Owner.Id,
  217. ctx.User.Id, form.AssigneeId, ctx.Repo.Repository.Name); err != nil {
  218. send(500, nil, err)
  219. return
  220. }
  221. if setting.AttachmentEnabled {
  222. uploadFiles(ctx, issue.Id, 0)
  223. }
  224. // Update mentions.
  225. ms := base.MentionPattern.FindAllString(issue.Content, -1)
  226. if len(ms) > 0 {
  227. for i := range ms {
  228. ms[i] = ms[i][1:]
  229. }
  230. if err := models.UpdateMentions(ms, issue.Id); err != nil {
  231. send(500, nil, err)
  232. return
  233. }
  234. }
  235. act := &models.Action{
  236. ActUserId: ctx.User.Id,
  237. ActUserName: ctx.User.Name,
  238. ActEmail: ctx.User.Email,
  239. OpType: models.OP_CREATE_ISSUE,
  240. Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name),
  241. RepoId: ctx.Repo.Repository.Id,
  242. RepoUserName: ctx.Repo.Owner.Name,
  243. RepoName: ctx.Repo.Repository.Name,
  244. RefName: ctx.Repo.BranchName,
  245. IsPrivate: ctx.Repo.Repository.IsPrivate,
  246. }
  247. // Notify watchers.
  248. if err := models.NotifyWatchers(act); err != nil {
  249. send(500, nil, err)
  250. return
  251. }
  252. // Mail watchers and mentions.
  253. if setting.Service.EnableNotifyMail {
  254. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  255. if err != nil {
  256. send(500, nil, err)
  257. return
  258. }
  259. tos = append(tos, ctx.User.LowerName)
  260. newTos := make([]string, 0, len(ms))
  261. for _, m := range ms {
  262. if com.IsSliceContainsStr(tos, m) {
  263. continue
  264. }
  265. newTos = append(newTos, m)
  266. }
  267. if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
  268. ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
  269. send(500, nil, err)
  270. return
  271. }
  272. }
  273. log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
  274. send(200, fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index), nil)
  275. }
  276. func checkLabels(labels, allLabels []*models.Label) {
  277. for _, l := range labels {
  278. for _, l2 := range allLabels {
  279. if l.Id == l2.Id {
  280. l2.IsChecked = true
  281. break
  282. }
  283. }
  284. }
  285. }
  286. func ViewIssue(ctx *middleware.Context, params martini.Params) {
  287. ctx.Data["AttachmentsEnabled"] = setting.AttachmentEnabled
  288. idx, _ := base.StrTo(params["index"]).Int64()
  289. if idx == 0 {
  290. ctx.Handle(404, "issue.ViewIssue", nil)
  291. return
  292. }
  293. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx)
  294. if err != nil {
  295. if err == models.ErrIssueNotExist {
  296. ctx.Handle(404, "issue.ViewIssue(GetIssueByIndex)", err)
  297. } else {
  298. ctx.Handle(500, "issue.ViewIssue(GetIssueByIndex)", err)
  299. }
  300. return
  301. }
  302. // Get labels.
  303. if err = issue.GetLabels(); err != nil {
  304. ctx.Handle(500, "issue.ViewIssue(GetLabels)", err)
  305. return
  306. }
  307. labels, err := models.GetLabels(ctx.Repo.Repository.Id)
  308. if err != nil {
  309. ctx.Handle(500, "issue.ViewIssue(GetLabels.2)", err)
  310. return
  311. }
  312. checkLabels(issue.Labels, labels)
  313. ctx.Data["Labels"] = labels
  314. // Get assigned milestone.
  315. if issue.MilestoneId > 0 {
  316. ctx.Data["Milestone"], err = models.GetMilestoneById(issue.MilestoneId)
  317. if err != nil {
  318. if err == models.ErrMilestoneNotExist {
  319. log.Warn("issue.ViewIssue(GetMilestoneById): %v", err)
  320. } else {
  321. ctx.Handle(500, "issue.ViewIssue(GetMilestoneById)", err)
  322. return
  323. }
  324. }
  325. }
  326. // Get all milestones.
  327. ctx.Data["OpenMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, false)
  328. if err != nil {
  329. ctx.Handle(500, "issue.ViewIssue(GetMilestones.1): %v", err)
  330. return
  331. }
  332. ctx.Data["ClosedMilestones"], err = models.GetMilestones(ctx.Repo.Repository.Id, true)
  333. if err != nil {
  334. ctx.Handle(500, "issue.ViewIssue(GetMilestones.2): %v", err)
  335. return
  336. }
  337. // Get all collaborators.
  338. ctx.Data["Collaborators"], err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
  339. if err != nil {
  340. ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err)
  341. return
  342. }
  343. if ctx.IsSigned {
  344. // Update issue-user.
  345. if err = models.UpdateIssueUserPairByRead(ctx.User.Id, issue.Id); err != nil {
  346. ctx.Handle(500, "issue.ViewIssue(UpdateIssueUserPairByRead): %v", err)
  347. return
  348. }
  349. }
  350. // Get poster and Assignee.
  351. if err = issue.GetPoster(); err != nil {
  352. ctx.Handle(500, "issue.ViewIssue(GetPoster): %v", err)
  353. return
  354. } else if err = issue.GetAssignee(); err != nil {
  355. ctx.Handle(500, "issue.ViewIssue(GetAssignee): %v", err)
  356. return
  357. }
  358. issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink))
  359. // Get comments.
  360. comments, err := models.GetIssueComments(issue.Id)
  361. if err != nil {
  362. ctx.Handle(500, "issue.ViewIssue(GetIssueComments): %v", err)
  363. return
  364. }
  365. // Get posters.
  366. for i := range comments {
  367. u, err := models.GetUserById(comments[i].PosterId)
  368. if err != nil {
  369. ctx.Handle(500, "issue.ViewIssue(GetUserById.2): %v", err)
  370. return
  371. }
  372. comments[i].Poster = u
  373. if comments[i].Type == models.COMMENT {
  374. comments[i].Content = string(base.RenderMarkdown([]byte(comments[i].Content), ctx.Repo.RepoLink))
  375. }
  376. }
  377. ctx.Data["AllowedTypes"] = setting.AttachmentAllowedTypes
  378. ctx.Data["Title"] = issue.Name
  379. ctx.Data["Issue"] = issue
  380. ctx.Data["Comments"] = comments
  381. ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
  382. ctx.Data["IsRepoToolbarIssues"] = true
  383. ctx.Data["IsRepoToolbarIssuesList"] = false
  384. ctx.HTML(200, ISSUE_VIEW)
  385. }
  386. func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
  387. idx, _ := base.StrTo(params["index"]).Int64()
  388. if idx <= 0 {
  389. ctx.Error(404)
  390. return
  391. }
  392. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx)
  393. if err != nil {
  394. if err == models.ErrIssueNotExist {
  395. ctx.Handle(404, "issue.UpdateIssue", err)
  396. } else {
  397. ctx.Handle(500, "issue.UpdateIssue(GetIssueByIndex)", err)
  398. }
  399. return
  400. }
  401. if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner {
  402. ctx.Error(403)
  403. return
  404. }
  405. issue.Name = form.IssueName
  406. issue.MilestoneId = form.MilestoneId
  407. issue.AssigneeId = form.AssigneeId
  408. issue.LabelIds = form.Labels
  409. issue.Content = form.Content
  410. // try get content from text, ignore conflict with preview ajax
  411. if form.Content == "" {
  412. issue.Content = ctx.Query("text")
  413. }
  414. if err = models.UpdateIssue(issue); err != nil {
  415. ctx.Handle(500, "issue.UpdateIssue(UpdateIssue)", err)
  416. return
  417. }
  418. ctx.JSON(200, map[string]interface{}{
  419. "ok": true,
  420. "title": issue.Name,
  421. "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink)),
  422. })
  423. }
  424. func UpdateIssueLabel(ctx *middleware.Context, params martini.Params) {
  425. if !ctx.Repo.IsOwner {
  426. ctx.Error(403)
  427. return
  428. }
  429. idx, _ := base.StrTo(params["index"]).Int64()
  430. if idx <= 0 {
  431. ctx.Error(404)
  432. return
  433. }
  434. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx)
  435. if err != nil {
  436. if err == models.ErrIssueNotExist {
  437. ctx.Handle(404, "issue.UpdateIssueLabel(GetIssueByIndex)", err)
  438. } else {
  439. ctx.Handle(500, "issue.UpdateIssueLabel(GetIssueByIndex)", err)
  440. }
  441. return
  442. }
  443. isAttach := ctx.Query("action") == "attach"
  444. labelStrId := ctx.Query("id")
  445. labelId, _ := base.StrTo(labelStrId).Int64()
  446. label, err := models.GetLabelById(labelId)
  447. if err != nil {
  448. if err == models.ErrLabelNotExist {
  449. ctx.Handle(404, "issue.UpdateIssueLabel(GetLabelById)", err)
  450. } else {
  451. ctx.Handle(500, "issue.UpdateIssueLabel(GetLabelById)", err)
  452. }
  453. return
  454. }
  455. isHad := strings.Contains(issue.LabelIds, "$"+labelStrId+"|")
  456. isNeedUpdate := false
  457. if isAttach {
  458. if !isHad {
  459. issue.LabelIds += "$" + labelStrId + "|"
  460. isNeedUpdate = true
  461. }
  462. } else {
  463. if isHad {
  464. issue.LabelIds = strings.Replace(issue.LabelIds, "$"+labelStrId+"|", "", -1)
  465. isNeedUpdate = true
  466. }
  467. }
  468. if isNeedUpdate {
  469. if err = models.UpdateIssue(issue); err != nil {
  470. ctx.Handle(500, "issue.UpdateIssueLabel(UpdateIssue)", err)
  471. return
  472. }
  473. if isAttach {
  474. label.NumIssues++
  475. if issue.IsClosed {
  476. label.NumClosedIssues++
  477. }
  478. } else {
  479. label.NumIssues--
  480. if issue.IsClosed {
  481. label.NumClosedIssues--
  482. }
  483. }
  484. if err = models.UpdateLabel(label); err != nil {
  485. ctx.Handle(500, "issue.UpdateIssueLabel(UpdateLabel)", err)
  486. return
  487. }
  488. }
  489. ctx.JSON(200, map[string]interface{}{
  490. "ok": true,
  491. })
  492. }
  493. func UpdateIssueMilestone(ctx *middleware.Context) {
  494. if !ctx.Repo.IsOwner {
  495. ctx.Error(403)
  496. return
  497. }
  498. issueId, err := base.StrTo(ctx.Query("issue")).Int64()
  499. if err != nil {
  500. ctx.Error(404)
  501. return
  502. }
  503. issue, err := models.GetIssueById(issueId)
  504. if err != nil {
  505. if err == models.ErrIssueNotExist {
  506. ctx.Handle(404, "issue.UpdateIssueMilestone(GetIssueById)", err)
  507. } else {
  508. ctx.Handle(500, "issue.UpdateIssueMilestone(GetIssueById)", err)
  509. }
  510. return
  511. }
  512. oldMid := issue.MilestoneId
  513. mid, _ := base.StrTo(ctx.Query("milestone")).Int64()
  514. if oldMid == mid {
  515. ctx.JSON(200, map[string]interface{}{
  516. "ok": true,
  517. })
  518. return
  519. }
  520. // Not check for invalid milestone id and give responsibility to owners.
  521. issue.MilestoneId = mid
  522. if err = models.ChangeMilestoneAssign(oldMid, mid, issue); err != nil {
  523. ctx.Handle(500, "issue.UpdateIssueMilestone(ChangeMilestoneAssign)", err)
  524. return
  525. } else if err = models.UpdateIssue(issue); err != nil {
  526. ctx.Handle(500, "issue.UpdateIssueMilestone(UpdateIssue)", err)
  527. return
  528. }
  529. ctx.JSON(200, map[string]interface{}{
  530. "ok": true,
  531. })
  532. }
  533. func UpdateAssignee(ctx *middleware.Context) {
  534. if !ctx.Repo.IsOwner {
  535. ctx.Error(403)
  536. return
  537. }
  538. issueId, err := base.StrTo(ctx.Query("issue")).Int64()
  539. if err != nil {
  540. ctx.Error(404)
  541. return
  542. }
  543. issue, err := models.GetIssueById(issueId)
  544. if err != nil {
  545. if err == models.ErrIssueNotExist {
  546. ctx.Handle(404, "issue.UpdateAssignee(GetIssueById)", err)
  547. } else {
  548. ctx.Handle(500, "issue.UpdateAssignee(GetIssueById)", err)
  549. }
  550. return
  551. }
  552. aid, _ := base.StrTo(ctx.Query("assigneeid")).Int64()
  553. // Not check for invalid assignne id and give responsibility to owners.
  554. issue.AssigneeId = aid
  555. if err = models.UpdateIssueUserPairByAssignee(aid, issue.Id); err != nil {
  556. ctx.Handle(500, "issue.UpdateAssignee(UpdateIssueUserPairByAssignee): %v", err)
  557. return
  558. } else if err = models.UpdateIssue(issue); err != nil {
  559. ctx.Handle(500, "issue.UpdateAssignee(UpdateIssue)", err)
  560. return
  561. }
  562. ctx.JSON(200, map[string]interface{}{
  563. "ok": true,
  564. })
  565. }
  566. func uploadFiles(ctx *middleware.Context, issueId, commentId int64) {
  567. if !setting.AttachmentEnabled {
  568. return
  569. }
  570. allowedTypes := strings.Split(setting.AttachmentAllowedTypes, "|")
  571. attachments := ctx.Req.MultipartForm.File["attachments"]
  572. if len(attachments) > setting.AttachmentMaxFiles {
  573. ctx.Handle(400, "issue.Comment", ErrTooManyFiles)
  574. return
  575. }
  576. for _, header := range attachments {
  577. file, err := header.Open()
  578. if err != nil {
  579. ctx.Handle(500, "issue.Comment(header.Open)", err)
  580. return
  581. }
  582. defer file.Close()
  583. allowed := false
  584. fileType := mime.TypeByExtension(header.Filename)
  585. for _, t := range allowedTypes {
  586. t := strings.Trim(t, " ")
  587. if t == "*/*" || t == fileType {
  588. allowed = true
  589. break
  590. }
  591. }
  592. if !allowed {
  593. ctx.Handle(400, "issue.Comment", ErrFileTypeForbidden)
  594. return
  595. }
  596. out, err := ioutil.TempFile(setting.AttachmentPath, "attachment_")
  597. if err != nil {
  598. ctx.Handle(500, "issue.Comment(ioutil.TempFile)", err)
  599. return
  600. }
  601. defer out.Close()
  602. _, err = io.Copy(out, file)
  603. if err != nil {
  604. ctx.Handle(500, "issue.Comment(io.Copy)", err)
  605. return
  606. }
  607. _, err = models.CreateAttachment(issueId, commentId, header.Filename, out.Name())
  608. if err != nil {
  609. ctx.Handle(500, "issue.Comment(io.Copy)", err)
  610. return
  611. }
  612. }
  613. }
  614. func Comment(ctx *middleware.Context, params martini.Params) {
  615. send := func(status int, data interface{}, err error) {
  616. log.Error("issue.Comment(?): %s", err)
  617. if err != nil {
  618. ctx.JSON(status, map[string]interface{}{
  619. "ok": false,
  620. "status": status,
  621. "error": err.Error(),
  622. })
  623. } else {
  624. ctx.JSON(status, map[string]interface{}{
  625. "ok": true,
  626. "status": status,
  627. "data": data,
  628. })
  629. }
  630. }
  631. index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
  632. if err != nil {
  633. send(404, nil, err)
  634. return
  635. }
  636. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, index)
  637. if err != nil {
  638. if err == models.ErrIssueNotExist {
  639. send(404, nil, err)
  640. } else {
  641. send(200, nil, err)
  642. }
  643. return
  644. }
  645. // Check if issue owner changes the status of issue.
  646. var newStatus string
  647. if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id {
  648. newStatus = ctx.Query("change_status")
  649. }
  650. if len(newStatus) > 0 {
  651. if (strings.Contains(newStatus, "Reopen") && issue.IsClosed) ||
  652. (strings.Contains(newStatus, "Close") && !issue.IsClosed) {
  653. issue.IsClosed = !issue.IsClosed
  654. if err = models.UpdateIssue(issue); err != nil {
  655. send(500, nil, err)
  656. return
  657. } else if err = models.UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil {
  658. send(500, nil, err)
  659. return
  660. }
  661. // Change open/closed issue counter for the associated milestone
  662. if issue.MilestoneId > 0 {
  663. if err = models.ChangeMilestoneIssueStats(issue); err != nil {
  664. send(500, nil, err)
  665. }
  666. }
  667. cmtType := models.CLOSE
  668. if !issue.IsClosed {
  669. cmtType = models.REOPEN
  670. }
  671. if _, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, cmtType, "", nil); err != nil {
  672. send(200, nil, err)
  673. return
  674. }
  675. log.Trace("%s Issue(%d) status changed: %v", ctx.Req.RequestURI, issue.Id, !issue.IsClosed)
  676. }
  677. }
  678. var comment *models.Comment
  679. var ms []string
  680. content := ctx.Query("content")
  681. // Fix #321. Allow empty comments, as long as we have attachments.
  682. if len(content) > 0 || len(ctx.Req.MultipartForm.File["attachments"]) > 0 {
  683. switch params["action"] {
  684. case "new":
  685. if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, issue.Id, 0, 0, models.COMMENT, content, nil); err != nil {
  686. send(500, nil, err)
  687. return
  688. }
  689. // Update mentions.
  690. ms = base.MentionPattern.FindAllString(issue.Content, -1)
  691. if len(ms) > 0 {
  692. for i := range ms {
  693. ms[i] = ms[i][1:]
  694. }
  695. if err := models.UpdateMentions(ms, issue.Id); err != nil {
  696. send(500, nil, err)
  697. return
  698. }
  699. }
  700. log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id)
  701. default:
  702. ctx.Handle(404, "issue.Comment", err)
  703. return
  704. }
  705. }
  706. if comment != nil {
  707. uploadFiles(ctx, issue.Id, comment.Id)
  708. }
  709. // Notify watchers.
  710. act := &models.Action{
  711. ActUserId: ctx.User.Id,
  712. ActUserName: ctx.User.LowerName,
  713. ActEmail: ctx.User.Email,
  714. OpType: models.OP_COMMENT_ISSUE,
  715. Content: fmt.Sprintf("%d|%s", issue.Index, strings.Split(content, "\n")[0]),
  716. RepoId: ctx.Repo.Repository.Id,
  717. RepoUserName: ctx.Repo.Owner.LowerName,
  718. RepoName: ctx.Repo.Repository.LowerName,
  719. }
  720. if err = models.NotifyWatchers(act); err != nil {
  721. send(500, nil, err)
  722. return
  723. }
  724. // Mail watchers and mentions.
  725. if setting.Service.EnableNotifyMail {
  726. issue.Content = content
  727. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  728. if err != nil {
  729. send(500, nil, err)
  730. return
  731. }
  732. tos = append(tos, ctx.User.LowerName)
  733. newTos := make([]string, 0, len(ms))
  734. for _, m := range ms {
  735. if com.IsSliceContainsStr(tos, m) {
  736. continue
  737. }
  738. newTos = append(newTos, m)
  739. }
  740. if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
  741. ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
  742. send(500, nil, err)
  743. return
  744. }
  745. }
  746. log.Error("url: %#v", fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index))
  747. send(200, fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, index), nil)
  748. }
  749. func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) {
  750. if ctx.HasError() {
  751. Issues(ctx)
  752. return
  753. }
  754. l := &models.Label{
  755. RepoId: ctx.Repo.Repository.Id,
  756. Name: form.Title,
  757. Color: form.Color,
  758. }
  759. if err := models.NewLabel(l); err != nil {
  760. ctx.Handle(500, "issue.NewLabel(NewLabel)", err)
  761. return
  762. }
  763. ctx.Redirect(ctx.Repo.RepoLink + "/issues")
  764. }
  765. func UpdateLabel(ctx *middleware.Context, params martini.Params, form auth.CreateLabelForm) {
  766. id, _ := base.StrTo(ctx.Query("id")).Int64()
  767. if id == 0 {
  768. ctx.Error(404)
  769. return
  770. }
  771. l := &models.Label{
  772. Id: id,
  773. Name: form.Title,
  774. Color: form.Color,
  775. }
  776. if err := models.UpdateLabel(l); err != nil {
  777. ctx.Handle(500, "issue.UpdateLabel(UpdateLabel)", err)
  778. return
  779. }
  780. ctx.Redirect(ctx.Repo.RepoLink + "/issues")
  781. }
  782. func DeleteLabel(ctx *middleware.Context) {
  783. removes := ctx.Query("remove")
  784. if len(strings.TrimSpace(removes)) == 0 {
  785. ctx.JSON(200, map[string]interface{}{
  786. "ok": true,
  787. })
  788. return
  789. }
  790. strIds := strings.Split(removes, ",")
  791. for _, strId := range strIds {
  792. if err := models.DeleteLabel(ctx.Repo.Repository.Id, strId); err != nil {
  793. ctx.Handle(500, "issue.DeleteLabel(DeleteLabel)", err)
  794. return
  795. }
  796. }
  797. ctx.JSON(200, map[string]interface{}{
  798. "ok": true,
  799. })
  800. }
  801. func Milestones(ctx *middleware.Context) {
  802. ctx.Data["Title"] = "Milestones"
  803. ctx.Data["IsRepoToolbarIssues"] = true
  804. ctx.Data["IsRepoToolbarIssuesList"] = true
  805. isShowClosed := ctx.Query("state") == "closed"
  806. miles, err := models.GetMilestones(ctx.Repo.Repository.Id, isShowClosed)
  807. if err != nil {
  808. ctx.Handle(500, "issue.Milestones(GetMilestones)", err)
  809. return
  810. }
  811. for _, m := range miles {
  812. m.RenderedContent = string(base.RenderSpecialLink([]byte(m.Content), ctx.Repo.RepoLink))
  813. m.CalOpenIssues()
  814. }
  815. ctx.Data["Milestones"] = miles
  816. if isShowClosed {
  817. ctx.Data["State"] = "closed"
  818. } else {
  819. ctx.Data["State"] = "open"
  820. }
  821. ctx.HTML(200, MILESTONE)
  822. }
  823. func NewMilestone(ctx *middleware.Context) {
  824. ctx.Data["Title"] = "New Milestone"
  825. ctx.Data["IsRepoToolbarIssues"] = true
  826. ctx.Data["IsRepoToolbarIssuesList"] = true
  827. ctx.HTML(200, MILESTONE_NEW)
  828. }
  829. func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) {
  830. ctx.Data["Title"] = "New Milestone"
  831. ctx.Data["IsRepoToolbarIssues"] = true
  832. ctx.Data["IsRepoToolbarIssuesList"] = true
  833. if ctx.HasError() {
  834. ctx.HTML(200, MILESTONE_NEW)
  835. return
  836. }
  837. var deadline time.Time
  838. var err error
  839. if len(form.Deadline) == 0 {
  840. form.Deadline = "12/31/9999"
  841. }
  842. deadline, err = time.Parse("01/02/2006", form.Deadline)
  843. if err != nil {
  844. ctx.Handle(500, "issue.NewMilestonePost(time.Parse)", err)
  845. return
  846. }
  847. mile := &models.Milestone{
  848. RepoId: ctx.Repo.Repository.Id,
  849. Index: int64(ctx.Repo.Repository.NumMilestones) + 1,
  850. Name: form.Title,
  851. Content: form.Content,
  852. Deadline: deadline,
  853. }
  854. if err = models.NewMilestone(mile); err != nil {
  855. ctx.Handle(500, "issue.NewMilestonePost(NewMilestone)", err)
  856. return
  857. }
  858. ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones")
  859. }
  860. func UpdateMilestone(ctx *middleware.Context, params martini.Params) {
  861. ctx.Data["Title"] = "Update Milestone"
  862. ctx.Data["IsRepoToolbarIssues"] = true
  863. ctx.Data["IsRepoToolbarIssuesList"] = true
  864. idx, _ := base.StrTo(params["index"]).Int64()
  865. if idx == 0 {
  866. ctx.Handle(404, "issue.UpdateMilestone", nil)
  867. return
  868. }
  869. mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx)
  870. if err != nil {
  871. if err == models.ErrMilestoneNotExist {
  872. ctx.Handle(404, "issue.UpdateMilestone(GetMilestoneByIndex)", err)
  873. } else {
  874. ctx.Handle(500, "issue.UpdateMilestone(GetMilestoneByIndex)", err)
  875. }
  876. return
  877. }
  878. action := params["action"]
  879. if len(action) > 0 {
  880. switch action {
  881. case "open":
  882. if mile.IsClosed {
  883. if err = models.ChangeMilestoneStatus(mile, false); err != nil {
  884. ctx.Handle(500, "issue.UpdateMilestone(ChangeMilestoneStatus)", err)
  885. return
  886. }
  887. }
  888. case "close":
  889. if !mile.IsClosed {
  890. mile.ClosedDate = time.Now()
  891. if err = models.ChangeMilestoneStatus(mile, true); err != nil {
  892. ctx.Handle(500, "issue.UpdateMilestone(ChangeMilestoneStatus)", err)
  893. return
  894. }
  895. }
  896. case "delete":
  897. if err = models.DeleteMilestone(mile); err != nil {
  898. ctx.Handle(500, "issue.UpdateMilestone(DeleteMilestone)", err)
  899. return
  900. }
  901. }
  902. ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones")
  903. return
  904. }
  905. mile.DeadlineString = mile.Deadline.UTC().Format("01/02/2006")
  906. if mile.DeadlineString == "12/31/9999" {
  907. mile.DeadlineString = ""
  908. }
  909. ctx.Data["Milestone"] = mile
  910. ctx.HTML(200, MILESTONE_EDIT)
  911. }
  912. func UpdateMilestonePost(ctx *middleware.Context, params martini.Params, form auth.CreateMilestoneForm) {
  913. ctx.Data["Title"] = "Update Milestone"
  914. ctx.Data["IsRepoToolbarIssues"] = true
  915. ctx.Data["IsRepoToolbarIssuesList"] = true
  916. idx, _ := base.StrTo(params["index"]).Int64()
  917. if idx == 0 {
  918. ctx.Handle(404, "issue.UpdateMilestonePost", nil)
  919. return
  920. }
  921. mile, err := models.GetMilestoneByIndex(ctx.Repo.Repository.Id, idx)
  922. if err != nil {
  923. if err == models.ErrMilestoneNotExist {
  924. ctx.Handle(404, "issue.UpdateMilestonePost(GetMilestoneByIndex)", err)
  925. } else {
  926. ctx.Handle(500, "issue.UpdateMilestonePost(GetMilestoneByIndex)", err)
  927. }
  928. return
  929. }
  930. if ctx.HasError() {
  931. ctx.HTML(200, MILESTONE_EDIT)
  932. return
  933. }
  934. var deadline time.Time
  935. if len(form.Deadline) == 0 {
  936. form.Deadline = "12/31/9999"
  937. }
  938. deadline, err = time.Parse("01/02/2006", form.Deadline)
  939. if err != nil {
  940. ctx.Handle(500, "issue.UpdateMilestonePost(time.Parse)", err)
  941. return
  942. }
  943. mile.Name = form.Title
  944. mile.Content = form.Content
  945. mile.Deadline = deadline
  946. if err = models.UpdateMilestone(mile); err != nil {
  947. ctx.Handle(500, "issue.UpdateMilestonePost(UpdateMilestone)", err)
  948. return
  949. }
  950. ctx.Redirect(ctx.Repo.RepoLink + "/issues/milestones")
  951. }
  952. func IssueGetAttachment(ctx *middleware.Context, params martini.Params) {
  953. id, err := base.StrTo(params["id"]).Int64()
  954. if err != nil {
  955. ctx.Handle(400, "issue.IssueGetAttachment(base.StrTo.Int64)", err)
  956. return
  957. }
  958. attachment, err := models.GetAttachmentById(id)
  959. if err != nil {
  960. ctx.Handle(404, "issue.IssueGetAttachment(models.GetAttachmentById)", err)
  961. return
  962. }
  963. // Fix #312. Attachments with , in their name are not handled correctly by Google Chrome.
  964. // We must put the name in " manually.
  965. ctx.ServeFile(attachment.Path, "\""+attachment.Name+"\"")
  966. }