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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. "strings"
  10. "time"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/context"
  13. issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
  14. "code.gitea.io/gitea/modules/notification"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/util"
  17. api "code.gitea.io/gitea/modules/structs"
  18. )
  19. // ListIssues list the issues of a repository
  20. func ListIssues(ctx *context.APIContext) {
  21. // swagger:operation GET /repos/{owner}/{repo}/issues issue issueListIssues
  22. // ---
  23. // summary: List a repository's issues
  24. // produces:
  25. // - application/json
  26. // parameters:
  27. // - name: owner
  28. // in: path
  29. // description: owner of the repo
  30. // type: string
  31. // required: true
  32. // - name: repo
  33. // in: path
  34. // description: name of the repo
  35. // type: string
  36. // required: true
  37. // - name: state
  38. // in: query
  39. // description: whether issue is open or closed
  40. // type: string
  41. // - name: labels
  42. // in: query
  43. // description: comma separated list of labels. Fetch only issues that have any of this labels. Non existent labels are discarded
  44. // type: string
  45. // - name: page
  46. // in: query
  47. // description: page number of requested issues
  48. // type: integer
  49. // - name: q
  50. // in: query
  51. // description: search string
  52. // type: string
  53. // responses:
  54. // "200":
  55. // "$ref": "#/responses/IssueList"
  56. var isClosed util.OptionalBool
  57. switch ctx.Query("state") {
  58. case "closed":
  59. isClosed = util.OptionalBoolTrue
  60. case "all":
  61. isClosed = util.OptionalBoolNone
  62. default:
  63. isClosed = util.OptionalBoolFalse
  64. }
  65. var issues []*models.Issue
  66. keyword := strings.Trim(ctx.Query("q"), " ")
  67. if strings.IndexByte(keyword, 0) >= 0 {
  68. keyword = ""
  69. }
  70. var issueIDs []int64
  71. var labelIDs []int64
  72. var err error
  73. if len(keyword) > 0 {
  74. issueIDs, err = issue_indexer.SearchIssuesByKeyword(ctx.Repo.Repository.ID, keyword)
  75. }
  76. if splitted := strings.Split(ctx.Query("labels"), ","); len(splitted) > 0 {
  77. labelIDs, err = models.GetLabelIDsInRepoByNames(ctx.Repo.Repository.ID, splitted)
  78. if err != nil {
  79. ctx.Error(500, "GetLabelIDsInRepoByNames", err)
  80. return
  81. }
  82. }
  83. // Only fetch the issues if we either don't have a keyword or the search returned issues
  84. // This would otherwise return all issues if no issues were found by the search.
  85. if len(keyword) == 0 || len(issueIDs) > 0 || len(labelIDs) > 0 {
  86. issues, err = models.Issues(&models.IssuesOptions{
  87. RepoIDs: []int64{ctx.Repo.Repository.ID},
  88. Page: ctx.QueryInt("page"),
  89. PageSize: setting.UI.IssuePagingNum,
  90. IsClosed: isClosed,
  91. IssueIDs: issueIDs,
  92. LabelIDs: labelIDs,
  93. })
  94. }
  95. if err != nil {
  96. ctx.Error(500, "Issues", err)
  97. return
  98. }
  99. apiIssues := make([]*api.Issue, len(issues))
  100. for i := range issues {
  101. apiIssues[i] = issues[i].APIFormat()
  102. }
  103. ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, setting.UI.IssuePagingNum)
  104. ctx.JSON(200, &apiIssues)
  105. }
  106. // GetIssue get an issue of a repository
  107. func GetIssue(ctx *context.APIContext) {
  108. // swagger:operation GET /repos/{owner}/{repo}/issues/{index} issue issueGetIssue
  109. // ---
  110. // summary: Get an issue
  111. // produces:
  112. // - application/json
  113. // parameters:
  114. // - name: owner
  115. // in: path
  116. // description: owner of the repo
  117. // type: string
  118. // required: true
  119. // - name: repo
  120. // in: path
  121. // description: name of the repo
  122. // type: string
  123. // required: true
  124. // - name: index
  125. // in: path
  126. // description: index of the issue to get
  127. // type: integer
  128. // format: int64
  129. // required: true
  130. // responses:
  131. // "200":
  132. // "$ref": "#/responses/Issue"
  133. issue, err := models.GetIssueWithAttrsByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  134. if err != nil {
  135. if models.IsErrIssueNotExist(err) {
  136. ctx.NotFound()
  137. } else {
  138. ctx.Error(500, "GetIssueByIndex", err)
  139. }
  140. return
  141. }
  142. ctx.JSON(200, issue.APIFormat())
  143. }
  144. // CreateIssue create an issue of a repository
  145. func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
  146. // swagger:operation POST /repos/{owner}/{repo}/issues issue issueCreateIssue
  147. // ---
  148. // summary: Create an issue. If using deadline only the date will be taken into account, and time of day ignored.
  149. // consumes:
  150. // - application/json
  151. // produces:
  152. // - application/json
  153. // parameters:
  154. // - name: owner
  155. // in: path
  156. // description: owner of the repo
  157. // type: string
  158. // required: true
  159. // - name: repo
  160. // in: path
  161. // description: name of the repo
  162. // type: string
  163. // required: true
  164. // - name: body
  165. // in: body
  166. // schema:
  167. // "$ref": "#/definitions/CreateIssueOption"
  168. // responses:
  169. // "201":
  170. // "$ref": "#/responses/Issue"
  171. var deadlineUnix util.TimeStamp
  172. if form.Deadline != nil && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  173. deadlineUnix = util.TimeStamp(form.Deadline.Unix())
  174. }
  175. issue := &models.Issue{
  176. RepoID: ctx.Repo.Repository.ID,
  177. Repo: ctx.Repo.Repository,
  178. Title: form.Title,
  179. PosterID: ctx.User.ID,
  180. Poster: ctx.User,
  181. Content: form.Body,
  182. DeadlineUnix: deadlineUnix,
  183. }
  184. var assigneeIDs = make([]int64, 0)
  185. var err error
  186. if ctx.Repo.CanWrite(models.UnitTypeIssues) {
  187. issue.MilestoneID = form.Milestone
  188. assigneeIDs, err = models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees)
  189. if err != nil {
  190. if models.IsErrUserNotExist(err) {
  191. ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err))
  192. } else {
  193. ctx.Error(500, "AddAssigneeByName", err)
  194. }
  195. return
  196. }
  197. } else {
  198. // setting labels is not allowed if user is not a writer
  199. form.Labels = make([]int64, 0)
  200. }
  201. if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
  202. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  203. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
  204. return
  205. }
  206. ctx.Error(500, "NewIssue", err)
  207. return
  208. }
  209. notification.NotifyNewIssue(issue)
  210. if form.Closed {
  211. if err := issue.ChangeStatus(ctx.User, true); err != nil {
  212. if models.IsErrDependenciesLeft(err) {
  213. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  214. return
  215. }
  216. ctx.Error(500, "ChangeStatus", err)
  217. return
  218. }
  219. }
  220. // Refetch from database to assign some automatic values
  221. issue, err = models.GetIssueByID(issue.ID)
  222. if err != nil {
  223. ctx.Error(500, "GetIssueByID", err)
  224. return
  225. }
  226. ctx.JSON(201, issue.APIFormat())
  227. }
  228. // EditIssue modify an issue of a repository
  229. func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
  230. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index} issue issueEditIssue
  231. // ---
  232. // summary: Edit an issue. If using deadline only the date will be taken into account, and time of day ignored.
  233. // consumes:
  234. // - application/json
  235. // produces:
  236. // - application/json
  237. // parameters:
  238. // - name: owner
  239. // in: path
  240. // description: owner of the repo
  241. // type: string
  242. // required: true
  243. // - name: repo
  244. // in: path
  245. // description: name of the repo
  246. // type: string
  247. // required: true
  248. // - name: index
  249. // in: path
  250. // description: index of the issue to edit
  251. // type: integer
  252. // format: int64
  253. // required: true
  254. // - name: body
  255. // in: body
  256. // schema:
  257. // "$ref": "#/definitions/EditIssueOption"
  258. // responses:
  259. // "201":
  260. // "$ref": "#/responses/Issue"
  261. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  262. if err != nil {
  263. if models.IsErrIssueNotExist(err) {
  264. ctx.NotFound()
  265. } else {
  266. ctx.Error(500, "GetIssueByIndex", err)
  267. }
  268. return
  269. }
  270. issue.Repo = ctx.Repo.Repository
  271. err = issue.LoadAttributes()
  272. if err != nil {
  273. ctx.Error(500, "LoadAttributes", err)
  274. return
  275. }
  276. if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  277. ctx.Status(403)
  278. return
  279. }
  280. if len(form.Title) > 0 {
  281. issue.Title = form.Title
  282. }
  283. if form.Body != nil {
  284. issue.Content = *form.Body
  285. }
  286. // Update the deadline
  287. if form.Deadline != nil && !form.Deadline.IsZero() && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  288. deadlineUnix := util.TimeStamp(form.Deadline.Unix())
  289. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  290. ctx.Error(500, "UpdateIssueDeadline", err)
  291. return
  292. }
  293. issue.DeadlineUnix = deadlineUnix
  294. }
  295. // Add/delete assignees
  296. // Deleting is done the GitHub way (quote from their api documentation):
  297. // https://developer.github.com/v3/issues/#edit-an-issue
  298. // "assignees" (array): Logins for Users to assign to this issue.
  299. // Pass one or more user logins to replace the set of assignees on this Issue.
  300. // Send an empty array ([]) to clear all assignees from the Issue.
  301. if ctx.Repo.CanWrite(models.UnitTypeIssues) && (form.Assignees != nil || form.Assignee != nil) {
  302. oneAssignee := ""
  303. if form.Assignee != nil {
  304. oneAssignee = *form.Assignee
  305. }
  306. err = models.UpdateAPIAssignee(issue, oneAssignee, form.Assignees, ctx.User)
  307. if err != nil {
  308. ctx.Error(500, "UpdateAPIAssignee", err)
  309. return
  310. }
  311. }
  312. if ctx.Repo.CanWrite(models.UnitTypeIssues) && form.Milestone != nil &&
  313. issue.MilestoneID != *form.Milestone {
  314. oldMilestoneID := issue.MilestoneID
  315. issue.MilestoneID = *form.Milestone
  316. if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  317. ctx.Error(500, "ChangeMilestoneAssign", err)
  318. return
  319. }
  320. }
  321. if err = models.UpdateIssue(issue); err != nil {
  322. ctx.Error(500, "UpdateIssue", err)
  323. return
  324. }
  325. if form.State != nil {
  326. if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
  327. if models.IsErrDependenciesLeft(err) {
  328. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  329. return
  330. }
  331. ctx.Error(500, "ChangeStatus", err)
  332. return
  333. }
  334. notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
  335. }
  336. // Refetch from database to assign some automatic values
  337. issue, err = models.GetIssueByID(issue.ID)
  338. if err != nil {
  339. ctx.Error(500, "GetIssueByID", err)
  340. return
  341. }
  342. ctx.JSON(201, issue.APIFormat())
  343. }
  344. // UpdateIssueDeadline updates an issue deadline
  345. func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
  346. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/deadline issue issueEditIssueDeadline
  347. // ---
  348. // 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.
  349. // consumes:
  350. // - application/json
  351. // produces:
  352. // - application/json
  353. // parameters:
  354. // - name: owner
  355. // in: path
  356. // description: owner of the repo
  357. // type: string
  358. // required: true
  359. // - name: repo
  360. // in: path
  361. // description: name of the repo
  362. // type: string
  363. // required: true
  364. // - name: index
  365. // in: path
  366. // description: index of the issue to create or update a deadline on
  367. // type: integer
  368. // format: int64
  369. // required: true
  370. // - name: body
  371. // in: body
  372. // schema:
  373. // "$ref": "#/definitions/EditDeadlineOption"
  374. // responses:
  375. // "201":
  376. // "$ref": "#/responses/IssueDeadline"
  377. // "403":
  378. // description: Not repo writer
  379. // "404":
  380. // description: Issue not found
  381. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  382. if err != nil {
  383. if models.IsErrIssueNotExist(err) {
  384. ctx.NotFound()
  385. } else {
  386. ctx.Error(500, "GetIssueByIndex", err)
  387. }
  388. return
  389. }
  390. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  391. ctx.Status(403)
  392. return
  393. }
  394. var deadlineUnix util.TimeStamp
  395. var deadline time.Time
  396. if form.Deadline != nil && !form.Deadline.IsZero() {
  397. deadline = time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  398. 23, 59, 59, 0, form.Deadline.Location())
  399. deadlineUnix = util.TimeStamp(deadline.Unix())
  400. }
  401. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  402. ctx.Error(500, "UpdateIssueDeadline", err)
  403. return
  404. }
  405. ctx.JSON(201, api.IssueDeadline{Deadline: &deadline})
  406. }
  407. // StartIssueStopwatch creates a stopwatch for the given issue.
  408. func StartIssueStopwatch(ctx *context.APIContext) {
  409. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch
  410. // ---
  411. // summary: Start stopwatch on an issue.
  412. // consumes:
  413. // - application/json
  414. // produces:
  415. // - application/json
  416. // parameters:
  417. // - name: owner
  418. // in: path
  419. // description: owner of the repo
  420. // type: string
  421. // required: true
  422. // - name: repo
  423. // in: path
  424. // description: name of the repo
  425. // type: string
  426. // required: true
  427. // - name: index
  428. // in: path
  429. // description: index of the issue to create the stopwatch on
  430. // type: integer
  431. // format: int64
  432. // required: true
  433. // responses:
  434. // "201":
  435. // "$ref": "#/responses/empty"
  436. // "403":
  437. // description: Not repo writer, user does not have rights to toggle stopwatch
  438. // "404":
  439. // description: Issue not found
  440. // "409":
  441. // description: Cannot start a stopwatch again if it already exists
  442. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  443. if err != nil {
  444. if models.IsErrIssueNotExist(err) {
  445. ctx.NotFound()
  446. } else {
  447. ctx.Error(500, "GetIssueByIndex", err)
  448. }
  449. return
  450. }
  451. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  452. ctx.Status(403)
  453. return
  454. }
  455. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  456. ctx.Status(403)
  457. return
  458. }
  459. if models.StopwatchExists(ctx.User.ID, issue.ID) {
  460. ctx.Error(409, "StopwatchExists", "a stopwatch has already been started for this issue")
  461. return
  462. }
  463. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  464. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  465. return
  466. }
  467. ctx.Status(201)
  468. }
  469. // StopIssueStopwatch stops a stopwatch for the given issue.
  470. func StopIssueStopwatch(ctx *context.APIContext) {
  471. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopWatch
  472. // ---
  473. // summary: Stop an issue's existing stopwatch.
  474. // consumes:
  475. // - application/json
  476. // produces:
  477. // - application/json
  478. // parameters:
  479. // - name: owner
  480. // in: path
  481. // description: owner of the repo
  482. // type: string
  483. // required: true
  484. // - name: repo
  485. // in: path
  486. // description: name of the repo
  487. // type: string
  488. // required: true
  489. // - name: index
  490. // in: path
  491. // description: index of the issue to stop the stopwatch on
  492. // type: integer
  493. // format: int64
  494. // required: true
  495. // responses:
  496. // "201":
  497. // "$ref": "#/responses/empty"
  498. // "403":
  499. // description: Not repo writer, user does not have rights to toggle stopwatch
  500. // "404":
  501. // description: Issue not found
  502. // "409":
  503. // description: Cannot stop a non existent stopwatch
  504. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  505. if err != nil {
  506. if models.IsErrIssueNotExist(err) {
  507. ctx.NotFound()
  508. } else {
  509. ctx.Error(500, "GetIssueByIndex", err)
  510. }
  511. return
  512. }
  513. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  514. ctx.Status(403)
  515. return
  516. }
  517. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  518. ctx.Status(403)
  519. return
  520. }
  521. if !models.StopwatchExists(ctx.User.ID, issue.ID) {
  522. ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch")
  523. return
  524. }
  525. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  526. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  527. return
  528. }
  529. ctx.Status(201)
  530. }