Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue.go 33KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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. "bytes"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/Unknwon/com"
  15. "github.com/Unknwon/paginater"
  16. "code.gitea.io/git"
  17. "code.gitea.io/gitea/models"
  18. "code.gitea.io/gitea/modules/auth"
  19. "code.gitea.io/gitea/modules/base"
  20. "code.gitea.io/gitea/modules/context"
  21. "code.gitea.io/gitea/modules/indexer"
  22. "code.gitea.io/gitea/modules/log"
  23. "code.gitea.io/gitea/modules/markup/markdown"
  24. "code.gitea.io/gitea/modules/notification"
  25. "code.gitea.io/gitea/modules/setting"
  26. "code.gitea.io/gitea/modules/util"
  27. )
  28. const (
  29. tplIssues base.TplName = "repo/issue/list"
  30. tplIssueNew base.TplName = "repo/issue/new"
  31. tplIssueView base.TplName = "repo/issue/view"
  32. tplMilestone base.TplName = "repo/issue/milestones"
  33. tplMilestoneNew base.TplName = "repo/issue/milestone_new"
  34. tplMilestoneEdit base.TplName = "repo/issue/milestone_edit"
  35. issueTemplateKey = "IssueTemplate"
  36. )
  37. var (
  38. // ErrFileTypeForbidden not allowed file type error
  39. ErrFileTypeForbidden = errors.New("File type is not allowed")
  40. // ErrTooManyFiles upload too many files
  41. ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
  42. // IssueTemplateCandidates issue templates
  43. IssueTemplateCandidates = []string{
  44. "ISSUE_TEMPLATE.md",
  45. "issue_template.md",
  46. ".gitea/ISSUE_TEMPLATE.md",
  47. ".gitea/issue_template.md",
  48. ".github/ISSUE_TEMPLATE.md",
  49. ".github/issue_template.md",
  50. }
  51. )
  52. // MustEnableIssues check if repository enable internal issues
  53. func MustEnableIssues(ctx *context.Context) {
  54. if !ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues) &&
  55. !ctx.Repo.Repository.UnitEnabled(models.UnitTypeExternalTracker) {
  56. ctx.Handle(404, "MustEnableIssues", nil)
  57. return
  58. }
  59. unit, err := ctx.Repo.Repository.GetUnit(models.UnitTypeExternalTracker)
  60. if err == nil {
  61. ctx.Redirect(unit.ExternalTrackerConfig().ExternalTrackerURL)
  62. return
  63. }
  64. }
  65. // MustAllowPulls check if repository enable pull requests
  66. func MustAllowPulls(ctx *context.Context) {
  67. if !ctx.Repo.Repository.AllowsPulls() {
  68. ctx.Handle(404, "MustAllowPulls", nil)
  69. return
  70. }
  71. // User can send pull request if owns a forked repository.
  72. if ctx.IsSigned && ctx.User.HasForkedRepo(ctx.Repo.Repository.ID) {
  73. ctx.Repo.PullRequest.Allowed = true
  74. ctx.Repo.PullRequest.HeadInfo = ctx.User.Name + ":" + ctx.Repo.BranchName
  75. }
  76. }
  77. // Issues render issues page
  78. func Issues(ctx *context.Context) {
  79. isPullList := ctx.Params(":type") == "pulls"
  80. if isPullList {
  81. MustAllowPulls(ctx)
  82. if ctx.Written() {
  83. return
  84. }
  85. ctx.Data["Title"] = ctx.Tr("repo.pulls")
  86. ctx.Data["PageIsPullList"] = true
  87. } else {
  88. MustEnableIssues(ctx)
  89. if ctx.Written() {
  90. return
  91. }
  92. ctx.Data["Title"] = ctx.Tr("repo.issues")
  93. ctx.Data["PageIsIssueList"] = true
  94. }
  95. viewType := ctx.Query("type")
  96. sortType := ctx.Query("sort")
  97. types := []string{"all", "assigned", "created_by", "mentioned"}
  98. if !com.IsSliceContainsStr(types, viewType) {
  99. viewType = "all"
  100. }
  101. var (
  102. assigneeID = ctx.QueryInt64("assignee")
  103. posterID int64
  104. mentionedID int64
  105. forceEmpty bool
  106. )
  107. if ctx.IsSigned {
  108. switch viewType {
  109. case "created_by":
  110. posterID = ctx.User.ID
  111. case "mentioned":
  112. mentionedID = ctx.User.ID
  113. }
  114. }
  115. repo := ctx.Repo.Repository
  116. selectLabels := ctx.Query("labels")
  117. milestoneID := ctx.QueryInt64("milestone")
  118. isShowClosed := ctx.Query("state") == "closed"
  119. keyword := strings.Trim(ctx.Query("q"), " ")
  120. if bytes.Contains([]byte(keyword), []byte{0x00}) {
  121. keyword = ""
  122. }
  123. var issueIDs []int64
  124. var err error
  125. if len(keyword) > 0 {
  126. issueIDs, err = indexer.SearchIssuesByKeyword(repo.ID, keyword)
  127. if len(issueIDs) == 0 {
  128. forceEmpty = true
  129. }
  130. }
  131. var issueStats *models.IssueStats
  132. if forceEmpty {
  133. issueStats = &models.IssueStats{}
  134. } else {
  135. var err error
  136. issueStats, err = models.GetIssueStats(&models.IssueStatsOptions{
  137. RepoID: repo.ID,
  138. Labels: selectLabels,
  139. MilestoneID: milestoneID,
  140. AssigneeID: assigneeID,
  141. MentionedID: mentionedID,
  142. PosterID: posterID,
  143. IsPull: isPullList,
  144. IssueIDs: issueIDs,
  145. })
  146. if err != nil {
  147. ctx.Handle(500, "GetIssueStats", err)
  148. return
  149. }
  150. }
  151. page := ctx.QueryInt("page")
  152. if page <= 1 {
  153. page = 1
  154. }
  155. var total int
  156. if !isShowClosed {
  157. total = int(issueStats.OpenCount)
  158. } else {
  159. total = int(issueStats.ClosedCount)
  160. }
  161. pager := paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  162. ctx.Data["Page"] = pager
  163. var issues []*models.Issue
  164. if forceEmpty {
  165. issues = []*models.Issue{}
  166. } else {
  167. issues, err = models.Issues(&models.IssuesOptions{
  168. AssigneeID: assigneeID,
  169. RepoID: repo.ID,
  170. PosterID: posterID,
  171. MentionedID: mentionedID,
  172. MilestoneID: milestoneID,
  173. Page: pager.Current(),
  174. PageSize: setting.UI.IssuePagingNum,
  175. IsClosed: util.OptionalBoolOf(isShowClosed),
  176. IsPull: util.OptionalBoolOf(isPullList),
  177. Labels: selectLabels,
  178. SortType: sortType,
  179. IssueIDs: issueIDs,
  180. })
  181. if err != nil {
  182. ctx.Handle(500, "Issues", err)
  183. return
  184. }
  185. }
  186. // Get posters.
  187. for i := range issues {
  188. // Check read status
  189. if !ctx.IsSigned {
  190. issues[i].IsRead = true
  191. } else if err = issues[i].GetIsRead(ctx.User.ID); err != nil {
  192. ctx.Handle(500, "GetIsRead", err)
  193. return
  194. }
  195. }
  196. ctx.Data["Issues"] = issues
  197. // Get milestones.
  198. ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(repo.ID)
  199. if err != nil {
  200. ctx.Handle(500, "GetAllRepoMilestones", err)
  201. return
  202. }
  203. // Get assignees.
  204. ctx.Data["Assignees"], err = repo.GetAssignees()
  205. if err != nil {
  206. ctx.Handle(500, "GetAssignees", err)
  207. return
  208. }
  209. if ctx.QueryInt64("assignee") == 0 {
  210. assigneeID = 0 // Reset ID to prevent unexpected selection of assignee.
  211. }
  212. ctx.Data["IssueStats"] = issueStats
  213. ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
  214. ctx.Data["ViewType"] = viewType
  215. ctx.Data["SortType"] = sortType
  216. ctx.Data["MilestoneID"] = milestoneID
  217. ctx.Data["AssigneeID"] = assigneeID
  218. ctx.Data["IsShowClosed"] = isShowClosed
  219. ctx.Data["Keyword"] = keyword
  220. if isShowClosed {
  221. ctx.Data["State"] = "closed"
  222. } else {
  223. ctx.Data["State"] = "open"
  224. }
  225. ctx.HTML(200, tplIssues)
  226. }
  227. // RetrieveRepoMilestonesAndAssignees find all the milestones and assignees of a repository
  228. func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repository) {
  229. var err error
  230. ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false, "")
  231. if err != nil {
  232. ctx.Handle(500, "GetMilestones", err)
  233. return
  234. }
  235. ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true, "")
  236. if err != nil {
  237. ctx.Handle(500, "GetMilestones", err)
  238. return
  239. }
  240. ctx.Data["Assignees"], err = repo.GetAssignees()
  241. if err != nil {
  242. ctx.Handle(500, "GetAssignees", err)
  243. return
  244. }
  245. }
  246. // RetrieveRepoMetas find all the meta information of a repository
  247. func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models.Label {
  248. if !ctx.Repo.IsWriter() {
  249. return nil
  250. }
  251. labels, err := models.GetLabelsByRepoID(repo.ID, "")
  252. if err != nil {
  253. ctx.Handle(500, "GetLabelsByRepoID", err)
  254. return nil
  255. }
  256. ctx.Data["Labels"] = labels
  257. RetrieveRepoMilestonesAndAssignees(ctx, repo)
  258. if ctx.Written() {
  259. return nil
  260. }
  261. brs, err := ctx.Repo.GitRepo.GetBranches()
  262. if err != nil {
  263. ctx.Handle(500, "GetBranches", err)
  264. return nil
  265. }
  266. ctx.Data["Branches"] = brs
  267. return labels
  268. }
  269. func getFileContentFromDefaultBranch(ctx *context.Context, filename string) (string, bool) {
  270. var r io.Reader
  271. var bytes []byte
  272. if ctx.Repo.Commit == nil {
  273. var err error
  274. ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
  275. if err != nil {
  276. return "", false
  277. }
  278. }
  279. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(filename)
  280. if err != nil {
  281. return "", false
  282. }
  283. if entry.Blob().Size() >= setting.UI.MaxDisplayFileSize {
  284. return "", false
  285. }
  286. r, err = entry.Blob().Data()
  287. if err != nil {
  288. return "", false
  289. }
  290. bytes, err = ioutil.ReadAll(r)
  291. if err != nil {
  292. return "", false
  293. }
  294. return string(bytes), true
  295. }
  296. func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleFiles []string) {
  297. for _, filename := range possibleFiles {
  298. content, found := getFileContentFromDefaultBranch(ctx, filename)
  299. if found {
  300. ctx.Data[ctxDataKey] = content
  301. return
  302. }
  303. }
  304. }
  305. // NewIssue render createing issue page
  306. func NewIssue(ctx *context.Context) {
  307. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  308. ctx.Data["PageIsIssueList"] = true
  309. ctx.Data["RequireHighlightJS"] = true
  310. ctx.Data["RequireSimpleMDE"] = true
  311. setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates)
  312. renderAttachmentSettings(ctx)
  313. RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  314. if ctx.Written() {
  315. return
  316. }
  317. ctx.HTML(200, tplIssueNew)
  318. }
  319. // ValidateRepoMetas check and returns repository's meta informations
  320. func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm) ([]int64, int64, int64) {
  321. var (
  322. repo = ctx.Repo.Repository
  323. err error
  324. )
  325. labels := RetrieveRepoMetas(ctx, ctx.Repo.Repository)
  326. if ctx.Written() {
  327. return nil, 0, 0
  328. }
  329. if !ctx.Repo.IsWriter() {
  330. return nil, 0, 0
  331. }
  332. var labelIDs []int64
  333. hasSelected := false
  334. // Check labels.
  335. if len(form.LabelIDs) > 0 {
  336. labelIDs, err = base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
  337. if err != nil {
  338. return nil, 0, 0
  339. }
  340. labelIDMark := base.Int64sToMap(labelIDs)
  341. for i := range labels {
  342. if labelIDMark[labels[i].ID] {
  343. labels[i].IsChecked = true
  344. hasSelected = true
  345. }
  346. }
  347. }
  348. ctx.Data["Labels"] = labels
  349. ctx.Data["HasSelectedLabel"] = hasSelected
  350. ctx.Data["label_ids"] = form.LabelIDs
  351. // Check milestone.
  352. milestoneID := form.MilestoneID
  353. if milestoneID > 0 {
  354. ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID)
  355. if err != nil {
  356. ctx.Handle(500, "GetMilestoneByID", err)
  357. return nil, 0, 0
  358. }
  359. ctx.Data["milestone_id"] = milestoneID
  360. }
  361. // Check assignee.
  362. assigneeID := form.AssigneeID
  363. if assigneeID > 0 {
  364. ctx.Data["Assignee"], err = repo.GetAssigneeByID(assigneeID)
  365. if err != nil {
  366. ctx.Handle(500, "GetAssigneeByID", err)
  367. return nil, 0, 0
  368. }
  369. ctx.Data["assignee_id"] = assigneeID
  370. }
  371. return labelIDs, milestoneID, assigneeID
  372. }
  373. // NewIssuePost response for creating new issue
  374. func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
  375. ctx.Data["Title"] = ctx.Tr("repo.issues.new")
  376. ctx.Data["PageIsIssueList"] = true
  377. ctx.Data["RequireHighlightJS"] = true
  378. ctx.Data["RequireSimpleMDE"] = true
  379. ctx.Data["ReadOnly"] = false
  380. renderAttachmentSettings(ctx)
  381. var (
  382. repo = ctx.Repo.Repository
  383. attachments []string
  384. )
  385. labelIDs, milestoneID, assigneeID := ValidateRepoMetas(ctx, form)
  386. if ctx.Written() {
  387. return
  388. }
  389. if setting.AttachmentEnabled {
  390. attachments = form.Files
  391. }
  392. if ctx.HasError() {
  393. ctx.HTML(200, tplIssueNew)
  394. return
  395. }
  396. issue := &models.Issue{
  397. RepoID: repo.ID,
  398. Title: form.Title,
  399. PosterID: ctx.User.ID,
  400. Poster: ctx.User,
  401. MilestoneID: milestoneID,
  402. AssigneeID: assigneeID,
  403. Content: form.Content,
  404. Ref: form.Ref,
  405. }
  406. if err := models.NewIssue(repo, issue, labelIDs, attachments); err != nil {
  407. ctx.Handle(500, "NewIssue", err)
  408. return
  409. }
  410. notification.Service.NotifyIssue(issue, ctx.User.ID)
  411. log.Trace("Issue created: %d/%d", repo.ID, issue.ID)
  412. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  413. }
  414. // ViewIssue render issue view page
  415. func ViewIssue(ctx *context.Context) {
  416. ctx.Data["RequireHighlightJS"] = true
  417. ctx.Data["RequireDropzone"] = true
  418. renderAttachmentSettings(ctx)
  419. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  420. if err != nil {
  421. if models.IsErrIssueNotExist(err) {
  422. ctx.Handle(404, "GetIssueByIndex", err)
  423. } else {
  424. ctx.Handle(500, "GetIssueByIndex", err)
  425. }
  426. return
  427. }
  428. ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
  429. var iw *models.IssueWatch
  430. var exists bool
  431. if ctx.User != nil {
  432. iw, exists, err = models.GetIssueWatch(ctx.User.ID, issue.ID)
  433. if err != nil {
  434. ctx.Handle(500, "GetIssueWatch", err)
  435. return
  436. }
  437. if !exists {
  438. iw = &models.IssueWatch{
  439. UserID: ctx.User.ID,
  440. IssueID: issue.ID,
  441. IsWatching: models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID),
  442. }
  443. }
  444. }
  445. ctx.Data["IssueWatch"] = iw
  446. // Make sure type and URL matches.
  447. if ctx.Params(":type") == "issues" && issue.IsPull {
  448. ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))
  449. return
  450. } else if ctx.Params(":type") == "pulls" && !issue.IsPull {
  451. ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index))
  452. return
  453. }
  454. if issue.IsPull {
  455. MustAllowPulls(ctx)
  456. if ctx.Written() {
  457. return
  458. }
  459. ctx.Data["PageIsPullList"] = true
  460. ctx.Data["PageIsPullConversation"] = true
  461. } else {
  462. MustEnableIssues(ctx)
  463. if ctx.Written() {
  464. return
  465. }
  466. ctx.Data["PageIsIssueList"] = true
  467. }
  468. issue.RenderedContent = string(markdown.Render([]byte(issue.Content), ctx.Repo.RepoLink,
  469. ctx.Repo.Repository.ComposeMetas()))
  470. repo := ctx.Repo.Repository
  471. // Get more information if it's a pull request.
  472. if issue.IsPull {
  473. if issue.PullRequest.HasMerged {
  474. ctx.Data["DisableStatusChange"] = issue.PullRequest.HasMerged
  475. PrepareMergedViewPullInfo(ctx, issue)
  476. } else {
  477. PrepareViewPullInfo(ctx, issue)
  478. }
  479. if ctx.Written() {
  480. return
  481. }
  482. }
  483. // Metas.
  484. // Check labels.
  485. labelIDMark := make(map[int64]bool)
  486. for i := range issue.Labels {
  487. labelIDMark[issue.Labels[i].ID] = true
  488. }
  489. labels, err := models.GetLabelsByRepoID(repo.ID, "")
  490. if err != nil {
  491. ctx.Handle(500, "GetLabelsByRepoID", err)
  492. return
  493. }
  494. hasSelected := false
  495. for i := range labels {
  496. if labelIDMark[labels[i].ID] {
  497. labels[i].IsChecked = true
  498. hasSelected = true
  499. }
  500. }
  501. ctx.Data["HasSelectedLabel"] = hasSelected
  502. ctx.Data["Labels"] = labels
  503. // Check milestone and assignee.
  504. if ctx.Repo.IsWriter() {
  505. RetrieveRepoMilestonesAndAssignees(ctx, repo)
  506. if ctx.Written() {
  507. return
  508. }
  509. }
  510. if ctx.IsSigned {
  511. // Update issue-user.
  512. if err = issue.ReadBy(ctx.User.ID); err != nil {
  513. ctx.Handle(500, "ReadBy", err)
  514. return
  515. }
  516. }
  517. var (
  518. tag models.CommentTag
  519. ok bool
  520. marked = make(map[int64]models.CommentTag)
  521. comment *models.Comment
  522. participants = make([]*models.User, 1, 10)
  523. )
  524. if ctx.Repo.Repository.IsTimetrackerEnabled() {
  525. if ctx.IsSigned {
  526. // Deal with the stopwatch
  527. ctx.Data["IsStopwatchRunning"] = models.StopwatchExists(ctx.User.ID, issue.ID)
  528. if !ctx.Data["IsStopwatchRunning"].(bool) {
  529. var exists bool
  530. var sw *models.Stopwatch
  531. if exists, sw, err = models.HasUserStopwatch(ctx.User.ID); err != nil {
  532. ctx.Handle(500, "HasUserStopwatch", err)
  533. return
  534. }
  535. ctx.Data["HasUserStopwatch"] = exists
  536. if exists {
  537. // Add warning if the user has already a stopwatch
  538. var otherIssue *models.Issue
  539. if otherIssue, err = models.GetIssueByID(sw.IssueID); err != nil {
  540. ctx.Handle(500, "GetIssueByID", err)
  541. return
  542. }
  543. // Add link to the issue of the already running stopwatch
  544. ctx.Data["OtherStopwatchURL"] = otherIssue.HTMLURL()
  545. }
  546. }
  547. ctx.Data["CanUseTimetracker"] = ctx.Repo.CanUseTimetracker(issue, ctx.User)
  548. } else {
  549. ctx.Data["CanUseTimetracker"] = false
  550. }
  551. if ctx.Data["WorkingUsers"], err = models.TotalTimes(models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil {
  552. ctx.Handle(500, "TotalTimes", err)
  553. return
  554. }
  555. }
  556. // Render comments and and fetch participants.
  557. participants[0] = issue.Poster
  558. for _, comment = range issue.Comments {
  559. if comment.Type == models.CommentTypeComment {
  560. comment.RenderedContent = string(markdown.Render([]byte(comment.Content), ctx.Repo.RepoLink,
  561. ctx.Repo.Repository.ComposeMetas()))
  562. // Check tag.
  563. tag, ok = marked[comment.PosterID]
  564. if ok {
  565. comment.ShowTag = tag
  566. continue
  567. }
  568. if repo.IsOwnedBy(comment.PosterID) ||
  569. (repo.Owner.IsOrganization() && repo.Owner.IsOwnedBy(comment.PosterID)) {
  570. comment.ShowTag = models.CommentTagOwner
  571. } else if comment.Poster.IsWriterOfRepo(repo) {
  572. comment.ShowTag = models.CommentTagWriter
  573. } else if comment.PosterID == issue.PosterID {
  574. comment.ShowTag = models.CommentTagPoster
  575. }
  576. marked[comment.PosterID] = comment.ShowTag
  577. isAdded := false
  578. for j := range participants {
  579. if comment.Poster == participants[j] {
  580. isAdded = true
  581. break
  582. }
  583. }
  584. if !isAdded && !issue.IsPoster(comment.Poster.ID) {
  585. participants = append(participants, comment.Poster)
  586. }
  587. } else if comment.Type == models.CommentTypeLabel {
  588. if err = comment.LoadLabel(); err != nil {
  589. ctx.Handle(500, "LoadLabel", err)
  590. return
  591. }
  592. } else if comment.Type == models.CommentTypeMilestone {
  593. if err = comment.LoadMilestone(); err != nil {
  594. ctx.Handle(500, "LoadMilestone", err)
  595. return
  596. }
  597. ghostMilestone := &models.Milestone{
  598. ID: -1,
  599. Name: ctx.Tr("repo.issues.deleted_milestone"),
  600. }
  601. if comment.OldMilestoneID > 0 && comment.OldMilestone == nil {
  602. comment.OldMilestone = ghostMilestone
  603. }
  604. if comment.MilestoneID > 0 && comment.Milestone == nil {
  605. comment.Milestone = ghostMilestone
  606. }
  607. } else if comment.Type == models.CommentTypeAssignees {
  608. if err = comment.LoadAssignees(); err != nil {
  609. ctx.Handle(500, "LoadAssignees", err)
  610. return
  611. }
  612. }
  613. }
  614. if issue.IsPull {
  615. pull := issue.PullRequest
  616. canDelete := false
  617. if ctx.IsSigned {
  618. if err := pull.GetHeadRepo(); err != nil {
  619. log.Error(4, "GetHeadRepo: %v", err)
  620. } else if pull.HeadRepo != nil && pull.HeadBranch != pull.HeadRepo.DefaultBranch && ctx.User.IsWriterOfRepo(pull.HeadRepo) {
  621. // Check if branch is not protected
  622. if protected, err := pull.HeadRepo.IsProtectedBranch(pull.HeadBranch, ctx.User); err != nil {
  623. log.Error(4, "IsProtectedBranch: %v", err)
  624. } else if !protected {
  625. canDelete = true
  626. ctx.Data["DeleteBranchLink"] = ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index) + "/cleanup"
  627. }
  628. }
  629. }
  630. ctx.Data["IsPullBranchDeletable"] = canDelete && pull.HeadRepo != nil && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch)
  631. }
  632. ctx.Data["Participants"] = participants
  633. ctx.Data["NumParticipants"] = len(participants)
  634. ctx.Data["Issue"] = issue
  635. ctx.Data["ReadOnly"] = true
  636. ctx.Data["IsIssueOwner"] = ctx.Repo.IsWriter() || (ctx.IsSigned && issue.IsPoster(ctx.User.ID))
  637. ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login?redirect_to=" + ctx.Data["Link"].(string)
  638. ctx.HTML(200, tplIssueView)
  639. }
  640. // GetActionIssue will return the issue which is used in the context.
  641. func GetActionIssue(ctx *context.Context) *models.Issue {
  642. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  643. if err != nil {
  644. ctx.NotFoundOrServerError("GetIssueByIndex", models.IsErrIssueNotExist, err)
  645. return nil
  646. }
  647. if issue.IsPull && !ctx.Repo.Repository.UnitEnabled(models.UnitTypePullRequests) ||
  648. !issue.IsPull && !ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues) {
  649. ctx.Handle(404, "IssueOrPullRequestUnitNotAllowed", nil)
  650. return nil
  651. }
  652. if err = issue.LoadAttributes(); err != nil {
  653. ctx.Handle(500, "LoadAttributes", nil)
  654. return nil
  655. }
  656. return issue
  657. }
  658. func getActionIssues(ctx *context.Context) []*models.Issue {
  659. commaSeparatedIssueIDs := ctx.Query("issue_ids")
  660. if len(commaSeparatedIssueIDs) == 0 {
  661. return nil
  662. }
  663. issueIDs := make([]int64, 0, 10)
  664. for _, stringIssueID := range strings.Split(commaSeparatedIssueIDs, ",") {
  665. issueID, err := strconv.ParseInt(stringIssueID, 10, 64)
  666. if err != nil {
  667. ctx.Handle(500, "ParseInt", err)
  668. return nil
  669. }
  670. issueIDs = append(issueIDs, issueID)
  671. }
  672. issues, err := models.GetIssuesByIDs(issueIDs)
  673. if err != nil {
  674. ctx.Handle(500, "GetIssuesByIDs", err)
  675. return nil
  676. }
  677. // Check access rights for all issues
  678. issueUnitEnabled := ctx.Repo.Repository.UnitEnabled(models.UnitTypeIssues)
  679. prUnitEnabled := ctx.Repo.Repository.UnitEnabled(models.UnitTypePullRequests)
  680. for _, issue := range issues {
  681. if issue.IsPull && !prUnitEnabled || !issue.IsPull && !issueUnitEnabled {
  682. ctx.Handle(404, "IssueOrPullRequestUnitNotAllowed", nil)
  683. return nil
  684. }
  685. if err = issue.LoadAttributes(); err != nil {
  686. ctx.Handle(500, "LoadAttributes", nil)
  687. return nil
  688. }
  689. }
  690. return issues
  691. }
  692. // UpdateIssueTitle change issue's title
  693. func UpdateIssueTitle(ctx *context.Context) {
  694. issue := GetActionIssue(ctx)
  695. if ctx.Written() {
  696. return
  697. }
  698. if !ctx.IsSigned || (!issue.IsPoster(ctx.User.ID) && !ctx.Repo.IsWriter()) {
  699. ctx.Error(403)
  700. return
  701. }
  702. title := ctx.QueryTrim("title")
  703. if len(title) == 0 {
  704. ctx.Error(204)
  705. return
  706. }
  707. if err := issue.ChangeTitle(ctx.User, title); err != nil {
  708. ctx.Handle(500, "ChangeTitle", err)
  709. return
  710. }
  711. ctx.JSON(200, map[string]interface{}{
  712. "title": issue.Title,
  713. })
  714. }
  715. // UpdateIssueContent change issue's content
  716. func UpdateIssueContent(ctx *context.Context) {
  717. issue := GetActionIssue(ctx)
  718. if ctx.Written() {
  719. return
  720. }
  721. if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.IsWriter()) {
  722. ctx.Error(403)
  723. return
  724. }
  725. content := ctx.Query("content")
  726. if err := issue.ChangeContent(ctx.User, content); err != nil {
  727. ctx.Handle(500, "ChangeContent", err)
  728. return
  729. }
  730. ctx.JSON(200, map[string]interface{}{
  731. "content": string(markdown.Render([]byte(issue.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
  732. })
  733. }
  734. // UpdateIssueMilestone change issue's milestone
  735. func UpdateIssueMilestone(ctx *context.Context) {
  736. issues := getActionIssues(ctx)
  737. if ctx.Written() {
  738. return
  739. }
  740. milestoneID := ctx.QueryInt64("id")
  741. for _, issue := range issues {
  742. oldMilestoneID := issue.MilestoneID
  743. if oldMilestoneID == milestoneID {
  744. continue
  745. }
  746. issue.MilestoneID = milestoneID
  747. if err := models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  748. ctx.Handle(500, "ChangeMilestoneAssign", err)
  749. return
  750. }
  751. }
  752. ctx.JSON(200, map[string]interface{}{
  753. "ok": true,
  754. })
  755. }
  756. // UpdateIssueAssignee change issue's assignee
  757. func UpdateIssueAssignee(ctx *context.Context) {
  758. issues := getActionIssues(ctx)
  759. if ctx.Written() {
  760. return
  761. }
  762. assigneeID := ctx.QueryInt64("id")
  763. for _, issue := range issues {
  764. if issue.AssigneeID == assigneeID {
  765. continue
  766. }
  767. if err := issue.ChangeAssignee(ctx.User, assigneeID); err != nil {
  768. ctx.Handle(500, "ChangeAssignee", err)
  769. return
  770. }
  771. }
  772. ctx.JSON(200, map[string]interface{}{
  773. "ok": true,
  774. })
  775. }
  776. // UpdateIssueStatus change issue's status
  777. func UpdateIssueStatus(ctx *context.Context) {
  778. issues := getActionIssues(ctx)
  779. if ctx.Written() {
  780. return
  781. }
  782. var isClosed bool
  783. switch action := ctx.Query("action"); action {
  784. case "open":
  785. isClosed = false
  786. case "close":
  787. isClosed = true
  788. default:
  789. log.Warn("Unrecognized action: %s", action)
  790. }
  791. if _, err := models.IssueList(issues).LoadRepositories(); err != nil {
  792. ctx.Handle(500, "LoadRepositories", err)
  793. return
  794. }
  795. for _, issue := range issues {
  796. if err := issue.ChangeStatus(ctx.User, issue.Repo, isClosed); err != nil {
  797. ctx.Handle(500, "ChangeStatus", err)
  798. return
  799. }
  800. }
  801. ctx.JSON(200, map[string]interface{}{
  802. "ok": true,
  803. })
  804. }
  805. // NewComment create a comment for issue
  806. func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
  807. issue := GetActionIssue(ctx)
  808. if ctx.Written() {
  809. return
  810. }
  811. var attachments []string
  812. if setting.AttachmentEnabled {
  813. attachments = form.Files
  814. }
  815. if ctx.HasError() {
  816. ctx.Flash.Error(ctx.Data["ErrorMsg"].(string))
  817. ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index))
  818. return
  819. }
  820. var comment *models.Comment
  821. defer func() {
  822. // Check if issue admin/poster changes the status of issue.
  823. if (ctx.Repo.IsWriter() || (ctx.IsSigned && issue.IsPoster(ctx.User.ID))) &&
  824. (form.Status == "reopen" || form.Status == "close") &&
  825. !(issue.IsPull && issue.PullRequest.HasMerged) {
  826. // Duplication and conflict check should apply to reopen pull request.
  827. var pr *models.PullRequest
  828. if form.Status == "reopen" && issue.IsPull {
  829. pull := issue.PullRequest
  830. pr, err := models.GetUnmergedPullRequest(pull.HeadRepoID, pull.BaseRepoID, pull.HeadBranch, pull.BaseBranch)
  831. if err != nil {
  832. if !models.IsErrPullRequestNotExist(err) {
  833. ctx.Handle(500, "GetUnmergedPullRequest", err)
  834. return
  835. }
  836. }
  837. // Regenerate patch and test conflict.
  838. if pr == nil {
  839. if err = issue.PullRequest.UpdatePatch(); err != nil {
  840. ctx.Handle(500, "UpdatePatch", err)
  841. return
  842. }
  843. issue.PullRequest.AddToTaskQueue()
  844. }
  845. }
  846. if pr != nil {
  847. ctx.Flash.Info(ctx.Tr("repo.pulls.open_unmerged_pull_exists", pr.Index))
  848. } else {
  849. if err := issue.ChangeStatus(ctx.User, ctx.Repo.Repository, form.Status == "close"); err != nil {
  850. log.Error(4, "ChangeStatus: %v", err)
  851. } else {
  852. log.Trace("Issue [%d] status changed to closed: %v", issue.ID, issue.IsClosed)
  853. notification.Service.NotifyIssue(issue, ctx.User.ID)
  854. }
  855. }
  856. }
  857. // Redirect to comment hashtag if there is any actual content.
  858. typeName := "issues"
  859. if issue.IsPull {
  860. typeName = "pulls"
  861. }
  862. if comment != nil {
  863. ctx.Redirect(fmt.Sprintf("%s/%s/%d#%s", ctx.Repo.RepoLink, typeName, issue.Index, comment.HashTag()))
  864. } else {
  865. ctx.Redirect(fmt.Sprintf("%s/%s/%d", ctx.Repo.RepoLink, typeName, issue.Index))
  866. }
  867. }()
  868. // Fix #321: Allow empty comments, as long as we have attachments.
  869. if len(form.Content) == 0 && len(attachments) == 0 {
  870. return
  871. }
  872. comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
  873. if err != nil {
  874. ctx.Handle(500, "CreateIssueComment", err)
  875. return
  876. }
  877. notification.Service.NotifyIssue(issue, ctx.User.ID)
  878. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  879. }
  880. // UpdateCommentContent change comment of issue's content
  881. func UpdateCommentContent(ctx *context.Context) {
  882. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  883. if err != nil {
  884. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  885. return
  886. }
  887. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  888. ctx.Error(403)
  889. return
  890. } else if comment.Type != models.CommentTypeComment {
  891. ctx.Error(204)
  892. return
  893. }
  894. comment.Content = ctx.Query("content")
  895. if len(comment.Content) == 0 {
  896. ctx.JSON(200, map[string]interface{}{
  897. "content": "",
  898. })
  899. return
  900. }
  901. if err = models.UpdateComment(comment); err != nil {
  902. ctx.Handle(500, "UpdateComment", err)
  903. return
  904. }
  905. ctx.JSON(200, map[string]interface{}{
  906. "content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
  907. })
  908. }
  909. // DeleteComment delete comment of issue
  910. func DeleteComment(ctx *context.Context) {
  911. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  912. if err != nil {
  913. ctx.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  914. return
  915. }
  916. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  917. ctx.Error(403)
  918. return
  919. } else if comment.Type != models.CommentTypeComment {
  920. ctx.Error(204)
  921. return
  922. }
  923. if err = models.DeleteComment(comment); err != nil {
  924. ctx.Handle(500, "DeleteCommentByID", err)
  925. return
  926. }
  927. ctx.Status(200)
  928. }
  929. // Milestones render milestones page
  930. func Milestones(ctx *context.Context) {
  931. ctx.Data["Title"] = ctx.Tr("repo.milestones")
  932. ctx.Data["PageIsIssueList"] = true
  933. ctx.Data["PageIsMilestones"] = true
  934. isShowClosed := ctx.Query("state") == "closed"
  935. openCount, closedCount := models.MilestoneStats(ctx.Repo.Repository.ID)
  936. ctx.Data["OpenCount"] = openCount
  937. ctx.Data["ClosedCount"] = closedCount
  938. sortType := ctx.Query("sort")
  939. page := ctx.QueryInt("page")
  940. if page <= 1 {
  941. page = 1
  942. }
  943. var total int
  944. if !isShowClosed {
  945. total = int(openCount)
  946. } else {
  947. total = int(closedCount)
  948. }
  949. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  950. miles, err := models.GetMilestones(ctx.Repo.Repository.ID, page, isShowClosed, sortType)
  951. if err != nil {
  952. ctx.Handle(500, "GetMilestones", err)
  953. return
  954. }
  955. for _, m := range miles {
  956. m.RenderedContent = string(markdown.Render([]byte(m.Content), ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas()))
  957. }
  958. ctx.Data["Milestones"] = miles
  959. if isShowClosed {
  960. ctx.Data["State"] = "closed"
  961. } else {
  962. ctx.Data["State"] = "open"
  963. }
  964. ctx.Data["SortType"] = sortType
  965. ctx.Data["IsShowClosed"] = isShowClosed
  966. ctx.HTML(200, tplMilestone)
  967. }
  968. // NewMilestone render creating milestone page
  969. func NewMilestone(ctx *context.Context) {
  970. ctx.Data["Title"] = ctx.Tr("repo.milestones.new")
  971. ctx.Data["PageIsIssueList"] = true
  972. ctx.Data["PageIsMilestones"] = true
  973. ctx.Data["RequireDatetimepicker"] = true
  974. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  975. ctx.HTML(200, tplMilestoneNew)
  976. }
  977. // NewMilestonePost response for creating milestone
  978. func NewMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) {
  979. ctx.Data["Title"] = ctx.Tr("repo.milestones.new")
  980. ctx.Data["PageIsIssueList"] = true
  981. ctx.Data["PageIsMilestones"] = true
  982. ctx.Data["RequireDatetimepicker"] = true
  983. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  984. if ctx.HasError() {
  985. ctx.HTML(200, tplMilestoneNew)
  986. return
  987. }
  988. if len(form.Deadline) == 0 {
  989. form.Deadline = "9999-12-31"
  990. }
  991. deadline, err := time.ParseInLocation("2006-01-02", form.Deadline, time.Local)
  992. if err != nil {
  993. ctx.Data["Err_Deadline"] = true
  994. ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), tplMilestoneNew, &form)
  995. return
  996. }
  997. if err = models.NewMilestone(&models.Milestone{
  998. RepoID: ctx.Repo.Repository.ID,
  999. Name: form.Title,
  1000. Content: form.Content,
  1001. Deadline: deadline,
  1002. }); err != nil {
  1003. ctx.Handle(500, "NewMilestone", err)
  1004. return
  1005. }
  1006. ctx.Flash.Success(ctx.Tr("repo.milestones.create_success", form.Title))
  1007. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  1008. }
  1009. // EditMilestone render edting milestone page
  1010. func EditMilestone(ctx *context.Context) {
  1011. ctx.Data["Title"] = ctx.Tr("repo.milestones.edit")
  1012. ctx.Data["PageIsMilestones"] = true
  1013. ctx.Data["PageIsEditMilestone"] = true
  1014. ctx.Data["RequireDatetimepicker"] = true
  1015. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  1016. m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  1017. if err != nil {
  1018. if models.IsErrMilestoneNotExist(err) {
  1019. ctx.Handle(404, "", nil)
  1020. } else {
  1021. ctx.Handle(500, "GetMilestoneByRepoID", err)
  1022. }
  1023. return
  1024. }
  1025. ctx.Data["title"] = m.Name
  1026. ctx.Data["content"] = m.Content
  1027. if len(m.DeadlineString) > 0 {
  1028. ctx.Data["deadline"] = m.DeadlineString
  1029. }
  1030. ctx.HTML(200, tplMilestoneNew)
  1031. }
  1032. // EditMilestonePost response for edting milestone
  1033. func EditMilestonePost(ctx *context.Context, form auth.CreateMilestoneForm) {
  1034. ctx.Data["Title"] = ctx.Tr("repo.milestones.edit")
  1035. ctx.Data["PageIsMilestones"] = true
  1036. ctx.Data["PageIsEditMilestone"] = true
  1037. ctx.Data["RequireDatetimepicker"] = true
  1038. ctx.Data["DateLang"] = setting.DateLang(ctx.Locale.Language())
  1039. if ctx.HasError() {
  1040. ctx.HTML(200, tplMilestoneNew)
  1041. return
  1042. }
  1043. if len(form.Deadline) == 0 {
  1044. form.Deadline = "9999-12-31"
  1045. }
  1046. deadline, err := time.ParseInLocation("2006-01-02", form.Deadline, time.Local)
  1047. if err != nil {
  1048. ctx.Data["Err_Deadline"] = true
  1049. ctx.RenderWithErr(ctx.Tr("repo.milestones.invalid_due_date_format"), tplMilestoneNew, &form)
  1050. return
  1051. }
  1052. m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  1053. if err != nil {
  1054. if models.IsErrMilestoneNotExist(err) {
  1055. ctx.Handle(404, "", nil)
  1056. } else {
  1057. ctx.Handle(500, "GetMilestoneByRepoID", err)
  1058. }
  1059. return
  1060. }
  1061. m.Name = form.Title
  1062. m.Content = form.Content
  1063. m.Deadline = deadline
  1064. if err = models.UpdateMilestone(m); err != nil {
  1065. ctx.Handle(500, "UpdateMilestone", err)
  1066. return
  1067. }
  1068. ctx.Flash.Success(ctx.Tr("repo.milestones.edit_success", m.Name))
  1069. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  1070. }
  1071. // ChangeMilestonStatus response for change a milestone's status
  1072. func ChangeMilestonStatus(ctx *context.Context) {
  1073. m, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  1074. if err != nil {
  1075. if models.IsErrMilestoneNotExist(err) {
  1076. ctx.Handle(404, "", err)
  1077. } else {
  1078. ctx.Handle(500, "GetMilestoneByRepoID", err)
  1079. }
  1080. return
  1081. }
  1082. switch ctx.Params(":action") {
  1083. case "open":
  1084. if m.IsClosed {
  1085. if err = models.ChangeMilestoneStatus(m, false); err != nil {
  1086. ctx.Handle(500, "ChangeMilestoneStatus", err)
  1087. return
  1088. }
  1089. }
  1090. ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=open")
  1091. case "close":
  1092. if !m.IsClosed {
  1093. m.ClosedDate = time.Now()
  1094. if err = models.ChangeMilestoneStatus(m, true); err != nil {
  1095. ctx.Handle(500, "ChangeMilestoneStatus", err)
  1096. return
  1097. }
  1098. }
  1099. ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=closed")
  1100. default:
  1101. ctx.Redirect(ctx.Repo.RepoLink + "/milestones")
  1102. }
  1103. }
  1104. // DeleteMilestone delete a milestone
  1105. func DeleteMilestone(ctx *context.Context) {
  1106. if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  1107. ctx.Flash.Error("DeleteMilestoneByRepoID: " + err.Error())
  1108. } else {
  1109. ctx.Flash.Success(ctx.Tr("repo.milestones.deletion_success"))
  1110. }
  1111. ctx.JSON(200, map[string]interface{}{
  1112. "redirect": ctx.Repo.RepoLink + "/milestones",
  1113. })
  1114. }