Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_tracked_time.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // ListTrackedTimes list all the tracked times of an issue
  11. func ListTrackedTimes(ctx *context.APIContext) {
  12. // swagger:route GET /repos/{username}/{reponame}/issues/{issue}/times repository issueTrackedTimes
  13. //
  14. // Produces:
  15. // - application/json
  16. //
  17. // Responses:
  18. // 200: TrackedTimes
  19. // 404: error
  20. // 500: error
  21. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  22. ctx.Error(404, "IsTimetrackerEnabled", "Timetracker is diabled")
  23. return
  24. }
  25. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  26. if err != nil {
  27. if models.IsErrIssueNotExist(err) {
  28. ctx.Error(404, "GetIssueByIndex", err)
  29. } else {
  30. ctx.Error(500, "GetIssueByIndex", err)
  31. }
  32. return
  33. }
  34. if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue.ID}); err != nil {
  35. ctx.Error(500, "GetTrackedTimesByIssue", err)
  36. } else {
  37. ctx.JSON(200, &trackedTimes)
  38. }
  39. }
  40. // AddTime adds time manual to the given issue
  41. func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
  42. // swagger:route Post /repos/{username}/{reponame}/issues/{issue}/times repository addTime
  43. //
  44. // Produces:
  45. // - application/json
  46. //
  47. // Responses:
  48. // 200: TrackedTime
  49. // 400: error
  50. // 403: error
  51. // 404: error
  52. // 500: error
  53. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  54. if err != nil {
  55. if models.IsErrIssueNotExist(err) {
  56. ctx.Error(404, "GetIssueByIndex", err)
  57. } else {
  58. ctx.Error(500, "GetIssueByIndex", err)
  59. }
  60. return
  61. }
  62. if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
  63. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  64. ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
  65. return
  66. }
  67. ctx.Status(403)
  68. return
  69. }
  70. var tt *models.TrackedTime
  71. if tt, err = models.AddTime(ctx.User, issue, form.Time); err != nil {
  72. ctx.Error(500, "AddTime", err)
  73. return
  74. }
  75. ctx.JSON(200, tt)
  76. }
  77. // ListTrackedTimesByUser lists all tracked times of the user
  78. func ListTrackedTimesByUser(ctx *context.APIContext) {
  79. // swagger:route GET /repos/{username}/{reponame}/times/{timetrackingusername} user userTrackedTimes
  80. //
  81. // Produces:
  82. // - application/json
  83. //
  84. // Responses:
  85. // 200: TrackedTimes
  86. // 400: error
  87. // 404: error
  88. // 500: error
  89. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  90. ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
  91. return
  92. }
  93. user, err := models.GetUserByName(ctx.Params(":timetrackingusername"))
  94. if err != nil {
  95. if models.IsErrUserNotExist(err) {
  96. ctx.Error(404, "GetUserByName", err)
  97. } else {
  98. ctx.Error(500, "GetUserByName", err)
  99. }
  100. return
  101. }
  102. if user == nil {
  103. ctx.Status(404)
  104. return
  105. }
  106. if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: user.ID, RepositoryID: ctx.Repo.Repository.ID}); err != nil {
  107. ctx.Error(500, "GetTrackedTimesByUser", err)
  108. } else {
  109. ctx.JSON(200, &trackedTimes)
  110. }
  111. }
  112. // ListTrackedTimesByRepository lists all tracked times of the user
  113. func ListTrackedTimesByRepository(ctx *context.APIContext) {
  114. // swagger:route GET /repos/{username}/{reponame}/times repository repoTrackedTimes
  115. //
  116. // Produces:
  117. // - application/json
  118. //
  119. // Responses:
  120. // 200: TrackedTimes
  121. // 400: error
  122. // 500: error
  123. if !ctx.Repo.Repository.IsTimetrackerEnabled() {
  124. ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
  125. return
  126. }
  127. if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{RepositoryID: ctx.Repo.Repository.ID}); err != nil {
  128. ctx.Error(500, "GetTrackedTimesByUser", err)
  129. } else {
  130. ctx.JSON(200, &trackedTimes)
  131. }
  132. }
  133. // ListMyTrackedTimes lists all tracked times of the current user
  134. func ListMyTrackedTimes(ctx *context.APIContext) {
  135. // swagger:route GET /user/times user userTrackedTimes
  136. //
  137. // Produces:
  138. // - application/json
  139. //
  140. // Responses:
  141. // 200: TrackedTimes
  142. // 500: error
  143. if trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{UserID: ctx.User.ID}); err != nil {
  144. ctx.Error(500, "GetTrackedTimesByUser", err)
  145. } else {
  146. ctx.JSON(200, &trackedTimes)
  147. }
  148. }