]> source.dussan.org Git - gitea.git/commitdiff
Move push commits events to notification (#8783)
authorLunny Xiao <xiaolunwen@gmail.com>
Sun, 3 Nov 2019 06:59:26 +0000 (14:59 +0800)
committerGitHub <noreply@github.com>
Sun, 3 Nov 2019 06:59:26 +0000 (14:59 +0800)
* 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
modules/notification/base/null.go
modules/notification/notification.go
modules/notification/webhook/webhook.go
modules/repofiles/action.go
services/mirror/sync.go

index b0e7b4cae827591db3f5b3b6f5a035edbf16487a..ff865f19cf846fcb13fcad816e62bfff2193827e 100644 (file)
@@ -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)
 }
index 3524a53c1554eb5ab200507ab71dfb1017cfb561..c10e1b63404551ee3cedb658c194c596e02fb844 100644 (file)
@@ -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) {
+}
index 70b1541e3525dd6d395f191303a0750a509d22bc..6532f9d614aad986ae81d6148c3c9f4476c5f767 100644 (file)
@@ -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)
+       }
+}
index a969ad74f793e9b2bb8b4e9fdad3f05f6b2dfaf7..13d33e10114faa14d13abbb3c5aeba7438c52da1 100644 (file)
@@ -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)
+       }
+}
index 79f6406c535d8d9d2c07046be55f9439bf9f73ad..e5f6bf871879d061ce0cd99ea0ca94dc5d6a848a 100644 (file)
@@ -12,6 +12,7 @@ import (
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
+       "code.gitea.io/gitea/modules/notification"
        "code.gitea.io/gitea/modules/setting"
        api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/webhook"
@@ -190,22 +191,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
        }
 
        if isHookEventPush {
-               commits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
-               if err != nil {
-                       return err
-               }
-               if err = webhook.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
-                       Ref:        opts.RefFullName,
-                       Before:     opts.OldCommitID,
-                       After:      opts.NewCommitID,
-                       CompareURL: setting.AppURL + opts.Commits.CompareURL,
-                       Commits:    commits,
-                       Repo:       apiRepo,
-                       Pusher:     apiPusher,
-                       Sender:     apiPusher,
-               }); err != nil {
-                       return fmt.Errorf("PrepareWebhooks: %v", err)
-               }
+               notification.NotifyPushCommits(pusher, repo, opts.RefFullName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
        }
 
        return nil
index a9ce189c030155c9154e0eaf643e43fc529b1efe..ba9e896dd50e3c50f6ea93f9ae5af77f810d9e3f 100644 (file)
@@ -9,9 +9,8 @@ import (
        "fmt"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/modules/notification"
        "code.gitea.io/gitea/modules/setting"
-       api "code.gitea.io/gitea/modules/structs"
-       "code.gitea.io/gitea/modules/webhook"
 )
 
 func syncAction(opType models.ActionType, repo *models.Repository, refName string, data []byte) error {
@@ -45,25 +44,9 @@ func SyncPushAction(repo *models.Repository, opts SyncPushActionOptions) error {
                opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
        }
 
-       apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
-       if err != nil {
-               return err
-       }
-
        opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
-       apiPusher := repo.MustOwner().APIFormat()
-       if err := webhook.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
-               Ref:        opts.RefName,
-               Before:     opts.OldCommitID,
-               After:      opts.NewCommitID,
-               CompareURL: setting.AppURL + opts.Commits.CompareURL,
-               Commits:    apiCommits,
-               Repo:       repo.APIFormat(models.AccessModeOwner),
-               Pusher:     apiPusher,
-               Sender:     apiPusher,
-       }); err != nil {
-               return fmt.Errorf("PrepareWebhooks: %v", err)
-       }
+
+       notification.NotifyPushCommits(repo.MustOwner(), repo, opts.RefName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
 
        data, err := json.Marshal(opts.Commits)
        if err != nil {