]> source.dussan.org Git - gitea.git/commitdiff
Move repofiles webhooks to notification (#8807)
authorLunny Xiao <xiaolunwen@gmail.com>
Wed, 6 Nov 2019 06:43:03 +0000 (14:43 +0800)
committerGitHub <noreply@github.com>
Wed, 6 Nov 2019 06:43:03 +0000 (14:43 +0800)
modules/notification/base/notifier.go
modules/notification/base/null.go
modules/notification/notification.go
modules/notification/webhook/webhook.go
modules/repofiles/action.go

index 286ebe5d69af50355222d7fdd7034a85c339662c..72bf52c938ac568686de98b08422766c4a30bd46 100644 (file)
@@ -43,4 +43,6 @@ type Notifier interface {
        NotifyDeleteRelease(doer *models.User, rel *models.Release)
 
        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)
 }
index 5b6359cbd55775a866a0d1259e52b8c60c1db682..a9d9d6a1640b5b68f234561a9558cd23339ad89a 100644 (file)
@@ -114,3 +114,11 @@ func (*NullNotifier) NotifyMigrateRepository(doer *models.User, u *models.User,
 // NotifyPushCommits notifies commits pushed to notifiers
 func (*NullNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
 }
+
+// NotifyCreateRef notifies branch or tag creation to notifiers
+func (*NullNotifier) NotifyCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
+}
+
+// NotifyDeleteRef notifies branch or tag deleteion to notifiers
+func (*NullNotifier) NotifyDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
+}
index a5e450ee66ca57844e767f5603a57c1ce7893172..5ac09a72e5c6dc70438bb84eaaf4fa358d1e7037 100644 (file)
@@ -199,3 +199,17 @@ func NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, ol
                notifier.NotifyPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
        }
 }
+
+// NotifyCreateRef notifies branch or tag creation to notifiers
+func NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
+       for _, notifier := range notifiers {
+               notifier.NotifyCreateRef(pusher, repo, refType, refFullName)
+       }
+}
+
+// NotifyDeleteRef notifies branch or tag deletion to notifiers
+func NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
+       for _, notifier := range notifiers {
+               notifier.NotifyDeleteRef(pusher, repo, refType, refFullName)
+       }
+}
index 39c63edb05c67f9c49a2fa3ac3485dde59daed6f..6eb03d3ebc66762accb55d31e79d8643f13e9c2f 100644 (file)
@@ -6,6 +6,7 @@ package webhook
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/notification/base"
        "code.gitea.io/gitea/modules/setting"
@@ -562,6 +563,34 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review
        }
 }
 
+func (m *webhookNotifier) NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
+       apiPusher := pusher.APIFormat()
+       apiRepo := repo.APIFormat(models.AccessModeNone)
+       refName := git.RefEndName(refFullName)
+
+       gitRepo, err := git.OpenRepository(repo.RepoPath())
+       if err != nil {
+               log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
+               return
+       }
+
+       shaSum, err := gitRepo.GetBranchCommitID(refName)
+       if err != nil {
+               log.Error("GetBranchCommitID[%s]: %v", refFullName, err)
+               return
+       }
+
+       if err = webhook_module.PrepareWebhooks(repo, models.HookEventCreate, &api.CreatePayload{
+               Ref:     refName,
+               Sha:     shaSum,
+               RefType: refType,
+               Repo:    apiRepo,
+               Sender:  apiPusher,
+       }); err != nil {
+               log.Error("PrepareWebhooks: %v", err)
+       }
+}
+
 func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *models.PullRequest) {
        if err := pr.LoadIssue(); err != nil {
                log.Error("pr.LoadIssue: %v", err)
@@ -572,7 +601,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *m
                return
        }
 
-       if err := webhook.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
+       if err := webhook_module.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
                Action:      api.HookIssueSynchronized,
                Index:       pr.Issue.Index,
                PullRequest: pr.Issue.PullRequest.APIFormat(),
@@ -582,3 +611,19 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *m
                log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
        }
 }
