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

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