gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issue
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "html"
  9. "net/url"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "time"
  14. issues_model "code.gitea.io/gitea/models/issues"
  15. access_model "code.gitea.io/gitea/models/perm/access"
  16. repo_model "code.gitea.io/gitea/models/repo"
  17. user_model "code.gitea.io/gitea/models/user"
  18. "code.gitea.io/gitea/modules/container"
  19. "code.gitea.io/gitea/modules/git"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/references"
  22. "code.gitea.io/gitea/modules/repository"
  23. )
  24. const (
  25. secondsByMinute = float64(time.Minute / time.Second) // seconds in a minute
  26. secondsByHour = 60 * secondsByMinute // seconds in an hour
  27. secondsByDay = 8 * secondsByHour // seconds in a day
  28. secondsByWeek = 5 * secondsByDay // seconds in a week
  29. secondsByMonth = 4 * secondsByWeek // seconds in a month
  30. )
  31. var reDuration = regexp.MustCompile(`(?i)^(?:(\d+([\.,]\d+)?)(?:mo))?(?:(\d+([\.,]\d+)?)(?:w))?(?:(\d+([\.,]\d+)?)(?:d))?(?:(\d+([\.,]\d+)?)(?:h))?(?:(\d+([\.,]\d+)?)(?:m))?$`)
  32. // timeLogToAmount parses time log string and returns amount in seconds
  33. func timeLogToAmount(str string) int64 {
  34. matches := reDuration.FindAllStringSubmatch(str, -1)
  35. if len(matches) == 0 {
  36. return 0
  37. }
  38. match := matches[0]
  39. var a int64
  40. // months
  41. if len(match[1]) > 0 {
  42. mo, _ := strconv.ParseFloat(strings.Replace(match[1], ",", ".", 1), 64)
  43. a += int64(mo * secondsByMonth)
  44. }
  45. // weeks
  46. if len(match[3]) > 0 {
  47. w, _ := strconv.ParseFloat(strings.Replace(match[3], ",", ".", 1), 64)
  48. a += int64(w * secondsByWeek)
  49. }
  50. // days
  51. if len(match[5]) > 0 {
  52. d, _ := strconv.ParseFloat(strings.Replace(match[5], ",", ".", 1), 64)
  53. a += int64(d * secondsByDay)
  54. }
  55. // hours
  56. if len(match[7]) > 0 {
  57. h, _ := strconv.ParseFloat(strings.Replace(match[7], ",", ".", 1), 64)
  58. a += int64(h * secondsByHour)
  59. }
  60. // minutes
  61. if len(match[9]) > 0 {
  62. d, _ := strconv.ParseFloat(strings.Replace(match[9], ",", ".", 1), 64)
  63. a += int64(d * secondsByMinute)
  64. }
  65. return a
  66. }
  67. func issueAddTime(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, time time.Time, timeLog string) error {
  68. amount := timeLogToAmount(timeLog)
  69. if amount == 0 {
  70. return nil
  71. }
  72. _, err := issues_model.AddTime(ctx, doer, issue, amount, time)
  73. return err
  74. }
  75. // getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
  76. // if the provided ref references a non-existent issue.
  77. func getIssueFromRef(ctx context.Context, repo *repo_model.Repository, index int64) (*issues_model.Issue, error) {
  78. issue, err := issues_model.GetIssueByIndex(ctx, repo.ID, index)
  79. if err != nil {
  80. if issues_model.IsErrIssueNotExist(err) {
  81. return nil, nil
  82. }
  83. return nil, err
  84. }
  85. return issue, nil
  86. }
  87. // UpdateIssuesCommit checks if issues are manipulated by commit message.
  88. func UpdateIssuesCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, commits []*repository.PushCommit, branchName string) error {
  89. // Commits are appended in the reverse order.
  90. for i := len(commits) - 1; i >= 0; i-- {
  91. c := commits[i]
  92. type markKey struct {
  93. ID int64
  94. Action references.XRefAction
  95. }
  96. refMarked := make(container.Set[markKey])
  97. var refRepo *repo_model.Repository
  98. var refIssue *issues_model.Issue
  99. var err error
  100. for _, ref := range references.FindAllIssueReferences(c.Message) {
  101. // issue is from another repo
  102. if len(ref.Owner) > 0 && len(ref.Name) > 0 {
  103. refRepo, err = repo_model.GetRepositoryByOwnerAndName(ctx, ref.Owner, ref.Name)
  104. if err != nil {
  105. if repo_model.IsErrRepoNotExist(err) {
  106. log.Warn("Repository referenced in commit but does not exist: %v", err)
  107. } else {
  108. log.Error("repo_model.GetRepositoryByOwnerAndName: %v", err)
  109. }
  110. continue
  111. }
  112. } else {
  113. refRepo = repo
  114. }
  115. if refIssue, err = getIssueFromRef(ctx, refRepo, ref.Index); err != nil {
  116. return err
  117. }
  118. if refIssue == nil {
  119. continue
  120. }
  121. perm, err := access_model.GetUserRepoPermission(ctx, refRepo, doer)
  122. if err != nil {
  123. return err
  124. }
  125. key := markKey{ID: refIssue.ID, Action: ref.Action}
  126. if !refMarked.Add(key) {
  127. continue
  128. }
  129. // FIXME: this kind of condition is all over the code, it should be consolidated in a single place
  130. canclose := perm.IsAdmin() || perm.IsOwner() || perm.CanWriteIssuesOrPulls(refIssue.IsPull) || refIssue.PosterID == doer.ID
  131. cancomment := canclose || perm.CanReadIssuesOrPulls(refIssue.IsPull)
  132. // Don't proceed if the user can't comment
  133. if !cancomment {
  134. continue
  135. }
  136. message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, html.EscapeString(repo.Link()), html.EscapeString(url.PathEscape(c.Sha1)), html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0]))
  137. if err = CreateRefComment(ctx, doer, refRepo, refIssue, message, c.Sha1); err != nil {
  138. if errors.Is(err, user_model.ErrBlockedUser) {
  139. continue
  140. }
  141. return err
  142. }
  143. // Only issues can be closed/reopened this way, and user needs the correct permissions
  144. if refIssue.IsPull || !canclose {
  145. continue
  146. }
  147. // Only process closing/reopening keywords
  148. if ref.Action != references.XRefActionCloses && ref.Action != references.XRefActionReopens {
  149. continue
  150. }
  151. if !repo.CloseIssuesViaCommitInAnyBranch {
  152. // If the issue was specified to be in a particular branch, don't allow commits in other branches to close it
  153. if refIssue.Ref != "" {
  154. issueBranchName := strings.TrimPrefix(refIssue.Ref, git.BranchPrefix)
  155. if branchName != issueBranchName {
  156. continue
  157. }
  158. // Otherwise, only process commits to the default branch
  159. } else if branchName != repo.DefaultBranch {
  160. continue
  161. }
  162. }
  163. refIssue.Repo = refRepo
  164. if ref.Action == references.XRefActionCloses && !refIssue.IsClosed {
  165. if len(ref.TimeLog) > 0 {
  166. if err := issueAddTime(ctx, refIssue, doer, c.Timestamp, ref.TimeLog); err != nil {
  167. return err
  168. }
  169. }
  170. if err := CloseIssue(ctx, refIssue, doer, c.Sha1); err != nil {
  171. return err
  172. }
  173. } else if ref.Action == references.XRefActionReopens && refIssue.IsClosed {
  174. if err := ReopenIssue(ctx, refIssue, doer, c.Sha1); err != nil {
  175. return err
  176. }
  177. }
  178. }
  179. }
  180. return nil
  181. }