Skip to content

Conversation

@theakshaypant
Copy link
Contributor

@theakshaypant theakshaypant commented Nov 12, 2025

📝 Description of the Change

Allow custom parameters from Repository CR to be accessed directly as CEL variables without template expansion. Parameters can now be used as: param_name == "value" on top of "{{param_name}}" == "value".

👨🏻‍ Linked Jira

https://issues.redhat.com/browse/SRVKP-9118

🔗 Linked GitHub Issue

N/A

🚀 Type of Change

  • 🐛 Bug fix (fix:)
  • ✨ New feature (feat:)
  • 💥 Breaking change (feat!:, fix!:)
  • 📚 Documentation update (docs:)
  • ⚙️ Chore (chore:)
  • 💅 Refactor (refactor:)
  • 🔧 Enhancement (enhance:)
  • 📦 Dependency update (deps:)

🧪 Testing Strategy

  • Unit tests
  • Integration tests
  • End-to-end tests
  • Manual testing
  • Not Applicable

🤖 AI Assistance

  • I have not used any AI assistance for this PR.
  • I have used AI assistance for this PR.

If you have used AI assistance, please provide the following details:

Which LLM was used?

  • GitHub Copilot
  • ChatGPT (OpenAI)
  • Claude (Anthropic)
  • Cursor
  • Gemini (Google)
  • Other: ____________

Extent of AI Assistance:

  • Documentation and research only
  • Unit tests or E2E tests only
  • Code generation (parts of the code)
  • Full code generation (most of the PR)
  • PR description and comments
  • Commit message(s)

Important

If the majority of the code in this PR was generated by an AI, please add a Co-authored-by trailer to your commit message.
For example:

Co-authored-by: Gemini gemini@google.com
Co-authored-by: ChatGPT noreply@chatgpt.com
Co-authored-by: Claude noreply@anthropic.com
Co-authored-by: Cursor noreply@cursor.com
Co-authored-by: Copilot Copilot@users.noreply.github.com

**💡You can use the script ./hack/add-llm-coauthor.sh to automatically add
these co-author trailers to your commits.

✅ Submitter Checklist

  • 📝 My commit messages are clear, informative, and follow the project's How to write a git commit message guide. The Gitlint linter ensures in CI it's properly validated
  • ✨ I have ensured my commit message prefix (e.g., fix:, feat:) matches the "Type of Change" I selected above.
  • ♽ I have run make test and make lint locally to check for and fix any
    issues. For an efficient workflow, I have considered installing
    pre-commit and running pre-commit install to
    automate these checks.
  • 📖 I have added or updated documentation for any user-facing changes.
  • 🧪 I have added sufficient unit tests for my code changes.
  • 🎁 I have added end-to-end tests where feasible. See README for more details.
  • 🔎 I have addressed any CI test flakiness or provided a clear reason to bypass it.
  • If adding a provider feature, I have filled in the following and updated the provider documentation:
    • GitHub App
    • GitHub Webhook
    • Gitea/Forgejo
    • GitLab
    • Bitbucket Cloud
    • Bitbucket Data Center

@pipelines-as-code
Copy link

pipelines-as-code bot commented Nov 12, 2025

🔍 PR Lint Feedback

Note: This automated check helps ensure your PR follows our contribution guidelines.

⚠️ Items that need attention:

🤖 AI attribution

The following commits lack an explicit AI attribution footer:

  • 7afc027 feat(cel): add direct custom param variable access in expressions

If no AI assistance was used for a commit, you can ignore this warning.
Otherwise add an Assisted-by: or Co-authored-by: footer referencing the AI used.


ℹ️ Next Steps

  • Review and address the items above
  • Push new commits to update this PR
  • This comment will be automatically updated when issues are resolved
🔧 Admin Tools (click to expand)

Automated Issue/Ticket Creation:

  • /issue-create - Generate a GitHub issue from this PR content using AI
  • /jira-create - Create a SRVKP Jira ticket from this PR content using AI

⚠️ Important: Always review and edit generated content before finalizing tickets/issues.
The AI-generated content should be used as a starting point and may need adjustments.

These commands are available to maintainers and will post the generated content as PR comments for review.

🤖 This feedback was generated automatically by the PR CI system

Copy link
Contributor Author

@theakshaypant theakshaypant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Docs update

