Skip to content

Commit a744645

Browse files
Warn user when on cel-expression is used along side on-target branch and 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 a744645

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

pkg/matcher/annotation_matcher.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"github.com/google/cel-go/common/types"
1111
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode"
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
apipac "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
15+
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
1416
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
1517
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1618
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
@@ -149,7 +151,7 @@ func getName(prun *tektonv1.PipelineRun) string {
149151
return name
150152
}
151153

152-
func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger, pruns []*tektonv1.PipelineRun, cs *params.Run, event *info.Event, vcx provider.Interface) ([]Match, error) {
154+
func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger, pruns []*tektonv1.PipelineRun, cs *params.Run, event *info.Event, vcx provider.Interface, eventEmitter *events.EventEmitter, repo *v1alpha1.Repository) ([]Match, error) {
153155
matchedPRs := []Match{}
154156
infomsg := fmt.Sprintf("matching pipelineruns to event: URL=%s, target-branch=%s, source-branch=%s, target-event=%s",
155157
event.URL,
@@ -226,6 +228,23 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
226228
if event.EventType == opscomments.NoOpsCommentEventType.String() || event.EventType == opscomments.OnCommentEventType.String() {
227229
continue
228230
}
231+
232+
if prun.GetObjectMeta().GetAnnotations()[keys.OnCelExpression] != "" {
233+
if prun.GetObjectMeta().GetAnnotations()[keys.OnEvent] != "" && prun.GetObjectMeta().GetAnnotations()[keys.OnTargetBranch] != "" {
234+
msg := fmt.Sprintf("Warning: The PipelineRun '%s' has both `on-cel-expression` and `on-event`/`on-target-branch` annotations. The `on-cel-expression` will take precedence, and the other annotations will be ignored.", prun.Name)
235+
logger.Warnf(msg)
236+
eventEmitter.EmitMessage(repo, zap.WarnLevel, "RepositoryCannotLocatePipelineRun", msg)
237+
} else if prun.GetObjectMeta().GetAnnotations()[keys.OnEvent] != "" {
238+
msg := fmt.Sprintf("Warning: The PipelineRun '%s' has both `on-cel-expression` and `on-event` annotations. The `on-cel-expression` will take precedence, and the `on-event` annotation will be ignored.", prun.Name)
239+
logger.Warnf(msg)
240+
eventEmitter.EmitMessage(repo, zap.WarnLevel, "RespositoryTakesOnCelExpressionPrecedence", msg)
241+
} else if prun.GetObjectMeta().GetAnnotations()[keys.OnTargetBranch] != "" {
242+
msg := fmt.Sprintf("Warning: The PipelineRun '%s' has both `on-cel-expression` and `on-target-branch` annotations. The `on-cel-expression` will take precedence, and the `on-target-branch` annotation will be ignored.", prun.Name)
243+
logger.Warnf(msg)
244+
eventEmitter.EmitMessage(repo, zap.WarnLevel, "RespositoryTakesOnCelExpressionPrecedence", msg)
245+
}
246+
}
247+
229248
if celExpr, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnCelExpression]; ok {
230249
out, err := celEvaluate(ctx, celExpr, event, vcx)
231250
if err != nil {

pkg/matcher/annotation_matcher_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ func runTest(ctx context.Context, t *testing.T, tt annotationTest, vcx provider.
14161416

14171417
matches, err := MatchPipelinerunByAnnotation(ctx, logger,
14181418
tt.args.pruns,
1419-
client, &tt.args.runevent, vcx,
1419+
client, &tt.args.runevent, vcx, nil, nil,
14201420
)
14211421

14221422
if tt.wantLog != "" {
@@ -1883,7 +1883,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
18831883
Clients: clients.Clients{},
18841884
Info: info.Info{},
18851885
}
1886-
matches, err := MatchPipelinerunByAnnotation(ctx, logger, tt.args.pruns, cs, &tt.args.runevent, &ghprovider.Provider{})
1886+
matches, err := MatchPipelinerunByAnnotation(ctx, logger, tt.args.pruns, cs, &tt.args.runevent, &ghprovider.Provider{}, nil, nil)
18871887
if (err != nil) != tt.wantErr {
18881888
t.Errorf("MatchPipelinerunByAnnotation() error = %v, wantErr %v", err, tt.wantErr)
18891889
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)