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_stopwatch.go 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/context"
  8. )
  9. // StartIssueStopwatch creates a stopwatch for the given issue.
  10. func StartIssueStopwatch(ctx *context.APIContext) {
  11. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/start issue issueStartStopWatch
  12. // ---
  13. // summary: Start stopwatch on an issue.
  14. // consumes:
  15. // - application/json
  16. // produces:
  17. // - application/json
  18. // parameters:
  19. // - name: owner
  20. // in: path
  21. // description: owner of the repo
  22. // type: string
  23. // required: true
  24. // - name: repo
  25. // in: path
  26. // description: name of the repo
  27. // type: string
  28. // required: true
  29. // - name: index
  30. // in: path
  31. // description: index of the issue to create the stopwatch on
  32. // type: integer
  33. // format: int64
  34. // required: true
  35. // responses:
  36. // "201":
  37. // "$ref": "#/responses/empty"
  38. // "403":
  39. // description: Not repo writer, user does not have rights to toggle stopwatch
  40. // "404":
  41. // description: Issue not found
  42. // "409":
  43. // description: Cannot start a stopwatch again if it already exists
  44. issue, err := prepareIssueStopwatch(ctx, false)
  45. if err != nil {
  46. return
  47. }
  48. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  49. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  50. return
  51. }
  52. ctx.Status(201)
  53. }
  54. // StopIssueStopwatch stops a stopwatch for the given issue.
  55. func StopIssueStopwatch(ctx *context.APIContext) {
  56. // swagger:operation POST /repos/{owner}/{repo}/issues/{index}/stopwatch/stop issue issueStopStopWatch
  57. // ---
  58. // summary: Stop an issue's existing stopwatch.
  59. // consumes:
  60. // - application/json
  61. // produces:
  62. // - application/json
  63. // parameters:
  64. // - name: owner
  65. // in: path
  66. // description: owner of the repo
  67. // type: string
  68. // required: true
  69. // - name: repo
  70. // in: path
  71. // description: name of the repo
  72. // type: string
  73. // required: true
  74. // - name: index
  75. // in: path
  76. // description: index of the issue to stop the stopwatch on
  77. // type: integer
  78. // format: int64
  79. // required: true
  80. // responses:
  81. // "201":
  82. // "$ref": "#/responses/empty"
  83. // "403":
  84. // description: Not repo writer, user does not have rights to toggle stopwatch
  85. // "404":
  86. // description: Issue not found
  87. // "409":
  88. // description: Cannot stop a non existent stopwatch
  89. issue, err := prepareIssueStopwatch(ctx, true)
  90. if err != nil {
  91. return
  92. }
  93. if err := models.CreateOrStopIssueStopwatch(ctx.User, issue); err != nil {
  94. ctx.Error(500, "CreateOrStopIssueStopwatch", err)
  95. return
  96. }
  97. ctx.Status(201)
  98. }
  99. // DeleteIssueStopwatch delete a specific stopwatch
  100. func DeleteIssueStopwatch(ctx *context.APIContext) {
  101. // swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/stopwatch/delete issue issueDeleteStopWatch
  102. // ---
  103. // summary: Delete an issue's existing stopwatch.
  104. // consumes:
  105. // - application/json
  106. // produces:
  107. // - application/json
  108. // parameters:
  109. // - name: owner
  110. // in: path
  111. // description: owner of the repo
  112. // type: string
  113. // required: true
  114. // - name: repo
  115. // in: path
  116. // description: name of the repo
  117. // type: string
  118. // required: true
  119. // - name: index
  120. // in: path
  121. // description: index of the issue to stop the stopwatch on
  122. // type: integer
  123. // format: int64
  124. // required: true
  125. // responses:
  126. // "204":
  127. // "$ref": "#/responses/empty"
  128. // "403":
  129. // description: Not repo writer, user does not have rights to toggle stopwatch
  130. // "404":
  131. // description: Issue not found
  132. // "409":
  133. // description: Cannot cancel a non existent stopwatch
  134. issue, err := prepareIssueStopwatch(ctx, true)
  135. if err != nil {
  136. return
  137. }
  138. if err := models.CancelStopwatch(ctx.User, issue); err != nil {
  139. ctx.Error(500, "CancelStopwatch", err)
  140. return
  141. }
  142. ctx.Status(204)
  143. }
  144. func prepareIssueStopwatch(ctx *context.APIContext, shouldExist bool) (*models.Issue, error) {
  145. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  146. if err != nil {
  147. if models.IsErrIssueNotExist(err) {
  148. ctx.NotFound()
  149. } else {
  150. ctx.Error(500, "GetIssueByIndex", err)
  151. }
  152. return nil, err
  153. }
  154. if !ctx.Repo.CanWrite(models.UnitTypeIssues) {
  155. ctx.Status(403)
  156. return nil, err
  157. }
  158. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  159. ctx.Status(403)
  160. return nil, err
  161. }
  162. if models.StopwatchExists(ctx.User.ID, issue.ID) != shouldExist {
  163. if shouldExist {
  164. ctx.Error(409, "StopwatchExists", "cannot stop/cancel a non existent stopwatch")
  165. } else {
  166. ctx.Error(409, "StopwatchExists", "cannot start a stopwatch again if it already exists")
  167. }
  168. return nil, err
  169. }
  170. return issue, nil
  171. }
  172. // GetStopwatches get all stopwatches
  173. func GetStopwatches(ctx *context.APIContext) {
  174. // swagger:operation GET /user/stopwatches user userGetStopWatches
  175. // ---
  176. // summary: Get list of all existing stopwatches
  177. // consumes:
  178. // - application/json
  179. // produces:
  180. // - application/json
  181. // responses:
  182. // "200":
  183. // "$ref": "#/responses/StopWatchList"
  184. sws, err := models.GetUserStopwatches(ctx.User.ID)
  185. if err != nil {
  186. ctx.Error(500, "GetUserStopwatches", err)
  187. return
  188. }
  189. apiSWs, err := sws.APIFormat()
  190. if err != nil {
  191. ctx.Error(500, "APIFormat", err)
  192. return
  193. }
  194. ctx.JSON(200, apiSWs)
  195. }