You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 16 kB

Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
Issue due date (#3794) * Started adding deadline to ui * Implemented basic issue due date managing * Improved UI for due date managing * Added at least write access to the repo in order to modify issue due dates * Ui improvements * Added issue comments creation when adding/modifying/removing a due date * Show due date in issue list * Added api support for issue due dates * Fixed lint suggestions * Added deadline to sdk * Updated css * Added support for adding/modifiying deadlines for pull requests via api * Fixed comments not created when updating or removing a deadline * update sdk (will do properly once go-gitea/go-sdk#103 is merged) * enhanced updateIssueDeadline * Removed unnessecary Issue.DeadlineString * UI improvements * Small improvments to comment creation + ui & validation improvements * Check if an issue is overdue is now a seperate function * Updated go-sdk with govendor as it was merged * Simplified isOverdue method * removed unessecary deadline to 0 set * Update swagger definitions * Added missing return * Added an explanary comment * Improved updateIssueDeadline method so it'll only update `deadline_unix` * Small changes and improvements * no need to explicitly load the issue when updating a deadline, just use whats already there * small optimisations * Added check if a deadline was modified before updating it * Moved comment creating logic into its own function * Code cleanup for creating deadline comment * locale improvement * When modifying a deadline, the old deadline is saved with the comment * small improvments to xorm session handling when updating an issue deadline + style nitpicks * style nitpicks * Moved checking for if the user has write acces to middleware
7 years ago
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. api "code.gitea.io/gitea/modules/structs"
  17. "code.gitea.io/gitea/modules/timeutil"
  18. "code.gitea.io/gitea/modules/util"
  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. } else {
  200. // setting labels is not allowed if user is not a writer
  201. form.Labels = make([]int64, 0)
  202. }
  203. if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
  204. if models.IsErrUserDoesNotHaveAccessToRepo(err) {
  205. ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
  206. return
  207. }
  208. ctx.Error(500, "NewIssue", err)
  209. return
  210. }
  211. notification.NotifyNewIssue(issue)
  212. if form.Closed {
  213. if err := issue.ChangeStatus(ctx.User, true); err != nil {
  214. if models.IsErrDependenciesLeft(err) {
  215. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  216. return
  217. }
  218. ctx.Error(500, "ChangeStatus", err)
  219. return
  220. }
  221. }
  222. // Refetch from database to assign some automatic values
  223. issue, err = models.GetIssueByID(issue.ID)
  224. if err != nil {
  225. ctx.Error(500, "GetIssueByID", err)
  226. return
  227. }
  228. ctx.JSON(201, issue.APIFormat())
  229. }
  230. // EditIssue modify an issue of a repository
  231. func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
  232. // swagger:operation PATCH /repos/{owner}/{repo}/issues/{index} issue issueEditIssue
  233. // ---
  234. // summary: Edit an issue. If using deadline only the date will be taken into account, and time of day ignored.
  235. // consumes:
  236. // - application/json
  237. // produces:
  238. // - application/json
  239. // parameters:
  240. // - name: owner
  241. // in: path
  242. // description: owner of the repo
  243. // type: string
  244. // required: true
  245. // - name: repo
  246. // in: path
  247. // description: name of the repo
  248. // type: string
  249. // required: true
  250. // - name: index
  251. // in: path
  252. // description: index of the issue to edit
  253. // type: integer
  254. // format: int64
  255. // required: true
  256. // - name: body
  257. // in: body
  258. // schema:
  259. // "$ref": "#/definitions/EditIssueOption"
  260. // responses:
  261. // "201":
  262. // "$ref": "#/responses/Issue"
  263. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  264. if err != nil {
  265. if models.IsErrIssueNotExist(err) {
  266. ctx.NotFound()
  267. } else {
  268. ctx.Error(500, "GetIssueByIndex", err)
  269. }
  270. return
  271. }
  272. issue.Repo = ctx.Repo.Repository
  273. err = issue.LoadAttributes()
  274. if err != nil {
  275. ctx.Error(500, "LoadAttributes", err)
  276. return
  277. }
  278. if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  279. ctx.Status(403)
  280. return
  281. }
  282. if len(form.Title) > 0 {
  283. issue.Title = form.Title
  284. }
  285. if form.Body != nil {
  286. issue.Content = *form.Body
  287. }
  288. // Update the deadline
  289. var deadlineUnix timeutil.TimeStamp
  290. if form.Deadline != nil && !form.Deadline.IsZero() && ctx.Repo.CanWrite(models.UnitTypeIssues) {
  291. deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
  292. }
  293. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  294. ctx.Error(500, "UpdateIssueDeadline", err)
  295. return
  296. }
  297. // Add/delete assignees
  298. // Deleting is done the GitHub way (quote from their api documentation):
  299. // https://developer.github.com/v3/issues/#edit-an-issue
  300. // "assignees" (array): Logins for Users to assign to this issue.
  301. // Pass one or more user logins to replace the set of assignees on this Issue.
  302. // Send an empty array ([]) to clear all assignees from the Issue.
  303. if ctx.Repo.CanWrite(models.UnitTypeIssues) && (form.Assignees != nil || form.Assignee != nil) {
  304. oneAssignee := ""
  305. if form.Assignee != nil {
  306. oneAssignee = *form.Assignee
  307. }
  308. err = models.UpdateAPIAssignee(issue, oneAssignee, form.Assignees, ctx.User)
  309. if err != nil {
  310. ctx.Error(500, "UpdateAPIAssignee", err)
  311. return
  312. }
  313. }
  314. if ctx.Repo.CanWrite(models.UnitTypeIssues) && form.Milestone != nil &&
  315. issue.MilestoneID != *form.Milestone {
  316. oldMilestoneID := issue.MilestoneID
  317. issue.MilestoneID = *form.Milestone
  318. if err = milestone_service.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
  319. ctx.Error(500, "ChangeMilestoneAssign", err)
  320. return
  321. }
  322. }
  323. if err = models.UpdateIssue(issue); err != nil {
  324. ctx.Error(500, "UpdateIssue", err)
  325. return
  326. }
  327. if form.State != nil {
  328. if err = issue.ChangeStatus(ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
  329. if models.IsErrDependenciesLeft(err) {
  330. ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
  331. return
  332. }
  333. ctx.Error(500, "ChangeStatus", err)
  334. return
  335. }
  336. notification.NotifyIssueChangeStatus(ctx.User, issue, api.StateClosed == api.StateType(*form.State))
  337. }
  338. // Refetch from database to assign some automatic values
  339. issue, err = models.GetIssueByID(issue.ID)
  340. if err != nil {
  341. ctx.Error(500, "GetIssueByID", err)
  342. return
  343. }
  344. ctx.JSON(201, issue.APIFormat())
  345. }
  346. // UpdateIssueDeadline updates an issue deadline
  347. func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
  348. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/deadline issue issueEditIssueDeadline
  349. // ---
  350. // 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.
  351. // consumes:
  352. // - application/json
  353. // produces:
  354. // - application/json
  355. // parameters:
  356. // - name: owner
  357. // in: path
  358. // description: owner of the repo
  359. // type: string
  360. // required: true
  361. // - name: repo
  362. // in: path
  363. // description: name of the repo
  364. // type: string
  365. // required: true
  366. // - name: index
  367. // in: path
  368. // description: index of the issue to create or update a deadline on
  369. // type: integer
  370. // format: int64
  371. // required: true
  372. // - name: body
  373. // in: body
  374. // schema:
  375. // "$ref": "#/definitions/EditDeadlineOption"
  376. // responses:
  377. // "201":
  378. // "$ref": "#/responses/IssueDeadline"
  379. // "403":
  380. // description: Not repo writer
  381. // "404":
  382. // description: Issue not found
  383. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  384. if err != nil {
  385. if models.IsErrIssueNotExist(err) {
  386. ctx.NotFound()
  387. } else {
  388. ctx.Error(500, "GetIssueByIndex", err)
  389. }
  390. return
  391. }
  392. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  393. ctx.Status(403)
  394. return
  395. }
  396. var deadlineUnix timeutil.TimeStamp
  397. var deadline time.Time
  398. if form.Deadline != nil && !form.Deadline.IsZero() {
  399. deadline = time.Date(form.Deadline.Year(), form.Deadline.Month(), form.Deadline.Day(),
  400. 23, 59, 59, 0, form.Deadline.Location())
  401. deadlineUnix = timeutil.TimeStamp(deadline.Unix())
  402. }
  403. if err := models.UpdateIssueDeadline(issue, deadlineUnix, ctx.User); err != nil {
  404. ctx.Error(500, "UpdateIssueDeadline", err)
  405. return
  406. }
  407. ctx.JSON(201, api.IssueDeadline{Deadline: &deadline})
  408. }
  409. // StartIssueStopwatch creates a stopwatch for the given issue.
  410. func StartIssueStopwatch(ctx *context.APIContext) {
  411. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch
  412. // ---
  413. // summary: Start stopwatch on an issue.
  414. // consumes:
  415. // - application/json
  416. // produces:
  417. // - application/json
  418. // parameters:
  419. // - name: owner
  420. // in: path
  421. // description: owner of the repo
  422. // type: string
  423. // required: true
  424. // - name: repo
  425. // in: path
  426. // description: name of the repo
  427. // type: string
  428. // required: true
  429. // - name: index
  430. // in: path
  431. // description: index of the issue to create the stopwatch on
  432. // type: integer
  433. // format: int64
  434. // required: true
  435. // responses:
  436. // "201":
  437. // "$ref": "#/responses/empty"
  438. // "403":
  439. // description: Not repo writer, user does not have rights to toggle stopwatch
  440. // "404":
  441. // description: Issue not found
  442. // "409":
  443. // description: Cannot start a stopwatch again if it already exists
  444. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  445. if err != nil {
  446. if models.IsErrIssueNotExist(err) {
  447. ctx.NotFound()
  448. } else {
  449. ctx.Error(500, "GetIssueByIndex", err)
  450. }
  451. return
  452. }
  453. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  454. ctx.Status(403)
  455. return
  456. }
  457. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  458. ctx.Status(403)
  459. return
  460. }
  461. if models.StopwatchExists(ctx.User.ID, issue.ID) {
  462. ctx.Error(409, "StopwatchExists", "a stopwatch has already been started for this issue")
  463. return
  464. }
  465. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  466. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  467. return
  468. }
  469. ctx.Status(201)
  470. }
  471. // StopIssueStopwatch stops a stopwatch for the given issue.
  472. func StopIssueStopwatch(ctx *context.APIContext) {
  473. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopWatch
  474. // ---
  475. // summary: Stop an issue's existing stopwatch.
  476. // consumes:
  477. // - application/json
  478. // produces:
  479. // - application/json
  480. // parameters:
  481. // - name: owner
  482. // in: path
  483. // description: owner of the repo
  484. // type: string
  485. // required: true
  486. // - name: repo
  487. // in: path
  488. // description: name of the repo
  489. // type: string
  490. // required: true
  491. // - name: index
  492. // in: path
  493. // description: index of the issue to stop the stopwatch on
  494. // type: integer
  495. // format: int64
  496. // required: true
  497. // responses:
  498. // "201":
  499. // "$ref": "#/responses/empty"
  500. // "403":
  501. // description: Not repo writer, user does not have rights to toggle stopwatch
  502. // "404":
  503. // description: Issue not found
  504. // "409":
  505. // description: Cannot stop a non existent stopwatch
  506. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  507. if err != nil {
  508. if models.IsErrIssueNotExist(err) {
  509. ctx.NotFound()
  510. } else {
  511. ctx.Error(500, "GetIssueByIndex", err)
  512. }
  513. return
  514. }
  515. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  516. ctx.Status(403)
  517. return
  518. }
  519. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  520. ctx.Status(403)
  521. return
  522. }
  523. if !models.StopwatchExists(ctx.User.ID, issue.ID) {
  524. ctx.Error(409, "StopwatchExists", "cannot stop a non existent stopwatch")
  525. return
  526. }
  527. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  528. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  529. return
  530. }
  531. ctx.Status(201)
  532. }