You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "fmt"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/convert"
  15. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/notification"
  18. "code.gitea.io/gitea/modules/setting"
  19. api "code.gitea.io/gitea/modules/structs"
  20. "code.gitea.io/gitea/modules/timeutil"
  21. "code.gitea.io/gitea/modules/util"
  22. "code.gitea.io/gitea/routers/api/v1/utils"
  23. issue_service "code.gitea.io/gitea/services/issue"
  24. )
  25. // SearchIssues searches for issues across the repositories that the user has access to
  26. func SearchIssues(ctx *context.APIContext) {
  27. // swagger:operation GET /repos/issues/search issue issueSearchIssues
  28. // ---
  29. // summary: Search for issues across the repositories that the user has access to
  30. // produces:
  31. // - application/json
  32. // parameters:
  33. // - name: state
  34. // in: query
  35. // description: whether issue is open or closed
  36. // type: string
  37. // - name: labels
  38. // in: query
  39. // description: comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded
  40. // type: string
  41. // - name: q
  42. // in: query
  43. // description: search string
  44. // type: string
  45. // - name: priority_repo_id
  46. // in: query
  47. // description: repository to prioritize in the results
  48. // type: integer
  49. // format: int64
  50. // - name: type
  51. // in: query
  52. // description: filter by type (issues / pulls) if set
  53. // type: string
  54. // - name: page
  55. // in: query
  56. // description: page number of requested issues
  57. // type: integer
  58. // responses:
  59. // "200":
  60. // "$ref": "#/responses/IssueList"
  61. var isClosed util.OptionalBool
  62. switch ctx.Query("state") {
  63. case "closed":
  64. isClosed = util.OptionalBoolTrue
  65. case "all":
  66. isClosed = util.OptionalBoolNone
  67. default:
  68. isClosed = util.OptionalBoolFalse
  69. }
  70. // find repos user can access (for issue search)
  71. repoIDs := make([]int64, 0)
  72. opts := &models.SearchRepoOptions{
  73. ListOptions: models.ListOptions{
  74. PageSize: 15,
  75. },
  76. Private: false,
  77. AllPublic: true,
  78. TopicOnly: false,
  79. Collaborate: util.OptionalBoolNone,
  80. // This needs to be a column that is not nil in fixtures or
  81. // MySQL will return different results when sorting by null in some cases
  82. OrderBy: models.SearchOrderByAlphabetically,
  83. Actor: ctx.User,
  84. }
  85. if ctx.IsSigned {
  86. opts.Private = true
  87. opts.AllLimited = true
  88. }
  89. issueCount := 0
  90. for page := 1; ; page++ {
  91. opts.Page = page
  92. repos, count, err := models.SearchRepositoryByName(opts)
  93. if err != nil {
  94. ctx.Error(http.StatusInternalServerError, "SearchRepositoryByName", err)
  95. return
  96. }
  97. if len(repos) == 0 {
  98. break
  99. }
  100. log.Trace("Processing next %d repos of %d", len(repos), count)
  101. for _, repo := range repos {
  102. switch isClosed {
  103. case util.OptionalBoolTrue:
  104. issueCount += repo.NumClosedIssues
  105. case util.OptionalBoolFalse:
  106. issueCount += repo.NumOpenIssues
  107. case util.OptionalBoolNone:
  108. issueCount += repo.NumIssues
  109. }
  110. repoIDs = append(repoIDs, repo.ID)
  111. }
  112. }
  113. var issues []*models.Issue
  114. keyword := strings.Trim(ctx.Query("q"), " ")
  115. if strings.IndexByte(keyword, 0) >= 0 {
  116. keyword = ""
  117. }
  118. var issueIDs []int64
  119. var labelIDs []int64
  120. var err error
  121. if len(keyword) > 0 && len(repoIDs) > 0 {
  122. issueIDs, err = issue_indexer.SearchIssuesByKeyword(repoIDs, keyword)
  123. }
  124. var isPull util.OptionalBool
  125. switch ctx.Query("type") {
  126. case "pulls":
  127. isPull = util.OptionalBoolTrue
  128. case "issues":
  129. isPull = util.OptionalBoolFalse
  130. default:
  131. isPull = util.OptionalBoolNone
  132. }
  133. labels := strings.TrimSpace(ctx.Query("labels"))
  134. var includedLabelNames []string
  135. if len(labels) > 0 {
  136. includedLabelNames = strings.Split(labels, ",")
  137. }
  138. // Only fetch the issues if we either don't have a keyword or the search returned issues
  139. // This would otherwise return all issues if no issues were found by the search.
  140. if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
  141. issues, err = models.Issues(&models.IssuesOptions{
  142. ListOptions: models.ListOptions{
  143. Page: ctx.QueryInt("page"),
  144. PageSize: setting.UI.IssuePagingNum,
  145. },
  146. RepoIDs: repoIDs,
  147. IsClosed: isClosed,
  148. IssueIDs: issueIDs,
  149. IncludedLabelNames: includedLabelNames,
  150. SortType: "priorityrepo",
  151. PriorityRepoID: ctx.QueryInt64("priority_repo_id"),
  152. IsPull: isPull,
  153. })
  154. }
  155. if err != nil {
  156. ctx.Error(http.StatusInternalServerError, "Issues", err)
  157. return
  158. }
  159. ctx.SetLinkHeader(issueCount, setting.UI.IssuePagingNum)
  160. ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues))
  161. }
  162. // ListIssues list the issues of a repository
  163. func ListIssues(ctx *context.APIContext) {
  164. // swagger:operation GET /repos/{owner}/{repo}/issues issue issueListIssues
  165. // ---
  166. // summary: List a repository's issues
  167. // produces:
  168. // - application/json
  169. // parameters:
  170. // - name: owner
  171. // in: path
  172. // description: owner of the repo
  173. // type: string
  174. // required: true
  175. // - name: repo
  176. // in: path
  177. // description: name of the repo
  178. // type: string
  179. // required: true
  180. // - name: state
  181. // in: query
  182. // description: whether issue is open or closed
  183. // type: string
  184. // enum: [closed, open, all]
  185. // - name: labels
  186. // in: query
  187. // description: comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded
  188. // type: string
  189. // - name: q
  190. // in: query
  191. // description: search string
  192. // type: string
  193. // - name: type
  194. // in: query
  195. // description: filter by type (issues / pulls) if set
  196. // type: string
  197. // enum: [issues, pulls]
  198. // - name: milestones
  199. // in: query
  200. // description: comma separated list of milestone names or ids. It uses names and fall back to ids. Fetch only issues that have any of this milestones. Non existent milestones are discarded
  201. // type: string
  202. // - name: page
  203. // in: query
  204. // description: page number of results to return (1-based)
  205. // type: integer
  206. // - name: limit
  207. // in: query
  208. // description: page size of results
  209. // type: integer
  210. // responses:
  211. // "200":
  212. // "$ref": "#/responses/IssueList"
  213. var isClosed util.OptionalBool
  214. switch ctx.Query("state") {
  215. case "closed":
  216. isClosed = util.OptionalBoolTrue
  217. case "all":
  218. isClosed = util.OptionalBoolNone
  219. default:
  220. isClosed = util.OptionalBoolFalse
  221. }
  222. var issues []*models.Issue
  223. keyword := strings.Trim(ctx.Query("q"), " ")
  224. if strings.IndexByte(keyword, 0) >= 0 {
  225. keyword = ""
  226. }
  227. var issueIDs []int64
  228. var labelIDs []int64
  229. var err error
  230. if len(keyword) > 0 {
  231. issueIDs, err = issue_indexer.SearchIssuesByKeyword([]int64{ctx.Repo.Repository.ID}, keyword)
  232. }
  233. if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
  234. labelIDs, err = models.GetLabelIDsInRepoByNames(ctx.Repo.Repository.ID, splitted)
  235. if err != nil {
  236. ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err)
  237. return
  238. }
  239. }
  240. var mileIDs []int64
  241. if part := strings.Split(ctx.Query("milestones"), ","); len(part) > 0 {
  242. for i := range part {
  243. // uses names and fall back to ids
  244. // non existent milestones are discarded
  245. mile, err := models.GetMilestoneByRepoIDANDName(ctx.Repo.Repository.ID, part[i])
  246. if err == nil {
  247. mileIDs = append(mileIDs, mile.ID)
  248. continue
  249. }
  250. if !models.IsErrMilestoneNotExist(err) {
  251. ctx.Error(http.StatusInternalServerError, "GetMilestoneByRepoIDANDName", err)
  252. return
  253. }
  254. id, err := strconv.ParseInt(part[i], 10, 64)
  255. if err != nil {
  256. continue
  257. }
  258. mile, err = models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, id)
  259. if err == nil {
  260. mileIDs = append(mileIDs, mile.ID)
  261. continue
  262. }
  263. if models.IsErrMilestoneNotExist(err) {
  264. continue
  265. }
  266. ctx.Error(http.StatusInternalServerError, "GetMilestoneByRepoID", err)
  267. }
  268. }
  269. listOptions := utils.GetListOptions(ctx)
  270. var isPull util.OptionalBool
  271. switch ctx.Query("type") {
  272. case "pulls":
  273. isPull = util.OptionalBoolTrue
  274. case "issues":
  275. isPull = util.OptionalBoolFalse
  276. default:
  277. isPull = util.OptionalBoolNone
  278. }
  279. // Only fetch the issues if we either don't have a keyword or the search returned issues
  280. // This would otherwise return all issues if no issues were found by the search.
  281. if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
  282. issues, err = models.Issues(&models.IssuesOptions{
  283. ListOptions: listOptions,
  284. RepoIDs: []int64{ctx.Repo.Repository.ID},
  285. IsClosed: isClosed,
  286. IssueIDs: issueIDs,
  287. LabelIDs: labelIDs,
  288. MilestoneIDs: mileIDs,
  289. IsPull: isPull,
  290. })
  291. }
  292. if err != nil {
  293. ctx.Error(http.StatusInternalServerError, "Issues", err)
  294. return
  295. }
  296. ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, listOptions.PageSize)
  297. ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", ctx.Repo.Repository.NumIssues))
  298. ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues))
  299. }
  300. // GetIssue get an issue of a repository
  301. func GetIssue(ctx *context.APIContext) {
  302. // swagger:operation GET /repos/{owner}/{repo}/issues/{index} issue issueGetIssue
  303. // ---
  304. // summary: Get an issue
  305. // produces:
  306. // - application/json
  307. // parameters:
  308. // - name: owner
  309. // in: path
  310. // description: owner of the repo
  311. // type: string
  312. // required: true
  313. // - name: repo
  314. // in: path
  315. // description: name of the repo
  316. // type: string
  317. // required: true
  318. // - name: index
  319. // in: path
  320. // description: index of the issue to get
  321. // type: integer
  322. // format: int64
  323. // required: true
  324. // responses:
  325. // "200":
  326. // "$ref": "#/responses/Issue"
  327. // "404":
  328. // "$ref": "#/responses/notFound"
  329. issue, err := models.GetIssueWithAttrsByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  330. if err != nil {
  331. if models.IsErrIssueNotExist(err) {
  332. ctx.NotFound()
  333. } else {
  334. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  335. }
  336. return
  337. }
  338. ctx.JSON(http.StatusOK, convert.ToAPIIssue(issue))
  339. }
  340. // CreateIssue create an issue of a repository
  341. func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
  342. // swagger:operation POST /repos/{owner}/{repo}/issues issue issueCreateIssue
  343. // ---
  344. // summary: Create an issue. If using deadline only the date will be taken into account, and time of day ignored.
  345. // consumes:
  346. // - application/json
  347. // produces:
  348. // - application/json
  349. // parameters:
  350. // - name: owner
  351. // in: path
  352. // description: owner of the repo
  353. // type: string
  354. // required: true
  355. // - name: repo
  356. // in: path
  357. // description: name of the repo
  358. // type: string
  359. // required: true
  360. // - name: body
  361. // in: body
  362. // schema:
  363. // "$ref": "#/definitions/CreateIssueOption"
  364. // responses:
  365. // "201":
  366. // "$ref": "#/responses/Issue"
  367. // "403":
  368. // "$ref": "#/responses/forbidden"
  369. // "412":
  370. // "$ref": "#/responses/error"
  371. // "422":
  372. // "$ref": "#/responses/validationError"
  373. var deadlineUnix timeutil.TimeStamp
  374. if form.Deadline != nil && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  375. deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
  376. }
  377. issue := &models.Issue{
  378. RepoID: ctx.Repo.Repository.ID,
  379. Repo: ctx.Repo.Repository,
  380. Title: form.Title,
  381. PosterID: ctx.User.ID,
  382. Poster: ctx.User,
  383. Content: form.Body,
  384. DeadlineUnix: deadlineUnix,
  385. }
  386. var assigneeIDs = make([]int64, 0)
  387. var err error
  388. if ctx.Repo.CanWrite(models.UnitTypeIssues) {
  389. issue.MilestoneID = form.Milestone
  390. assigneeIDs, err = models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
  391. if err != nil {
  392. if models.IsErrUserNotExist(err) {
  393. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  394. } else {
  395. ctx.Error(http.StatusInternalServerError, "AddAssigneeByName", err)
  396. }
  397. return
  398. }
  399. // Check if the passed assignees is assignable
  400. for _, aID := range assigneeIDs {
  401. assignee, err := models.GetUserByID(aID)
  402. if err != nil {
  403. ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
  404. return
  405. }
  406. valid, err := models.CanBeAssigned(assignee, ctx.Repo.Repository, false)
  407. if err != nil {
  408. ctx.Error(http.StatusInternalServerError, "canBeAssigned", err)
  409. return
  410. }
  411. if !valid {
  412. ctx.Error(http.StatusUnprocessableEntity, "canBeAssigned", models.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: ctx.Repo.Repository.Name})
  413. return
  414. }
  415. }
  416. } else {
  417. // setting labels is not allowed if user is not a writer
  418. form.Labels = make([]int64, 0)
  419. }
  420. if err := issue_service.NewIssue(ctx.Repo.Repository, issue, form.Labels, nil, assigneeIDs); err != nil {
  421. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  422. ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err)
  423. return
  424. }
  425. ctx.Error(http.StatusInternalServerError, "NewIssue", err)
  426. return
  427. }
  428. if form.Closed {
  429. if err := issue_service.ChangeStatus(issue, ctx.User, true); err != nil {
  430. if models.IsErrDependenciesLeft(err) {
  431. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  432. return
  433. }
  434. ctx.Error(http.StatusInternalServerError, "ChangeStatus", err)
  435. return
  436. }
  437. }
  438. // Refetch from database to assign some automatic values
  439. issue, err = models.GetIssueByID(issue.ID)
  440. if err != nil {
  441. ctx.Error(http.StatusInternalServerError, "GetIssueByID", err)
  442. return
  443. }
  444. ctx.JSON(http.StatusCreated, convert.ToAPIIssue(issue))
  445. }
  446. // EditIssue modify an issue of a repository
  447. func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
  448. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index} issue issueEditIssue
  449. // ---
  450. // summary: Edit an issue. If using deadline only the date will be taken into account, and time of day ignored.
  451. // consumes:
  452. // - application/json
  453. // produces:
  454. // - application/json
  455. // parameters:
  456. // - name: owner
  457. // in: path
  458. // description: owner of the repo
  459. // type: string
  460. // required: true
  461. // - name: repo
  462. // in: path
  463. // description: name of the repo
  464. // type: string
  465. // required: true
  466. // - name: index
  467. // in: path
  468. // description: index of the issue to edit
  469. // type: integer
  470. // format: int64
  471. // required: true
  472. // - name: body
  473. // in: body
  474. // schema:
  475. // "$ref": "#/definitions/EditIssueOption"
  476. // responses:
  477. // "201":
  478. // "$ref": "#/responses/Issue"
  479. // "403":
  480. // "$ref": "#/responses/forbidden"
  481. // "404":
  482. // "$ref": "#/responses/notFound"
  483. // "412":
  484. // "$ref": "#/responses/error"
  485. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  486. if err != nil {
  487. if models.IsErrIssueNotExist(err) {
  488. ctx.NotFound()
  489. } else {
  490. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  491. }
  492. return
  493. }
  494. issue.Repo = ctx.Repo.Repository
  495. canWrite := ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
  496. err = issue.LoadAttributes()
  497. if err != nil {
  498. ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
  499. return
  500. }
  501. if !issue.IsPoster(ctx.User.ID) && !canWrite {
  502. ctx.Status(http.StatusForbidden)
  503. return
  504. }
  505. oldTitle := issue.Title
  506. if len(form.Title) > 0 {
  507. issue.Title = form.Title
  508. }
  509. if form.Body != nil {
  510. issue.Content = *form.Body
  511. }
  512. // Update or remove the deadline, only if set and allowed
  513. if (form.Deadline != nil || form.RemoveDeadline != nil) && canWrite {
  514. var deadlineUnix timeutil.TimeStamp
  515. if (form.RemoveDeadline == nil || !*form.RemoveDeadline) && !form.Deadline.IsZero() {
  516. deadline := time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  517. 23, 59, 59, 0, form.Deadline.Location())
  518. deadlineUnix = timeutil.TimeStamp(deadline.Unix())
  519. }
  520. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  521. ctx.Error(http.StatusInternalServerError, "UpdateIssueDeadline", err)
  522. return
  523. }
  524. issue.DeadlineUnix = deadlineUnix
  525. }
  526. // Add/delete assignees
  527. // Deleting is done the GitHub way (quote from their api documentation):
  528. // https://developer.github.com/v3/issues/#edit-an-issue
  529. // "assignees" (array): Logins for Users to assign to this issue.
  530. // Pass one or more user logins to replace the set of assignees on this Issue.
  531. // Send an empty array ([]) to clear all assignees from the Issue.
  532. if canWrite && (form.Assignees != nil || form.Assignee != nil) {
  533. oneAssignee := ""
  534. if form.Assignee != nil {
  535. oneAssignee = *form.Assignee
  536. }
  537. err = issue_service.UpdateAssignees(issue, oneAssignee, form.Assignees, ctx.User)
  538. if err != nil {
  539. ctx.Error(http.StatusInternalServerError, "UpdateAssignees", err)
  540. return
  541. }
  542. }
  543. if canWrite && form.Milestone != nil &&
  544. issue.MilestoneID != *form.Milestone {
  545. oldMilestoneID := issue.MilestoneID
  546. issue.MilestoneID = *form.Milestone
  547. if err = issue_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  548. ctx.Error(http.StatusInternalServerError, "ChangeMilestoneAssign", err)
  549. return
  550. }
  551. }
  552. if form.State != nil {
  553. issue.IsClosed = (api.StateClosed == api.StateType(*form.State))
  554. }
  555. statusChangeComment, titleChanged, err := models.UpdateIssueByAPI(issue, ctx.User)
  556. if err != nil {
  557. if models.IsErrDependenciesLeft(err) {
  558. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  559. return
  560. }
  561. ctx.Error(http.StatusInternalServerError, "UpdateIssueByAPI", err)
  562. return
  563. }
  564. if titleChanged {
  565. notification.NotifyIssueChangeTitle(ctx.User, issue, oldTitle)
  566. }
  567. if statusChangeComment != nil {
  568. notification.NotifyIssueChangeStatus(ctx.User, issue, statusChangeComment, issue.IsClosed)
  569. }
  570. // Refetch from database to assign some automatic values
  571. issue, err = models.GetIssueByID(issue.ID)
  572. if err != nil {
  573. ctx.InternalServerError(err)
  574. return
  575. }
  576. if err = issue.LoadMilestone(); err != nil {
  577. ctx.InternalServerError(err)
  578. return
  579. }
  580. ctx.JSON(http.StatusCreated, convert.ToAPIIssue(issue))
  581. }
  582. // UpdateIssueDeadline updates an issue deadline
  583. func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
  584. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/deadline issue issueEditIssueDeadline
  585. // ---
  586. // summary: Set an issue deadline. If set to null, the deadline is deleted. If using deadline only the date will be taken into account, and time of day ignored.
  587. // consumes:
  588. // - application/json
  589. // produces:
  590. // - application/json
  591. // parameters:
  592. // - name: owner
  593. // in: path
  594. // description: owner of the repo
  595. // type: string
  596. // required: true
  597. // - name: repo
  598. // in: path
  599. // description: name of the repo
  600. // type: string
  601. // required: true
  602. // - name: index
  603. // in: path
  604. // description: index of the issue to create or update a deadline on
  605. // type: integer
  606. // format: int64
  607. // required: true
  608. // - name: body
  609. // in: body
  610. // schema:
  611. // "$ref": "#/definitions/EditDeadlineOption"
  612. // responses:
  613. // "201":
  614. // "$ref": "#/responses/IssueDeadline"
  615. // "403":
  616. // "$ref": "#/responses/forbidden"
  617. // "404":
  618. // "$ref": "#/responses/notFound"
  619. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  620. if err != nil {
  621. if models.IsErrIssueNotExist(err) {
  622. ctx.NotFound()
  623. } else {
  624. ctx.Error(http.StatusInternalServerError, "GetIssueByIndex", err)
  625. }
  626. return
  627. }
  628. if !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull) {
  629. ctx.Error(http.StatusForbidden, "", "Not repo writer")
  630. return
  631. }
  632. var deadlineUnix timeutil.TimeStamp
  633. var deadline time.Time
  634. if form.Deadline != nil && !form.Deadline.IsZero() {
  635. deadline = time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  636. 23, 59, 59, 0, time.Local)
  637. deadlineUnix = timeutil.TimeStamp(deadline.Unix())
  638. }
  639. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  640. ctx.Error(http.StatusInternalServerError, "UpdateIssueDeadline", err)
  641. return
  642. }
  643. ctx.JSON(http.StatusCreated, api.IssueDeadline{Deadline: &deadline})
  644. }