From 491887d44132b8103ed0d753f95ecd43d600adba Mon Sep 17 00:00:00 2001
From: Lunny Xiao <xiaolunwen@gmail.com>
Date: Mon, 4 Nov 2019 04:59:09 +0800
Subject: [PATCH] Move actions to notification (#8785)

---
 modules/notification/action/action.go   | 77 +++++++++++++++++++++++++++++++++
 modules/notification/notification.go    |  2 +
 modules/notification/webhook/webhook.go | 36 +++++++++++++++
 services/issue/issue.go                 | 15 -------
 services/pull/pull.go                   | 25 ++---------
 5 files changed, 118 insertions(+), 37 deletions(-)
 create mode 100644 modules/notification/action/action.go

diff --git a/modules/notification/action/action.go b/modules/notification/action/action.go
new file mode 100644
index 0000000000..15228f65e7
--- /dev/null
+++ b/modules/notification/action/action.go
@@ -0,0 +1,77 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package action
+
+import (
+	"fmt"
+
+	"code.gitea.io/gitea/models"
+	"code.gitea.io/gitea/modules/log"
+	"code.gitea.io/gitea/modules/notification/base"
+)
+
+type actionNotifier struct {
+	base.NullNotifier
+}
+
+var (
+	_ base.Notifier = &actionNotifier{}
+)
+
+// NewNotifier create a new webhookNotifier notifier
+func NewNotifier() base.Notifier {
+	return &actionNotifier{}
+}
+
+func (a *actionNotifier) NotifyNewIssue(issue *models.Issue) {
+	if err := issue.LoadPoster(); err != nil {
+		log.Error("issue.LoadPoster: %v", err)
+		return
+	}
+	if err := issue.LoadRepo(); err != nil {
+		log.Error("issue.LoadRepo: %v", err)
+		return
+	}
+	repo := issue.Repo
+
+	if err := models.NotifyWatchers(&models.Action{
+		ActUserID: issue.Poster.ID,
+		ActUser:   issue.Poster,
+		OpType:    models.ActionCreateIssue,
+		Content:   fmt.Sprintf("%d|%s", issue.Index, issue.Title),
+		RepoID:    repo.ID,
+		Repo:      repo,
+		IsPrivate: repo.IsPrivate,
+	}); err != nil {
+		log.Error("NotifyWatchers: %v", err)
+	}
+}
+
+func (a *actionNotifier) NotifyNewPullRequest(pull *models.PullRequest) {
+	if err := pull.LoadIssue(); err != nil {
+		log.Error("pull.LoadIssue: %v", err)
+		return
+	}
+	if err := pull.Issue.LoadRepo(); err != nil {
+		log.Error("pull.Issue.LoadRepo: %v", err)
+		return
+	}
+	if err := pull.Issue.LoadPoster(); err != nil {
+		log.Error("pull.Issue.LoadPoster: %v", err)
+		return
+	}
+
+	if err := models.NotifyWatchers(&models.Action{
+		ActUserID: pull.Issue.Poster.ID,
+		ActUser:   pull.Issue.Poster,
+		OpType:    models.ActionCreatePullRequest,
+		Content:   fmt.Sprintf("%d|%s", pull.Issue.Index, pull.Issue.Title),
+		RepoID:    pull.Issue.Repo.ID,
+		Repo:      pull.Issue.Repo,
+		IsPrivate: pull.Issue.Repo.IsPrivate,
+	}); err != nil {
+		log.Error("NotifyWatchers: %v", err)
+	}
+}
diff --git a/modules/notification/notification.go b/modules/notification/notification.go
index 6532f9d614..1fd3022940 100644
--- a/modules/notification/notification.go
+++ b/modules/notification/notification.go
@@ -7,6 +7,7 @@ package notification
 import (
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/git"
+	"code.gitea.io/gitea/modules/notification/action"
 	"code.gitea.io/gitea/modules/notification/base"
 	"code.gitea.io/gitea/modules/notification/indexer"
 	"code.gitea.io/gitea/modules/notification/mail"
@@ -33,6 +34,7 @@ func NewContext() {
 	}
 	RegisterNotifier(indexer.NewNotifier())
 	RegisterNotifier(webhook.NewNotifier())
+	RegisterNotifier(action.NewNotifier())
 }
 
 // NotifyCreateIssueComment notifies issue comment related message to notifiers
diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go
index 13d33e1011..7d28c1c8b9 100644
--- a/modules/notification/webhook/webhook.go
+++ b/modules/notification/webhook/webhook.go
@@ -10,6 +10,7 @@ import (
 	"code.gitea.io/gitea/modules/notification/base"
 	"code.gitea.io/gitea/modules/setting"
 	api "code.gitea.io/gitea/modules/structs"
+	"code.gitea.io/gitea/modules/webhook"
 	webhook_module "code.gitea.io/gitea/modules/webhook"
 )
 
@@ -251,6 +252,15 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *mode
 }
 
 func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue) {
+	if err := issue.LoadRepo(); err != nil {
+		log.Error("issue.LoadRepo: %v", err)
+		return
+	}
+	if err := issue.LoadPoster(); err != nil {
+		log.Error("issue.LoadPoster: %v", err)
+		return
+	}
+
 	mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
 	if err := webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
 		Action:     api.HookIssueOpened,
@@ -263,6 +273,32 @@ func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue) {
 	}
 }
 
