Skip to content

Fix ok-to-test comment issue for external user on GitHub #1899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/content/docs/guide/gitops_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ Here are the possible event types:
* `cancel-comment`: The event is a `/cancel <PipelineRun>` that would cancel a specific PipelineRun.
* `ok-to-test-comment`: The event is a `/ok-to-test` that would allow running the CI for an unauthorized user.

When a repository owner issues the `/ok-to-test` command on a pull request raised by an unauthorized user, and no PipelineRun exists in the .tekton directory for `pull_request` event,
Pipelines-as-Code will create a neutral check-run status. This status serves to indicate that no PipelineRun has been matched, preventing any workflows from being blocked such as auto-merge, will proceed as expected.

{{< hint info >}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to slow down on the hint everywhere..... it's fine now but next time let's just use a normal tone part of the documentation....


Note: This neutral check-run status functionality is only supported on GitHub.

{{< /hint >}}

When using the `{{ event_type }}` [dynamic variable]({{< relref "/docs/guide/authoringprs.md#dynamic-variables" >}}) for the following event types:

* `test-all-comment`
Expand Down
24 changes: 24 additions & 0 deletions pkg/pipelineascode/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
if matchedPRs, err = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, pipelineRuns, p.run, p.event, p.vcx); err != nil {
// Don't fail when you don't have a match between pipeline and annotations
p.eventEmitter.EmitMessage(nil, zap.WarnLevel, "RepositoryNoMatch", err.Error())
// In a scenario where an external user submits a pull request and the repository owner uses the
// GitOps command `/ok-to-test` to trigger CI, but no matching pull request is found,
// a neutral check-run will be created on the pull request to indicate that no PipelineRun was triggered
if p.event.EventType == opscomments.OkToTestCommentEventType.String() && len(matchedPRs) == 0 {
err = p.createNeutralStatus(ctx)
if err != nil {
p.eventEmitter.EmitMessage(nil, zap.WarnLevel, "RepositoryCreateStatus", err.Error())
}
}
return nil, nil
}
}
Expand Down Expand Up @@ -386,3 +395,18 @@ func (p *PacRun) checkAccessOrErrror(ctx context.Context, repo *v1alpha1.Reposit
}
return false, nil
}

func (p *PacRun) createNeutralStatus(ctx context.Context) error {
status := provider.StatusOpts{
Status: CompletedStatus,
Title: "No PipelineRun matched",
Text: fmt.Sprintf("No matching PipelineRun found for the '%s' event in Pipelines as Code. Please ensure that PipelineRun is configured for '%s' event.", p.event.TriggerTarget.String(), p.event.TriggerTarget.String()),
Conclusion: neutralConclusion,
DetailsURL: p.event.URL,
}
if err := p.vcx.CreateStatus(ctx, p.event, status); err != nil {
return fmt.Errorf("failed to run create status, user is not allowed to run the CI:: %w", err)
}

return nil
}
29 changes: 29 additions & 0 deletions pkg/pipelineascode/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ func TestGetPipelineRunsFromRepo(t *testing.T) {
EventType: "pull_request",
TriggerTarget: "pull_request",
}
okToTestEvent := &info.Event{
SHA: "principale",
Organization: "organizationes",
Repository: "lagaffe",
URL: "https://service/documentation",
HeadBranch: "main",
BaseBranch: "main",
Sender: "fantasio",
EventType: "ok-to-test-comment",
TriggerTarget: "pull_request",
}
testExplicitNoMatchPREvent := &info.Event{
SHA: "principale",
Organization: "organizationes",
Expand Down Expand Up @@ -188,6 +199,22 @@ func TestGetPipelineRunsFromRepo(t *testing.T) {
expectedNumberOfPruns: 1,
event: testExplicitNoMatchPREvent,
},
{
name: "no-match pipelineruns in .tekton dir, on ok-to-test command for an external user",
repositories: &v1alpha1.Repository{
ObjectMeta: metav1.ObjectMeta{
Name: "testrepo",
Namespace: "test",
},
Spec: v1alpha1.RepositorySpec{},
},
// if `testdata/no_yaml` dir is supplied here p.getPipelineRunsFromRepo func will return after
// GetTektonDir so providing `testdat/push_branch` so that it should call MatchPipelineRunsByAnnotation
// first and then create a neutral check-run.
tektondir: "testdata/push_branch",
expectedNumberOfPruns: 0,
event: okToTestEvent,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -221,10 +248,12 @@ func TestGetPipelineRunsFromRepo(t *testing.T) {
}
pacInfo := &info.PacOpts{
Settings: settings.Settings{
ApplicationName: "Pipelines as Code CI",
SecretAutoCreation: true,
RemoteTasks: true,
},
}
vcx.SetPacInfo(pacInfo)
p := NewPacs(tt.event, vcx, cs, pacInfo, k8int, logger, nil)
p.eventEmitter = events.NewEventEmitter(stdata.Kube, logger)
matchedPRs, err := p.getPipelineRunsFromRepo(ctx, tt.repositories)
Expand Down
1 change: 1 addition & 0 deletions pkg/pipelineascode/pipelineascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
queuedStatus = "queued"
failureConclusion = "failure"
pendingConclusion = "pending"
neutralConclusion = "neutral"
)

type PacRun struct {
Expand Down
6 changes: 4 additions & 2 deletions pkg/provider/github/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,10 @@ func (v *Provider) CreateStatus(ctx context.Context, runevent *info.Event, statu
statusOpts.Title = "Cancelled"
statusOpts.Summary = "has been <b>cancelled</b>."
case "neutral":
statusOpts.Title = "Unknown"
statusOpts.Summary = "doesn't know what happened with this commit."
if statusOpts.Title == "" {
statusOpts.Title = "Unknown"
}
statusOpts.Summary = "<b>Completed</b>"
}

if statusOpts.Status == "in_progress" {
Expand Down
Loading