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_tracked_time.go 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2017 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. api "code.gitea.io/gitea/modules/structs"
  9. )
  10. func trackedTimesToAPIFormat(trackedTimes []*models.TrackedTime) []*api.TrackedTime {
  11. apiTrackedTimes := make([]*api.TrackedTime, len(trackedTimes))
  12. for i, trackedTime := range trackedTimes {
  13. apiTrackedTimes[i] = trackedTime.APIFormat()
  14. }
  15. return apiTrackedTimes
  16. }
  17. // ListTrackedTimes list all the tracked times of an issue
  18. func ListTrackedTimes(ctx *context.APIContext) {
  19. // swagger:operation GET /repos/{owner}/{repo}/issues/{id}/times issue issueTrackedTimes
  20. // ---
  21. // summary: List an issue's tracked times
  22. // produces:
  23. // - application/json
  24. // parameters:
  25. // - name: owner
  26. // in: path
  27. // description: owner of the repo
  28. // type: string
  29. // required: true
  30. // - name: repo
  31. // in: path
  32. // description: name of the repo
  33. // type: string
  34. // required: true
  35. // - name: id
  36. // in: path
  37. // description: index of the issue
  38. // type: integer
  39. // format: int64
  40. // required: true
  41. // responses:
  42. // "200":
  43. // "$ref": "#/responses/TrackedTimeList"
  44. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  45. ctx.NotFound("Timetracker is disabled")
  46. return
  47. }
  48. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  49. if err != nil {
  50. if models.IsErrIssueNotExist(err) {
  51. ctx.NotFound(err)
  52. } else {
  53. ctx.Error(500, "GetIssueByIndex", err)
  54. }
  55. return
  56. }
  57. trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue.ID})
  58. if err != nil {
  59. ctx.Error(500, "GetTrackedTimesByIssue", err)
  60. return
  61. }
  62. apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
  63. ctx.JSON(200, &apiTrackedTimes)
  64. }
  65. // AddTime adds time manual to the given issue
  66. func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
  67. // swagger:operation Post /repos/{owner}/{repo}/issues/{id}/times issue issueAddTime
  68. // ---
  69. // summary: Add a tracked time to a issue
  70. // consumes:
  71. // - application/json
  72. // produces:
  73. // - application/json
  74. // parameters:
  75. // - name: owner
  76. // in: path
  77. // description: owner of the repo
  78. // type: string
  79. // required: true
  80. // - name: repo
  81. // in: path
  82. // description: name of the repo
  83. // type: string
  84. // required: true
  85. // - name: id
  86. // in: path
  87. // description: index of the issue to add tracked time to
  88. // type: integer
  89. // format: int64
  90. // required: true
  91. // - name: body
  92. // in: body
  93. // schema:
  94. // "$ref": "#/definitions/AddTimeOption"
  95. // responses:
  96. // "200":
  97. // "$ref": "#/responses/TrackedTime"
  98. // "400":
  99. // "$ref": "#/responses/error"
  100. // "403":
  101. // "$ref": "#/responses/error"
  102. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  103. if err != nil {
  104. if models.IsErrIssueNotExist(err) {
  105. ctx.NotFound(err)
  106. } else {
  107. ctx.Error(500, "GetIssueByIndex", err)
  108. }
  109. return
  110. }
  111. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  112. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  113. ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
  114. return
  115. }
  116. ctx.Status(403)
  117. return
  118. }
  119. trackedTime, err := models.AddTime(ctx.User, issue, form.Time)
  120. if err != nil {
  121. ctx.Error(500, "AddTime", err)
  122. return
  123. }
  124. ctx.JSON(200, trackedTime.APIFormat())
  125. }
  126. // ListTrackedTimesByUser lists all tracked times of the user
  127. func ListTrackedTimesByUser(ctx *context.APIContext) {
  128. // swagger:operation GET /repos/{owner}/{repo}/times/{user} user userTrackedTimes
  129. // ---
  130. // summary: List a user's tracked times in a repo
  131. // produces:
  132. // - application/json
  133. // parameters:
  134. // - name: owner
  135. // in: path
  136. // description: owner of the repo
  137. // type: string
  138. // required: true
  139. // - name: repo
  140. // in: path
  141. // description: name of the repo
  142. // type: string
  143. // required: true
  144. // - name: user
  145. // in: path
  146. // description: username of user
  147. // type: string
  148. // required: true
  149. // responses:
  150. // "200":
  151. // "$ref": "#/responses/TrackedTimeList"
  152. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  153. ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
  154. return
  155. }
  156. user, err := models.GetUserByName(ctx.Params(":timetrackingusername"))
  157. if err != nil {
  158. if models.IsErrUserNotExist(err) {
  159. ctx.NotFound(err)
  160. } else {
  161. ctx.Error(500, "GetUserByName", err)
  162. }
  163. return
  164. }
  165. if user == nil {
  166. ctx.NotFound()
  167. return
  168. }
  169. trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{
  170. UserID: user.ID,
  171. RepositoryID: ctx.Repo.Repository.ID})
  172. if err != nil {
  173. ctx.Error(500, "GetTrackedTimesByUser", err)
  174. return
  175. }
  176. apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
  177. ctx.JSON(200, &apiTrackedTimes)
  178. }
  179. // ListTrackedTimesByRepository lists all tracked times of the repository
  180. func ListTrackedTimesByRepository(ctx *context.APIContext) {
  181. // swagger:operation GET /repos/{owner}/{repo}/times repository repoTrackedTimes
  182. // ---
  183. // summary: List a repo's tracked times
  184. // produces:
  185. // - application/json
  186. // parameters:
  187. // - name: owner
  188. // in: path
  189. // description: owner of the repo
  190. // type: string
  191. // required: true
  192. // - name: repo
  193. // in: path
  194. // description: name of the repo
  195. // type: string
  196. // required: true
  197. // responses:
  198. // "200":
  199. // "$ref": "#/responses/TrackedTimeList"
  200. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  201. ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
  202. return
  203. }
  204. trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{
  205. RepositoryID: ctx.Repo.Repository.ID})
  206. if err != nil {
  207. ctx.Error(500, "GetTrackedTimesByUser", err)
  208. return
  209. }
  210. apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
  211. ctx.JSON(200, &apiTrackedTimes)
  212. }
  213. // ListMyTrackedTimes lists all tracked times of the current user
  214. func ListMyTrackedTimes(ctx *context.APIContext) {
  215. // swagger:operation GET /user/times user userCurrentTrackedTimes
  216. // ---
  217. // summary: List the current user's tracked times
  218. // produces:
  219. // - application/json
  220. // responses:
  221. // "200":
  222. // "$ref": "#/responses/TrackedTimeList"
  223. trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: ctx.User.ID})
  224. if err != nil {
  225. ctx.Error(500, "GetTrackedTimesByUser", err)
  226. return
  227. }
  228. apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
  229. ctx.JSON(200, &apiTrackedTimes)
  230. }