Skip to content

Commit a0b30ad

Browse files
committed
support issue_comment
1 parent 5e72526 commit a0b30ad

File tree

4 files changed

+146
-4
lines changed

4 files changed

+146
-4
lines changed

modules/actions/github.go

Lines changed: 44 additions & 0 deletions
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.HookEventUncyclo:
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,95 @@ func (n *actionsNotifier) CreateIssueComment(ctx context.Context, doer *user_mod
258258
Notify(ctx)
259259
}
260260

261+
func (m *actionsNotifier) UpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
262+
ctx = withMethod(ctx, "UpdateComment")
263+
264+
if err := c.LoadIssue(ctx); err != nil {
265+
log.Error("LoadIssue: %v", err)
266+
return
267+
}
268+
if err := c.Issue.LoadAttributes(ctx); err != nil {
269+
log.Error("LoadAttributes: %v", err)
270+
return
271+
}
272+
273+
permission, _ := access_model.GetUserRepoPermission(ctx, c.Issue.Repo, doer)
274+
275+
payload := &api.IssueCommentPayload{
276+
Action: api.HookIssueCommentEdited,
277+
Issue: convert.ToAPIIssue(ctx, c.Issue),
278+
Comment: convert.ToAPIComment(ctx, c.Issue.Repo, c),
279+
Repository: convert.ToRepo(ctx, c.Issue.Repo, permission),
280+
Changes: &api.ChangesPayload{
281+
Body: &api.ChangesFromPayload{
282+
From: oldContent,
283+
},
284+
},
285+
Sender: convert.ToUser(ctx, doer, nil),
286+
IsPull: c.Issue.IsPull,
287+
}
288+
289+
if c.Issue.IsPull {
290+
if err := c.Issue.LoadPullRequest(ctx); err != nil {
291+
log.Error("LoadPullRequest: %v", err)
292+
return
293+
}
294+
newNotifyInputFromIssue(c.Issue, webhook_module.HookEventPullRequestComment).
295+
WithDoer(doer).
296+
WithPayload(payload).
297+
WithPullRequest(c.Issue.PullRequest).
298+
Notify(ctx)
299+
return
300+
}
301+
302+
newNotifyInputFromIssue(c.Issue, webhook_module.HookEventIssueComment).
303+
WithDoer(doer).
304+
WithPayload(payload).
305+
Notify(ctx)
306+
}
307+
308+
func (m *actionsNotifier) DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
309+
ctx = withMethod(ctx, "DeleteComment")
310+
311+
if err := comment.LoadIssue(ctx); err != nil {
312+
log.Error("LoadIssue: %v", err)
313+
return
314+
}
315+
if err := comment.Issue.LoadAttributes(ctx); err != nil {
316+
log.Error("LoadAttributes: %v", err)
317+
return
318+
}
319+
320+
permission, _ := access_model.GetUserRepoPermission(ctx, comment.Issue.Repo, doer)
321+
322+
payload := &api.IssueCommentPayload{
323+
Action: api.HookIssueCommentDeleted,
324+
Issue: convert.ToAPIIssue(ctx, comment.Issue),
325+
Comment: convert.ToAPIComment(ctx, comment.Issue.Repo, comment),
326+
Repository: convert.ToRepo(ctx, comment.Issue.Repo, permission),
327+
Sender: convert.ToUser(ctx, doer, nil),
328+
IsPull: comment.Issue.IsPull,
329+
}
330+
331+
if comment.Issue.IsPull {
332+
if err := comment.Issue.LoadPullRequest(ctx); err != nil {
333+
log.Error("LoadPullRequest: %v", err)
334+
return
335+
}
336+
newNotifyInputFromIssue(comment.Issue, webhook_module.HookEventPullRequestComment).
337+
WithDoer(doer).
338+
WithPayload(payload).
339+
WithPullRequest(comment.Issue.PullRequest).
340+
Notify(ctx)
341+
return
342+
}
343+
344+
newNotifyInputFromIssue(comment.Issue, webhook_module.HookEventIssueComment).
345+
WithDoer(doer).
346+
WithPayload(payload).
347+
Notify(ctx)
348+
}
349+
261350
func (n *actionsNotifier) NewPullRequest(ctx context.Context, pull *issues_model.PullRequest, _ []*user_model.User) {
262351
ctx = withMethod(ctx, "NewPullRequest")
263352

services/actions/notifier_helper.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ func notify(ctx context.Context, input *notifyInput) error {
136136
defer gitRepo.Close()
137137

138138
ref := input.Ref
139-
if input.Event == webhook_module.HookEventDelete {
140-
// The event is deleting a reference, so it will fail to get the commit for a deleted reference.
141-
// Set ref to empty string to fall back to the default branch.
142-
ref = ""
139+
if ref != input.Repo.DefaultBranch && actions_module.IsDefaultBranchWorkflow(input.Event) {
140+
if ref != "" {
141+
log.Warn("Event %q should only trigger workflows on the default branch, but its ref is %q. Will fall back to the default branch",
142+
input.Event, ref)
143+
}
144+
ref = input.Repo.DefaultBranch
143145
}
144146
if ref == "" {
147+
log.Warn("Ref of event %q is empty, will fall back to the default branch", input.Event)
145148
ref = input.Repo.DefaultBranch
146149
}
147150

0 commit comments

Comments
 (0)