Implement some action notifier functions (#29173) (#29308)

Backport #29173

Fix #29166

Add support for the following activity types of `pull_request`
- assigned
- unassigned
- review_requested
- review_request_removed
- milestoned
- demilestoned
This commit is contained in:
yp05327 2024-02-22 12:55:03 +09:00 committed by GitHub
parent c0b97d0485
commit 0870e0bc9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 13 deletions

View File

@ -52,7 +52,9 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent
case webhook_module.HookEventPullRequest, case webhook_module.HookEventPullRequest,
webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestSync,
webhook_module.HookEventPullRequestAssign, webhook_module.HookEventPullRequestAssign,
webhook_module.HookEventPullRequestLabel: webhook_module.HookEventPullRequestLabel,
webhook_module.HookEventPullRequestReviewRequest,
webhook_module.HookEventPullRequestMilestone:
return true return true
default: default:

View File

@ -221,7 +221,9 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web
webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequest,
webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestSync,
webhook_module.HookEventPullRequestAssign, webhook_module.HookEventPullRequestAssign,
webhook_module.HookEventPullRequestLabel: webhook_module.HookEventPullRequestLabel,
webhook_module.HookEventPullRequestReviewRequest,
webhook_module.HookEventPullRequestMilestone:
return matchPullRequestEvent(gitRepo, commit, payload.(*api.PullRequestPayload), evt) return matchPullRequestEvent(gitRepo, commit, payload.(*api.PullRequestPayload), evt)
case // pull_request_review case // pull_request_review
@ -397,13 +399,13 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa
} else { } else {
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
// Actions with the same name: // Actions with the same name:
// opened, edited, closed, reopened, assigned, unassigned // opened, edited, closed, reopened, assigned, unassigned, review_requested, review_request_removed, milestoned, demilestoned
// Actions need to be converted: // Actions need to be converted:
// synchronized -> synchronize // synchronized -> synchronize
// label_updated -> labeled // label_updated -> labeled
// label_cleared -> unlabeled // label_cleared -> unlabeled
// Unsupported activity types: // Unsupported activity types:
// converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled // converted_to_draft, ready_for_review, locked, unlocked, auto_merge_enabled, auto_merge_disabled, enqueued, dequeued
action := prPayload.Action action := prPayload.Action
switch action { switch action {

View File

@ -143,11 +143,40 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode
Notify(ctx) Notify(ctx)
} }
// IssueChangeAssignee notifies assigned or unassigned to notifiers
func (n *actionsNotifier) IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
ctx = withMethod(ctx, "IssueChangeAssignee")
var action api.HookIssueAction
if removed {
action = api.HookIssueUnassigned
} else {
action = api.HookIssueAssigned
}
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestAssign, action)
}
// IssueChangeMilestone notifies assignee to notifiers
func (n *actionsNotifier) IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
ctx = withMethod(ctx, "IssueChangeMilestone")
var action api.HookIssueAction
if issue.MilestoneID > 0 {
action = api.HookIssueMilestoned
} else {
action = api.HookIssueDemilestoned
}
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestMilestone, action)
}
func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
_, _ []*issues_model.Label, _, _ []*issues_model.Label,
) { ) {
ctx = withMethod(ctx, "IssueChangeLabels") ctx = withMethod(ctx, "IssueChangeLabels")
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestLabel, api.HookIssueLabelUpdated)
}
func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, event webhook_module.HookEventType, action api.HookIssueAction) {
var err error var err error
if err = issue.LoadRepo(ctx); err != nil { if err = issue.LoadRepo(ctx); err != nil {
log.Error("LoadRepo: %v", err) log.Error("LoadRepo: %v", err)
@ -159,20 +188,15 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
return return
} }
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
if issue.IsPull { if issue.IsPull {
if err = issue.LoadPullRequest(ctx); err != nil { if err = issue.LoadPullRequest(ctx); err != nil {
log.Error("loadPullRequest: %v", err) log.Error("loadPullRequest: %v", err)
return return
} }
if err = issue.PullRequest.LoadIssue(ctx); err != nil { newNotifyInputFromIssue(issue, event).
log.Error("LoadIssue: %v", err)
return
}
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestLabel).
WithDoer(doer). WithDoer(doer).
WithPayload(&api.PullRequestPayload{ WithPayload(&api.PullRequestPayload{
Action: api.HookIssueLabelUpdated, Action: action,
Index: issue.Index, Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil), PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}), Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
@ -182,10 +206,11 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
Notify(ctx) Notify(ctx)
return return
} }
newNotifyInputFromIssue(issue, webhook_module.HookEventIssueLabel). permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
newNotifyInputFromIssue(issue, event).
WithDoer(doer). WithDoer(doer).
WithPayload(&api.IssuePayload{ WithPayload(&api.IssuePayload{
Action: api.HookIssueLabelUpdated, Action: action,
Index: issue.Index, Index: issue.Index,
Issue: convert.ToAPIIssue(ctx, issue), Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(ctx, issue.Repo, permission), Repository: convert.ToRepo(ctx, issue.Repo, permission),
@ -347,6 +372,39 @@ func (n *actionsNotifier) PullRequestReview(ctx context.Context, pr *issues_mode
}).Notify(ctx) }).Notify(ctx)
} }
func (n *actionsNotifier) PullRequestReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) {
if !issue.IsPull {
log.Warn("PullRequestReviewRequest: issue is not a pull request: %v", issue.ID)
return
}
ctx = withMethod(ctx, "PullRequestReviewRequest")
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
if err := issue.LoadPullRequest(ctx); err != nil {
log.Error("LoadPullRequest failed: %v", err)
return
}
var action api.HookIssueAction
if isRequest {
action = api.HookIssueReviewRequested
} else {
action = api.HookIssueReviewRequestRemoved
}
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestReviewRequest).
WithDoer(doer).
WithPayload(&api.PullRequestPayload{
Action: action,
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
RequestedReviewer: convert.ToUser(ctx, reviewer, nil),
Repository: convert.ToRepo(ctx, issue.Repo, permission),
Sender: convert.ToUser(ctx, doer, nil),
}).
WithPullRequest(issue.PullRequest).
Notify(ctx)
}
func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) { func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
ctx = withMethod(ctx, "MergePullRequest") ctx = withMethod(ctx, "MergePullRequest")