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

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