本站源代码
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.

601 line
16KB

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