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.

notify.go 13KB

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