issue.go 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. "net/http"
  9. "net/url"
  10. "strings"
  11. "time"
  12. "github.com/Unknwon/com"
  13. "github.com/Unknwon/paginater"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/auth"
  16. "github.com/gogits/gogs/modules/base"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/mailer"
  19. "github.com/gogits/gogs/modules/middleware"
  20. "github.com/gogits/gogs/modules/setting"
  21. )
  22. const (
  23. ISSUES base.TplName = "repo/issue/list"
  24. ISSUE_NEW base.TplName = "repo/issue/new"
  25. ISSUE_VIEW base.TplName = "repo/issue/view"
  26. LABELS base.TplName = "repo/issue/labels"
  27. MILESTONE base.TplName = "repo/issue/milestones"
  28. MILESTONE_NEW base.TplName = "repo/issue/milestone_new"
  29. MILESTONE_EDIT base.TplName = "repo/issue/milestone_edit"
  30. )
  31. var (
  32. ErrFileTypeForbidden = errors.New("File type is not allowed")
  33. ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
  34. )
  35. func RetrieveLabels(ctx *middleware.Context) {
  36. labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID)
  37. if err != nil {
  38. ctx.Handle(500, "RetrieveLabels.GetLabels: %v", err)
  39. return
  40. }
  41. for _, l := range labels {
  42. l.CalOpenIssues()
  43. }
  44. ctx.Data["Labels"] = labels
  45. ctx.Data["NumLabels"] = len(labels)
  46. }
  47. func Issues(ctx *middleware.Context) {
  48. ctx.Data["Title"] = ctx.Tr("repo.issues")
  49. ctx.Data["PageIsIssueList"] = true
  50. viewType := ctx.Query("type")
  51. sortType := ctx.Query("sort")
  52. types := []string{"assigned", "created_by", "mentioned"}
  53. if !com.IsSliceContainsStr(types, viewType) {
  54. viewType = "all"
  55. }
  56. // Must sign in to see issues about you.
  57. if viewType != "all" && !ctx.IsSigned {
  58. ctx.SetCookie("redirect_to", "/"+url.QueryEscape(setting.AppSubUrl+ctx.Req.RequestURI), 0, setting.AppSubUrl)
  59. ctx.Redirect(setting.AppSubUrl + "/user/login")
  60. return
  61. }
  62. var (
  63. assigneeID = ctx.QueryInt64("assignee")
  64. posterID int64
  65. )
  66. filterMode := models.FM_ALL
  67. switch viewType {
  68. case "assigned":
  69. assigneeID = ctx.User.Id
  70. filterMode = models.FM_ASSIGN
  71. case "created_by":
  72. posterID = ctx.User.Id
  73. filterMode = models.FM_CREATE
  74. case "mentioned":
  75. filterMode = models.FM_MENTION
  76. }
  77. var uid int64 = -1
  78. if ctx.IsSigned {
  79. uid = ctx.User.Id
  80. }
  81. repo := ctx.Repo.Repository
  82. selectLabels := ctx.Query("labels")
  83. milestoneID := ctx.QueryInt64("milestone")
  84. isShowClosed := ctx.Query("state") == "closed"
  85. issueStats := models.GetIssueStats(repo.ID, uid, com.StrTo(selectLabels).MustInt64(), milestoneID, assigneeID, isShowClosed, filterMode)
  86. page := ctx.QueryInt("page")
  87. if page <= 1 {
  88. page = 1
  89. }
  90. var total int
  91. if !isShowClosed {
  92. total = int(issueStats.OpenCount)
  93. } else {
  94. total = int(issueStats.ClosedCount)
  95. }
  96. ctx.Data["Page"] = paginater.New(total, setting.IssuePagingNum, page, 5)
  97. // Get issues.
  98. issues, err := models.Issues(uid, assigneeID, repo.ID, posterID, milestoneID,
  99. page, isShowClosed, filterMode == models.FM_MENTION, selectLabels, sortType)
  100. if err != nil {
  101. ctx.Handle(500, "Issues: %v", err)
  102. return
  103. }
  104. // Get issue-user relations.
  105. pairs, err := models.GetIssueUsers(repo.ID, posterID, isShowClosed)
  106. if err != nil {
  107. ctx.Handle(500, "GetIssueUsers: %v", err)
  108. return
  109. }
  110. // Get posters.
  111. for i := range issues {
  112. if err = issues[i].GetPoster(); err != nil {
  113. ctx.Handle(500, "GetPoster", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  114. return
  115. }
  116. if err = issues[i].GetLabels(); err != nil {
  117. ctx.Handle(500, "GetLabels", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  118. return
  119. }
  120. if !ctx.IsSigned {
  121. issues[i].IsRead = true
  122. continue
  123. }
  124. // Check read status.
  125. idx := models.PairsContains(pairs, issues[i].ID, ctx.User.Id)
  126. if idx > -1 {
  127. issues[i].IsRead = pairs[idx].IsRead
  128. } else {
  129. issues[i].IsRead = true
  130. }
  131. }
  132. ctx.Data["Issues"] = issues
  133. // Get milestones.
  134. ctx.Data["Milestones"], err = models.GetAllRepoMilestones(repo.ID)
  135. if err != nil {
  136. ctx.Handle(500, "GetAllRepoMilestones: %v", err)
  137. return
  138. }
  139. // Get assignees.
  140. ctx.Data["Assignees"], err = repo.GetAssignees()
  141. if err != nil {
  142. ctx.Handle(500, "GetAssignees: %v", err)
  143. return
  144. }
  145. ctx.Data["IssueStats"] = issueStats
  146. ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
  147. ctx.Data["ViewType"] = viewType
  148. ctx.Data["SortType"] = sortType
  149. ctx.Data["MilestoneID"] = milestoneID
  150. ctx.Data["AssigneeID"] = assigneeID
  151. ctx.Data["IsShowClosed"] = isShowClosed
  152. if isShowClosed {
  153. ctx.Data["State"] = "closed"
  154. } else {
  155. ctx.Data["State"] = "open"
  156. }
  157. ctx.HTML(200, ISSUES)
  158. }
  159. func renderAttachmentSettings(ctx *middleware.Context) {
  160. ctx.Data["IsAttachmentEnabled"] = setting.AttachmentEnabled
  161. ctx.Data["AttachmentAllowedTypes"] = setting.AttachmentAllowedTypes
  162. ctx.Data["AttachmentMaxFiles"] = setting.AttachmentMaxFiles
  163. }
  164. func NewIssue(ctx *middleware.Context) {
  165. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  166. ctx.Data["PageIsIssueList"] = true
  167. ctx.Data["RequireDropzone"] = true
  168. renderAttachmentSettings(ctx)
  169. if ctx.Repo.IsAdmin() {
  170. var (
  171. repo = ctx.Repo.Repository
  172. err error
  173. )
  174. ctx.Data["Labels"], err = models.GetLabelsByRepoID(repo.ID)
  175. if err != nil {
  176. ctx.Handle(500, "GetLabelsByRepoID: %v", err)
  177. return
  178. }
  179. ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false)
  180. if err != nil {
  181. ctx.Handle(500, "GetMilestones: %v", err)
  182. return
  183. }
  184. ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true)
  185. if err != nil {
  186. ctx.Handle(500, "GetMilestones: %v", err)
  187. return
  188. }
  189. ctx.Data["Assignees"], err = repo.GetAssignees()
  190. if err != nil {
  191. ctx.Handle(500, "GetAssignees: %v", err)
  192. return
  193. }
  194. }
  195. ctx.HTML(200, ISSUE_NEW)
  196. }
  197. func NewIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) {
  198. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  199. ctx.Data["PageIsIssueList"] = true
  200. ctx.Data["RequireDropzone"] = true
  201. renderAttachmentSettings(ctx)
  202. var (
  203. repo = ctx.Repo.Repository
  204. labelIDs []int64
  205. milestoneID int64
  206. assigneeID int64
  207. attachments []string
  208. )
  209. if ctx.Repo.IsAdmin() {
  210. // Check labels.
  211. labelIDs = base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
  212. labelIDMark := base.Int64sToMap(labelIDs)
  213. labels, err := models.GetLabelsByRepoID(repo.ID)
  214. if err != nil {
  215. ctx.Handle(500, "GetLabelsByRepoID: %v", err)
  216. return
  217. }
  218. hasSelected := false
  219. for i := range labels {
  220. if labelIDMark[labels[i].ID] {
  221. labels[i].IsChecked = true
  222. hasSelected = true
  223. }
  224. }
  225. ctx.Data["HasSelectedLabel"] = hasSelected
  226. ctx.Data["label_ids"] = form.LabelIDs
  227. ctx.Data["Labels"] = labels
  228. // Check milestone.
  229. milestoneID = form.MilestoneID
  230. if milestoneID > 0 {
  231. ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false)
  232. if err != nil {
  233. ctx.Handle(500, "GetMilestones: %v", err)
  234. return
  235. }
  236. ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true)
  237. if err != nil {
  238. ctx.Handle(500, "GetMilestones: %v", err)
  239. return
  240. }
  241. ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID)
  242. if err != nil {
  243. ctx.Handle(500, "GetMilestoneByID: %v", err)
  244. return
  245. }
  246. ctx.Data["milestone_id"] = milestoneID
  247. }
  248. // Check assignee.
  249. assigneeID = form.AssigneeID
  250. if assigneeID > 0 {
  251. ctx.Data["Assignees"], err = repo.GetAssignees()
  252. if err != nil {
  253. ctx.Handle(500, "GetAssignees: %v", err)
  254. return
  255. }
  256. ctx.Data["Assignee"], err = repo.GetAssigneeByID(assigneeID)
  257. if err != nil {
  258. ctx.Handle(500, "GetAssigneeByID: %v", err)
  259. return
  260. }
  261. ctx.Data["assignee_id"] = assigneeID
  262. }
  263. }
  264. if setting.AttachmentEnabled {
  265. attachments = form.Attachments
  266. }
  267. if ctx.HasError() {
  268. ctx.HTML(200, ISSUE_NEW)
  269. return
  270. }
  271. issue := &models.Issue{
  272. RepoID: ctx.Repo.Repository.ID,
  273. Index: int64(repo.NumIssues) + 1,
  274. Name: form.Title,
  275. PosterID: ctx.User.Id,
  276. Poster: ctx.User,
  277. MilestoneID: milestoneID,
  278. AssigneeID: assigneeID,
  279. Content: form.Content,
  280. }
  281. if err := models.NewIssue(repo, issue, labelIDs, attachments); err != nil {
  282. ctx.Handle(500, "NewIssue", err)
  283. return
  284. }
  285. // Update mentions.
  286. mentions := base.MentionPattern.FindAllString(issue.Content, -1)
  287. if len(mentions) > 0 {
  288. for i := range mentions {
  289. mentions[i] = mentions[i][1:]
  290. }
  291. if err := models.UpdateMentions(mentions, issue.ID); err != nil {
  292. ctx.Handle(500, "UpdateMentions", err)
  293. return
  294. }
  295. }
  296. // Mail watchers and mentions.
  297. if setting.Service.EnableNotifyMail {
  298. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  299. if err != nil {
  300. ctx.Handle(500, "SendIssueNotifyMail", err)
  301. return
  302. }
  303. tos = append(tos, ctx.User.LowerName)
  304. newTos := make([]string, 0, len(mentions))
  305. for _, m := range mentions {
  306. if com.IsSliceContainsStr(tos, m) {
  307. continue
  308. }
  309. newTos = append(newTos, m)
  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, "SendIssueMentionMail", err)
  314. return
  315. }
  316. }
  317. log.Trace("Issue created: %d/%d", ctx.Repo.Repository.ID, issue.ID)
  318. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  319. }
  320. func UploadIssueAttachment(ctx *middleware.Context) {
  321. if !setting.AttachmentEnabled {
  322. ctx.Error(404, "attachment is not enabled")
  323. return
  324. }
  325. allowedTypes := strings.Split(setting.AttachmentAllowedTypes, ",")
  326. file, header, err := ctx.Req.FormFile("file")
  327. if err != nil {
  328. ctx.Error(500, fmt.Sprintf("FormFile: %v", err))
  329. return
  330. }
  331. defer file.Close()
  332. buf := make([]byte, 1024)
  333. n, _ := file.Read(buf)
  334. if n > 0 {
  335. buf = buf[:n]
  336. }
  337. fileType := http.DetectContentType(buf)
  338. allowed := false
  339. for _, t := range allowedTypes {
  340. t := strings.Trim(t, " ")
  341. if t == "*/*" || t == fileType {
  342. allowed = true
  343. break
  344. }
  345. }
  346. if !allowed {
  347. ctx.Error(400, ErrFileTypeForbidden.Error())
  348. return
  349. }
  350. attach, err := models.NewAttachment(header.Filename, buf, file)
  351. if err != nil {
  352. ctx.Error(500, fmt.Sprintf("NewAttachment: %v", err))
  353. return
  354. }
  355. log.Trace("New attachment uploaded: %s", attach.UUID)
  356. ctx.JSON(200, map[string]string{
  357. "uuid": attach.UUID,
  358. })
  359. }
  360. func ViewIssue(ctx *middleware.Context) {
  361. ctx.Data["PageIsIssueList"] = true
  362. ctx.Data["RequireDropzone"] = true
  363. renderAttachmentSettings(ctx)
  364. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  365. if err != nil {
  366. if models.IsErrIssueNotExist(err) {
  367. ctx.Handle(404, "GetIssueByIndex", err)
  368. } else {
  369. ctx.Handle(500, "GetIssueByIndex", err)
  370. }
  371. return
  372. }
  373. ctx.Data["Title"] = issue.Name
  374. if err = issue.GetPoster(); err != nil {
  375. ctx.Handle(500, "GetPoster", err)
  376. return
  377. }
  378. issue.RenderedContent = string(base.RenderMarkdown([]byte(issue.Content), ctx.Repo.RepoLink))
  379. repo := ctx.Repo.Repository
  380. // Metas.
  381. // Check labels.
  382. if err = issue.GetLabels(); err != nil {
  383. ctx.Handle(500, "GetLabels", err)
  384. return
  385. }
  386. labelIDMark := make(map[int64]bool)
  387. for i := range issue.Labels {
  388. labelIDMark[issue.Labels[i].ID] = true
  389. }
  390. labels, err := models.GetLabelsByRepoID(repo.ID)
  391. if err != nil {
  392. ctx.Handle(500, "GetLabelsByRepoID: %v", err)
  393. return
  394. }
  395. hasSelected := false
  396. for i := range labels {
  397. if labelIDMark[labels[i].ID] {
  398. labels[i].IsChecked = true
  399. hasSelected = true
  400. }
  401. }
  402. ctx.Data["HasSelectedLabel"] = hasSelected
  403. ctx.Data["Labels"] = labels
  404. // Check milestone and assignee.
  405. if ctx.Repo.IsAdmin() {
  406. ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false)
  407. if err != nil {
  408. ctx.Handle(500, "GetMilestones: %v", err)
  409. return
  410. }
  411. ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true)
  412. if err != nil {
  413. ctx.Handle(500, "GetMilestones: %v", err)
  414. return
  415. }
  416. ctx.Data["Assignees"], err = repo.GetAssignees()
  417. if err != nil {
  418. ctx.Handle(500, "GetAssignees: %v", err)
  419. return
  420. }
  421. }
  422. if ctx.IsSigned {
  423. // Update issue-user.
  424. if err = issue.ReadBy(ctx.User.Id); err != nil {
  425. ctx.Handle(500, "ReadBy", err)
  426. return
  427. }
  428. }
  429. var (
  430. tag models.CommentTag
  431. ok bool
  432. marked = make(map[int64]models.CommentTag)
  433. comment *models.Comment
  434. )
  435. // Render comments.
  436. for _, comment = range issue.Comments {
  437. if comment.Type == models.COMMENT_TYPE_COMMENT {
  438. comment.RenderedContent = string(base.RenderMarkdown([]byte(comment.Content), ctx.Repo.RepoLink))
  439. // Check tag.
  440. tag, ok = marked[comment.PosterID]
  441. if ok {
  442. comment.ShowTag = tag
  443. continue
  444. }
  445. if repo.IsOwnedBy(comment.PosterID) ||
  446. (repo.Owner.IsOrganization() && repo.Owner.IsOwnedBy(comment.PosterID)) {
  447. comment.ShowTag = models.COMMENT_TAG_OWNER
  448. } else if comment.Poster.IsAdminOfRepo(repo) {
  449. comment.ShowTag = models.COMMENT_TAG_ADMIN
  450. } else if comment.PosterID == issue.PosterID {
  451. comment.ShowTag = models.COMMENT_TAG_POSTER
  452. }
  453. marked[comment.PosterID] = comment.ShowTag
  454. }
  455. }
  456. ctx.Data["Issue"] = issue
  457. ctx.Data["IsIssueOwner"] = ctx.Repo.IsAdmin() || (ctx.IsSigned && issue.IsPoster(ctx.User.Id))
  458. ctx.HTML(200, ISSUE_VIEW)
  459. }
  460. func getActionIssue(ctx *middleware.Context) *models.Issue {
  461. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  462. if err != nil {
  463. if models.IsErrIssueNotExist(err) {
  464. ctx.Error(404, "GetIssueByIndex")
  465. } else {
  466. ctx.Handle(500, "GetIssueByIndex", err)
  467. }
  468. return nil
  469. }
  470. return issue
  471. }
  472. func UpdateIssueTitle(ctx *middleware.Context) {
  473. issue := getActionIssue(ctx)
  474. if ctx.Written() {
  475. return
  476. }
  477. if !ctx.IsSigned || (ctx.User.Id != issue.PosterID && !ctx.Repo.IsAdmin()) {
  478. ctx.Error(403)
  479. return
  480. }
  481. issue.Name = ctx.Query("title")
  482. if len(issue.Name) == 0 {
  483. ctx.Error(204)
  484. return
  485. }
  486. if err := models.UpdateIssue(issue); err != nil {
  487. ctx.Handle(500, "UpdateIssue", err)
  488. return
  489. }
  490. ctx.JSON(200, map[string]interface{}{
  491. "title": issue.Name,
  492. })
  493. }
  494. func UpdateIssueContent(ctx *middleware.Context) {
  495. issue := getActionIssue(ctx)
  496. if ctx.Written() {
  497. return
  498. }
  499. if !ctx.IsSigned || (ctx.User.Id != issue.PosterID && !ctx.Repo.IsAdmin()) {
  500. ctx.Error(403)
  501. return
  502. }
  503. issue.Content = ctx.Query("content")
  504. if err := models.UpdateIssue(issue); err != nil {
  505. ctx.Handle(500, "UpdateIssue", err)
  506. return
  507. }
  508. ctx.JSON(200, map[string]interface{}{
  509. "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Query("context"))),
  510. })
  511. }
  512. func UpdateIssueLabel(ctx *middleware.Context) {
  513. issue := getActionIssue(ctx)
  514. if ctx.Written() {
  515. return
  516. }
  517. if ctx.Query("action") == "clear" {
  518. if err := issue.ClearLabels(); err != nil {
  519. ctx.Handle(500, "ClearLabels", err)
  520. return
  521. }
  522. } else {
  523. isAttach := ctx.Query("action") == "attach"
  524. label, err := models.GetLabelByID(ctx.QueryInt64("id"))
  525. if err != nil {
  526. if models.IsErrLabelNotExist(err) {
  527. ctx.Error(404, "GetLabelByID")
  528. } else {
  529. ctx.Handle(500, "GetLabelByID", err)
  530. }
  531. return
  532. }
  533. if isAttach && !issue.HasLabel(label.ID) {
  534. if err = issue.AddLabel(label); err != nil {
  535. ctx.Handle(500, "AddLabel", err)
  536. return
  537. }
  538. } else if !isAttach && issue.HasLabel(label.ID) {
  539. if err = issue.RemoveLabel(label); err != nil {
  540. ctx.Handle(500, "RemoveLabel", err)
  541. return
  542. }
  543. }
  544. }
  545. ctx.JSON(200, map[string]interface{}{
  546. "ok": true,
  547. })
  548. }
  549. func UpdateIssueMilestone(ctx *middleware.Context) {
  550. issue := getActionIssue(ctx)
  551. if ctx.Written() {
  552. return
  553. }
  554. oldMid := issue.MilestoneID
  555. mid := ctx.QueryInt64("id")
  556. if oldMid == mid {
  557. ctx.JSON(200, map[string]interface{}{
  558. "ok": true,
  559. })
  560. return
  561. }
  562. // Not check for invalid milestone id and give responsibility to owners.
  563. issue.MilestoneID = mid
  564. if err := models.ChangeMilestoneAssign(oldMid, issue); err != nil {
  565. ctx.Handle(500, "ChangeMilestoneAssign", err)
  566. return
  567. }
  568. ctx.JSON(200, map[string]interface{}{
  569. "ok": true,
  570. })
  571. }
  572. func UpdateIssueAssignee(ctx *middleware.Context) {
  573. issue := getActionIssue(ctx)
  574. if ctx.Written() {
  575. return
  576. }
  577. aid := ctx.QueryInt64("id")
  578. if issue.AssigneeID == aid {
  579. ctx.JSON(200, map[string]interface{}{
  580. "ok": true,
  581. })
  582. return
  583. }
  584. // Not check for invalid assignee id and give responsibility to owners.
  585. issue.AssigneeID = aid
  586. if err := models.UpdateIssueUserByAssignee(issue); err != nil {
  587. ctx.Handle(500, "UpdateIssueUserByAssignee: %v", err)
  588. return
  589. }
  590. ctx.JSON(200, map[string]interface{}{
  591. "ok": true,
  592. })
  593. }
  594. func NewComment(ctx *middleware.Context, form auth.CreateCommentForm) {
  595. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  596. if err != nil {
  597. if models.IsErrIssueNotExist(err) {
  598. ctx.Handle(404, "GetIssueByIndex", err)
  599. } else {
  600. ctx.Handle(500, "GetIssueByIndex", err)
  601. }
  602. return
  603. }
  604. var attachments []string
  605. if setting.AttachmentEnabled {
  606. attachments = form.Attachments
  607. }
  608. if ctx.HasError() {
  609. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  610. ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index))
  611. return
  612. }
  613. // Check if issue owner/poster changes the status of issue.
  614. if (ctx.Repo.IsOwner() || (ctx.IsSigned && issue.IsPoster(ctx.User.Id))) &&
  615. (form.Status == "reopen" || form.Status == "close") {
  616. issue.Repo = ctx.Repo.Repository
  617. if err = issue.ChangeStatus(ctx.User, form.Status == "close"); err != nil {
  618. ctx.Handle(500, "ChangeStatus", err)
  619. return
  620. }
  621. log.Trace("%s Issue[%d] status changed: %v", ctx.Req.RequestURI, issue.ID, !issue.IsClosed)
  622. }
  623. // Fix #321: Allow empty comments, as long as we have attachments.
  624. if len(form.Content) == 0 && len(attachments) == 0 {
  625. ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index))
  626. return
  627. }
  628. comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
  629. if err != nil {
  630. ctx.Handle(500, "CreateIssueComment", err)
  631. return
  632. }
  633. // Update mentions.
  634. mentions := base.MentionPattern.FindAllString(comment.Content, -1)
  635. if len(mentions) > 0 {
  636. for i := range mentions {
  637. mentions[i] = mentions[i][1:]
  638. }
  639. if err := models.UpdateMentions(mentions, issue.ID); err != nil {
  640. ctx.Handle(500, "UpdateMentions", err)
  641. return
  642. }
  643. }
  644. // Mail watchers and mentions.
  645. if setting.Service.EnableNotifyMail {
  646. issue.Content = form.Content
  647. tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue)
  648. if err != nil {
  649. ctx.Handle(500, "SendIssueNotifyMail", err)
  650. return
  651. }
  652. tos = append(tos, ctx.User.LowerName)
  653. newTos := make([]string, 0, len(mentions))
  654. for _, m := range mentions {
  655. if com.IsSliceContainsStr(tos, m) {
  656. continue
  657. }
  658. newTos = append(newTos, m)
  659. }
  660. if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner,
  661. ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil {
  662. ctx.Handle(500, "SendIssueMentionMail", err)
  663. return
  664. }
  665. }
  666. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  667. ctx.Redirect(fmt.Sprintf("%s/issues/%d#%s", ctx.Repo.RepoLink, issue.Index, comment.HashTag()))
  668. }
  669. func UpdateCommentContent(ctx *middleware.Context) {
  670. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  671. if err != nil {
  672. if models.IsErrCommentNotExist(err) {
  673. ctx.Error(404, "GetCommentByID")
  674. } else {
  675. ctx.Handle(500, "GetCommentByID", err)
  676. }
  677. return
  678. }
  679. if !ctx.IsSigned || (ctx.User.Id != comment.PosterID && !ctx.Repo.IsAdmin()) {
  680. ctx.Error(403)
  681. return
  682. } else if comment.Type != models.COMMENT_TYPE_COMMENT {
  683. ctx.Error(204)
  684. return
  685. }
  686. comment.Content = ctx.Query("content")
  687. if len(comment.Content) == 0 {
  688. ctx.JSON(200, map[string]interface{}{
  689. "content": "",
  690. })
  691. return
  692. }
  693. if err := models.UpdateComment(comment); err != nil {
  694. ctx.Handle(500, "UpdateComment", err)
  695. return
  696. }
  697. ctx.JSON(200, map[string]interface{}{
  698. "content": string(base.RenderMarkdown([]byte(comment.Content), ctx.Query("context"))),
  699. })
  700. }
  701. func Labels(ctx *middleware.Context) {
  702. ctx.Data["Title"] = ctx.Tr("repo.labels")
  703. ctx.Data["PageIsLabels"] = true
  704. ctx.Data["RequireMinicolors"] = true
  705. ctx.HTML(200, LABELS)
  706. }
  707. func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) {
  708. ctx.Data["Title"] = ctx.Tr("repo.labels")
  709. ctx.Data["PageIsLabels"] = true
  710. if ctx.HasError() {
  711. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  712. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  713. return
  714. }
  715. l := &models.Label{
  716. RepoID: ctx.Repo.Repository.ID,
  717. Name: form.Title,
  718. Color: form.Color,
  719. }
  720. if err := models.NewLabel(l); err != nil {
  721. ctx.Handle(500, "NewLabel", err)
  722. return
  723. }
  724. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  725. }
  726. func UpdateLabel(ctx *middleware.Context, form auth.CreateLabelForm) {
  727. l, err := models.GetLabelByID(form.ID)
  728. if err != nil {
  729. switch {
  730. case models.IsErrLabelNotExist(err):
  731. ctx.Error(404)
  732. default:
  733. ctx.Handle(500, "UpdateLabel", err)
  734. }
  735. return
  736. }
  737. l.Name = form.Title
  738. l.Color = form.Color
  739. if err := models.UpdateLabel(l); err != nil {
  740. ctx.Handle(500, "UpdateLabel", err)
  741. return
  742. }
  743. ctx.Redirect(ctx.Repo.RepoLink + "/labels")
  744. }
  745. func DeleteLabel(ctx *middleware.Context) {
  746. if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  747. ctx.Flash.Error("DeleteLabel: " + err.Error())
  748. } else {
  749. ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
  750. }
  751. ctx.JSON(200, map[string]interface{}{
  752. "redirect": ctx.Repo.RepoLink + "/labels",
  753. })
  754. return
  755. }
  756. func Milestones(ctx *middleware.Context) {
  757. ctx.Data["Title"] = ctx.Tr("repo.milestones")
  758. ctx.Data["PageIsMilestones"] = true
  759. isShowClosed := ctx.Query("state") == "closed"
  760. openCount, closedCount := models.MilestoneStats(ctx.Repo.Repository.ID)
  761. ctx.Data["OpenCount"] = openCount
  762. ctx.Data["ClosedCount"] = closedCount
  763. page := ctx.QueryInt("page")
  764. if page <= 1 {
  765. page = 1
  766. }
  767. var total int
  768. if !isShowClosed {
  769. total = int(openCount)
  770. } else {
  771. total = int(closedCount)
  772. }
  773. ctx.Data["Page"] = paginater.New(total, setting.IssuePagingNum, page, 5)
  774. miles, err := models.GetMilestones(ctx.Repo.Repository.ID, page, isShowClosed)
  775. if err != nil {
  776. ctx.Handle(500, "GetMilestones", err)
  777. return
  778. }
  779. for _, m := range miles {
  780. m.RenderedContent = string(base.RenderMarkdown([]byte(m.Content), ctx.Repo.RepoLink))
  781. m.CalOpenIssues()
  782. }
  783. ctx.Data["Milestones"] = miles
  784. if isShowClosed {
  785. ctx.Data["State"] = "closed"
  786. } else {
  787. ctx.Data["State"] = "open"
  788. }
  789. ctx.Data["IsShowClosed"] = isShowClosed
  790. ctx.HTML(200, MILESTONE)
  791. }
  792. func NewMilestone(ctx *middleware.Context) {
  793. ctx.Data["Title"] = ctx.Tr("repo.milestones.new")
  794. ctx.Data["PageIsMilestones"] = true
  795. ctx.Data["RequireDatetimepicker"] = true
  796. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  797. ctx.HTML(200, MILESTONE_NEW)
  798. }
  799. func NewMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) {
  800. ctx.Data["Title"] = ctx.Tr("repo.milestones.new")
  801. ctx.Data["PageIsMilestones"] = true
  802. ctx.Data["RequireDatetimepicker"] = true
  803. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  804. if ctx.HasError() {
  805. ctx.HTML(200, MILESTONE_NEW)
  806. return
  807. }
  808. if len(form.Deadline) == 0 {
  809. form.Deadline = "9999-12-31"
  810. }
  811. deadline, err := time.Parse("2006-01-02", form.Deadline)
  812. if err != nil {
  813. ctx.Data["Err_Deadline"] = true
  814. ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), MILESTONE_NEW, &form)
  815. return
  816. }
  817. if err = models.NewMilestone(&models.Milestone{
  818. RepoID: ctx.Repo.Repository.ID,
  819. Name: form.Title,
  820. Content: form.Content,
  821. Deadline: deadline,
  822. }); err != nil {
  823. ctx.Handle(500, "NewMilestone", err)
  824. return
  825. }
  826. ctx.Flash.Success(ctx.Tr("repo.milestones.create_success", form.Title))
  827. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  828. }
  829. func EditMilestone(ctx *middleware.Context) {
  830. ctx.Data["Title"] = ctx.Tr("repo.milestones.edit")
  831. ctx.Data["PageIsMilestones"] = true
  832. ctx.Data["PageIsEditMilestone"] = true
  833. ctx.Data["RequireDatetimepicker"] = true
  834. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  835. m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id"))
  836. if err != nil {
  837. if models.IsErrMilestoneNotExist(err) {
  838. ctx.Handle(404, "GetMilestoneByID", nil)
  839. } else {
  840. ctx.Handle(500, "GetMilestoneByID", err)
  841. }
  842. return
  843. }
  844. ctx.Data["title"] = m.Name
  845. ctx.Data["content"] = m.Content
  846. if len(m.DeadlineString) > 0 {
  847. ctx.Data["deadline"] = m.DeadlineString
  848. }
  849. ctx.HTML(200, MILESTONE_NEW)
  850. }
  851. func EditMilestonePost(ctx *middleware.Context, form auth.CreateMilestoneForm) {
  852. ctx.Data["Title"] = ctx.Tr("repo.milestones.edit")
  853. ctx.Data["PageIsMilestones"] = true
  854. ctx.Data["PageIsEditMilestone"] = true
  855. ctx.Data["RequireDatetimepicker"] = true
  856. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  857. if ctx.HasError() {
  858. ctx.HTML(200, MILESTONE_NEW)
  859. return
  860. }
  861. if len(form.Deadline) == 0 {
  862. form.Deadline = "9999-12-31"
  863. }
  864. deadline, err := time.Parse("2006-01-02", form.Deadline)
  865. if err != nil {
  866. ctx.Data["Err_Deadline"] = true
  867. ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), MILESTONE_NEW, &form)
  868. return
  869. }
  870. m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id"))
  871. if err != nil {
  872. if models.IsErrMilestoneNotExist(err) {
  873. ctx.Handle(404, "GetMilestoneByID", nil)
  874. } else {
  875. ctx.Handle(500, "GetMilestoneByID", err)
  876. }
  877. return
  878. }
  879. m.Name = form.Title
  880. m.Content = form.Content
  881. m.Deadline = deadline
  882. if err = models.UpdateMilestone(m); err != nil {
  883. ctx.Handle(500, "UpdateMilestone", err)
  884. return
  885. }
  886. ctx.Flash.Success(ctx.Tr("repo.milestones.edit_success", m.Name))
  887. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  888. }
  889. func ChangeMilestonStatus(ctx *middleware.Context) {
  890. m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id"))
  891. if err != nil {
  892. if models.IsErrMilestoneNotExist(err) {
  893. ctx.Handle(404, "GetMilestoneByID", err)
  894. } else {
  895. ctx.Handle(500, "GetMilestoneByID", err)
  896. }
  897. return
  898. }
  899. switch ctx.Params(":action") {
  900. case "open":
  901. if m.IsClosed {
  902. if err = models.ChangeMilestoneStatus(m, false); err != nil {
  903. ctx.Handle(500, "ChangeMilestoneStatus", err)
  904. return
  905. }
  906. }
  907. ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=open")
  908. case "close":
  909. if !m.IsClosed {
  910. m.ClosedDate = time.Now()
  911. if err = models.ChangeMilestoneStatus(m, true); err != nil {
  912. ctx.Handle(500, "ChangeMilestoneStatus", err)
  913. return
  914. }
  915. }
  916. ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=closed")
  917. default:
  918. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  919. }
  920. }
  921. func DeleteMilestone(ctx *middleware.Context) {
  922. if err := models.DeleteMilestoneByID(ctx.QueryInt64("id")); err != nil {
  923. ctx.Flash.Error("DeleteMilestoneByID: " + err.Error())
  924. } else {
  925. ctx.Flash.Success(ctx.Tr("repo.milestones.deletion_success"))
  926. }
  927. ctx.JSON(200, map[string]interface{}{
  928. "redirect": ctx.Repo.RepoLink + "/milestones",
  929. })
  930. }
  931. func PullRequest2(ctx *middleware.Context) {
  932. ctx.HTML(200, "repo/pr2/list")
  933. }