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.

359 lines
10 KiB

  1. // Copyright 2019 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 repofiles
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "html"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/notification"
  17. "code.gitea.io/gitea/modules/references"
  18. "code.gitea.io/gitea/modules/repository"
  19. "code.gitea.io/gitea/modules/setting"
  20. )
  21. const (
  22. secondsByMinute = float64(time.Minute / time.Second) // seconds in a minute
  23. secondsByHour = 60 * secondsByMinute // seconds in an hour
  24. secondsByDay = 8 * secondsByHour // seconds in a day
  25. secondsByWeek = 5 * secondsByDay // seconds in a week
  26. secondsByMonth = 4 * secondsByWeek // seconds in a month
  27. )
  28. var reDuration = regexp.MustCompile(`(?i)^(?:(\d+([\.,]\d+)?)(?:mo))?(?:(\d+([\.,]\d+)?)(?:w))?(?:(\d+([\.,]\d+)?)(?:d))?(?:(\d+([\.,]\d+)?)(?:h))?(?:(\d+([\.,]\d+)?)(?:m))?$`)
  29. // getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
  30. // if the provided ref references a non-existent issue.
  31. func getIssueFromRef(repo *models.Repository, index int64) (*models.Issue, error) {
  32. issue, err := models.GetIssueByIndex(repo.ID, index)
  33. if err != nil {
  34. if models.IsErrIssueNotExist(err) {
  35. return nil, nil
  36. }
  37. return nil, err
  38. }
  39. return issue, nil
  40. }
  41. // timeLogToAmount parses time log string and returns amount in seconds
  42. func timeLogToAmount(str string) int64 {
  43. matches := reDuration.FindAllStringSubmatch(str, -1)
  44. if len(matches) == 0 {
  45. return 0
  46. }
  47. match := matches[0]
  48. var a int64
  49. // months
  50. if len(match[1]) > 0 {
  51. mo, _ := strconv.ParseFloat(strings.Replace(match[1], ",", ".", 1), 64)
  52. a += int64(mo * secondsByMonth)
  53. }
  54. // weeks
  55. if len(match[3]) > 0 {
  56. w, _ := strconv.ParseFloat(strings.Replace(match[3], ",", ".", 1), 64)
  57. a += int64(w * secondsByWeek)
  58. }
  59. // days
  60. if len(match[5]) > 0 {
  61. d, _ := strconv.ParseFloat(strings.Replace(match[5], ",", ".", 1), 64)
  62. a += int64(d * secondsByDay)
  63. }
  64. // hours
  65. if len(match[7]) > 0 {
  66. h, _ := strconv.ParseFloat(strings.Replace(match[7], ",", ".", 1), 64)
  67. a += int64(h * secondsByHour)
  68. }
  69. // minutes
  70. if len(match[9]) > 0 {
  71. d, _ := strconv.ParseFloat(strings.Replace(match[9], ",", ".", 1), 64)
  72. a += int64(d * secondsByMinute)
  73. }
  74. return a
  75. }
  76. func issueAddTime(issue *models.Issue, doer *models.User, time time.Time, timeLog string) error {
  77. amount := timeLogToAmount(timeLog)
  78. if amount == 0 {
  79. return nil
  80. }
  81. _, err := models.AddTime(doer, issue, amount, time)
  82. return err
  83. }
  84. func changeIssueStatus(repo *models.Repository, issue *models.Issue, doer *models.User, closed bool) error {
  85. stopTimerIfAvailable := func(doer *models.User, issue *models.Issue) error {
  86. if models.StopwatchExists(doer.ID, issue.ID) {
  87. if err := models.CreateOrStopIssueStopwatch(doer, issue); err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. issue.Repo = repo
  94. comment, err := issue.ChangeStatus(doer, closed)
  95. if err != nil {
  96. // Don't return an error when dependencies are open as this would let the push fail
  97. if models.IsErrDependenciesLeft(err) {
  98. return stopTimerIfAvailable(doer, issue)
  99. }
  100. return err
  101. }
  102. notification.NotifyIssueChangeStatus(doer, issue, comment, closed)
  103. return stopTimerIfAvailable(doer, issue)
  104. }
  105. // UpdateIssuesCommit checks if issues are manipulated by commit message.
  106. func UpdateIssuesCommit(doer *models.User, repo *models.Repository, commits []*repository.PushCommit, branchName string) error {
  107. // Commits are appended in the reverse order.
  108. for i := len(commits) - 1; i >= 0; i-- {
  109. c := commits[i]
  110. type markKey struct {
  111. ID int64
  112. Action references.XRefAction
  113. }
  114. refMarked := make(map[markKey]bool)
  115. var refRepo *models.Repository
  116. var refIssue *models.Issue
  117. var err error
  118. for _, ref := range references.FindAllIssueReferences(c.Message) {
  119. // issue is from another repo
  120. if len(ref.Owner) > 0 && len(ref.Name) > 0 {
  121. refRepo, err = models.GetRepositoryFromMatch(ref.Owner, ref.Name)
  122. if err != nil {
  123. continue
  124. }
  125. } else {
  126. refRepo = repo
  127. }
  128. if refIssue, err = getIssueFromRef(refRepo, ref.Index); err != nil {
  129. return err
  130. }
  131. if refIssue == nil {
  132. continue
  133. }
  134. perm, err := models.GetUserRepoPermission(refRepo, doer)
  135. if err != nil {
  136. return err
  137. }
  138. key := markKey{ID: refIssue.ID, Action: ref.Action}
  139. if refMarked[key] {
  140. continue
  141. }
  142. refMarked[key] = true
  143. // FIXME: this kind of condition is all over the code, it should be consolidated in a single place
  144. canclose := perm.IsAdmin() || perm.IsOwner() || perm.CanWriteIssuesOrPulls(refIssue.IsPull) || refIssue.PosterID == doer.ID
  145. cancomment := canclose || perm.CanReadIssuesOrPulls(refIssue.IsPull)
  146. // Don't proceed if the user can't comment
  147. if !cancomment {
  148. continue
  149. }
  150. message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0]))
  151. if err = models.CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil {
  152. return err
  153. }
  154. // Only issues can be closed/reopened this way, and user needs the correct permissions
  155. if refIssue.IsPull || !canclose {
  156. continue
  157. }
  158. // Only process closing/reopening keywords
  159. if ref.Action != references.XRefActionCloses && ref.Action != references.XRefActionReopens {
  160. continue
  161. }
  162. if !repo.CloseIssuesViaCommitInAnyBranch {
  163. // If the issue was specified to be in a particular branch, don't allow commits in other branches to close it
  164. if refIssue.Ref != "" {
  165. if branchName != refIssue.Ref {
  166. continue
  167. }
  168. // Otherwise, only process commits to the default branch
  169. } else if branchName != repo.DefaultBranch {
  170. continue
  171. }
  172. }
  173. close := (ref.Action == references.XRefActionCloses)
  174. if close && len(ref.TimeLog) > 0 {
  175. if err := issueAddTime(refIssue, doer, c.Timestamp, ref.TimeLog); err != nil {
  176. return err
  177. }
  178. }
  179. if close != refIssue.IsClosed {
  180. if err := changeIssueStatus(refRepo, refIssue, doer, close); err != nil {
  181. return err
  182. }
  183. }
  184. }
  185. }
  186. return nil
  187. }
  188. // CommitRepoActionOptions represent options of a new commit action.
  189. type CommitRepoActionOptions struct {
  190. PushUpdateOptions
  191. RepoOwnerID int64
  192. Commits *repository.PushCommits
  193. }
  194. // CommitRepoAction adds new commit action to the repository, and prepare
  195. // corresponding webhooks.
  196. func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
  197. var pusher *models.User
  198. var repo *models.Repository
  199. actions := make([]*models.Action, len(optsList))
  200. for i, opts := range optsList {
  201. if pusher == nil || pusher.Name != opts.PusherName {
  202. var err error
  203. pusher, err = models.GetUserByName(opts.PusherName)
  204. if err != nil {
  205. return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
  206. }
  207. }
  208. if repo == nil || repo.OwnerID != opts.RepoOwnerID || repo.Name != opts.RepoName {
  209. var err error
  210. if repo != nil {
  211. // Change repository empty status and update last updated time.
  212. if err := models.UpdateRepository(repo, false); err != nil {
  213. return fmt.Errorf("UpdateRepository: %v", err)
  214. }
  215. }
  216. repo, err = models.GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
  217. if err != nil {
  218. return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
  219. }
  220. }
  221. refName := git.RefEndName(opts.RefFullName)
  222. // Change default branch and empty status only if pushed ref is non-empty branch.
  223. if repo.IsEmpty && opts.IsBranch() && !opts.IsDelRef() {
  224. repo.DefaultBranch = refName
  225. repo.IsEmpty = false
  226. if refName != "master" {
  227. gitRepo, err := git.OpenRepository(repo.RepoPath())
  228. if err != nil {
  229. return err
  230. }
  231. if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
  232. if !git.IsErrUnsupportedVersion(err) {
  233. gitRepo.Close()
  234. return err
  235. }
  236. }
  237. gitRepo.Close()
  238. }
  239. }
  240. opType := models.ActionCommitRepo
  241. // Check it's tag push or branch.
  242. if opts.IsTag() {
  243. opType = models.ActionPushTag
  244. if opts.IsDelRef() {
  245. opType = models.ActionDeleteTag
  246. }
  247. opts.Commits = &repository.PushCommits{}
  248. } else if opts.IsDelRef() {
  249. opType = models.ActionDeleteBranch
  250. opts.Commits = &repository.PushCommits{}
  251. } else {
  252. // if not the first commit, set the compare URL.
  253. if !opts.IsNewRef() {
  254. opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
  255. }
  256. if err := UpdateIssuesCommit(pusher, repo, opts.Commits.Commits, refName); err != nil {
  257. log.Error("updateIssuesCommit: %v", err)
  258. }
  259. }
  260. if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
  261. opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
  262. }
  263. data, err := json.Marshal(opts.Commits)
  264. if err != nil {
  265. return fmt.Errorf("Marshal: %v", err)
  266. }
  267. actions[i] = &models.Action{
  268. ActUserID: pusher.ID,
  269. ActUser: pusher,
  270. OpType: opType,
  271. Content: string(data),
  272. RepoID: repo.ID,
  273. Repo: repo,
  274. RefName: refName,
  275. IsPrivate: repo.IsPrivate,
  276. }
  277. var isHookEventPush = true
  278. switch opType {
  279. case models.ActionCommitRepo: // Push
  280. if opts.IsNewBranch() {
  281. notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName)
  282. }
  283. case models.ActionDeleteBranch: // Delete Branch
  284. notification.NotifyDeleteRef(pusher, repo, "branch", opts.RefFullName)
  285. case models.ActionPushTag: // Create
  286. notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName)
  287. case models.ActionDeleteTag: // Delete Tag
  288. notification.NotifyDeleteRef(pusher, repo, "tag", opts.RefFullName)
  289. default:
  290. isHookEventPush = false
  291. }
  292. if isHookEventPush {
  293. notification.NotifyPushCommits(pusher, repo, opts.RefFullName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
  294. }
  295. }
  296. if repo != nil {
  297. // Change repository empty status and update last updated time.
  298. if err := models.UpdateRepository(repo, false); err != nil {
  299. return fmt.Errorf("UpdateRepository: %v", err)
  300. }
  301. }
  302. if err := models.NotifyWatchers(actions...); err != nil {
  303. return fmt.Errorf("NotifyWatchers: %v", err)
  304. }
  305. return nil
  306. }