]> source.dussan.org Git - gitea.git/commitdiff
Move pull webhook to notification (#8805)
authorLunny Xiao <xiaolunwen@gmail.com>
Tue, 5 Nov 2019 11:04:08 +0000 (19:04 +0800)
committerGitHub <noreply@github.com>
Tue, 5 Nov 2019 11:04:08 +0000 (19:04 +0800)
* Move pull webhook to notification

* fix fmt

modules/notification/base/notifier.go
modules/notification/base/null.go
modules/notification/notification.go
modules/notification/webhook/webhook.go
services/pull/merge.go
services/pull/pull.go
services/pull/review.go

index ff865f19cf846fcb13fcad816e62bfff2193827e..286ebe5d69af50355222d7fdd7034a85c339662c 100644 (file)
@@ -30,6 +30,7 @@ type Notifier interface {
 
        NotifyNewPullRequest(*models.PullRequest)
        NotifyMergePullRequest(*models.PullRequest, *models.User, *git.Repository)
+       NotifyPullRequestSynchronized(doer *models.User, pr *models.PullRequest)
        NotifyPullRequestReview(*models.PullRequest, *models.Review, *models.Comment)
 
        NotifyCreateIssueComment(*models.User, *models.Repository,
index c10e1b63404551ee3cedb658c194c596e02fb844..5b6359cbd55775a866a0d1259e52b8c60c1db682 100644 (file)
@@ -46,6 +46,10 @@ func (*NullNotifier) NotifyPullRequestReview(pr *models.PullRequest, r *models.R
 func (*NullNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
 }
 
+// NotifyPullRequestSynchronized places a place holder function
+func (*NullNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *models.PullRequest) {
+}
+
 // NotifyUpdateComment places a place holder function
 func (*NullNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
 }
index 1fd30229406bb173a6430c6bbe288a0728ae9e70..a5e450ee66ca57844e767f5603a57c1ce7893172 100644 (file)
@@ -73,6 +73,13 @@ func NotifyNewPullRequest(pr *models.PullRequest) {
        }
 }
 
+// NotifyPullRequestSynchronized notifies Synchronized pull request
+func NotifyPullRequestSynchronized(doer *models.User, pr *models.PullRequest) {
+       for _, notifier := range notifiers {
+               notifier.NotifyPullRequestSynchronized(doer, pr)
+       }
+}
+
 // NotifyPullRequestReview notifies new pull request review
 func NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
        for _, notifier := range notifiers {
index 7d28c1c8b98373af56af356a0f5f982f8064d73c..39c63edb05c67f9c49a2fa3ac3485dde59daed6f 100644 (file)
@@ -520,3 +520,65 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Re
                log.Error("PrepareWebhooks: %v", err)
        }
 }
+
+func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
+       var reviewHookType models.HookEventType
+
+       switch review.Type {
+       case models.ReviewTypeApprove:
+               reviewHookType = models.HookEventPullRequestApproved
+       case models.ReviewTypeComment:
+               reviewHookType = models.HookEventPullRequestComment
+       case models.ReviewTypeReject:
+               reviewHookType = models.HookEventPullRequestRejected
+       default:
+               // unsupported review webhook type here
+               log.Error("Unsupported review webhook type")
+               return
+       }
+
+       if err := pr.LoadIssue(); err != nil {
+               log.Error("pr.LoadIssue: %v", err)
+               return
+       }
+
+       mode, err := models.AccessLevel(review.Issue.Poster, review.Issue.Repo)
+       if err != nil {
+               log.Error("models.AccessLevel: %v", err)
+               return
+       }
+       if err := webhook.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
+               Action:      api.HookIssueSynchronized,
+               Index:       review.Issue.Index,
+               PullRequest: pr.APIFormat(),
+               Repository:  review.Issue.Repo.APIFormat(mode),
+               Sender:      review.Reviewer.APIFormat(),
+               Review: &api.ReviewPayload{
+                       Type:    string(reviewHookType),
+                       Content: review.Content,
+               },
+       }); 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)
+               return
+       }
+       if err := pr.Issue.LoadAttributes(); err != nil {
+               log.Error("LoadAttributes: %v", err)
+               return
+       }
+
+       if err := webhook.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
+               Action:      api.HookIssueSynchronized,
+               Index:       pr.Issue.Index,
+               PullRequest: pr.Issue.PullRequest.APIFormat(),
+               Repository:  pr.Issue.Repo.APIFormat(models.AccessModeNone),
+               Sender:      doer.APIFormat(),
+       }); err != nil {
+               log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
+       }
+}
index 4a2f4511c44701f144db73342011aeaa3278f95b..c6607910a25d68871b756aa6db60027d8ba68193 100644 (file)
@@ -20,10 +20,9 @@ import (
        "code.gitea.io/gitea/modules/cache"
        "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/timeutil"
-       "code.gitea.io/gitea/modules/webhook"
 
        "github.com/mcuadros/go-version"
 )
