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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. "code.gitea.io/gitea/modules/indexer"
  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/sdk/gitea"
  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 = 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.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  134. if err != nil {
  135. if models.IsErrIssueNotExist(err) {
  136. ctx.Status(404)
  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.Status(404)
  265. } else {
  266. ctx.Error(500, "GetIssueByIndex", err)
  267. }
  268. return
  269. }
  270. issue.Repo = ctx.Repo.Repository
  271. if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  272. ctx.Status(403)
  273. return
  274. }
  275. if len(form.Title) > 0 {
  276. issue.Title = form.Title
  277. }
  278. if form.Body != nil {
  279. issue.Content = *form.Body
  280. }
  281. // Update the deadline
  282. var deadlineUnix util.TimeStamp
  283. if form.Deadline != nil && !form.Deadline.IsZero() && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  284. deadlineUnix = util.TimeStamp(form.Deadline.Unix())
  285. }
  286. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  287. ctx.Error(500, "UpdateIssueDeadline", err)
  288. return
  289. }
  290. // Add/delete assignees
  291. // Deleting is done the Github way (quote from their api documentation):
  292. // https://developer.github.com/v3/issues/#edit-an-issue
  293. // "assignees" (array): Logins for Users to assign to this issue.
  294. // Pass one or more user logins to replace the set of assignees on this Issue.
  295. // Send an empty array ([]) to clear all assignees from the Issue.
  296. if ctx.Repo.CanWrite(models.UnitTypeIssues) && (form.Assignees != nil || form.Assignee != nil) {
  297. oneAssignee := ""
  298. if form.Assignee != nil {
  299. oneAssignee = *form.Assignee
  300. }
  301. err = models.UpdateAPIAssignee(issue, oneAssignee, form.Assignees, ctx.User)
  302. if err != nil {
  303. ctx.Error(500, "UpdateAPIAssignee", err)
  304. return
  305. }
  306. }
  307. if ctx.Repo.CanWrite(models.UnitTypeIssues) && form.Milestone != nil &&
  308. issue.MilestoneID != *form.Milestone {
  309. oldMilestoneID := issue.MilestoneID
  310. issue.MilestoneID = *form.Milestone
  311. if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  312. ctx.Error(500, "ChangeMilestoneAssign", err)
  313. return
  314. }
  315. }
  316. if err = models.UpdateIssue(issue); err != nil {
  317. ctx.Error(500, "UpdateIssue", err)
  318. return
  319. }
  320. if form.State != nil {
  321. if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
  322. if models.IsErrDependenciesLeft(err) {
  323. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  324. return
  325. }
  326. ctx.Error(500, "ChangeStatus", err)
  327. return
  328. }
  329. notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
  330. }
  331. // Refetch from database to assign some automatic values
  332. issue, err = models.GetIssueByID(issue.ID)
  333. if err != nil {
  334. ctx.Error(500, "GetIssueByID", err)
  335. return
  336. }
  337. ctx.JSON(201, issue.APIFormat())
  338. }
  339. // UpdateIssueDeadline updates an issue deadline
  340. func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
  341. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/deadline issue issueEditIssueDeadline
  342. // ---
  343. // 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.
  344. // consumes:
  345. // - application/json
  346. // produces:
  347. // - application/json
  348. // parameters:
  349. // - name: owner
  350. // in: path
  351. // description: owner of the repo
  352. // type: string
  353. // required: true
  354. // - name: repo
  355. // in: path
  356. // description: name of the repo
  357. // type: string
  358. // required: true
  359. // - name: index
  360. // in: path
  361. // description: index of the issue to create or update a deadline on
  362. // type: integer
  363. // format: int64
  364. // required: true
  365. // - name: body
  366. // in: body
  367. // schema:
  368. // "$ref": "#/definitions/EditDeadlineOption"
  369. // responses:
  370. // "201":
  371. // "$ref": "#/responses/IssueDeadline"
  372. // "403":
  373. // description: Not repo writer
  374. // "404":
  375. // description: Issue not found
  376. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  377. if err != nil {
  378. if models.IsErrIssueNotExist(err) {
  379. ctx.Status(404)
  380. } else {
  381. ctx.Error(500, "GetIssueByIndex", err)
  382. }
  383. return
  384. }
  385. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  386. ctx.Status(403)
  387. return
  388. }
  389. var deadlineUnix util.TimeStamp
  390. var deadline time.Time
  391. if form.Deadline != nil && !form.Deadline.IsZero() {
  392. deadline = time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  393. 23, 59, 59, 0, form.Deadline.Location())
  394. deadlineUnix = util.TimeStamp(deadline.Unix())
  395. }
  396. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  397. ctx.Error(500, "UpdateIssueDeadline", err)
  398. return
  399. }
  400. ctx.JSON(201, api.IssueDeadline{Deadline: &deadline})
  401. }
  402. // StartIssueStopwatch creates a stopwatch for the given issue.
  403. func StartIssueStopwatch(ctx *context.APIContext) {
  404. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch
  405. // ---
  406. // summary: Start stopwatch on an issue.
  407. // consumes:
  408. // - application/json
  409. // produces:
  410. // - application/json
  411. // parameters:
  412. // - name: owner
  413. // in: path
  414. // description: owner of the repo
  415. // type: string
  416. // required: true
  417. // - name: repo
  418. // in: path
  419. // description: name of the repo
  420. // type: string
  421. // required: true
  422. // - name: index
  423. // in: path
  424. // description: index of the issue to create the stopwatch on
  425. // type: integer
  426. // format: int64
  427. // required: true
  428. // responses:
  429. // "201":
  430. // "$ref": "#/responses/empty"
  431. // "403":
  432. // description: Not repo writer, user does not have rights to toggle stopwatch
  433. // "404":
  434. // description: Issue not found
  435. // "409":
  436. // description: Cannot start a stopwatch again if it already exists
  437. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  438. if err != nil {
  439. if models.IsErrIssueNotExist(err) {
  440. ctx.Status(404)
  441. } else {
  442. ctx.Error(500, "GetIssueByIndex", err)
  443. }
  444. return
  445. }
  446. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  447. ctx.Status(403)
  448. return
  449. }
  450. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  451. ctx.Status(403)
  452. return
  453. }
  454. if models.StopwatchExists(ctx.User.ID, issue.ID) {
  455. ctx.Error(409, "StopwatchExists", "a stopwatch has already been started for this issue")
  456. return
  457. }
  458. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  459. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  460. return
  461. }
  462. ctx.Status(201)
  463. }
  464. // StopIssueStopwatch stops a stopwatch for the given issue.
  465. func StopIssueStopwatch(ctx *context.APIContext) {
  466. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopWatch
  467. // ---
  468. // summary: Stop an issue's existing stopwatch.
  469. // consumes:
  470. // - application/json
  471. // produces:
  472. // - application/json
  473. // parameters:
  474. // - name: owner
  475. // in: path
  476. // description: owner of the repo
  477. // type: string
  478. // required: true
  479. // - name: repo
  480. // in: path
  481. // description: name of the repo
  482. // type: string
  483. // required: true
  484. // - name: index
  485. // in: path
  486. // description: index of the issue to stop the stopwatch on
  487. // type: integer
  488. // format: int64
  489. // required: true
  490. // responses:
  491. // "201":
  492. // "$ref": "#/responses/empty"
  493. // "403":
  494. // description: Not repo writer, user does not have rights to toggle stopwatch
  495. // "404":
  496. // description: Issue not found
  497. // "409":
  498. // description: Cannot stop a non existent stopwatch
  499. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  500. if err != nil {
  501. if models.IsErrIssueNotExist(err) {
  502. ctx.Status(404)
  503. } else {
  504. ctx.Error(500, "GetIssueByIndex", err)
  505. }
  506. return
  507. }
  508. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  509. ctx.Status(403)
  510. return
  511. }
  512. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  513. ctx.Status(403)
  514. return
  515. }
  516. if !models.StopwatchExists(ctx.User.ID, issue.ID) {
  517. ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch")
  518. return
  519. }
  520. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  521. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  522. return
  523. }
  524. ctx.Status(201)
  525. }