Skip to content

Commit 608c97b

Browse files
Disable Github comments for pipeline start/end events to reduce PR noise
The Github webhook integration was previously generating a comment on every pipeline start and end filling pull requests with redundant updates Hence this patch introduces a new setting to completely disable comment creation By using `comment_strategy: disable_all`, no comments will be added for either pipeline start or completion Also includes e2e tests to validate the new behavior Signed-off-by: PuneetPunamiya <[email protected]>
1 parent 67c2fb1 commit 608c97b

File tree

7 files changed

+97
-11
lines changed

7 files changed

+97
-11
lines changed

config/300-repositories.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,18 @@ spec:
334334
Settings contains the configuration settings for the repository, including
335335
authorization policies, provider-specific configuration, and provenance settings.
336336
properties:
337+
github:
338+
properties:
339+
comment_strategy:
340+
description: |-
341+
CommentStrategy defines how GitLab comments are handled for pipeline results.
342+
Options:
343+
- 'disable_all': Disables all comments on merge requests
344+
enum:
345+
- ""
346+
- disable_all
347+
type: string
348+
type: object
337349
github_app_token_scope_repos:
338350
description: |-
339351
GithubAppTokenScopeRepos lists repositories that can access the GitHub App token when using the

docs/content/docs/guide/repositorycrd.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ spec:
134134

135135
When you set the value of `comment_strategy` to `disable_all` it will not add any comment on the merge request for the start and the end of pipelinerun
136136

137+
## Disabling all comments for Pipelineruns in GitHub Pull Requests on GitHub Webhook Setup
138+
139+
`comment_strategy` allows you to disable the comments on GitHub PR for a Repository
140+
141+
```yaml
142+
spec:
143+
settings:
144+
github:
145+
comment_strategy: "disable_all"
146+
```
147+
148+
When `comment_strategy` is set to `disable_all` Pipelines as Code will not create any comment on the pull request for PipelineRun Status
149+
137150
## Concurrency
138151

139152
`concurrency_limit` allows you to define the maximum number of PipelineRuns running at any time for a Repository.

pkg/apis/pipelinesascode/v1alpha1/types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ type Settings struct {
158158
// Gitlab contains GitLab-specific settings for repositories hosted on GitLab.
159159
// +optional
160160
Gitlab *GitlabSettings `json:"gitlab,omitempty"`
161+
162+
Github *GithubSettings `json:"github,omitempty"`
161163
}
162164

