Skip to content

Commit f1cffe5

Browse files
Zettat123GiteaBot
authored andcommitted
Improve the issue_comment workflow trigger event (go-gitea#29277)
Fix go-gitea#29175 Replace go-gitea#29207 This PR makes some improvements to the `issue_comment` workflow trigger event. 1. Fix the bug that pull requests cannot trigger `issue_comment` workflows 2. Previously the `issue_comment` event only supported the `created` activity type. This PR adds support for the missing `edited` and `deleted` activity types. 3. Some events (including `issue_comment`, `issues`, etc. ) only trigger workflows that belong to the workflow file on the default branch. This PR introduces the `IsDefaultBranchWorkflow` function to check for these events.
1 parent ed5e0c8 commit f1cffe5

File tree

4 files changed

+130
-27
lines changed

4 files changed

+130
-27
lines changed

modules/actions/github.go

+44
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,45 @@ const (
2525
GithubEventSchedule = "schedule"
2626
)
2727

28+
// IsDefaultBranchWorkflow returns true if the event only triggers workflows on the default branch
29+
func IsDefaultBranchWorkflow(triggedEvent webhook_module.HookEventType) bool {
30+
switch triggedEvent {
31+
case webhook_module.HookEventDelete:
32+
// GitHub "delete" event
33+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#delete
34+
return true
35+
case webhook_module.HookEventFork:
36+
// GitHub "fork" event
37+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#fork
38+
return true
39+
case webhook_module.HookEventIssueComment:
40+
// GitHub "issue_comment" event
41+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment
42+
return true
43+
case webhook_module.HookEventPullRequestComment:
44+
// GitHub "pull_request_comment" event
45+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
46+
return true
47+
case webhook_module.HookEventWiki:
48+
// GitHub "gollum" event
49+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
50+
return true
51+
case webhook_module.HookEventSchedule:
52+
// GitHub "schedule" event
53+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
54+
return true
55+
case webhook_module.HookEventIssues,
56+
webhook_module.HookEventIssueAssign,
57+
webhook_module.HookEventIssueLabel,
58+
webhook_module.HookEventIssueMilestone:
59+
// Github "issues" event
60+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
61+
return true
62+
}
63+
64+
return false
65+
}
66+
2867
// canGithubEventMatch check if the input Github event can match any Gitea event.
2968
func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
3069
switch eventName {
@@ -75,6 +114,11 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent
75114
case GithubEventSchedule:
76115
return triggedEvent == webhook_module.HookEventSchedule
77116

117+
case GithubEventIssueComment:
118+
// https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
119+
return triggedEvent == webhook_module.HookEventIssueComment ||
120+
triggedEvent == webhook_module.HookEventPullRequestComment
121+
78122
default:
79123
return eventName == string(triggedEvent)
80124
}

modules/actions/github_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ func TestCanGithubEventMatch(t *testing.T) {
103103
webhook_module.HookEventCreate,
104104
true,
105105
},
106+
{
107+
"create pull request comment",
108+
GithubEventIssueComment,
109+
webhook_module.HookEventPullRequestComment,
110+
true,
111+
},
106112
}
107113