+func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest) {
+	if err := pull.LoadIssue(); err != nil {
+		log.Error("pull.LoadIssue: %v", err)
+		return
+	}
+	if err := pull.Issue.LoadRepo(); err != nil {
+		log.Error("pull.Issue.LoadRepo: %v", err)
+		return
+	}
+	if err := pull.Issue.LoadPoster(); err != nil {
+		log.Error("pull.Issue.LoadPoster: %v", err)
+		return
+	}
+
+	mode, _ := models.AccessLevel(pull.Issue.Poster, pull.Issue.Repo)
+	if err := webhook.PrepareWebhooks(pull.Issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
+		Action:      api.HookIssueOpened,
+		Index:       pull.Issue.Index,
+		PullRequest: pull.APIFormat(),
+		Repository:  pull.Issue.Repo.APIFormat(mode),
+		Sender:      pull.Issue.Poster.APIFormat(),
+	}); err != nil {
+		log.Error("PrepareWebhooks: %v", err)
+	}
+}
+
 func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
 	mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
 	var err error
diff --git a/services/issue/issue.go b/services/issue/issue.go
index ee2f176a43..aa06ba4097 100644
--- a/services/issue/issue.go
+++ b/services/issue/issue.go
@@ -5,10 +5,7 @@
 package issue
 
 import (
-	"fmt"
-
 	"code.gitea.io/gitea/models"
-	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/notification"
 )
 
@@ -24,18 +21,6 @@ func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, uu
 		}
 	}
 
-	if err := models.NotifyWatchers(&models.Action{
-		ActUserID: issue.Poster.ID,
-		ActUser:   issue.Poster,
-		OpType:    models.ActionCreateIssue,
-		Content:   fmt.Sprintf("%d|%s", issue.Index, issue.Title),
-		RepoID:    repo.ID,
-		Repo:      repo,
-		IsPrivate: repo.IsPrivate,
-	}); err != nil {
-		log.Error("NotifyWatchers: %v", err)
-	}
-
 	notification.NotifyNewIssue(issue)
 
 	return nil
diff --git a/services/pull/pull.go b/services/pull/pull.go
index 0a4c4a7eee..20939c397f 100644
--- a/services/pull/pull.go
+++ b/services/pull/pull.go
@@ -10,6 +10,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"
 	api "code.gitea.io/gitea/modules/structs"
 	"code.gitea.io/gitea/modules/webhook"
 	issue_service "code.gitea.io/gitea/services/issue"
@@ -27,30 +28,10 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6
 		}
 	}
 
-	if err := models.NotifyWatchers(&models.Action{
-		ActUserID: pull.Poster.ID,
-		ActUser:   pull.Poster,
-		OpType:    models.ActionCreatePullRequest,
-		Content:   fmt.Sprintf("%d|%s", pull.Index, pull.Title),
-		RepoID:    repo.ID,
-		Repo:      repo,
-		IsPrivate: repo.IsPrivate,
-	}); err != nil {
-		log.Error("NotifyWatchers: %v", err)
-	}
-
 	pr.Issue = pull
 	pull.PullRequest = pr
-	mode, _ := models.AccessLevel(pull.Poster, repo)
-	if err := webhook.PrepareWebhooks(repo, models.HookEventPullRequest, &api.PullRequestPayload{
-		Action:      api.HookIssueOpened,
-		Index:       pull.Index,
-		PullRequest: pr.APIFormat(),
-		Repository:  repo.APIFormat(mode),
-		Sender:      pull.Poster.APIFormat(),
-	}); err != nil {
-		log.Error("PrepareWebhooks: %v", err)
-	}
+
+	notification.NotifyNewPullRequest(pr)
 
 	return nil
 }