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.

action.go 8.2KB

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