From 88b0ab89d0906023a8f3b57f60377f0ad21f0af7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 19 Nov 2022 16:20:36 +0800 Subject: [PATCH] Gitea bot will not fire the events otherwise it may causes a forever loop --- modules/notification/notification.go | 149 +++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) diff --git a/modules/notification/notification.go b/modules/notification/notification.go index e122dcf604..fe1abf9386 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -11,6 +11,7 @@ import ( packages_model "code.gitea.io/gitea/models/packages" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/action" "code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/notification/bots" @@ -46,6 +47,9 @@ func NewContext() { // NotifyNewWikiPage notifies creating new wiki pages to notifiers func NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyNewWikiPage(ctx, doer, repo, page, comment) } @@ -53,6 +57,9 @@ func NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_mo // NotifyEditWikiPage notifies editing or renaming wiki pages to notifiers func NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyEditWikiPage(ctx, doer, repo, page, comment) } @@ -60,6 +67,9 @@ func NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_m // NotifyDeleteWikiPage notifies deleting wiki pages to notifiers func NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyDeleteWikiPage(ctx, doer, repo, page) } @@ -69,6 +79,9 @@ func NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo func NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User, ) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyCreateIssueComment(ctx, doer, repo, issue, comment, mentions) } @@ -76,6 +89,9 @@ func NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo * // NotifyNewIssue notifies new issue to notifiers func NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) { + if issue.Poster.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyNewIssue(ctx, issue, mentions) } @@ -83,6 +99,9 @@ func NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, mentions []* // NotifyIssueChangeStatus notifies close or reopen issue to notifiers func NotifyIssueChangeStatus(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, actionComment *issues_model.Comment, closeOrReopen bool) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueChangeStatus(ctx, doer, issue, actionComment, closeOrReopen) } @@ -90,6 +109,9 @@ func NotifyIssueChangeStatus(ctx context.Context, doer *user_model.User, issue * // NotifyDeleteIssue notify when some issue deleted func NotifyDeleteIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyDeleteIssue(ctx, doer, issue) } @@ -97,6 +119,9 @@ func NotifyDeleteIssue(ctx context.Context, doer *user_model.User, issue *issues // NotifyMergePullRequest notifies merge pull request to notifiers func NotifyMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyMergePullRequest(ctx, doer, pr) } @@ -104,6 +129,9 @@ func NotifyMergePullRequest(ctx context.Context, doer *user_model.User, pr *issu // NotifyAutoMergePullRequest notifies merge pull request to notifiers func NotifyAutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyAutoMergePullRequest(ctx, doer, pr) } @@ -111,6 +139,13 @@ func NotifyAutoMergePullRequest(ctx context.Context, doer *user_model.User, pr * // NotifyNewPullRequest notifies new pull request to notifiers func NotifyNewPullRequest(ctx context.Context, pr *issues_model.PullRequest, mentions []*user_model.User) { + if err := pr.LoadIssue(ctx); err != nil { + log.Error("%v", err) + return + } + if pr.Issue.Poster.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyNewPullRequest(ctx, pr, mentions) } @@ -118,6 +153,9 @@ func NotifyNewPullRequest(ctx context.Context, pr *issues_model.PullRequest, men // NotifyPullRequestSynchronized notifies Synchronized pull request func NotifyPullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPullRequestSynchronized(ctx, doer, pr) } @@ -125,6 +163,13 @@ func NotifyPullRequestSynchronized(ctx context.Context, doer *user_model.User, p // NotifyPullRequestReview notifies new pull request review func NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) { + if err := review.LoadReviewer(ctx); err != nil { + log.Error("%v", err) + return + } + if review.Reviewer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPullRequestReview(ctx, pr, review, comment, mentions) } @@ -132,6 +177,13 @@ func NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, // NotifyPullRequestCodeComment notifies new pull request code comment func NotifyPullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, mentions []*user_model.User) { + if err := comment.LoadPoster(ctx); err != nil { + log.Error("LoadPoster: %v", err) + return + } + if comment.Poster.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPullRequestCodeComment(ctx, pr, comment, mentions) } @@ -139,6 +191,9 @@ func NotifyPullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequ // NotifyPullRequestChangeTargetBranch notifies when a pull request's target branch was changed func NotifyPullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPullRequestChangeTargetBranch(ctx, doer, pr, oldBranch) } @@ -146,6 +201,9 @@ func NotifyPullRequestChangeTargetBranch(ctx context.Context, doer *user_model.U // NotifyPullRequestPushCommits notifies when push commits to pull request's head branch func NotifyPullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPullRequestPushCommits(ctx, doer, pr, comment) } @@ -153,6 +211,9 @@ func NotifyPullRequestPushCommits(ctx context.Context, doer *user_model.User, pr // NotifyPullReviewDismiss notifies when a review was dismissed by repo admin func NotifyPullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPullReviewDismiss(ctx, doer, review, comment) } @@ -160,6 +221,9 @@ func NotifyPullReviewDismiss(ctx context.Context, doer *user_model.User, review // NotifyUpdateComment notifies update comment to notifiers func NotifyUpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyUpdateComment(ctx, doer, c, oldContent) } @@ -167,6 +231,9 @@ func NotifyUpdateComment(ctx context.Context, doer *user_model.User, c *issues_m // NotifyDeleteComment notifies delete comment to notifiers func NotifyDeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyDeleteComment(ctx, doer, c) } @@ -174,6 +241,13 @@ func NotifyDeleteComment(ctx context.Context, doer *user_model.User, c *issues_m // NotifyNewRelease notifies new release to notifiers func NotifyNewRelease(ctx context.Context, rel *repo_model.Release) { + if err := rel.LoadAttributes(ctx); err != nil { + log.Error("LoadPublisher: %v", err) + return + } + if rel.Publisher.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyNewRelease(ctx, rel) } @@ -181,6 +255,9 @@ func NotifyNewRelease(ctx context.Context, rel *repo_model.Release) { // NotifyUpdateRelease notifies update release to notifiers func NotifyUpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyUpdateRelease(ctx, doer, rel) } @@ -188,6 +265,9 @@ func NotifyUpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_m // NotifyDeleteRelease notifies delete release to notifiers func NotifyDeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyDeleteRelease(ctx, doer, rel) } @@ -195,6 +275,9 @@ func NotifyDeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_m // NotifyIssueChangeMilestone notifies change milestone to notifiers func NotifyIssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueChangeMilestone(ctx, doer, issue, oldMilestoneID) } @@ -202,6 +285,9 @@ func NotifyIssueChangeMilestone(ctx context.Context, doer *user_model.User, issu // NotifyIssueChangeContent notifies change content to notifiers func NotifyIssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueChangeContent(ctx, doer, issue, oldContent) } @@ -209,6 +295,9 @@ func NotifyIssueChangeContent(ctx context.Context, doer *user_model.User, issue // NotifyIssueChangeAssignee notifies change content to notifiers func NotifyIssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueChangeAssignee(ctx, doer, issue, assignee, removed, comment) } @@ -216,6 +305,9 @@ func NotifyIssueChangeAssignee(ctx context.Context, doer *user_model.User, issue // NotifyPullReviewRequest notifies Request Review change func NotifyPullReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPullReviewRequest(ctx, doer, issue, reviewer, isRequest, comment) } @@ -223,6 +315,9 @@ func NotifyPullReviewRequest(ctx context.Context, doer *user_model.User, issue * // NotifyIssueClearLabels notifies clear labels to notifiers func NotifyIssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueClearLabels(ctx, doer, issue) } @@ -230,6 +325,9 @@ func NotifyIssueClearLabels(ctx context.Context, doer *user_model.User, issue *i // NotifyIssueChangeTitle notifies change title to notifiers func NotifyIssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueChangeTitle(ctx, doer, issue, oldTitle) } @@ -237,6 +335,9 @@ func NotifyIssueChangeTitle(ctx context.Context, doer *user_model.User, issue *i // NotifyIssueChangeRef notifies change reference to notifiers func NotifyIssueChangeRef(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldRef string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueChangeRef(ctx, doer, issue, oldRef) } @@ -246,6 +347,9 @@ func NotifyIssueChangeRef(ctx context.Context, doer *user_model.User, issue *iss func NotifyIssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, addedLabels, removedLabels []*issues_model.Label, ) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyIssueChangeLabels(ctx, doer, issue, addedLabels, removedLabels) } @@ -253,6 +357,9 @@ func NotifyIssueChangeLabels(ctx context.Context, doer *user_model.User, issue * // NotifyCreateRepository notifies create repository to notifiers func NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyCreateRepository(ctx, doer, u, repo) } @@ -260,6 +367,9 @@ func NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo // NotifyMigrateRepository notifies create repository to notifiers func NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyMigrateRepository(ctx, doer, u, repo) } @@ -267,6 +377,9 @@ func NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo // NotifyTransferRepository notifies create repository to notifiers func NotifyTransferRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, newOwnerName string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyTransferRepository(ctx, doer, repo, newOwnerName) } @@ -274,6 +387,9 @@ func NotifyTransferRepository(ctx context.Context, doer *user_model.User, repo * // NotifyDeleteRepository notifies delete repository to notifiers func NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyDeleteRepository(ctx, doer, repo) } @@ -281,6 +397,9 @@ func NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *re // NotifyForkRepository notifies fork repository to notifiers func NotifyForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyForkRepository(ctx, doer, oldRepo, repo) } @@ -288,6 +407,9 @@ func NotifyForkRepository(ctx context.Context, doer *user_model.User, oldRepo, r // NotifyRenameRepository notifies repository renamed func NotifyRenameRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldName string) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyRenameRepository(ctx, doer, repo, oldName) } @@ -295,6 +417,9 @@ func NotifyRenameRepository(ctx context.Context, doer *user_model.User, repo *re // NotifyPushCommits notifies commits pushed to notifiers func NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { + if pusher.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPushCommits(ctx, pusher, repo, opts, commits) } @@ -302,6 +427,9 @@ func NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_ // NotifyCreateRef notifies branch or tag creation to notifiers func NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { + if pusher.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyCreateRef(ctx, pusher, repo, refType, refFullName, refID) } @@ -309,6 +437,9 @@ func NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_mo // NotifyDeleteRef notifies branch or tag deletion to notifiers func NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { + if pusher.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyDeleteRef(ctx, pusher, repo, refType, refFullName) } @@ -316,6 +447,9 @@ func NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_mo // NotifySyncPushCommits notifies commits pushed to notifiers func NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { + if pusher.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifySyncPushCommits(ctx, pusher, repo, opts, commits) } @@ -323,6 +457,9 @@ func NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *r // NotifySyncCreateRef notifies branch or tag creation to notifiers func NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) { + if pusher.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifySyncCreateRef(ctx, pusher, repo, refType, refFullName, refID) } @@ -330,6 +467,9 @@ func NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *rep // NotifySyncDeleteRef notifies branch or tag deletion to notifiers func NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) { + if pusher.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifySyncDeleteRef(ctx, pusher, repo, refType, refFullName) } @@ -337,6 +477,9 @@ func NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *rep // NotifyRepoPendingTransfer notifies creation of pending transfer to notifiers func NotifyRepoPendingTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyRepoPendingTransfer(ctx, doer, newOwner, repo) } @@ -344,6 +487,9 @@ func NotifyRepoPendingTransfer(ctx context.Context, doer, newOwner *user_model.U // NotifyPackageCreate notifies creation of a package to notifiers func NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPackageCreate(ctx, doer, pd) } @@ -351,6 +497,9 @@ func NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *package // NotifyPackageDelete notifies deletion of a package to notifiers func NotifyPackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) { + if doer.IsBots() { + return + } for _, notifier := range notifiers { notifier.NotifyPackageDelete(ctx, doer, pd) }