Skip to content

Commit 9b5ec18

Browse files
PuneetPunamiyachmouel
authored andcommitted
Warn user when on-cel-expression is used with on-target-branch/on-event
This patch warns users in both the `user events namespaces` and `controller logs`when using `on-event` and `on-target-branch` annotations along side `on-cel-expression` Hence warning users would avoid further confusion Fixes: openshift-pipelines#1974 Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
1 parent 8c9c443 commit 9b5ec18

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

pkg/matcher/annotation_matcher.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode"
1212
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1313
apipac "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
14+
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
1415
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1516
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1617
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
@@ -149,7 +150,42 @@ func getName(prun *tektonv1.PipelineRun) string {
149150
return name
150151
}
151152

152-
func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger, pruns []*tektonv1.PipelineRun, cs *params.Run, event *info.Event, vcx provider.Interface) ([]Match, error) {
153+
// checkPipelineRunAnnotation checks if the Pipelinerun has
154+
// `on-event`/`on-target-branch annotations` with `on-cel-expression`
155+
// and if present then warns the user that `on-cel-expression` will take precedence.
156+
func checkPipelineRunAnnotation(prun *tektonv1.PipelineRun, eventEmitter *events.EventEmitter, repo *apipac.Repository) {
157+
// Define the annotations to check in a slice for easy iteration
158+
checks := []struct {
159+
key string
160+
value string
161+
}{
162+
{"on-event", prun.GetObjectMeta().GetAnnotations()[keys.OnEvent]},
163+
{"on-target-branch", prun.GetObjectMeta().GetAnnotations()[keys.OnTargetBranch]},
164+
}
165+
166+
// Preallocate the annotations slice with the exact capacity needed
167+
annotations := make([]string, 0, len(checks))
168+
169+
// Iterate through each check and append the key if the value is non-empty
170+
for _, check := range checks {
171+
if check.value != "" {
172+
annotations = append(annotations, check.key)
173+
}
174+
}
175+
176+
prName := getName(prun)
177+
if len(annotations) > 0 {
178+
ignoredAnnotations := strings.Join(annotations, ", ")
179+
msg := fmt.Sprintf(
180+
"Warning: The Pipelinerun '%s' has 'on-cel-expression' defined along with [%s] annotation(s). The 'on-cel-expression' will take precedence and these annotations will be ignored",
181+
prName,
182+
ignoredAnnotations,
183+
)
184+
eventEmitter.EmitMessage(repo, zap.WarnLevel, "RespositoryTakesOnCelExpressionPrecedence", msg)
185+
}
186+
}
187+
188+
func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger, pruns []*tektonv1.PipelineRun, cs *params.Run, event *info.Event, vcx provider.Interface, eventEmitter *events.EventEmitter, repo *apipac.Repository) ([]Match, error) {
153189
matchedPRs := []Match{}
154190
infomsg := fmt.Sprintf("matching pipelineruns to event: URL=%s, target-branch=%s, source-branch=%s, target-event=%s",
155191
event.URL,
@@ -236,6 +272,8 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
236272
}
237273

238274
if celExpr, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnCelExpression]; ok {
275+
checkPipelineRunAnnotation(prun, eventEmitter, repo)
276+
239277
out, err := celEvaluate(ctx, celExpr, event, vcx)
240278
if err != nil {
241279
logger.Errorf("there was an error evaluating the CEL expression, skipping: %v", err)

pkg/matcher/annotation_matcher_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/jonboulle/clockwork"
1515
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
1616
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
17+
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
1718
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1819
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1920
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
@@ -1414,9 +1415,10 @@ func runTest(ctx context.Context, t *testing.T, tt annotationTest, vcx provider.
14141415
Info: info.Info{},
14151416
}
14161417

1418+
eventEmitter := events.NewEventEmitter(cs.Kube, logger)
14171419
matches, err := MatchPipelinerunByAnnotation(ctx, logger,
14181420
tt.args.pruns,
1419-
client, &tt.args.runevent, vcx,
1421+
client, &tt.args.runevent, vcx, eventEmitter, nil,
14201422
)
14211423

14221424
if tt.wantLog != "" {
@@ -1533,6 +1535,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
15331535
wantErr bool
15341536
wantPrName string
15351537
wantLog []string
1538+
logLevel int
15361539
}{
15371540
{
15381541
name: "good-match-with-only-one",
@@ -1609,6 +1612,38 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
16091612
wantErr: false,
16101613
wantPrName: pipelineCel.GetName(),
16111614
},
1615+
{
1616+
name: "cel-expression-takes-precedence-over-annotations",
1617+
args: args{
1618+
pruns: []*tektonv1.PipelineRun{
1619+
{
1620+
ObjectMeta: metav1.ObjectMeta{
1621+
Name: "pipeline-on-cel-test",
1622+
Annotations: map[string]string{
1623+
keys.OnEvent: "[pull_request]",
1624+
keys.OnTargetBranch: "[main]",
1625+
keys.OnCelExpression: `event == "pull_request" && target_branch == "main" && source_branch == "warn-for-cel"`,
1626+
},
1627+
},
1628+
},
1629+
},
1630+
runevent: info.Event{
1631+
URL: "https://hello/moto",
1632+
TriggerTarget: "pull_request",
1633+
EventType: "pull_request",
1634+
BaseBranch: "main",
1635+
HeadBranch: "warn-for-cel",
1636+
PullRequestNumber: 10,
1637+
Request: &info.Request{
1638+
Header: http.Header{},
1639+
},
1640+
},
1641+
},
1642+
wantErr: false,
1643+
wantLog: []string{
1644+
`Warning: The Pipelinerun 'pipeline-on-cel-test' has 'on-cel-expression' defined along with [on-event, on-target-branch] annotation(s). The 'on-cel-expression' will take precedence and these annotations will be ignored`,
1645+
},
1646+
},
16121647
{
16131648
name: "no-match-on-label",
16141649
args: args{
@@ -1914,7 +1949,9 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
19141949
Clients: clients.Clients{},
19151950
Info: info.Info{},
19161951
}
1917-
matches, err := MatchPipelinerunByAnnotation(ctx, logger, tt.args.pruns, cs, &tt.args.runevent, &ghprovider.Provider{})
1952+
1953+
eventEmitter := events.NewEventEmitter(cs.Clients.Kube, logger)
1954+
matches, err := MatchPipelinerunByAnnotation(ctx, logger, tt.args.pruns, cs, &tt.args.runevent, &ghprovider.Provider{}, eventEmitter, nil)
19181955
if (err != nil) != tt.wantErr {
19191956
t.Errorf("MatchPipelinerunByAnnotation() error = %v, wantErr %v", err, tt.wantErr)
19201957
return

pkg/pipelineascode/match.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
217217
return nil, err
218218
}
219219
// Don't fail or do anything if we don't have a match yet, we will do it properly later in this function
220-
_, _ = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, rtypes.PipelineRuns, p.run, p.event, p.vcx)
220+
_, _ = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, rtypes.PipelineRuns, p.run, p.event, p.vcx, p.eventEmitter, repo)
221221
}
222222
// Replace those {{var}} placeholders user has in her template to the run.Info variable
223223
allTemplates := p.makeTemplate(ctx, repo, rawTemplates)
@@ -248,7 +248,7 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
248248
// Match the PipelineRun with annotation
249249
var matchedPRs []matcher.Match
250250
if p.event.TargetTestPipelineRun == "" {
251-
if matchedPRs, err = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, pipelineRuns, p.run, p.event, p.vcx); err != nil {
251+
if matchedPRs, err = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, pipelineRuns, p.run, p.event, p.vcx, p.eventEmitter, repo); err != nil {
252252
// Don't fail when you don't have a match between pipeline and annotations
253253
p.eventEmitter.EmitMessage(nil, zap.WarnLevel, "RepositoryNoMatch", err.Error())
254254
// In a scenario where an external user submits a pull request and the repository owner uses the
@@ -336,7 +336,7 @@ func (p *PacRun) getPipelineRunsFromRepo(ctx context.Context, repo *v1alpha1.Rep
336336
}}, nil
337337
}
338338

339-
matchedPRs, err = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, pipelineRuns, p.run, p.event, p.vcx)
339+
matchedPRs, err = matcher.MatchPipelinerunByAnnotation(ctx, p.logger, pipelineRuns, p.run, p.event, p.vcx, p.eventEmitter, repo)
340340
if err != nil {
341341
// Don't fail when you don't have a match between pipeline and annotations
342342
p.eventEmitter.EmitMessage(nil, zap.WarnLevel, "RepositoryNoMatch", err.Error())

0 commit comments

Comments
 (0)