@@ -11,6 +11,7 @@ import (
11
11
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode"
12
12
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
13
13
apipac "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
14
+ "github.com/openshift-pipelines/pipelines-as-code/pkg/events"
14
15
"github.com/openshift-pipelines/pipelines-as-code/pkg/opscomments"
15
16
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
16
17
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
@@ -149,7 +150,48 @@ func getName(prun *tektonv1.PipelineRun) string {
149
150
return name
150
151
}
151
152
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 := ""
177
+ if prun .Name != "" {
178
+ prName = prun .Name
179
+ } else {
180
+ prName = prun .GetObjectMeta ().GetAnnotations ()[keys .OriginalPRName ]
181
+ }
182
+
183
+ if len (annotations ) > 0 {
184
+ ignoredAnnotations := strings .Join (annotations , ", " )
185
+ msg := fmt .Sprintf (
186
+ "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" ,
187
+ prName ,
188
+ ignoredAnnotations ,
189
+ )
190
+ eventEmitter .EmitMessage (repo , zap .WarnLevel , "RespositoryTakesOnCelExpressionPrecedence" , msg )
191
+ }
192
+ }
193
+
194
+ 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 ) {
153
195
matchedPRs := []Match {}
154
196
infomsg := fmt .Sprintf ("matching pipelineruns to event: URL=%s, target-branch=%s, source-branch=%s, target-event=%s" ,
155
197
event .URL ,
@@ -226,7 +268,10 @@ func MatchPipelinerunByAnnotation(ctx context.Context, logger *zap.SugaredLogger
226
268
if event .EventType == opscomments .NoOpsCommentEventType .String () || event .EventType == opscomments .OnCommentEventType .String () {
227
269
continue
228
270
}
271
+
229
272
if celExpr , ok := prun .GetObjectMeta ().GetAnnotations ()[keys .OnCelExpression ]; ok {
273
+ checkPipelineRunAnnotation (prun , eventEmitter , repo )
274
+
230
275
out , err := celEvaluate (ctx , celExpr , event , vcx )
231
276
if err != nil {
232
277
logger .Errorf ("there was an error evaluating the CEL expression, skipping: %v" , err )
0 commit comments