code.gitea.io/gitea@v1.21.7/services/notify/notify.go (about)

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