From 022d2d8beb6297016ed26b0090c6a4a4ac404437 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 3 Nov 2019 14:59:26 +0800 Subject: Move push commits events to notification (#8783) * Move push commits events to notification * Update modules/notification/base/null.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> --- modules/notification/base/notifier.go | 2 ++ modules/notification/base/null.go | 4 ++++ modules/notification/notification.go | 7 +++++++ modules/notification/webhook/webhook.go | 23 +++++++++++++++++++++++ 4 files changed, 36 insertions(+) (limited to 'modules/notification') diff --git a/modules/notification/base/notifier.go b/modules/notification/base/notifier.go index b0e7b4cae8..ff865f19cf 100644 --- a/modules/notification/base/notifier.go +++ b/modules/notification/base/notifier.go @@ -40,4 +40,6 @@ type Notifier interface { NotifyNewRelease(rel *models.Release) NotifyUpdateRelease(doer *models.User, rel *models.Release) NotifyDeleteRelease(doer *models.User, rel *models.Release) + + NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) } diff --git a/modules/notification/base/null.go b/modules/notification/base/null.go index 3524a53c15..c10e1b6340 100644 --- a/modules/notification/base/null.go +++ b/modules/notification/base/null.go @@ -106,3 +106,7 @@ func (*NullNotifier) NotifyCreateRepository(doer *models.User, u *models.User, r // NotifyMigrateRepository places a place holder function func (*NullNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) { } + +// NotifyPushCommits notifies commits pushed to notifiers +func (*NullNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) { +} diff --git a/modules/notification/notification.go b/modules/notification/notification.go index 70b1541e35..6532f9d614 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -183,3 +183,10 @@ func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Rep notifier.NotifyMigrateRepository(doer, u, repo) } } + +// NotifyPushCommits notifies commits pushed to notifiers +func NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) { + for _, notifier := range notifiers { + notifier.NotifyPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits) + } +} diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index a969ad74f7..13d33e1011 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification/base" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" webhook_module "code.gitea.io/gitea/modules/webhook" ) @@ -461,3 +462,25 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err) } } + +func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) { + apiPusher := pusher.APIFormat() + apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) + if err != nil { + log.Error("commits.ToAPIPayloadCommits failed: %v", err) + return + } + + if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{ + Ref: refName, + Before: oldCommitID, + After: newCommitID, + CompareURL: setting.AppURL + commits.CompareURL, + Commits: apiCommits, + Repo: repo.APIFormat(models.AccessModeOwner), + Pusher: apiPusher, + Sender: apiPusher, + }); err != nil { + log.Error("PrepareWebhooks: %v", err) + } +} -- cgit v1.2.3