@@ -360,16 +359,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
                return nil
        }
 
-       mode, _ := models.AccessLevel(doer, pr.Issue.Repo)
-       if err = webhook.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
-               Action:      api.HookIssueClosed,
-               Index:       pr.Index,
-               PullRequest: pr.APIFormat(),
-               Repository:  pr.Issue.Repo.APIFormat(mode),
-               Sender:      doer.APIFormat(),
-       }); err != nil {
-               log.Error("PrepareWebhooks: %v", err)
-       }
+       notification.NotifyIssueChangeStatus(doer, pr.Issue, true)
 
        return nil
 }
index 20939c397fa4daf14df00d8d522959fd7509597d..4e981b2b260c9286c004d2890dc24bc41475fec3 100644 (file)
@@ -11,8 +11,6 @@ import (
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/notification"
-       api "code.gitea.io/gitea/modules/structs"
-       "code.gitea.io/gitea/modules/webhook"
        issue_service "code.gitea.io/gitea/services/issue"
 )
 
@@ -90,23 +88,9 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy
                if err == nil {
                        for _, pr := range prs {
                                pr.Issue.PullRequest = pr
-                               if err = pr.Issue.LoadAttributes(); err != nil {
-                                       log.Error("LoadAttributes: %v", err)
-                                       continue
-                               }
-                               if err = webhook.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
-                                       Action:      api.HookIssueSynchronized,
-                                       Index:       pr.Issue.Index,
-                                       PullRequest: pr.Issue.PullRequest.APIFormat(),
-                                       Repository:  pr.Issue.Repo.APIFormat(models.AccessModeNone),
-                                       Sender:      doer.APIFormat(),
-                               }); err != nil {
-                                       log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
-                                       continue
-                               }
+                               notification.NotifyPullRequestSynchronized(doer, pr)
                        }
                }
-
        }
 
        addHeadRepoTasks(prs)
index ffb7be82b2eece23d19d6786356fead388b22dd4..e4aae3c0d5885b390d285d871b2686ccc62ea7e3 100644 (file)
@@ -7,8 +7,7 @@ package pull
 
 import (
        "code.gitea.io/gitea/models"
-       api "code.gitea.io/gitea/modules/structs"
-       "code.gitea.io/gitea/modules/webhook"
+       "code.gitea.io/gitea/modules/notification"
 )
 
 // CreateReview creates a new review based on opts
@@ -18,7 +17,9 @@ func CreateReview(opts models.CreateReviewOptions) (*models.Review, error) {
                return nil, err
        }
 
-       return review, reviewHook(review)
+       notification.NotifyPullRequestReview(review.Issue.PullRequest, review, nil)
+
+       return review, nil
 }
 
 // UpdateReview updates a review
@@ -28,43 +29,7 @@ func UpdateReview(review *models.Review) error {
                return err
        }
 
-       return reviewHook(review)
-}
-
-func reviewHook(review *models.Review) error {
-       var reviewHookType models.HookEventType
-
-       switch review.Type {
-       case models.ReviewTypeApprove:
-               reviewHookType = models.HookEventPullRequestApproved
-       case models.ReviewTypeComment:
-               reviewHookType = models.HookEventPullRequestComment
-       case models.ReviewTypeReject:
-               reviewHookType = models.HookEventPullRequestRejected
-       default:
-               // unsupported review webhook type here
-               return nil
-       }
-
-       pr := review.Issue.PullRequest
+       notification.NotifyPullRequestReview(review.Issue.PullRequest, review, nil)
 
-       if err := pr.LoadIssue(); err != nil {
-               return err
-       }
-
-       mode, err := models.AccessLevel(review.Issue.Poster, review.Issue.Repo)
-       if err != nil {
-               return err
-       }
-       return webhook.PrepareWebhooks(review.Issue.Repo, reviewHookType, &api.PullRequestPayload{
-               Action:      api.HookIssueSynchronized,
-               Index:       review.Issue.Index,
-               PullRequest: pr.APIFormat(),
-               Repository:  review.Issue.Repo.APIFormat(mode),
-               Sender:      review.Reviewer.APIFormat(),
-               Review: &api.ReviewPayload{
-                       Type:    string(reviewHookType),
-                       Content: review.Content,
-               },
-       })
+       return nil
 }