Skip to content

Commit 1a6e1cb

Browse files
authored
Implement some action notifier functions (#29173)
Fix #29166 Add support for the following activity types of `pull_request` - assigned - unassigned - review_requested - review_request_removed - milestoned - demilestoned
1 parent 67adc5c commit 1a6e1cb

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
@@ -101,11 +101,40 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode
101101
Notify(ctx)
102102
}
103103

104+
// IssueChangeAssignee notifies assigned or unassigned to notifiers
105+
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) {
106+
ctx = withMethod(ctx, "IssueChangeAssignee")
107+
108+
var action api.HookIssueAction
109+
if removed {
110+
action = api.HookIssueUnassigned
111+
} else {
112+
action = api.HookIssueAssigned
113+
}
114+
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestAssign, action)
115+
}
116+
117+
// IssueChangeMilestone notifies assignee to notifiers
118+
func (n *actionsNotifier) IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
119+
ctx = withMethod(ctx, "IssueChangeMilestone")
120+
121+
var action api.HookIssueAction
122+
if issue.MilestoneID > 0 {
123+
action = api.HookIssueMilestoned
124+
} else {
125+
action = api.HookIssueDemilestoned
126+
}
127+
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestMilestone, action)
128+
}
129+
104130
func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
105131
_, _ []*issues_model.Label,
106132
) {
107133
ctx = withMethod(ctx, "IssueChangeLabels")
134+
notifyIssueChange(ctx, doer, issue, webhook_module.HookEventPullRequestLabel, api.HookIssueLabelUpdated)
135+
}
108136

137+
func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, event webhook_module.HookEventType, action api.HookIssueAction) {
109138
var err error
110139
if err = issue.LoadRepo(ctx); err != nil {
111140
log.Error("LoadRepo: %v", err)
@@ -117,20 +146,15 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
117146
return
118147
}
119148

120-
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
121149
if issue.IsPull {
122150
if err = issue.LoadPullRequest(ctx); err != nil {
123151
log.Error("loadPullRequest: %v", err)
124152
return
125153
}
126-
if err = issue.PullRequest.LoadIssue(ctx); err != nil {
127-
log.Error("LoadIssue: %v", err)
128-
return
129-
}
130-
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestLabel).
154+
newNotifyInputFromIssue(issue, event).
131155
WithDoer(doer).
132156
WithPayload(&api.PullRequestPayload{
133-
Action: api.HookIssueLabelUpdated,
157+
Action: action,
134158
Index: issue.Index,
135159
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
136160
Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
@@ -140,10 +164,11 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
140164
Notify(ctx)
141165
return
142166
}
143-
newNotifyInputFromIssue(issue, webhook_module.HookEventIssueLabel).
167+
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
168+
newNotifyInputFromIssue(issue, event).
144169
WithDoer(doer).
145170
WithPayload(&api.IssuePayload{
146-
Action: api.HookIssueLabelUpdated,
171+
Action: action,
147172
Index: issue.Index,
148173
Issue: convert.ToAPIIssue(ctx, issue),
149174
Repository: convert.ToRepo(ctx, issue.Repo, permission),
@@ -305,6 +330,39 @@ func (n *actionsNotifier) PullRequestReview(ctx context.Context, pr *issues_mode
305330
}).Notify(ctx)
306331
}
307332

333+
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) {
334+
if !issue.IsPull {
335+
log.Warn("PullRequestReviewRequest: issue is not a pull request: %v", issue.ID)
336+
return
337+
}
338+
339+
ctx = withMethod(ctx, "PullRequestReviewRequest")
340+
341+
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
342+
if err := issue.LoadPullRequest(ctx); err != nil {
343+
log.Error("LoadPullRequest failed: %v", err)
344+
return
345+
}
346+
var action api.HookIssueAction
347+
if isRequest {
348+
action = api.HookIssueReviewRequested
349+
} else {
350+
action = api.HookIssueReviewRequestRemoved
351+
}
352+
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestReviewRequest).
353+
WithDoer(doer).
354+
WithPayload(&api.PullRequestPayload{
355+
Action: action,
356+
Index: issue.Index,
357+
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
358+
RequestedReviewer: convert.ToUser(ctx, reviewer, nil),
359+
Repository: convert.ToRepo(ctx, issue.Repo, permission),
360+
Sender: convert.ToUser(ctx, doer, nil),
361+
}).
362+
WithPullRequest(issue.PullRequest).
363+
Notify(ctx)
364+
}
365+
308366
func (*actionsNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
309367
ctx = withMethod(ctx, "MergePullRequest")
310368

0 commit comments

Comments
 (0)