diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-11-24 13:16:59 +0800 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-11-24 00:16:59 -0500 |
commit | 8ab35eefc4ff5db3f2f0a62f6f0272eae9be0585 (patch) | |
tree | 5249bcee68fb96652adc8f7dd46b604cbf15cd5e /modules/notification | |
parent | e3f22ad2cca094cba057683f35f8536e3f71a582 (diff) | |
download | gitea-8ab35eefc4ff5db3f2f0a62f6f0272eae9be0585.tar.gz gitea-8ab35eefc4ff5db3f2f0a62f6f0272eae9be0585.zip |
Move mirror sync actions to notification (#9022)
* Move mirror sync actions to notification
* fix lint
Diffstat (limited to 'modules/notification')
-rw-r--r-- | modules/notification/action/action.go | 50 | ||||
-rw-r--r-- | modules/notification/base/notifier.go | 4 | ||||
-rw-r--r-- | modules/notification/base/null.go | 12 | ||||
-rw-r--r-- | modules/notification/notification.go | 21 | ||||
-rw-r--r-- | modules/notification/webhook/webhook.go | 22 |
5 files changed, 109 insertions, 0 deletions
diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go index 70ab9975b2..9caeb5aac0 100644 --- a/modules/notification/action/action.go +++ b/modules/notification/action/action.go @@ -5,6 +5,7 @@ package action import ( + "encoding/json" "fmt" "path" "strings" @@ -206,3 +207,52 @@ func (*actionNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mode log.Error("NotifyWatchers [%d]: %v", pr.ID, err) } } + +func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) { + data, err := json.Marshal(commits) + if err != nil { + log.Error("json.Marshal: %v", err) + return + } + + if err := models.NotifyWatchers(&models.Action{ + ActUserID: repo.OwnerID, + ActUser: repo.MustOwner(), + OpType: models.ActionMirrorSyncPush, + RepoID: repo.ID, + Repo: repo, + IsPrivate: repo.IsPrivate, + RefName: refName, + Content: string(data), + }); err != nil { + log.Error("notifyWatchers: %v", err) + } +} + +func (a *actionNotifier) NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) { + if err := models.NotifyWatchers(&models.Action{ + ActUserID: repo.OwnerID, + ActUser: repo.MustOwner(), + OpType: models.ActionMirrorSyncCreate, + RepoID: repo.ID, + Repo: repo, + IsPrivate: repo.IsPrivate, + RefName: refFullName, + }); err != nil { + log.Error("notifyWatchers: %v", err) + } +} + +func (a *actionNotifier) NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) { + if err := models.NotifyWatchers(&models.Action{ + ActUserID: repo.OwnerID, + ActUser: repo.MustOwner(), + OpType: models.ActionMirrorSyncCreate, + RepoID: repo.ID, + Repo: repo, + IsPrivate: repo.IsPrivate, + RefName: refFullName, + }); err != nil { + log.Error("notifyWatchers: %v", err) + } +} diff --git a/modules/notification/base/notifier.go b/modules/notification/base/notifier.go index 1935c30557..c8c89d22bd 100644 --- a/modules/notification/base/notifier.go +++ b/modules/notification/base/notifier.go @@ -47,4 +47,8 @@ type Notifier interface { NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) NotifyCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) NotifyDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) + + NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) + NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) + NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) } diff --git a/modules/notification/base/null.go b/modules/notification/base/null.go index b9ecaed425..79e9f7f7fc 100644 --- a/modules/notification/base/null.go +++ b/modules/notification/base/null.go @@ -130,3 +130,15 @@ func (*NullNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repo // NotifyTransferRepository places a place holder function func (*NullNotifier) NotifyTransferRepository(doer *models.User, repo *models.Repository, oldOwnerName string) { } + +// NotifySyncPushCommits places a place holder function +func (*NullNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) { +} + +// NotifySyncCreateRef places a place holder function +func (*NullNotifier) NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) { +} + +// NotifySyncDeleteRef places a place holder function +func (*NullNotifier) NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) { +} diff --git a/modules/notification/notification.go b/modules/notification/notification.go index fa0b280e71..71d6e79e6d 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -227,3 +227,24 @@ func NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refF notifier.NotifyDeleteRef(pusher, repo, refType, refFullName) } } + +// NotifySyncPushCommits notifies commits pushed to notifiers +func NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) { + for _, notifier := range notifiers { + notifier.NotifySyncPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits) + } +} + +// NotifySyncCreateRef notifies branch or tag creation to notifiers +func NotifySyncCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { + for _, notifier := range notifiers { + notifier.NotifySyncCreateRef(pusher, repo, refType, refFullName) + } +} + +// NotifySyncDeleteRef notifies branch or tag deletion to notifiers +func NotifySyncDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { + for _, notifier := range notifiers { + notifier.NotifySyncDeleteRef(pusher, repo, refType, refFullName) + } +} diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index 213e33c096..4ef60fef84 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -695,3 +695,25 @@ func (m *webhookNotifier) NotifyUpdateRelease(doer *models.User, rel *models.Rel func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Release) { sendReleaseHook(doer, rel, api.HookReleaseDeleted) } + +func (m *webhookNotifier) NotifySyncPushCommits(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) + } +} |