Skip to content

Commit 0870e0b

Browse files
authored
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
1 parent c0b97d0 commit 0870e0b

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

modules/actions/github.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent
5252
case webhook_module.HookEventPullRequest,
5353
webhook_module.HookEventPullRequestSync,
5454
webhook_module.HookEventPullRequestAssign,
55-
webhook_module.HookEventPullRequestLabel:
55+
webhook_module.HookEventPullRequestLabel,
56+
webhook_module.HookEventPullRequestReviewRequest,
57+
webhook_module.HookEventPullRequestMilestone:
5658
return true
5759

5860
default:

modules/actions/workflows.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web
221221
webhook_module.HookEventPullRequest,
222222
webhook_module.HookEventPullRequestSync,
223223
webhook_module.HookEventPullRequestAssign,
224-
webhook_module.HookEventPullRequestLabel:
224+
webhook_module.HookEventPullRequestLabel,
225+
webhook_module.HookEventPullRequestReviewRequest,
226+
webhook_module.HookEventPullRequestMilestone:
225227
return matchPullRequestEvent(gitRepo, commit, payload.(*api.PullRequestPayload), evt)
226228

227229
case // pull_request_review
@@ -397,13 +399,13 @@ func matchPullRequestEvent(gitRepo *git.Repository, commit *git.Commit, prPayloa
397399
} else {
398400
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
399401
// Actions with the same name:
400-
// opened, edited, closed, reopened, assigned, unassigned
402+
// opened, edited, closed, reopened, assigned, unassigned, review_requested, review_request_removed, milestoned, demilestoned
401403
// Actions need to be converted:
402404
// synchronized -> synchronize
403405
// label_updated -> labeled
404406
// label_cleared -> unlabeled
405407
// Unsupported activity types:
406-
// converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled
408+
// converted_to_draft, ready_for_review, locked, unlocked, auto_merge_enabled, auto_merge_disabled, enqueued, dequeued
407409

408410
action := prPayload.Action
409411
switch action {

services/actions/notifier.go

+67-9
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,40 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode
143143
Notify(ctx)
144144
}
145145

146+
// IssueChangeAssignee notifies assigned or unassigned to notifiers
147+
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) {
148+
ctx = withMethod(ctx, "IssueChangeAssignee")
149+
150+
var action api.HookIssueAction
151+
if removed {
152+
action = api.HookIssueUnassigned
153+
} else {
154+
action = api.HookIssueAssigned
155+
}
156+
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestAssign, action)
157+
}
158+
159+
// IssueChangeMilestone notifies assignee to notifiers
160+
func (n *actionsNotifier) IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
161+
ctx = withMethod(ctx, "IssueChangeMilestone")
162+
163+
var action api.HookIssueAction
164+
if issue.MilestoneID > 0 {
165+
action = api.HookIssueMilestoned
166+
} else {
167+
action = api.HookIssueDemilestoned
168+
}
169+
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestMilestone, action)
170+
}
171+
146172
func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
147173
_, _ []*issues_model.Label,
148174
) {
149175
ctx = withMethod(ctx, "IssueChangeLabels")
176+
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestLabel, api.HookIssueLabelUpdated)
177+
}
150178

179+
func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, event webhook_module.HookEventType, action api.HookIssueAction) {
151180
var err error
152181
if err = issue.LoadRepo(ctx); err != nil {
153182
log.Error("LoadRepo: %v", err)
@@ -159,20 +188,15 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
159188
return
160189
}
161190

162-
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
163191
if issue.IsPull {
164192
if err = issue.LoadPullRequest(ctx); err != nil {
165193
log.Error("loadPullRequest: %v", err)
166194
return
167195
}
168-
if err = issue.PullRequest.LoadIssue(ctx); err != nil {
169-
log.Error("LoadIssue: %v", err)
170-
return
171-
}
172-
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestLabel).
196+
newNotifyInputFromIssue(issue, event).
173197
WithDoer(doer).
174198
WithPayload(&api.PullRequestPayload{
175-
Action: api.HookIssueLabelUpdated,
199+
Action: action,
176200
Index: issue.Index,
177201
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
178202
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
182206
Notify(ctx)
183207
return
184208
}
185-
newNotifyInputFromIssue(issue, webhook_module.HookEventIssueLabel).
209+
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
210+
newNotifyInputFromIssue(issue, event).
186211
WithDoer(doer).
187212
WithPayload(&api.IssuePayload{
188-
Action: api.HookIssueLabelUpdated,
213+
Action: action,
189214
Index: issue.Index,
190215
Issue: convert.ToAPIIssue(ctx, issue),
191216
Repository: convert.ToRepo(ctx, issue.Repo, permission),
@@ -347,6 +372,39 @@ func (n *actionsNotifier) PullRequestReview(ctx context.Context, pr *issues_mode
347372
}).Notify(ctx)
348373
}
349374

375+
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) {
376+
if !issue.IsPull {
377+
log.Warn("PullRequestReviewRequest: issue is not a pull request: %v", issue.ID)
378+
return
379+
}
380+
381+
ctx = withMethod(ctx, "PullRequestReviewRequest")
382+
383+
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
384+
if err := issue.LoadPullRequest(ctx); err != nil {
385+
log.Error("LoadPullRequest failed: %v", err)
386+
return
387+
}
388+
var action api.HookIssueAction
389+
if isRequest {
390+
action = api.HookIssueReviewRequested
391+
} else {
392+
action = api.HookIssueReviewRequestRemoved
393+
}
394+
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestReviewRequest).
395+
WithDoer(doer).
396+
WithPayload(&api.PullRequestPayload{
397+
Action: action,
398+
Index: issue.Index,
399+
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
400+
RequestedReviewer: convert.ToUser(ctx, reviewer, nil),
401+
Repository: convert.ToRepo(ctx, issue.Repo, permission),
402+
Sender: convert.ToUser(ctx, doer, nil),
403+
}).
404+
WithPullRequest(issue.PullRequest).
405+
Notify(ctx)
406+
}
407+
350408
func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
351409
ctx = withMethod(ctx, "MergePullRequest")
352410

0 commit comments

Comments
 (0)