You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

action.go 8.6 kB

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