gitea源码

notify.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package notify
  4. import (
  5. "context"
  6. actions_model "code.gitea.io/gitea/models/actions"
  7. git_model "code.gitea.io/gitea/models/git"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. packages_model "code.gitea.io/gitea/models/packages"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/repository"
  15. )
  16. var notifiers []Notifier
  17. // RegisterNotifier providers method to receive notify messages
  18. func RegisterNotifier(notifier Notifier) {
  19. go notifier.Run()
  20. notifiers = append(notifiers, notifier)
  21. }
  22. // NewWikiPage notifies creating new wiki pages to notifiers
  23. func NewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
  24. for _, notifier := range notifiers {
  25. notifier.NewWikiPage(ctx, doer, repo, page, comment)
  26. }
  27. }
  28. // EditWikiPage notifies editing or renaming wiki pages to notifiers
  29. func EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
  30. for _, notifier := range notifiers {
  31. notifier.EditWikiPage(ctx, doer, repo, page, comment)
  32. }
  33. }
  34. // DeleteWikiPage notifies deleting wiki pages to notifiers
  35. func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
  36. for _, notifier := range notifiers {
  37. notifier.DeleteWikiPage(ctx, doer, repo, page)
  38. }
  39. }
  40. func shouldSendCommentChangeNotification(ctx context.Context, comment *issues_model.Comment) bool {
  41. if err := comment.LoadReview(ctx); err != nil {
  42. log.Error("LoadReview: %v", err)
  43. return false
  44. } else if comment.Review != nil && comment.Review.Type == issues_model.ReviewTypePending {
  45. // Pending review comments updating should not triggered
  46. return false
  47. }
  48. return true
  49. }
  50. // CreateIssueComment notifies issue comment related message to notifiers
  51. func CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
  52. issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
  53. ) {
  54. if !shouldSendCommentChangeNotification(ctx, comment) {
  55. return
  56. }
  57. for _, notifier := range notifiers {
  58. notifier.CreateIssueComment(ctx, doer, repo, issue, comment, mentions)
  59. }
  60. }
  61. // NewIssue notifies new issue to notifiers
  62. func NewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
  63. for _, notifier := range notifiers {
  64. notifier.NewIssue(ctx, issue, mentions)
  65. }
  66. }
  67. // IssueChangeStatus notifies close or reopen issue to notifiers
  68. func IssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, closeOrReopen bool) {
  69. for _, notifier := range notifiers {
  70. notifier.IssueChangeStatus(ctx, doer, commitID, issue, actionComment, closeOrReopen)
  71. }
  72. }
  73. // DeleteIssue notify when some issue deleted
  74. func DeleteIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
  75. for _, notifier := range notifiers {
  76. notifier.DeleteIssue(ctx, doer, issue)
  77. }
  78. }
  79. // MergePullRequest notifies merge pull request to notifiers
  80. func MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  81. for _, notifier := range notifiers {
  82. notifier.MergePullRequest(ctx, doer, pr)
  83. }
  84. }
  85. // AutoMergePullRequest notifies merge pull request to notifiers
  86. func AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  87. for _, notifier := range notifiers {
  88. notifier.AutoMergePullRequest(ctx, doer, pr)
  89. }
  90. }
  91. // NewPullRequest notifies new pull request to notifiers
  92. func NewPullRequest(ctx context.Context, pr *issues_model.PullRequest, mentions []*user_model.User) {
  93. if err := pr.LoadIssue(ctx); err != nil {
  94. log.Error("LoadIssue failed: %v", err)
  95. return
  96. }
  97. if err := pr.Issue.LoadPoster(ctx); err != nil {
  98. return
  99. }
  100. for _, notifier := range notifiers {
  101. notifier.NewPullRequest(ctx, pr, mentions)
  102. }
  103. }
  104. // PullRequestSynchronized notifies Synchronized pull request
  105. func PullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  106. for _, notifier := range notifiers {
  107. notifier.PullRequestSynchronized(ctx, doer, pr)
  108. }
  109. }
  110. // PullRequestReview notifies new pull request review
  111. func PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
  112. if err := review.LoadReviewer(ctx); err != nil {
  113. log.Error("LoadReviewer failed: %v", err)
  114. return
  115. }
  116. for _, notifier := range notifiers {
  117. notifier.PullRequestReview(ctx, pr, review, comment, mentions)
  118. }
  119. }
  120. // PullRequestCodeComment notifies new pull request code comment
  121. func PullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, mentions []*user_model.User) {
  122. if err := comment.LoadPoster(ctx); err != nil {
  123. log.Error("LoadPoster: %v", err)
  124. return
  125. }
  126. for _, notifier := range notifiers {
  127. notifier.PullRequestCodeComment(ctx, pr, comment, mentions)
  128. }
  129. }
  130. // PullRequestChangeTargetBranch notifies when a pull request's target branch was changed
  131. func PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
  132. for _, notifier := range notifiers {
  133. notifier.PullRequestChangeTargetBranch(ctx, doer, pr, oldBranch)
  134. }
  135. }
  136. // PullRequestPushCommits notifies when push commits to pull request's head branch
  137. func PullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment) {
  138. for _, notifier := range notifiers {
  139. notifier.PullRequestPushCommits(ctx, doer, pr, comment)
  140. }
  141. }
  142. // PullReviewDismiss notifies when a review was dismissed by repo admin
  143. func PullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) {
  144. for _, notifier := range notifiers {
  145. notifier.PullReviewDismiss(ctx, doer, review, comment)
  146. }
  147. }
  148. // UpdateComment notifies update comment to notifiers
  149. func UpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
  150. if !shouldSendCommentChangeNotification(ctx, c) {
  151. return
  152. }
  153. for _, notifier := range notifiers {
  154. notifier.UpdateComment(ctx, doer, c, oldContent)
  155. }
  156. }
  157. // DeleteComment notifies delete comment to notifiers
  158. func DeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment) {
  159. if !shouldSendCommentChangeNotification(ctx, c) {
  160. return
  161. }
  162. for _, notifier := range notifiers {
  163. notifier.DeleteComment(ctx, doer, c)
  164. }
  165. }
  166. // NewRelease notifies new release to notifiers
  167. func NewRelease(ctx context.Context, rel *repo_model.Release) {
  168. if err := rel.LoadAttributes(ctx); err != nil {
  169. log.Error("LoadPublisher: %v", err)
  170. return
  171. }
  172. for _, notifier := range notifiers {
  173. notifier.NewRelease(ctx, rel)
  174. }
  175. }
  176. // UpdateRelease notifies update release to notifiers
  177. func UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
  178. for _, notifier := range notifiers {
  179. notifier.UpdateRelease(ctx, doer, rel)
  180. }
  181. }
  182. // DeleteRelease notifies delete release to notifiers
  183. func DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
  184. for _, notifier := range notifiers {
  185. notifier.DeleteRelease(ctx, doer, rel)
  186. }
  187. }
  188. // IssueChangeMilestone notifies change milestone to notifiers
  189. func IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
  190. for _, notifier := range notifiers {
  191. notifier.IssueChangeMilestone(ctx, doer, issue, oldMilestoneID)
  192. }
  193. }
  194. // IssueChangeContent notifies change content to notifiers
  195. func IssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
  196. for _, notifier := range notifiers {
  197. notifier.IssueChangeContent(ctx, doer, issue, oldContent)
  198. }
  199. }
  200. // IssueChangeAssignee notifies change content to notifiers
  201. func IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
  202. for _, notifier := range notifiers {
  203. notifier.IssueChangeAssignee(ctx, doer, issue, assignee, removed, comment)
  204. }
  205. }
  206. // PullRequestReviewRequest notifies Request Review change
  207. func PullRequestReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) {
  208. for _, notifier := range notifiers {
  209. notifier.PullRequestReviewRequest(ctx, doer, issue, reviewer, isRequest, comment)
  210. }
  211. }
  212. // IssueClearLabels notifies clear labels to notifiers
  213. func IssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
  214. for _, notifier := range notifiers {
  215. notifier.IssueClearLabels(ctx, doer, issue)
  216. }
  217. }
  218. // IssueChangeTitle notifies change title to notifiers
  219. func IssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
  220. for _, notifier := range notifiers {
  221. notifier.IssueChangeTitle(ctx, doer, issue, oldTitle)
  222. }
  223. }
  224. // IssueChangeRef notifies change reference to notifiers
  225. func IssueChangeRef(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldRef string) {
  226. for _, notifier := range notifiers {
  227. notifier.IssueChangeRef(ctx, doer, issue, oldRef)
  228. }
  229. }
  230. // IssueChangeLabels notifies change labels to notifiers
  231. func IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
  232. addedLabels, removedLabels []*issues_model.Label,
  233. ) {
  234. for _, notifier := range notifiers {
  235. notifier.IssueChangeLabels(ctx, doer, issue, addedLabels, removedLabels)
  236. }
  237. }
  238. // CreateRepository notifies create repository to notifiers
  239. func CreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
  240. for _, notifier := range notifiers {
  241. notifier.CreateRepository(ctx, doer, u, repo)
  242. }
  243. }
  244. // AdoptRepository notifies the adoption of a repository to notifiers
  245. func AdoptRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
  246. for _, notifier := range notifiers {
  247. notifier.AdoptRepository(ctx, doer, u, repo)
  248. }
  249. }
  250. // MigrateRepository notifies create repository to notifiers
  251. func MigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
  252. for _, notifier := range notifiers {
  253. notifier.MigrateRepository(ctx, doer, u, repo)
  254. }
  255. }
  256. // TransferRepository notifies create repository to notifiers
  257. func TransferRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldOwnerName string) {
  258. for _, notifier := range notifiers {
  259. notifier.TransferRepository(ctx, doer, repo, oldOwnerName)
  260. }
  261. }
  262. // DeleteRepository notifies delete repository to notifiers
  263. func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
  264. for _, notifier := range notifiers {
  265. notifier.DeleteRepository(ctx, doer, repo)
  266. }
  267. }
  268. // ForkRepository notifies fork repository to notifiers
  269. func ForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
  270. for _, notifier := range notifiers {
  271. notifier.ForkRepository(ctx, doer, oldRepo, repo)
  272. }
  273. }
  274. // RenameRepository notifies repository renamed
  275. func RenameRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldName string) {
  276. for _, notifier := range notifiers {
  277. notifier.RenameRepository(ctx, doer, repo, oldName)
  278. }
  279. }
  280. // PushCommits notifies commits pushed to notifiers
  281. func PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
  282. for _, notifier := range notifiers {
  283. notifier.PushCommits(ctx, pusher, repo, opts, commits)
  284. }
  285. }
  286. // CreateRef notifies branch or tag creation to notifiers
  287. func CreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
  288. for _, notifier := range notifiers {
  289. notifier.CreateRef(ctx, pusher, repo, refFullName, refID)
  290. }
  291. }
  292. // DeleteRef notifies branch or tag deletion to notifiers
  293. func DeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
  294. for _, notifier := range notifiers {
  295. notifier.DeleteRef(ctx, pusher, repo, refFullName)
  296. }
  297. }
  298. // SyncPushCommits notifies commits pushed to notifiers
  299. func SyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
  300. for _, notifier := range notifiers {
  301. notifier.SyncPushCommits(ctx, pusher, repo, opts, commits)
  302. }
  303. }
  304. // SyncCreateRef notifies branch or tag creation to notifiers
  305. func SyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
  306. for _, notifier := range notifiers {
  307. notifier.SyncCreateRef(ctx, pusher, repo, refFullName, refID)
  308. }
  309. }
  310. // SyncDeleteRef notifies branch or tag deletion to notifiers
  311. func SyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
  312. for _, notifier := range notifiers {
  313. notifier.SyncDeleteRef(ctx, pusher, repo, refFullName)
  314. }
  315. }
  316. // RepoPendingTransfer notifies creation of pending transfer to notifiers
  317. func RepoPendingTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) {
  318. for _, notifier := range notifiers {
  319. notifier.RepoPendingTransfer(ctx, doer, newOwner, repo)
  320. }
  321. }
  322. // PackageCreate notifies creation of a package to notifiers
  323. func PackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
  324. for _, notifier := range notifiers {
  325. notifier.PackageCreate(ctx, doer, pd)
  326. }
  327. }
  328. // PackageDelete notifies deletion of a package to notifiers
  329. func PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
  330. for _, notifier := range notifiers {
  331. notifier.PackageDelete(ctx, doer, pd)
  332. }
  333. }
  334. // ChangeDefaultBranch notifies change default branch to notifiers
  335. func ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
  336. for _, notifier := range notifiers {
  337. notifier.ChangeDefaultBranch(ctx, repo)
  338. }
  339. }
  340. func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) {
  341. for _, notifier := range notifiers {
  342. notifier.CreateCommitStatus(ctx, repo, commit, sender, status)
  343. }
  344. }
  345. func WorkflowRunStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, run *actions_model.ActionRun) {
  346. for _, notifier := range notifiers {
  347. notifier.WorkflowRunStatusUpdate(ctx, repo, sender, run)
  348. }
  349. }
  350. func WorkflowJobStatusUpdate(ctx context.Context, repo *repo_model.Repository, sender *user_model.User, job *actions_model.ActionRunJob, task *actions_model.ActionTask) {
  351. for _, notifier := range notifiers {
  352. notifier.WorkflowJobStatusUpdate(ctx, repo, sender, job, task)
  353. }
  354. }