code.gitea.io/gitea@v1.19.3/modules/notification/notification.go (about)

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