aboutsummaryrefslogtreecommitdiffstats
path: root/modules/notification
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-11-05 19:04:08 +0800
committerGitHub <noreply@github.com>2019-11-05 19:04:08 +0800
commitaaeef295bb07b281fe0554aa6e97f24596d073aa (patch)
tree6df2fb31d6d3f3d50a14e1a992ab3f719683a658 /modules/notification
parent05e7715c4b2af174aefc56fe459e5c08a96a3017 (diff)
downloadgitea-aaeef295bb07b281fe0554aa6e97f24596d073aa.tar.gz
gitea-aaeef295bb07b281fe0554aa6e97f24596d073aa.zip
Move pull webhook to notification (#8805)
* Move pull webhook to notification * fix fmt
Diffstat (limited to 'modules/notification')
-rw-r--r--modules/notification/base/notifier.go1
-rw-r--r--modules/notification/base/null.go4
-rw-r--r--modules/notification/notification.go7
-rw-r--r--modules/notification/webhook/webhook.go62
4 files changed, 74 insertions, 0 deletions
diff --git a/modules/notification/base/notifier.go b/modules/notification/base/notifier.go
index ff865f19cf..286ebe5d69 100644
--- a/modules/notification/base/notifier.go
+++ b/modules/notification/base/notifier.go
@@ -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,
diff --git a/modules/notification/base/null.go b/modules/notification/base/null.go
index c10e1b6340..5b6359cbd5 100644
--- a/modules/notification/base/null.go
+++ b/modules/notification/base/null.go
@@ -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) {
}
diff --git a/modules/notification/notification.go b/modules/notification/notification.go
index 1fd3022940..a5e450ee66 100644
--- a/modules/notification/notification.go
+++ b/modules/notification/notification.go
@@ -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 {
diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go
index 7d28c1c8b9..39c63edb05 100644
--- a/modules/notification/webhook/webhook.go
+++ b/modules/notification/webhook/webhook.go
@@ -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)
+ }
+}