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.5KB

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