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 13KB

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