+
+func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
+       apiPusher := pusher.APIFormat()
+       apiRepo := repo.APIFormat(models.AccessModeNone)
+       refName := git.RefEndName(refFullName)
+
+       if err := webhook_module.PrepareWebhooks(repo, models.HookEventDelete, &api.DeletePayload{
+               Ref:        refName,
+               RefType:    "branch",
+               PusherType: api.PusherTypeUser,
+               Repo:       apiRepo,
+               Sender:     apiPusher,
+       }); err != nil {
+               log.Error("PrepareWebhooks.(delete branch): %v", err)
+       }
+}
index e5f6bf871879d061ce0cd99ea0ca94dc5d6a848a..996363863d9a41b23e6f8efde9bc2290271c532b 100644 (file)
@@ -14,8 +14,6 @@ import (
        "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"
 )
 
 // CommitRepoActionOptions represent options of a new commit action.
@@ -113,81 +111,23 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
                return fmt.Errorf("NotifyWatchers: %v", err)
        }
 
-       apiPusher := pusher.APIFormat()
-       apiRepo := repo.APIFormat(models.AccessModeNone)
-
-       var shaSum string
-       var isHookEventPush = false
+       var isHookEventPush = true
        switch opType {
        case models.ActionCommitRepo: // Push
-               isHookEventPush = true
-
                if isNewBranch {
-                       gitRepo, err := git.OpenRepository(repo.RepoPath())
-                       if err != nil {
-                               log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
-                       }
-
-                       shaSum, err = gitRepo.GetBranchCommitID(refName)
-                       if err != nil {
-                               log.Error("GetBranchCommitID[%s]: %v", opts.RefFullName, err)
-                       }
-                       if err = webhook.PrepareWebhooks(repo, models.HookEventCreate, &api.CreatePayload{
-                               Ref:     refName,
-                               Sha:     shaSum,
-                               RefType: "branch",
-                               Repo:    apiRepo,
-                               Sender:  apiPusher,
-                       }); err != nil {
-                               return fmt.Errorf("PrepareWebhooks: %v", err)
-                       }
+                       notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName)
                }
 
        case models.ActionDeleteBranch: // Delete Branch
-               isHookEventPush = true
-
-               if err = webhook.PrepareWebhooks(repo, models.HookEventDelete, &api.DeletePayload{
-                       Ref:        refName,
-                       RefType:    "branch",
-                       PusherType: api.PusherTypeUser,
-                       Repo:       apiRepo,
-                       Sender:     apiPusher,
-               }); err != nil {
-                       return fmt.Errorf("PrepareWebhooks.(delete branch): %v", err)
-               }
+               notification.NotifyDeleteRef(pusher, repo, "branch", opts.RefFullName)
 
        case models.ActionPushTag: // Create
-               isHookEventPush = true
+               notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName)
 
-               gitRepo, err := git.OpenRepository(repo.RepoPath())
-               if err != nil {
-                       log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
-               }
-               shaSum, err = gitRepo.GetTagCommitID(refName)
-               if err != nil {
-                       log.Error("GetTagCommitID[%s]: %v", opts.RefFullName, err)
-               }
-               if err = webhook.PrepareWebhooks(repo, models.HookEventCreate, &api.CreatePayload{
-                       Ref:     refName,
-                       Sha:     shaSum,
-                       RefType: "tag",
-                       Repo:    apiRepo,
-                       Sender:  apiPusher,
-               }); err != nil {
-                       return fmt.Errorf("PrepareWebhooks: %v", err)
-               }
        case models.ActionDeleteTag: // Delete Tag
-               isHookEventPush = true
-
-               if err = webhook.PrepareWebhooks(repo, models.HookEventDelete, &api.DeletePayload{
-                       Ref:        refName,
-                       RefType:    "tag",
-                       PusherType: api.PusherTypeUser,
-                       Repo:       apiRepo,
-                       Sender:     apiPusher,
-               }); err != nil {
-                       return fmt.Errorf("PrepareWebhooks.(delete tag): %v", err)
-               }
+               notification.NotifyDeleteRef(pusher, repo, "tag", opts.RefFullName)
+       default:
+               isHookEventPush = false
        }
 
        if isHookEventPush {