108114
for _, tc := range testCases {

services/actions/notifier.go

+73-23
Original file line numberDiff line numberDiff line change
@@ -225,37 +225,88 @@ func (n *actionsNotifier) CreateIssueComment(ctx context.Context, doer *user_mod
225225
) {
226226
ctx = withMethod(ctx, "CreateIssueComment")
227227

228-
permission, _ := access_model.GetUserRepoPermission(ctx, repo, doer)
229-
230228
if issue.IsPull {
231-
if err := issue.LoadPullRequest(ctx); err != nil {
229+
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventPullRequestComment, api.HookIssueCommentCreated)
230+
return
231+
}
232+
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventIssueComment, api.HookIssueCommentCreated)
233+
}
234+
235+
func (n *actionsNotifier) UpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
236+
ctx = withMethod(ctx, "UpdateComment")
237+
238+
if err := c.LoadIssue(ctx); err != nil {
239+
log.Error("LoadIssue: %v", err)
240+
return
241+
}
242+
243+
if c.Issue.IsPull {
244+
notifyIssueCommentChange(ctx, doer, c, oldContent, webhook_module.HookEventPullRequestComment, api.HookIssueCommentEdited)
245+
return
246+
}
247+
notifyIssueCommentChange(ctx, doer, c, oldContent, webhook_module.HookEventIssueComment, api.HookIssueCommentEdited)
248+
}
249+
250+
func (n *actionsNotifier) DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
251+
ctx = withMethod(ctx, "DeleteComment")
252+
253+
if err := comment.LoadIssue(ctx); err != nil {
254+
log.Error("LoadIssue: %v", err)
255+
return
256+
}
257+
258+
if comment.Issue.IsPull {
259+
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventPullRequestComment, api.HookIssueCommentDeleted)
260+
return
261+
}
262+
notifyIssueCommentChange(ctx, doer, comment, "", webhook_module.HookEventIssueComment, api.HookIssueCommentDeleted)
263+
}
264+
265+
func notifyIssueCommentChange(ctx context.Context, doer *user_model.User, comment *issues_model.Comment, oldContent string, event webhook_module.HookEventType, action api.HookIssueCommentAction) {
266+
if err := comment.LoadIssue(ctx); err != nil {
267+
log.Error("LoadIssue: %v", err)
268+
return
269+
}
270+
if err := comment.Issue.LoadAttributes(ctx); err != nil {
271+
log.Error("LoadAttributes: %v", err)
272+
return
273+
}
274+
275+
permission, _ := access_model.GetUserRepoPermission(ctx, comment.Issue.Repo, doer)
276+
277+
payload := &api.IssueCommentPayload{
278+
Action: action,
279+
Issue: convert.ToAPIIssue(ctx, comment.Issue),
280+
Comment: convert.ToAPIComment(ctx, comment.Issue.Repo, comment),
281+
Repository: convert.ToRepo(ctx, comment.Issue.Repo, permission),
282+
Sender: convert.ToUser(ctx, doer, nil),
283+
IsPull: comment.Issue.IsPull,
284+
}
285+
286+
if action == api.HookIssueCommentEdited {
287+
payload.Changes = &api.ChangesPayload{
288+
Body: &api.ChangesFromPayload{
289+
From: oldContent,
290+
},
291+
}
292+
}
293+
294+
if comment.Issue.IsPull {
295+
if err := comment.Issue.LoadPullRequest(ctx); err != nil {
232296
log.Error("LoadPullRequest: %v", err)
233297
return
234298
}
235-
newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequestComment).
299+
newNotifyInputFromIssue(comment.Issue, event).
236300
WithDoer(doer).
237-
WithPayload(&api.IssueCommentPayload{
238-
Action: api.HookIssueCommentCreated,
239-
Issue: convert.ToAPIIssue(ctx, issue),
240-
Comment: convert.ToAPIComment(ctx, repo, comment),
241-
Repository: convert.ToRepo(ctx, repo, permission),
242-
Sender: convert.ToUser(ctx, doer, nil),
243-
IsPull: true,
244-
}).
245-
WithPullRequest(issue.PullRequest).
301+
WithPayload(payload).
302+
WithPullRequest(comment.Issue.PullRequest).
246303
Notify(ctx)
247304
return
248305
}
249-
newNotifyInputFromIssue(issue, webhook_module.HookEventIssueComment).
306+
307+
newNotifyInputFromIssue(comment.Issue, event).
250308
WithDoer(doer).
251-
WithPayload(&api.IssueCommentPayload{
252-
Action: api.HookIssueCommentCreated,
253-
Issue: convert.ToAPIIssue(ctx, issue),
254-
Comment: convert.ToAPIComment(ctx, repo, comment),
255-
Repository: convert.ToRepo(ctx, repo, permission),
256-
Sender: convert.ToUser(ctx, doer, nil),
257-
IsPull: false,
258-
}).
309+
WithPayload(payload).
259310
Notify(ctx)
260311
}
261312

@@ -497,7 +548,6 @@ func (n *actionsNotifier) DeleteRef(ctx context.Context, pusher *user_model.User
497548
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm_model.AccessModeNone})
498549

499550
newNotifyInput(repo, pusher, webhook_module.HookEventDelete).
500-
WithRef(refFullName.ShortName()). // FIXME: should we use a full ref name
501551
WithPayload(&api.DeletePayload{
502552
Ref: refFullName.ShortName(),
503553
RefType: refFullName.RefType(),

services/actions/notifier_helper.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,15 @@ func notify(ctx context.Context, input *notifyInput) error {
132132
defer gitRepo.Close()
133133

134134
ref := input.Ref
135-
if input.Event == webhook_module.HookEventDelete {
136-
// The event is deleting a reference, so it will fail to get the commit for a deleted reference.
137-
// Set ref to empty string to fall back to the default branch.
138-
ref = ""
135+
if ref != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
136+
if ref != "" {
137+
log.Warn("Event %q should only trigger workflows on the default branch, but its ref is %q. Will fall back to the default branch",
138+
input.Event, ref)
139+
}
140+
ref = input.Repo.DefaultBranch
139141
}
140142
if ref == "" {
143+
log.Warn("Ref of event %q is empty, will fall back to the default branch", input.Event)
141144
ref = input.Repo.DefaultBranch
142145
}
143146

0 commit comments

Comments
 (0)