cel.Lib(celPac{vcx, ctx, event}),
conflictingVar := make(map[string]bool)
for k, v := range customParams {
// Don't overwrite standard params
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to confirm if we want this behaviour. Modeled it on this

for k := range customParams {
// Don't overwrite standard params
if _, ok := conflictingVar[k]; !ok {
customParamDecls = append(customParamDecls, decls.NewVariable(k, types.StringType))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All variables are used as strings, do we want them to be DynType?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO string is preferable since the values are strictly strings in the input, but I don't feel super strongly in that conviction

@theakshaypant
Copy link
Contributor Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a useful feature allowing custom parameters from the Repository CR to be accessed directly as CEL variables, which improves the usability of CEL expressions. The implementation looks solid, reusing existing logic for parameter resolution and safely adding the custom parameters to the CEL context. The test coverage for the new functionality is also comprehensive.

I have a couple of minor suggestions for code simplification and to improve clarity in one of the new tests.

@theakshaypant theakshaypant force-pushed the SRVKP-9118-fix-cel-custom-params branch from 3d13ed7 to 6e26664 Compare November 13, 2025 12:34
@pipelines-as-code pipelines-as-code bot added documentation Improvements or additions to documentation feature New feature or request labels Nov 13, 2025
@theakshaypant
Copy link
Contributor Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a great feature allowing direct access to custom parameters as CEL variables. The implementation is solid, and the documentation updates are clear. I've identified one important bug related to how parameters are resolved when target-namespace is used, and I've suggested a fix. I also have a couple of suggestions to improve logging for conflicting parameters and to enhance documentation consistency. Overall, great work!

Comment on lines +215 to +219
// Resolve custom params once for all PipelineRuns (for use in CEL expressions)
customParams := resolveCustomParamsForCEL(ctx, repo, event, cs, vcx, eventEmitter, logger)
if len(customParams) > 0 {
logger.Debugf("resolved %d custom params from repo for CEL", len(customParams))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There is a bug in how custom parameters are resolved for CEL expressions. They are resolved only once at the beginning of MatchPipelinerunByAnnotation, using the initial repository. This doesn't account for PipelineRuns that use the pipelinesascode.tekton.dev/target-namespace annotation to target a different repository, which may have its own set of custom parameters. This can lead to CEL expressions being evaluated with incorrect parameter values.

To fix this, parameter resolution should be performed inside the for...range pruns loop for each PipelineRun, after its effective repository has been determined. To avoid performance degradation from resolving parameters repeatedly for the same repository, the results should be cached.

Suggested Refactoring:

  1. Remove this block of code (lines 215-219).
  2. Before the for...range pruns loop, initialize a cache, e.g., customParamsCache := make(map[string]map[string]string).
  3. Inside the loop, within the if celExpr, ok := ... block, implement logic to get parameters from the cache or resolve and cache them, using the correct repository for the current PipelineRun.

Example logic for inside the if celExpr, ok := ... block:

effectiveRepo := repo // Use the default repo passed to the function
if prMatch.Repo != nil {
    effectiveRepo = prMatch.Repo // A different repo was found via target-namespace
}

var customParams map[string]string
if effectiveRepo != nil {
    cacheKey := fmt.Sprintf("%s/%s", effectiveRepo.Namespace, effectiveRepo.Name)
    var ok bool
    if customParams, ok = customParamsCache[cacheKey]; !ok {
        // Not in cache, resolve and add it
        customParams = resolveCustomParamsForCEL(ctx, effectiveRepo, event, cs, vcx, eventEmitter, logger)
        customParamsCache[cacheKey] = customParams
    }
}

// Now evaluate with the correct customParams
out, err := celEvaluate(ctx, celExpr, event, vcx, customParams)
// ...

Comment on lines 96 to 98
} else {
conflictingVar[k] = true
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When a custom parameter conflicts with a standard CEL variable (e.g., a user defines a parameter named event), it is silently ignored. This can be confusing for users and make debugging difficult.

It would be more user-friendly to log a warning when this happens. To do this, you would need to:

  1. Pass the logger from MatchPipelinerunByAnnotation into this celEvaluate function.
  2. Add a logging statement inside this else block, like:
    logger.Warnf("Custom parameter %q conflicts with a standard CEL variable and will be ignored", k)

@theakshaypant theakshaypant force-pushed the SRVKP-9118-fix-cel-custom-params branch 2 times, most recently from 52e29ce to 354354e Compare November 13, 2025 14:36
@theakshaypant theakshaypant changed the title feat(cel): add direct custom param variable access feat(cel): add direct custom param variable access in expressions Nov 13, 2025
@theakshaypant theakshaypant marked this pull request as ready for review November 14, 2025 02:16
Copy link
Member

@aThorp96 aThorp96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good so far. Thanks! I haven't gone through it entirely yet; I had a couple questions/suggestions which might change the implementation approach

if cached, found := customParamsCache[cacheKey]; found {
customParams = cached
} else {
customParams = resolveCustomParamsForCEL(ctx, effectiveRepo, event, cs, vcx, eventEmitter, logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the Repository CR is specific to an event, we can evaluate the customParams at the top of the loop so we shouldn't need caching logic. Good thought to have the caching though, had the Repository CRs been dependent on the specific pipelinerun

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No caching anymore having removed the effective repo block :)

@theakshaypant theakshaypant force-pushed the SRVKP-9118-fix-cel-custom-params branch from 354354e to d328cc6 Compare November 18, 2025 05:12
@theakshaypant
Copy link
Contributor Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable feature that allows custom parameters from the Repository CR to be used directly in CEL expressions, enhancing the flexibility of pipeline matching. The implementation is well-structured, effectively reusing existing parameter resolution logic, and is accompanied by a comprehensive set of unit tests. I've identified a couple of areas for improvement: an inaccurate code comment and a newly introduced CEL variable that needs to be documented. Addressing these points will improve code maintainability and user experience.

}

// resolveCustomParamsForCEL resolves custom parameters from the Repository CR for use in CEL expressions.
// It returns a map of parameter names to values, excluding reserved keywords.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This comment is slightly inaccurate. The function does not exclude reserved keywords; that logic is handled in celEvaluate to prevent overwriting standard variables. To improve clarity, I suggest updating the comment to more accurately describe what the function does.

Suggested change
// It returns a map of parameter names to values, excluding reserved keywords.
// It returns a map of parameter names to values.


data := map[string]any{
"event": event.TriggerTarget.String(),
"event_type": event.EventType,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The addition of the event_type variable is a great enhancement for more specific event matching in CEL expressions. However, this new variable is not documented in docs/content/docs/guide/matchingevents.md. Please update the documentation to include event_type in the table of available CEL variables. It would be helpful to clarify that event refers to the trigger type (e.g., pull_request, push), while event_type provides the more specific provider event string (e.g., pull_request:labeled).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 event_type should be documented as well

@theakshaypant theakshaypant force-pushed the SRVKP-9118-fix-cel-custom-params branch 3 times, most recently from 317e497 to 78e3f5a Compare November 21, 2025 04:34
Copy link
Member

@aThorp96 aThorp96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a lot better, thanks! I have a few smaller requests

For example, with this Repository CR configuration:

```yaml
apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, for consistency

Suggested change
apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
apiVersion: pipelinesascode.tekton.dev/v1alpha1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 7afc027

Custom parameters from secrets are also available:

```yaml
apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
apiVersion: "pipelinesascode.tekton.dev/v1alpha1"
apiVersion: pipelinesascode.tekton.dev/v1alpha1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 7afc027

# ... pipeline spec
```

For more information on CEL expressions and event matching, see the [Advanced event matching using CEL]({{< relref "/docs/guide/matchingevents#advanced-event-matching-using-cel" >}}) documentation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to document that referencing a param with a filter will cause a CEL error if the filter is false on the event. E.g.:

kind: Repository
metadata:
  name: my-repo
spec:
  url: "https://github.com/owner/repo"
  params:
    - name: meaning_of_life
      value: "42"
      filter: pac.event_type == "pull_request"
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: my-pipeline
  annotations:
    pipelinesascode.tekton.dev/on-cel-expression: |
      meaning_of_life == "ohana"
spec:
  # ... pipeline spec

IIUC on a push event, the above pipelinerun's CEL expression won't be false, it will be an error.

Copy link
Member

@aThorp96 aThorp96 Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we should document that custom parameters here do not override standard cel variables- I have been asked several times if a custom-param could override standard variables like trigger_target and even target_branch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more details in docs in 7afc027


data := map[string]any{
"event": event.TriggerTarget.String(),
"event_type": event.EventType,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 event_type should be documented as well

}
env, err := cel.NewEnv(
cel.Lib(celPac{vcx, ctx, event}),
conflictingVar := make(map[string]bool)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be simplified a bit:

	varDecls := []cel.EnvOption{
		cel.Lib(celPac{vcx, ctx, event}),
		cel.VariableDecls(
			decls.NewVariable("event", types.StringType),
			decls.NewVariable("event_type", types.StringType),
			decls.NewVariable("headers", types.NewMapType(types.StringType, types.DynType)),
			decls.NewVariable("body", types.NewMapType(types.StringType, types.DynType)),
			decls.NewVariable("event_title", types.StringType),
			decls.NewVariable("target_branch", types.StringType),
			decls.NewVariable("source_branch", types.StringType),
			decls.NewVariable("target_url", types.StringType),
			decls.NewVariable("source_url", types.StringType),
			decls.NewVariable("files", types.NewMapType(types.StringType, types.DynType)),
		),
	}
	// Add declarations for custom params (all as strings)
	for k, v := range customParams {
		// Don't overwrite standard params
		if _, ok := data[k]; !ok {
			data[k] = v
			varDecls = append(varDecls, cel.VariableDecls(decls.NewVariable(k, types.StringType)))
		}
	}

	env, err := cel.NewEnv(varDecls...)
	if err != nil {
		return nil, err
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is good simplification, updated the logic in 7afc027

for k := range customParams {
// Don't overwrite standard params
if _, ok := conflictingVar[k]; !ok {
customParamDecls = append(customParamDecls, decls.NewVariable(k, types.StringType))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO string is preferable since the values are strictly strings in the input, but I don't feel super strongly in that conviction

Allow custom parameters from Repository CR to be accessed directly as
CEL variables without template expansion. Parameters can now be used
as: param_name == "value" on top of "{{param_name}}" == "value".

Jira: https://issues.redhat.com/browse/SRVKP-9118

Signed-off-by: Akshay Pant <akshay.akshaypant@gmail.com>
@theakshaypant theakshaypant force-pushed the SRVKP-9118-fix-cel-custom-params branch from 78e3f5a to 7afc027 Compare November 25, 2025 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation feature New feature or request repo-cr

Development

Successfully merging this pull request may close these issues.

2 participants