163165
type GitlabSettings struct {
@@ -169,6 +171,15 @@ type GitlabSettings struct {
169171
CommentStrategy string `json:"comment_strategy,omitempty"`
170172
}
171173

174+
type GithubSettings struct {
175+
// CommentStrategy defines how GitLab comments are handled for pipeline results.
176+
// Options:
177+
// - 'disable_all': Disables all comments on merge requests
178+
// +optional
179+
// +kubebuilder:validation:Enum="";disable_all
180+
CommentStrategy string `json:"comment_strategy,omitempty"`
181+
}
182+
172183
func (s *Settings) Merge(newSettings *Settings) {
173184
if newSettings.PipelineRunProvenance != "" && s.PipelineRunProvenance == "" {
174185
s.PipelineRunProvenance = newSettings.PipelineRunProvenance

pkg/provider/github/status.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,27 @@ func (v *Provider) createStatusCommit(ctx context.Context, runevent *info.Event,
332332
eventType = triggertype.PullRequest
333333
}
334334

335-
if (status.Status == "completed" || (status.Status == "queued" && status.Title == pendingApproval)) &&
336-
status.Text != "" && eventType == triggertype.PullRequest {
337-
_, _, err = v.Client().Issues.CreateComment(ctx, runevent.Organization, runevent.Repository,
338-
runevent.PullRequestNumber,
339-
&github.IssueComment{
340-
Body: github.Ptr(fmt.Sprintf("%s<br>%s", status.Summary, status.Text)),
341-
},
342-
)
343-
if err != nil {
344-
return err
335+
var commentStrategy string
336+
if v.repo != nil && v.repo.Spec.Settings != nil && v.repo.Spec.Settings.Github != nil {
337+
commentStrategy = v.repo.Spec.Settings.Github.CommentStrategy
338+
}
339+
340+
switch commentStrategy {
341+
case "disable_all":
342+
v.Logger.Warn("github: comments related to PipelineRuns status have been disabled for Github pull requests")
343+
return nil
344+
default:
345+
if (status.Status == "completed" || (status.Status == "queued" && status.Title == pendingApproval)) &&
346+
status.Text != "" && eventType == triggertype.PullRequest {
347+
_, _, err = v.Client().Issues.CreateComment(ctx, runevent.Organization, runevent.Repository,
348+
runevent.PullRequestNumber,
349+
&github.IssueComment{
350+
Body: github.Ptr(fmt.Sprintf("%s<br>%s", status.Summary, status.Text)),
351+
},
352+
)
353+
if err != nil {
354+
return err
355+
}
345356
}
346357
}
347358

test/github_pullrequest_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
13+
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
1314
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1415
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1516
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
@@ -582,6 +583,40 @@ func TestGithubPullandPushMatchTriggerOnlyPull(t *testing.T) {
582583
assert.NilError(t, err)
583584
}
584585

586+
func TestGithubDisableCommentsOnPR(t *testing.T) {
587+
if os.Getenv("TEST_GITHUB_REPO_OWNER_WEBHOOK") == "" {
588+
t.Skip("TEST_GITHUB_REPO_OWNER_WEBHOOK is not set")
589+
return
590+
}
591+
ctx := context.Background()
592+
593+
g := &tgithub.PRTest{
594+
Label: "Github PullRequest onWebhook",
595+
YamlFiles: []string{"testdata/pipelinerun.yaml"},
596+
Webhook: true,
597+
}
598+
599+
commentStrategy := v1alpha1.Settings{
600+
Github: &v1alpha1.GithubSettings{
601+
CommentStrategy: "disable_all",
602+
},
603+
}
604+
605+
g.Options.Settings = commentStrategy
606+
g.RunPullRequest(ctx, t)
607+
defer g.TearDown(ctx, t)
608+
609+
comments, _, _ := g.Provider.Client().Issues.ListComments(ctx, g.Options.Organization, g.Options.Repo, g.PRNumber, nil)
610+
commentRegexp := regexp.MustCompile(`.*Pipelines as Code CI/*`)
611+
successCommentsPost := 0
612+
for _, n := range comments {
613+
if commentRegexp.MatchString(*n.Body) {
614+
successCommentsPost++
615+
}
616+
}
617+
assert.Equal(t, 0, successCommentsPost)
618+
}
619+
585620
// Local Variables:
586621
// compile-command: "go test -tags=e2e -v -info TestGithubPullRequest$ ."
587622
// End:

test/pkg/github/crd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func CreateCRD(ctx context.Context, t *testing.T, repoinfo *ghlib.Repository, ru
2121
Name: targetNS,
2222
},
2323
Spec: v1alpha1.RepositorySpec{
24-
URL: repoinfo.GetHTMLURL(),
24+
URL: repoinfo.GetHTMLURL(),
25+
Settings: &opts.Settings,
2526
},
2627
}
2728

test/pkg/github/pr.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func (g *PRTest) RunPullRequest(ctx context.Context, t *testing.T) {
137137
t.Errorf("Repository %s not found in %s", opts.Organization, opts.Repo)
138138
}
139139

140+
if g.Options.Settings.Github != nil {
141+
opts.Settings = g.Options.Settings
142+
}
140143
err = CreateCRD(ctx, t, repoinfo, runcnx, opts, targetNS)
141144
assert.NilError(t, err)
142145

0 commit comments

Comments
 (0)