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.

notification.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package notification
  4. import (
  5. "context"
  6. issues_model "code.gitea.io/gitea/models/issues"
  7. packages_model "code.gitea.io/gitea/models/packages"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/notification/action"
  12. "code.gitea.io/gitea/modules/notification/base"
  13. "code.gitea.io/gitea/modules/notification/indexer"
  14. "code.gitea.io/gitea/modules/notification/mail"
  15. "code.gitea.io/gitea/modules/notification/mirror"
  16. "code.gitea.io/gitea/modules/notification/ui"
  17. "code.gitea.io/gitea/modules/repository"
  18. "code.gitea.io/gitea/modules/setting"
  19. )
  20. var notifiers []base.Notifier
  21. // RegisterNotifier providers method to receive notify messages
  22. func RegisterNotifier(notifier base.Notifier) {
  23. go notifier.Run()
  24. notifiers = append(notifiers, notifier)
  25. }
  26. // NewContext registers notification handlers
  27. func NewContext() {
  28. RegisterNotifier(ui.NewNotifier())
  29. if setting.Service.EnableNotifyMail {
  30. RegisterNotifier(mail.NewNotifier())
  31. }
  32. RegisterNotifier(indexer.NewNotifier())
  33. RegisterNotifier(action.NewNotifier())
  34. RegisterNotifier(mirror.NewNotifier())
  35. }
  36. // NotifyNewWikiPage notifies creating new wiki pages to notifiers
  37. func NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
  38. for _, notifier := range notifiers {
  39. notifier.NotifyNewWikiPage(ctx, doer, repo, page, comment)
  40. }
  41. }
  42. // NotifyEditWikiPage notifies editing or renaming wiki pages to notifiers
  43. func NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
  44. for _, notifier := range notifiers {
  45. notifier.NotifyEditWikiPage(ctx, doer, repo, page, comment)
  46. }
  47. }
  48. // NotifyDeleteWikiPage notifies deleting wiki pages to notifiers
  49. func NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
  50. for _, notifier := range notifiers {
  51. notifier.NotifyDeleteWikiPage(ctx, doer, repo, page)
  52. }
  53. }
  54. // NotifyCreateIssueComment notifies issue comment related message to notifiers
  55. func NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
  56. issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
  57. ) {
  58. for _, notifier := range notifiers {
  59. notifier.NotifyCreateIssueComment(ctx, doer, repo, issue, comment, mentions)
  60. }
  61. }
  62. // NotifyNewIssue notifies new issue to notifiers
  63. func NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
  64. for _, notifier := range notifiers {
  65. notifier.NotifyNewIssue(ctx, issue, mentions)
  66. }
  67. }
  68. // NotifyIssueChangeStatus notifies close or reopen issue to notifiers
  69. func NotifyIssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, closeOrReopen bool) {
  70. for _, notifier := range notifiers {
  71. notifier.NotifyIssueChangeStatus(ctx, doer, commitID, issue, actionComment, closeOrReopen)
  72. }
  73. }
  74. // NotifyDeleteIssue notify when some issue deleted
  75. func NotifyDeleteIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
  76. for _, notifier := range notifiers {
  77. notifier.NotifyDeleteIssue(ctx, doer, issue)
  78. }
  79. }
  80. // NotifyMergePullRequest notifies merge pull request to notifiers
  81. func NotifyMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  82. for _, notifier := range notifiers {
  83. notifier.NotifyMergePullRequest(ctx, doer, pr)
  84. }
  85. }
  86. // NotifyAutoMergePullRequest notifies merge pull request to notifiers
  87. func NotifyAutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  88. for _, notifier := range notifiers {
  89. notifier.NotifyAutoMergePullRequest(ctx, doer, pr)
  90. }
  91. }
  92. // NotifyNewPullRequest notifies new pull request to notifiers
  93. func NotifyNewPullRequest(ctx context.Context, pr *issues_model.PullRequest, mentions []*user_model.User) {
  94. if err := pr.LoadIssue(ctx); err != nil {
  95. log.Error("%v", err)
  96. return
  97. }
  98. if err := pr.Issue.LoadPoster(ctx); err != nil {
  99. return
  100. }
  101. for _, notifier := range notifiers {
  102. notifier.NotifyNewPullRequest(ctx, pr, mentions)
  103. }
  104. }
  105. // NotifyPullRequestSynchronized notifies Synchronized pull request
  106. func NotifyPullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
  107. for _, notifier := range notifiers {
  108. notifier.NotifyPullRequestSynchronized(ctx, doer, pr)
  109. }
  110. }
  111. // NotifyPullRequestReview notifies new pull request review
  112. func NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
  113. if err := review.LoadReviewer(ctx); err != nil {
  114. log.Error("%v", err)
  115. return
  116. }
  117. for _, notifier := range notifiers {
  118. notifier.NotifyPullRequestReview(ctx, pr, review, comment, mentions)
  119. }
  120. }
  121. // NotifyPullRequestCodeComment notifies new pull request code comment
  122. func NotifyPullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, mentions []*user_model.User) {
  123. if err := comment.LoadPoster(ctx); err != nil {
  124. log.Error("LoadPoster: %v", err)
  125. return
  126. }
  127. for _, notifier := range notifiers {
  128. notifier.NotifyPullRequestCodeComment(ctx, pr, comment, mentions)
  129. }
  130. }
  131. // NotifyPullRequestChangeTargetBranch notifies when a pull request's target branch was changed
  132. func NotifyPullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
  133. for _, notifier := range notifiers {
  134. notifier.NotifyPullRequestChangeTargetBranch(ctx, doer, pr, oldBranch)
  135. }
  136. }
  137. // NotifyPullRequestPushCommits notifies when push commits to pull request's head branch
  138. func NotifyPullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment) {
  139. for _, notifier := range notifiers {
  140. notifier.NotifyPullRequestPushCommits(ctx, doer, pr, comment)
  141. }
  142. }
  143. // NotifyPullReviewDismiss notifies when a review was dismissed by repo admin
  144. func NotifyPullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) {
  145. for _, notifier := range notifiers {
  146. notifier.NotifyPullReviewDismiss(ctx, doer, review, comment)
  147. }
  148. }
  149. // NotifyUpdateComment notifies update comment to notifiers
  150. func NotifyUpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
  151. for _, notifier := range notifiers {
  152. notifier.NotifyUpdateComment(ctx, doer, c, oldContent)
  153. }
  154. }
  155. // NotifyDeleteComment notifies delete comment to notifiers
  156. func NotifyDeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment) {
  157. for _, notifier := range notifiers {
  158. notifier.NotifyDeleteComment(ctx, doer, c)
  159. }
  160. }
  161. // NotifyNewRelease notifies new release to notifiers
  162. func NotifyNewRelease(ctx context.Context, rel *repo_model.Release) {
  163. if err := rel.LoadAttributes(ctx); err != nil {
  164. log.Error("LoadPublisher: %v", err)
  165. return
  166. }
  167. for _, notifier := range notifiers {
  168. notifier.NotifyNewRelease(ctx, rel)
  169. }
  170. }
  171. // NotifyUpdateRelease notifies update release to notifiers
  172. func NotifyUpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
  173. for _, notifier := range notifiers {
  174. notifier.NotifyUpdateRelease(ctx, doer, rel)
  175. }
  176. }
  177. // NotifyDeleteRelease notifies delete release to notifiers
  178. func NotifyDeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
  179. for _, notifier := range notifiers {
  180. notifier.NotifyDeleteRelease(ctx, doer, rel)
  181. }
  182. }
  183. // NotifyIssueChangeMilestone notifies change milestone to notifiers
  184. func NotifyIssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
  185. for _, notifier := range notifiers {
  186. notifier.NotifyIssueChangeMilestone(ctx, doer, issue, oldMilestoneID)
  187. }
  188. }
  189. // NotifyIssueChangeContent notifies change content to notifiers
  190. func NotifyIssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
  191. for _, notifier := range notifiers {
  192. notifier.NotifyIssueChangeContent(ctx, doer, issue, oldContent)
  193. }
  194. }
  195. // NotifyIssueChangeAssignee notifies change content to notifiers
  196. func NotifyIssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
  197. for _, notifier := range notifiers {
  198. notifier.NotifyIssueChangeAssignee(ctx, doer, issue, assignee, removed, comment)
  199. }
  200. }
  201. // NotifyPullReviewRequest notifies Request Review change
  202. func NotifyPullReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) {
  203. for _, notifier := range notifiers {
  204. notifier.NotifyPullReviewRequest(ctx, doer, issue, reviewer, isRequest, comment)
  205. }
  206. }
  207. // NotifyIssueClearLabels notifies clear labels to notifiers
  208. func NotifyIssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
  209. for _, notifier := range notifiers {
  210. notifier.NotifyIssueClearLabels(ctx, doer, issue)
  211. }
  212. }
  213. // NotifyIssueChangeTitle notifies change title to notifiers
  214. func NotifyIssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
  215. for _, notifier := range notifiers {
  216. notifier.NotifyIssueChangeTitle(ctx, doer, issue, oldTitle)
  217. }
  218. }
  219. // NotifyIssueChangeRef notifies change reference to notifiers
  220. func NotifyIssueChangeRef(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldRef string) {
  221. for _, notifier := range notifiers {
  222. notifier.NotifyIssueChangeRef(ctx, doer, issue, oldRef)
  223. }
  224. }
  225. // NotifyIssueChangeLabels notifies change labels to notifiers
  226. func NotifyIssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
  227. addedLabels, removedLabels []*issues_model.Label,
  228. ) {
  229. for _, notifier := range notifiers {
  230. notifier.NotifyIssueChangeLabels(ctx, doer, issue, addedLabels, removedLabels)
  231. }
  232. }
  233. // NotifyCreateRepository notifies create repository to notifiers
  234. func NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
  235. for _, notifier := range notifiers {
  236. notifier.NotifyCreateRepository(ctx, doer, u, repo)
  237. }
  238. }
  239. // NotifyMigrateRepository notifies create repository to notifiers
  240. func NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
  241. for _, notifier := range notifiers {
  242. notifier.NotifyMigrateRepository(ctx, doer, u, repo)
  243. }
  244. }
  245. // NotifyTransferRepository notifies create repository to notifiers
  246. func NotifyTransferRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, newOwnerName string) {
  247. for _, notifier := range notifiers {
  248. notifier.NotifyTransferRepository(ctx, doer, repo, newOwnerName)
  249. }
  250. }
  251. // NotifyDeleteRepository notifies delete repository to notifiers
  252. func NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
  253. for _, notifier := range notifiers {
  254. notifier.NotifyDeleteRepository(ctx, doer, repo)
  255. }
  256. }
  257. // NotifyForkRepository notifies fork repository to notifiers
  258. func NotifyForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
  259. for _, notifier := range notifiers {
  260. notifier.NotifyForkRepository(ctx, doer, oldRepo, repo)
  261. }
  262. }
  263. // NotifyRenameRepository notifies repository renamed
  264. func NotifyRenameRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldName string) {
  265. for _, notifier := range notifiers {
  266. notifier.NotifyRenameRepository(ctx, doer, repo, oldName)
  267. }
  268. }
  269. // NotifyPushCommits notifies commits pushed to notifiers
  270. func NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
  271. for _, notifier := range notifiers {
  272. notifier.NotifyPushCommits(ctx, pusher, repo, opts, commits)
  273. }
  274. }
  275. // NotifyCreateRef notifies branch or tag creation to notifiers
  276. func NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
  277. for _, notifier := range notifiers {
  278. notifier.NotifyCreateRef(ctx, pusher, repo, refType, refFullName, refID)
  279. }
  280. }
  281. // NotifyDeleteRef notifies branch or tag deletion to notifiers
  282. func NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
  283. for _, notifier := range notifiers {
  284. notifier.NotifyDeleteRef(ctx, pusher, repo, refType, refFullName)
  285. }
  286. }
  287. // NotifySyncPushCommits notifies commits pushed to notifiers
  288. func NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
  289. for _, notifier := range notifiers {
  290. notifier.NotifySyncPushCommits(ctx, pusher, repo, opts, commits)
  291. }
  292. }
  293. // NotifySyncCreateRef notifies branch or tag creation to notifiers
  294. func NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
  295. for _, notifier := range notifiers {
  296. notifier.NotifySyncCreateRef(ctx, pusher, repo, refType, refFullName, refID)
  297. }
  298. }
  299. // NotifySyncDeleteRef notifies branch or tag deletion to notifiers
  300. func NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
  301. for _, notifier := range notifiers {
  302. notifier.NotifySyncDeleteRef(ctx, pusher, repo, refType, refFullName)
  303. }
  304. }
  305. // NotifyRepoPendingTransfer notifies creation of pending transfer to notifiers
  306. func NotifyRepoPendingTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) {
  307. for _, notifier := range notifiers {
  308. notifier.NotifyRepoPendingTransfer(ctx, doer, newOwner, repo)
  309. }
  310. }
  311. // NotifyPackageCreate notifies creation of a package to notifiers
  312. func NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
  313. for _, notifier := range notifiers {
  314. notifier.NotifyPackageCreate(ctx, doer, pd)
  315. }
  316. }
  317. // NotifyPackageDelete notifies deletion of a package to notifiers
  318. func NotifyPackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
  319. for _, notifier := range notifiers {
  320. notifier.NotifyPackageDelete(ctx, doer, pd)
  321. }
  322. }