fix(linter): improve rules:if: expression evaluator
Four correctness fixes to the GitLab CI expression parser in
internal/cicontext/eval.go:
- Multi-line: \n and \r are now treated as whitespace in skipWS so
block-scalar or folded-scalar if: values with || / && on continuation
lines evaluate correctly instead of falling back to permissive true.
- ${VAR} curly-brace variable syntax now supported in parseValue.
- Regex flags (/pattern/i, /pattern/m, /pattern/s) are now consumed and
translated to Go (?i)/(?m)/(?s) prefixes via applyRegexFlags.
- Variable on RHS of =~ / !~: when the right operand is $VAR, the
variable's value is interpreted as a /regex/[flags] string via
extractRegexFromString; non-regex values fall back to permissive true.
Adds 16 new unit tests covering all four cases and a testdata fixture
(rules_if_expr.yml) exercising multi-line, ${VAR}, and /pattern/i in a
real pipeline with context flags.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
---
|
||||
# rules_if_expr.yml
|
||||
# Exercises the rules:if: expression evaluator for:
|
||||
# - Multi-line block-scalar expressions (|| on next line)
|
||||
# - ${VAR} curly-brace variable syntax
|
||||
# - Regex flags (/pattern/i case-insensitive)
|
||||
# - Parenthesised compound expressions
|
||||
# Expected: exits 0 (lints clean with no context).
|
||||
|
||||
stages:
|
||||
- build
|
||||
- deploy
|
||||
|
||||
variables:
|
||||
DEPLOY_ENVIRONMENTS:
|
||||
value: "staging"
|
||||
description: "Target deployment environment"
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script: make build
|
||||
rules:
|
||||
# Multi-line expression: || on next line
|
||||
- if: |
|
||||
$CI_COMMIT_BRANCH == "main" ||
|
||||
$CI_COMMIT_BRANCH == "develop"
|
||||
when: on_success
|
||||
- when: never
|
||||
|
||||
deploy-feature:
|
||||
stage: deploy
|
||||
script: make deploy
|
||||
rules:
|
||||
# ${VAR} curly-brace syntax
|
||||
- if: '${CI_COMMIT_BRANCH} != null && ${CI_COMMIT_TAG} == null'
|
||||
when: manual
|
||||
- when: never
|
||||
|
||||
release:
|
||||
stage: deploy
|
||||
script: make release
|
||||
rules:
|
||||
# Case-insensitive regex flag
|
||||
- if: '$CI_COMMIT_BRANCH =~ /^(main|master)$/i'
|
||||
when: on_success
|
||||
# Parenthesised compound with multi-line
|
||||
- if: >-
|
||||
($CI_COMMIT_TAG != null) &&
|
||||
($CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "web")
|
||||
when: on_success
|
||||
- when: never
|
||||
Reference in New Issue
Block a user