gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/models/db"
  9. issues_model "code.gitea.io/gitea/models/issues"
  10. "code.gitea.io/gitea/modules/util"
  11. "code.gitea.io/gitea/modules/web"
  12. "code.gitea.io/gitea/services/context"
  13. "code.gitea.io/gitea/services/forms"
  14. issue_service "code.gitea.io/gitea/services/issue"
  15. )
  16. // AddTimeManually tracks time manually
  17. func AddTimeManually(c *context.Context) {
  18. form := web.GetForm(c).(*forms.AddTimeManuallyForm)
  19. issue := GetActionIssue(c)
  20. if c.Written() {
  21. return
  22. }
  23. if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
  24. c.NotFound(nil)
  25. return
  26. }
  27. if c.HasError() {
  28. c.JSONError(c.GetErrMsg())
  29. return
  30. }
  31. total := time.Duration(form.Hours)*time.Hour + time.Duration(form.Minutes)*time.Minute
  32. if total <= 0 {
  33. c.JSONError(c.Tr("repo.issues.add_time_sum_to_small"))
  34. return
  35. }
  36. if _, err := issues_model.AddTime(c, c.Doer, issue, int64(total.Seconds()), time.Now()); err != nil {
  37. c.ServerError("AddTime", err)
  38. return
  39. }
  40. c.JSONRedirect("")
  41. }
  42. // DeleteTime deletes tracked time
  43. func DeleteTime(c *context.Context) {
  44. issue := GetActionIssue(c)
  45. if c.Written() {
  46. return
  47. }
  48. if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
  49. c.NotFound(nil)
  50. return
  51. }
  52. t, err := issues_model.GetTrackedTimeByID(c, c.PathParamInt64("timeid"))
  53. if err != nil {
  54. if db.IsErrNotExist(err) {
  55. c.NotFound(err)
  56. return
  57. }
  58. c.HTTPError(http.StatusInternalServerError, "GetTrackedTimeByID", err.Error())
  59. return
  60. }
  61. // only OP or admin may delete
  62. if !c.IsSigned || (!c.IsUserSiteAdmin() && c.Doer.ID != t.UserID) {
  63. c.HTTPError(http.StatusForbidden, "not allowed")
  64. return
  65. }
  66. if err = issues_model.DeleteTime(c, t); err != nil {
  67. c.ServerError("DeleteTime", err)
  68. return
  69. }
  70. c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToHours(t.Time)))
  71. c.JSONRedirect("")
  72. }
  73. func UpdateIssueTimeEstimate(ctx *context.Context) {
  74. issue := GetActionIssue(ctx)
  75. if ctx.Written() {
  76. return
  77. }
  78. if !ctx.IsSigned || (!issue.IsPoster(ctx.Doer.ID) && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
  79. ctx.HTTPError(http.StatusForbidden)
  80. return
  81. }
  82. timeStr := strings.TrimSpace(ctx.FormString("time_estimate"))
  83. total, err := util.TimeEstimateParse(timeStr)
  84. if err != nil {
  85. ctx.JSONError(ctx.Tr("repo.issues.time_estimate_invalid"))
  86. return
  87. }
  88. // No time changed
  89. if issue.TimeEstimate == total {
  90. ctx.JSONRedirect("")
  91. return
  92. }
  93. if err := issue_service.ChangeTimeEstimate(ctx, issue, ctx.Doer, total); err != nil {
  94. ctx.ServerError("ChangeTimeEstimate", err)
  95. return
  96. }
  97. ctx.JSONRedirect("")
  98. }