Skip to content

Commit 846416e

Browse files
zakiskchmouel
authored andcommitted
fix: Improve handling of missing .tekton directory for ok-to-test events
Update getPipelineRunsFromRepo to create a neutral status with specific messaging when .tekton directory is missing during ok-to-test events, clarifying that the directory doesn't exist in the repository's root. Adjust error logging for push events to handle empty rawTemplates case without creating comments, ensuring consistent behavior across event types. Modify unit tests in match_test.go to cover the new missing .tekton directory scenario, including a new test case using "testdata/no_tekton_dir" and removing unnecessary status expectation fields for simplicity. https://issues.redhat.com/browse/SRVKP-7680 Signed-off-by: Zaki Shaikh <[email protected]>
1 parent e790bc0 commit 846416e

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

docs/content/docs/guide/gitops_commands.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,7 @@ Here are the possible event types:
262262
* `cancel-comment`: The event is a `/cancel <PipelineRun>` that would cancel a specific PipelineRun.
263263
* `ok-to-test-comment`: The event is a `/ok-to-test` that would allow running the CI for an unauthorized user.
264264

265-
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,
266-
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.
265+
If a repository owner comments `/ok-to-test` on a pull request from an external contributor but no PipelineRun **matches** the `pull_request` event (or the repository has no `.tekton` directory), Pipelines-as-Code sets a **neutral** commit status. This indicates that no PipelineRun was matched, allowing other workflows—such as auto-merge—to proceed without being blocked.
267266

268267
{{< hint info >}}
269268

pkg/pipelineascode/errors.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import (
1313
"go.uber.org/zap"
1414
)
1515

16-
const validationErrorTemplate = `> [!CAUTION]
16+
const (
17+
validationErrorTemplate = `> [!CAUTION]
1718
> There are some errors in your PipelineRun template.
1819
1920
| PipelineRun | Error |
2021
|------|-------|`
22+
tektonDirMissingError = ".tekton/ directory doesn't exist in repository's root directory"
23+
)
2124

2225
var regexpIgnoreErrors = regexp.MustCompile(`.*no kind.*is registered for version.*in scheme.*`)
2326

pkg/pipelineascode/match.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
196196
return nil, err
197197
}
198198

199+
if rawTemplates == "" && p.event.EventType == opscomments.OkToTestCommentEventType.String() {
200+
err = p.createNeutralStatus(ctx, ".tekton directory not found", tektonDirMissingError)
201+
if err != nil {
202+
p.eventEmitter.EmitMessage(nil, zap.ErrorLevel, "RepositoryCreateStatus", err.Error())
203+
}
204+
}
205+
199206
// This is for push event error logging because we can't create comment for yaml validation errors on push
200207
if err != nil || rawTemplates == "" {
201208
msg := ""
@@ -279,10 +286,12 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
279286
// GitOps command `/ok-to-test` to trigger CI, but no matching pull request is found,
280287
// a neutral check-run will be created on the pull request to indicate that no PipelineRun was triggered
281288
if p.event.EventType == opscomments.OkToTestCommentEventType.String() && len(matchedPRs) == 0 {
282-
err = p.createNeutralStatus(ctx)
289+
text := fmt.Sprintf("No matching PipelineRun found for the '%s' event in .tekton/ directory. Please ensure that PipelineRun is configured for '%s' event.", p.event.TriggerTarget.String(), p.event.TriggerTarget.String())
290+
err = p.createNeutralStatus(ctx, "No PipelineRun matched", text)
283291
if err != nil {
284292
p.eventEmitter.EmitMessage(nil, zap.WarnLevel, "RepositoryCreateStatus", err.Error())
285293
}
294+
p.eventEmitter.EmitMessage(nil, zap.InfoLevel, "RepositoryNoMatch", text)
286295
}
287296
return nil, nil
288297
}
@@ -426,11 +435,11 @@ func (p *PacRun) checkNeedUpdate(_ string) (string, bool) {
426435
return "", false
427436
}
428437

429-
func (p *PacRun) createNeutralStatus(ctx context.Context) error {
438+
func (p *PacRun) createNeutralStatus(ctx context.Context, title, text string) error {
430439
status := provider.StatusOpts{
431440
Status: CompletedStatus,
432-
Title: "No PipelineRun matched",
433-
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()),
441+
Title: title,
442+
Text: text,
434443
Conclusion: neutralConclusion,
435444
DetailsURL: p.event.URL,
436445
}

pkg/pipelineascode/match_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ func TestGetPipelineRunsFromRepo(t *testing.T) {
234234
expectedNumberOfPruns: 0,
235235
event: okToTestEvent,
236236
},
237+
{
238+
name: "no .tekton dir in repository",
239+
repositories: &v1alpha1.Repository{
240+
ObjectMeta: metav1.ObjectMeta{
241+
Name: "testrepo",
242+
Namespace: "test",
243+
},
244+
Spec: v1alpha1.RepositorySpec{},
245+
},
246+
tektondir: "testdata/no_tekton_dir",
247+
expectedNumberOfPruns: 0,
248+
event: okToTestEvent,
249+
},
237250
}
238251
for _, tt := range tests {
239252
t.Run(tt.name, func(t *testing.T) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
we need this file in order to keep no_tekton_dir folder otherwise git ignores it.

0 commit comments

Comments
 (0)