Skip to content

Commit de6de63

Browse files
zakiskchmouel
authored andcommitted
chore: return early from detect for edited comments
when edited comment event is recieved, we used to return in ParsePayload which is quite late because we don't wanna entertain that event for issue_comment and commit_comment so this commit makes it return early in detect func. Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 2dc12b5 commit de6de63

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

pkg/provider/github/detect.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ func (v *Provider) detectTriggerTypeFromPayload(ghEventType string, eventInt any
7676
}
7777
return "", fmt.Sprintf("pull_request: unsupported action \"%s\"", event.GetAction())
7878
case *github.IssueCommentEvent:
79-
if event.GetAction() == "created" &&
80-
event.GetIssue().IsPullRequest() &&
79+
if event.GetAction() != "created" {
80+
return "", fmt.Sprintf("issue_comment: unsupported action \"%s\"", event.GetAction())
81+
}
82+
83+
if event.GetIssue().IsPullRequest() &&
8184
event.GetIssue().GetState() == "open" {
8285
if opscomments.IsTestRetestComment(event.GetComment().GetBody(), gitOpsCommentPrefix) {
8386
return triggertype.Retest, ""
@@ -101,18 +104,20 @@ func (v *Provider) detectTriggerTypeFromPayload(ghEventType string, eventInt any
101104
}
102105
return "", fmt.Sprintf("check_run: unsupported action \"%s\"", event.GetAction())
103106
case *github.CommitCommentEvent:
104-
if event.GetAction() == "created" {
105-
if opscomments.IsTestRetestComment(event.GetComment().GetBody(), gitOpsCommentPrefix) {
106-
return triggertype.Retest, ""
107-
}
108-
if opscomments.IsCancelComment(event.GetComment().GetBody(), gitOpsCommentPrefix) {
109-
return triggertype.Cancel, ""
110-
}
111-
// Here, the `/ok-to-test` command is ignored because it is intended for pull requests.
112-
// For unauthorized users, it has no relevance to pushed commits, as only authorized users
113-
// are allowed to run CI on pushed commits. Therefore, the `ok-to-test` command holds no significance in this context.
114-
// However, it is left to be processed by the `on-comment` annotation rather than returning an error.
107+
if event.GetAction() != "created" {
108+
return "", fmt.Sprintf("commit_comment: unsupported action \"%s\"", event.GetAction())
109+
}
110+
111+
if opscomments.IsTestRetestComment(event.GetComment().GetBody(), gitOpsCommentPrefix) {
112+
return triggertype.Retest, ""
113+
}
114+
if opscomments.IsCancelComment(event.GetComment().GetBody(), gitOpsCommentPrefix) {
115+
return triggertype.Cancel, ""
115116
}
117+
// Here, the `/ok-to-test` command is ignored because it is intended for pull requests.
118+
// For unauthorized users, it has no relevance to pushed commits, as only authorized users
119+
// are allowed to run CI on pushed commits. Therefore, the `ok-to-test` command holds no significance in this context.
120+
// However, it is left to be processed by the `on-comment` annotation rather than returning an error.
116121
return triggertype.Comment, ""
117122
}
118123
return "", fmt.Sprintf("github: event \"%v\" is not supported", ghEventType)

pkg/provider/github/detect_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ func TestProviderDetect(t *testing.T) {
7474
event: github.CommitCommentEvent{
7575
Action: github.Ptr("something"),
7676
},
77+
wantReason: "commit_comment: unsupported action \"something\"",
7778
eventType: "commit_comment",
7879
isGH: true,
79-
processReq: true,
80+
processReq: false,
8081
},
8182
{
8283
name: "invalid check run Event",
@@ -92,9 +93,10 @@ func TestProviderDetect(t *testing.T) {
9293
event: github.IssueCommentEvent{
9394
Action: github.Ptr("deleted"),
9495
},
96+
wantReason: "issue_comment: unsupported action \"deleted\"",
9597
eventType: "issue_comment",
9698
isGH: true,
97-
processReq: true,
99+
processReq: false,
98100
},
99101
{
100102
name: "issue comment Event with no valid comment",

pkg/provider/github/parse_payload.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,6 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
353353
return nil, fmt.Errorf("no github client has been initialized, " +
354354
"exiting... (hint: did you forget setting a secret on your repo?)")
355355
}
356-
if gitEvent.GetAction() != "created" {
357-
return nil, fmt.Errorf("only newly created comment is supported, received: %s", gitEvent.GetAction())
358-
}
359356
processedEvent, err = v.handleIssueCommentEvent(ctx, gitEvent)
360357
if err != nil {
361358
return nil, err

pkg/provider/github/parse_payload_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -744,14 +744,6 @@ func TestParsePayLoad(t *testing.T) {
744744
},
745745
},
746746
},
747-
{
748-
name: "bad/issue_comment_not_from_created",
749-
wantErrString: "only newly created comment is supported, received: deleted",
750-
payloadEventStruct: github.IssueCommentEvent{Action: github.Ptr("deleted")},
751-
eventType: "issue_comment",
752-
triggerTarget: "pull_request",
753-
githubClient: true,
754-
},
755747
{
756748
name: "good/issue comment",
757749
eventType: "issue_comment",

0 commit comments

Comments
 (0)