Skip to content

Commit 11f71dc

Browse files
earl-warrenforgejo-backport-action
authored andcommitted
fix: add label to issues and PR labeled/unlabeled events
When a workflow has on: pull_request: types: - labeled - unlabeled The payload misses the label field describing the added or removed label. The unlabeled event type was also incorrectly mapped to the labeled event type. (cherry picked from commit 58e3c1f)
1 parent 7ec30b6 commit 11f71dc

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

modules/structs/hook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ type IssuePayload struct {
362362
Repository *Repository `json:"repository"`
363363
Sender *User `json:"sender"`
364364
CommitID string `json:"commit_id"`
365+
Label *Label `json:"label,omitempty"`
365366
}
366367

367368
// JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces.
@@ -399,6 +400,7 @@ type PullRequestPayload struct {
399400
Sender *User `json:"sender"`
400401
CommitID string `json:"commit_id"`
401402
Review *ReviewPayload `json:"review"`
403+
Label *Label `json:"label,omitempty"`
402404
}
403405

404406
// JSONPayload FIXME

services/actions/notifier.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (n *actionsNotifier) IssueChangeAssignee(ctx context.Context, doer *user_mo
168168
hookEvent = webhook_module.HookEventPullRequestAssign
169169
}
170170

171-
notifyIssueChange(ctx, doer, issue, hookEvent, action)
171+
notifyIssueChange(ctx, doer, issue, hookEvent, action, nil)
172172
}
173173

174174
// IssueChangeMilestone notifies assignee to notifiers
@@ -187,11 +187,11 @@ func (n *actionsNotifier) IssueChangeMilestone(ctx context.Context, doer *user_m
187187
hookEvent = webhook_module.HookEventPullRequestMilestone
188188
}
189189

190-
notifyIssueChange(ctx, doer, issue, hookEvent, action)
190+
notifyIssueChange(ctx, doer, issue, hookEvent, action, nil)
191191
}
192192

193193
func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
194-
_, _ []*issues_model.Label,
194+
addedLabels, removedLabels []*issues_model.Label,
195195
) {
196196
ctx = withMethod(ctx, "IssueChangeLabels")
197197

@@ -200,10 +200,15 @@ func (n *actionsNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
200200
hookEvent = webhook_module.HookEventPullRequestLabel
201201
}
202202

203-
notifyIssueChange(ctx, doer, issue, hookEvent, api.HookIssueLabelUpdated)
203+
for _, added := range addedLabels {
204+
notifyIssueChange(ctx, doer, issue, hookEvent, api.HookIssueLabelUpdated, added)
205+
}
206+
for _, removed := range removedLabels {
207+
notifyIssueChange(ctx, doer, issue, hookEvent, api.HookIssueLabelCleared, removed)
208+
}
204209
}
205210

206-
func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, event webhook_module.HookEventType, action api.HookIssueAction) {
211+
func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, event webhook_module.HookEventType, action api.HookIssueAction, label *issues_model.Label) {
207212
var err error
208213
if err = issue.LoadRepo(ctx); err != nil {
209214
log.Error("LoadRepo: %v", err)
@@ -215,6 +220,11 @@ func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues
215220
return
216221
}
217222

223+
var apiLabel *api.Label
224+
if action == api.HookIssueLabelUpdated || action == api.HookIssueLabelCleared {
225+
apiLabel = convert.ToLabel(label, issue.Repo, nil)
226+
}
227+
218228
if issue.IsPull {
219229
if err = issue.LoadPullRequest(ctx); err != nil {
220230
log.Error("loadPullRequest: %v", err)
@@ -228,6 +238,7 @@ func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues
228238
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
229239
Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}),
230240
Sender: convert.ToUser(ctx, doer, nil),
241+
Label: apiLabel,
231242
}).
232243
WithPullRequest(issue.PullRequest).
233244
Notify(ctx)
@@ -242,6 +253,7 @@ func notifyIssueChange(ctx context.Context, doer *user_model.User, issue *issues
242253
Issue: convert.ToAPIIssue(ctx, doer, issue),
243254
Repository: convert.ToRepo(ctx, issue.Repo, permission),
244255
Sender: convert.ToUser(ctx, doer, nil),
256+
Label: apiLabel,
245257
}).
246258
Notify(ctx)
247259
}

0 commit comments

Comments
 (0)