Skip to content

Commit af8825c

Browse files
committed
Implement some action notifier functions (go-gitea#29173)
Backport go-gitea#29173 Fix go-gitea#29166 Add support for the following activity types of `pull_request` - assigned - unassigned - review_requested - review_request_removed - milestoned - demilestoned
1 parent 39735c4 commit af8825c

3 files changed

Lines changed: 75 additions & 13 deletions

File tree

modules/actions/github.go

Lines changed: 3 additions & 1 deletion
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,40 @@ func (n *actionsNotifier) IssueChangeStatus(ctx context.Context, doer *user_mode
102102
Notify(ctx)
103103
}
104104

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

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

121-
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
122150
if issue.IsPull {
123151
if err = issue.LoadPullRequest(ctx); err != nil {
124152
log.Error("loadPullRequest: %v", err)
125153
return
126154
}
127-
if err = issue.PullRequest.LoadIssue(ctx); err != nil {
128-
log.Error("LoadIssue: %v", err)
129-
return
130-
}
131-
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestLabel).
155+
newNotifyInputFromIssue(issue, event).
132156
WithDoer(doer).
133157
WithPayload(&api.PullRequestPayload{
134-
Action: api.HookIssueLabelUpdated,
158+
Action: action,
135159
Index: issue.Index,
136160
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
137161
Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
@@ -141,10 +165,11 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
141165
Notify(ctx)
142166
return
143167
}
144-
newNotifyInputFromIssue(issue, webhook_module.HookEventIssueLabel).
168+
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
169+
newNotifyInputFromIssue(issue, event).
145170
WithDoer(doer).
146171
WithPayload(&api.IssuePayload{
147-
Action: api.HookIssueLabelUpdated,
172+
Action: action,
148173
Index: issue.Index,
149174
Issue: convert.ToAPIIssue(ctx, issue),
150175
Repository: convert.ToRepo(ctx, issue.Repo, permission),
@@ -306,6 +331,39 @@ func (n *actionsNotifier) PullRequestReview(ctx context.Context, pr *issues_mode
306331
}).Notify(ctx)
307332
}
308333

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

0 commit comments

Comments
 (0)