Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move merge actions to notification #9024

Merged
merged 4 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func (pr *PullRequest) loadIssue(e Engine) (err error) {
}

pr.Issue, err = getIssueByID(e, pr.IssueID)
if err == nil {
pr.Issue.PullRequest = pr
}
return err
}

Expand Down
15 changes: 15 additions & 0 deletions modules/notification/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification/base"
)
Expand Down Expand Up @@ -191,3 +192,17 @@ func (a *actionNotifier) NotifyPullRequestReview(pr *models.PullRequest, review
log.Error("notify watchers '%d/%d': %v", review.Reviewer.ID, review.Issue.RepoID, err)
}
}

func (*actionNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
if err := models.NotifyWatchers(&models.Action{
ActUserID: doer.ID,
ActUser: doer,
OpType: models.ActionMergePullRequest,
Content: fmt.Sprintf("%d|%s", pr.Issue.Index, pr.Issue.Title),
RepoID: pr.Issue.Repo.ID,
Repo: pr.Issue.Repo,
IsPrivate: pr.Issue.Repo.IsPrivate,
}); err != nil {
log.Error("NotifyWatchers [%d]: %v", pr.ID, err)
}
}
12 changes: 12 additions & 0 deletions modules/notification/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"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/services/mailer"
Expand Down Expand Up @@ -98,3 +99,14 @@ func (m *mailNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *model
mailer.SendIssueAssignedMail(issue, doer, ct, comment, []string{assignee.Email})
}
}

func (m *mailNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
if err := pr.LoadIssue(); err != nil {
log.Error("pr.LoadIssue: %v", err)
return
}

if err := mailer.MailParticipants(pr.Issue, doer, models.ActionClosePullRequest); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just realizing that mailer.MailParticipants() will also mail the users mentioned in the content of the PR. Is that OK or should I change it so it will only do so when action is create issue?

log.Error("MailParticipants: %v", err)
}
}
38 changes: 38 additions & 0 deletions modules/notification/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,44 @@ func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Re
}
}

func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseRepo *git.Repository) {
// Reload pull request information.
if err := pr.LoadAttributes(); err != nil {
log.Error("LoadAttributes: %v", err)
return
}

if err := pr.LoadIssue(); err != nil {
log.Error("LoadAttributes: %v", err)
return
}

if err := pr.Issue.LoadRepo(); err != nil {
log.Error("pr.Issue.LoadRepo: %v", err)
return
}

mode, err := models.AccessLevel(doer, pr.Issue.Repo)
if err != nil {
log.Error("models.AccessLevel: %v", err)
return
}

// Merge pull request calls issue.changeStatus so we need to handle separately.
apiPullRequest := &api.PullRequestPayload{
Index: pr.Issue.Index,
PullRequest: pr.APIFormat(),
Repository: pr.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
Action: api.HookIssueClosed,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's peculiar that there is no HookPullMerged action. I guess it's for historical reasons... So if the PR is closed without being merged the web hook gets the same data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also realized this but for this PR I prefer to let it only a refactor. Please feel free to open a new PR to improve them.

}

err = webhook_module.PrepareWebhooks(pr.Issue.Repo, models.HookEventPullRequest, apiPullRequest)
if err != nil {
log.Error("PrepareWebhooks: %v", err)
}
}

func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
var reviewHookType models.HookEventType

Expand Down
2 changes: 0 additions & 2 deletions routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,6 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
return
}

notification.NotifyMergePullRequest(pr, ctx.User, ctx.Repo.GitRepo)

log.Trace("Pull request merged: %d", pr.ID)
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
}
Expand Down
20 changes: 1 addition & 19 deletions services/pull/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,29 +426,11 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
log.Error("setMerged [%d]: %v", pr.ID, err)
}

if err := models.NotifyWatchers(&models.Action{
ActUserID: doer.ID,
ActUser: doer,
OpType: models.ActionMergePullRequest,
Content: fmt.Sprintf("%d|%s", pr.Issue.Index, pr.Issue.Title),
RepoID: pr.Issue.Repo.ID,
Repo: pr.Issue.Repo,
IsPrivate: pr.Issue.Repo.IsPrivate,
}); err != nil {
log.Error("NotifyWatchers [%d]: %v", pr.ID, err)
}
notification.NotifyMergePullRequest(pr, doer, baseGitRepo)

// Reset cached commit count
cache.Remove(pr.Issue.Repo.GetCommitsCountCacheKey(pr.BaseBranch, true))

// Reload pull request information.
if err = pr.LoadAttributes(); err != nil {
log.Error("LoadAttributes: %v", err)
return nil
}

notification.NotifyIssueChangeStatus(doer, pr.Issue, true)

// Resolve cross references
refs, err := pr.ResolveCrossReferences()
if err != nil {
Expand Down