Skip to content

Commit b4305a4

Browse files
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 <[email protected]>
1 parent 8ce32f3 commit b4305a4

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

pkg/matcher/annotation_matcher.go

+25-1
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,16 @@ 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+
func appendAnnotationIfNotEmpty(annotationKey, annotationValue string) []string {
154+
annotations := []string{}
155+
if annotationValue != "" {
156+
annotations = append(annotations, annotationKey)
157+
return annotations
158+
}
159+
return nil
160+
}
161+
162+
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) {
153163
matchedPRs := []Match{}
154164
infomsg := fmt.Sprintf("matching pipelineruns to event: URL=%s, target-branch=%s, source-branch=%s, target-event=%s",
155165
event.URL,
@@ -226,7 +236,21 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
226236
if event.EventType == opscomments.NoOpsCommentEventType.String() || event.EventType == opscomments.OnCommentEventType.String() {
227237
continue
228238
}
239+
229240
if celExpr, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnCelExpression]; ok {
241+
// Checks if the Pipelinerun has `on-event`/`on-target-branch annotations` with `on-cel-expression`
242+
// and if present then warns the user that `on-cel-expression` will take precedence
243+
annotations := []string{}
244+
annotations = append(annotations, appendAnnotationIfNotEmpty("on-event", prun.GetObjectMeta().GetAnnotations()[keys.OnEvent])...)
245+
annotations = append(annotations, appendAnnotationIfNotEmpty("on-target-branch", prun.GetObjectMeta().GetAnnotations()[keys.OnTargetBranch])...)
246+
247+
if len(annotations) > 0 {
248+
ignoredAnnotations := strings.Join(annotations, ", ")
249+
msg := fmt.Sprintf("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", prun.Name, ignoredAnnotations)
250+
logger.Warnf(msg)
251+
eventEmitter.EmitMessage(repo, zap.WarnLevel, "RespositoryTakesOnCelExpressionPrecedence", msg)
252+
}
253+
230254
out, err := celEvaluate(ctx, celExpr, event, vcx)
231255
if err != nil {
232256
logger.Errorf("there was an error evaluating the CEL expression, skipping: %v", err)

pkg/matcher/annotation_matcher_test.go

+6-2
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 != "" {
@@ -1883,7 +1885,9 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
18831885
Clients: clients.Clients{},
18841886
Info: info.Info{},
18851887
}
1886-
matches, err := MatchPipelinerunByAnnotation(ctx, logger, tt.args.pruns, cs, &tt.args.runevent, &ghprovider.Provider{})
1888+
1889+
eventEmitter := events.NewEventEmitter(cs.Clients.Kube, logger)
1890+
matches, err := MatchPipelinerunByAnnotation(ctx, logger, tt.args.pruns, cs, &tt.args.runevent, &ghprovider.Provider{}, eventEmitter, nil)
18871891
if (err != nil) != tt.wantErr {
18881892
t.Errorf("MatchPipelinerunByAnnotation() error = %v, wantErr %v", err, tt.wantErr)
18891893
return

pkg/pipelineascode/match.go

+3-3
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)