Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 18c8fc82c9 | |||
| f48bf02152 | |||
| 6d0aefca5b | |||
| de6a526560 | |||
| a0e2582cf1 | |||
| e931b9d1c9 | |||
| b21a7d60dc | |||
| d34c39927d | |||
| a303f63a5e | |||
| a962c996c1 | |||
| e5f926b55f | |||
| 8c3ce050f5 | |||
| c4ab64391d | |||
| 4cc50afb5f |
@@ -16,7 +16,15 @@ jobs:
|
||||
- name: Install tools
|
||||
run: apk add --no-cache curl git jq
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout
|
||||
env:
|
||||
TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SERVER_URL: ${{ github.server_url }}
|
||||
REPO: ${{ github.repository }}
|
||||
REF: ${{ github.ref_name }}
|
||||
run: |
|
||||
git clone --depth 1 --branch "$REF" \
|
||||
"$(echo "$SERVER_URL" | sed "s|https://|https://oauth2:${TOKEN}@|")/${REPO}.git" .
|
||||
|
||||
- name: Build Linux (amd64)
|
||||
env:
|
||||
|
||||
@@ -7,6 +7,39 @@ This project uses [Semantic Versioning](https://semver.org).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
|
||||
- **Variable reference validation (GL032)** — glint now warns when a `rules:if:` expression references a variable (`$VAR` or `${VAR}`) that is not declared anywhere in the pipeline YAML: pipeline-level `variables:`, the job's own `variables:`, or any `workflow:rules:variables:` block. Predefined GitLab CI variable namespaces (`CI_*`, `GITLAB_*`, `FF_*`, `RUNNER_*`, `TRIGGER_*`, `CHAT_*`) are exempt. Because variables can also be set in GitLab CI/CD project settings (invisible to glint), the finding is a `[WARNING]` rather than an error. Each undeclared variable is reported at most once per job to keep the output concise.
|
||||
|
||||
- **Structured rule IDs** — every finding now carries a stable `GL###` identifier (e.g. `GL003`) that appears in the output between the location and the message: `[ERROR] job "deploy" (file.yml:14) GL003: missing required field 'script'`. IDs are assigned per check function across 31 rules (GL001–GL031) and are stable across versions. The `linter.Finding` struct exposes the ID as a `Rule string` field for programmatic consumers. The README lint rules table is updated with ID columns.
|
||||
|
||||
- **`workflow:rules:variables:` now propagate to job rule evaluation** — when a `workflow:rules:` entry matches, any `variables:` it defines are injected into the evaluation context so job `rules:if:` expressions can reference them. Pipeline-level `variables:` are also available as defaults (lower priority). Variable priority order, highest first: `--var` CLI overrides → `--branch`/`--tag`/`--source` shortcuts → workflow-rule variables → pipeline-level variable defaults. This means `$DEPLOY_TARGET == "production"` in a job rule correctly evaluates when a workflow rule sets `DEPLOY_TARGET: production` for the matching branch. The `glint graph tree` command benefits from the same enrichment.
|
||||
|
||||
- **`rules:if:` expression evaluator improvements** — six correctness fixes to the GitLab CI expression parser:
|
||||
- **Multi-line expressions** — newlines (`\n`, `\r`) are now treated as whitespace between tokens, so block-scalar `if:` values (e.g. `if: | ...`) and folded YAML scalars with `||`/`&&` on a continuation line are parsed correctly instead of falling back to permissive `true`.
|
||||
- **`${VAR}` curly-brace variable syntax** — `${CI_COMMIT_BRANCH}` is now equivalent to `$CI_COMMIT_BRANCH` everywhere a value is expected.
|
||||
- **Regex flags** — `/pattern/i`, `/pattern/m`, `/pattern/s` are now honoured; the `i` flag (case-insensitive) is translated to Go's `(?i)` prefix before compiling. Unknown flags are silently ignored.
|
||||
- **Variable as regex RHS** — `$BRANCH =~ $PATTERN` where `$PATTERN` holds a `/regex/[flags]` string is now evaluated by extracting and compiling the pattern from the variable's value; if the value is empty or does not look like a regex literal the expression falls back to permissive `true`.
|
||||
- **`true` / `false` keywords** — bare `true` and `false` (without quotes) are now recognised as the string values `"true"` and `"false"`, matching GitLab CI's own behaviour. `$GATEWAY_ENABLED == true` and `$FEATURE_FLAG == false` now evaluate correctly.
|
||||
- **Integer literals** — bare integers (e.g. `$PARALLEL == 4`, `$ENABLED == 1`, `$DISABLED == 0`) are now parsed as their decimal string representations and compared accordingly.
|
||||
|
||||
- **File and line numbers on findings** — every finding now includes the source file and line where the job is defined, e.g. `[ERROR] job "deploy" (src/deploy.yml:14): …`. For jobs that come from local or fetched includes the file reflects the include source. Pipeline-level findings (workflow rules, missing stages) reference the root pipeline file.
|
||||
|
||||
- **`glint graph includes` shows jobs per file** — each node in the Mermaid include dependency graph now shows the jobs defined directly in that file. Jobs are rendered as rounded nodes (`(name)`) in a distinct light-purple style, connected with dashed arrows (`-.->`) to distinguish ownership from the include hierarchy (solid `-->` arrows). The root pipeline file always shows its direct jobs; local and fetched project/component nodes show theirs when the file can be read.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **`include: remote:` URL includes are now fetched and merged** — glint fetches plain HTTPS URLs in `include: remote:` entries (no authentication), parses the resulting YAML, merges its jobs into the pipeline, and recursively resolves any sub-includes the remote file itself declares. Unreachable or unparseable URLs emit a `[WARNING]` and lint continues on the rest of the pipeline. The `glint graph includes` command now expands remote nodes with their jobs and sub-include tree, matching the behaviour of local and project includes.
|
||||
|
||||
- **`needs: optional: true` downgraded to warning** — a `needs:` entry that carries `optional: true` and references a job not present in the pipeline now emits `[WARNING]` instead of `[ERROR]`. GitLab CI silently skips such dependencies at runtime (the job is absent when its include was not triggered), so the finding was a false positive. Non-optional missing needs remain errors. Optional missing deps are also excluded from the cycle-detection graph.
|
||||
|
||||
- **`extends:` jobs with missing script downgraded to warning** — a job that declares `extends:` but has no `script` after resolution now emits `[WARNING]` instead of `[ERROR]`. The script may legitimately come from a base job in a remote include that could not be fetched at lint time (e.g. no token configured).
|
||||
|
||||
- **Variable map form now parses correctly** — `variables:` entries that use the extended `{value, description, options}` form (GitLab CI 13.7+) no longer cause `yaml: cannot unmarshal !!map into string`. Both `Pipeline.Variables` and per-job `Variables` now accept either plain strings or map-form declarations.
|
||||
- **`default.image` map form now parses correctly** — `default: image: {name: ..., pull_policy: ...}` used to cause `yaml: cannot unmarshal !!map into string`; `DefaultConfig.Image` is now typed as `any` to match `Job.Image`.
|
||||
- **`default.before_script` / `default.after_script` now accept both list and scalar forms** — previously `DefaultConfig.BeforeScript` and `DefaultConfig.AfterScript` were `[]string`, causing a parse error when the field was written as a block scalar string. They are now typed as `any` to match the corresponding `Job` fields.
|
||||
- **`rules.changes` / `rules.exists` map form now parses correctly** — extended `changes: {paths: [...], compare_to: "..."}` syntax (GitLab CI 15.3+) used to cause `yaml: cannot unmarshal !!map into []string`.
|
||||
|
||||
## [0.2.0] - 2026-06-11
|
||||
|
||||
### Added
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
# glint
|
||||
<p align="center">
|
||||
<img src="assets/glint-logo.png" alt="glint logo" width="220" />
|
||||
</p>
|
||||
|
||||
[](LICENSE)
|
||||
[](CHANGELOG.md)
|
||||
<h1 align="center">glint</h1>
|
||||
|
||||
<p align="center">
|
||||
<a href="LICENSE"><img src="https://img.shields.io/badge/license-Apache%202.0-blue.svg" alt="License"></a>
|
||||
<a href="CHANGELOG.md"><img src="https://img.shields.io/badge/release-v0.2.0-blue.svg" alt="Release"></a>
|
||||
</p>
|
||||
|
||||
> **Disclaimer:** This tool was built through iterative AI-assisted development with [Claude](https://claude.ai). It is experimental, incomplete, and not intended for production use. Coverage of GitLab CI keywords is best-effort and may lag behind GitLab's evolving spec. Use it at your own discretion — no correctness guarantees are made. Contributions and bug reports are welcome.
|
||||
|
||||
@@ -19,6 +25,7 @@ A local tool to validate and lint `.gitlab-ci.yml` pipelines without needing a G
|
||||
- **CI/CD catalog components** — resolves `include: component:` references from the GitLab CI/CD Catalog; public components work without a token
|
||||
- **Deprecation warnings** — flags `only`/`except` usage in favour of `rules`
|
||||
- **Local include resolution** — `include: local:` entries are read from disk and recursively merged before linting, so multi-file pipelines are fully validated
|
||||
- **Extended variable declarations** — `variables:` entries may use the `{value, description, options}` map form (GitLab CI 13.7+); `default.image` accepts both string and map form; `rules.changes`/`rules.exists` accept both list and `{paths, compare_to}` map form
|
||||
- **Graph output** — `glint graph` prints a job tree (stages → jobs) to the terminal; `glint graph includes` emits a Mermaid include dependency diagram; `glint graph pipeline` renders a GitLab CI-style PNG/SVG
|
||||
- **Context simulation** — pass `--branch`, `--tag`, or `--source` to `glint check` or `glint graph` to see which jobs would be active, manual, or skipped for a specific pipeline event; evaluates `rules:if:` expressions and `only`/`except` filters
|
||||
|
||||
@@ -238,63 +245,64 @@ OK: .gitlab-ci.yml — no issues found (5 jobs, 3 stages)
|
||||
|
||||
## Lint rules
|
||||
|
||||
Every finding includes a stable rule ID (e.g. `GL003`) that can be used to filter output or reference a specific check in documentation.
|
||||
|
||||
### Pipeline-level
|
||||
|
||||
| Severity | Rule |
|
||||
|----------|------|
|
||||
| ERROR | `workflow.rules[*].when` is not `always` or `never` |
|
||||
| WARNING | No `stages` defined (GitLab falls back to default stages) |
|
||||
| ID | Severity | Rule |
|
||||
|----|----------|------|
|
||||
| GL002 | ERROR | `workflow.rules[*].when` is not `always` or `never` |
|
||||
| GL001 | WARNING | No `stages` defined (GitLab falls back to default stages) |
|
||||
|
||||
### Job-level — structure
|
||||
|
||||
| Severity | Rule |
|
||||
|----------|------|
|
||||
| ERROR | Job is missing required `script` (or `run`) — non-trigger, non-template jobs |
|
||||
| ERROR | Job references a `stage` not declared in `stages` |
|
||||
| ERROR | `only` and `rules` used together on the same job |
|
||||
| ERROR | `except` and `rules` used together on the same job |
|
||||
| WARNING | `only`/`except` used (deprecated, prefer `rules`) |
|
||||
| ID | Severity | Rule |
|
||||
|----|----------|------|
|
||||
| GL003 | ERROR | Job is missing required `script` (or `run`) — non-trigger, non-template jobs |
|
||||
| GL004 | ERROR | Job references a `stage` not declared in `stages` |
|
||||
| GL005 | ERROR | `only` and `rules` used together on the same job |
|
||||
| GL006 | ERROR | `except` and `rules` used together on the same job |
|
||||
| GL007 | WARNING | `only`/`except` used (deprecated, prefer `rules`) |
|
||||
|
||||
### Job-level — keyword constraints
|
||||
|
||||
| Severity | Rule |
|
||||
|----------|------|
|
||||
| ERROR | `when` is not one of `on_success`, `on_failure`, `always`, `manual`, `delayed`, `never` |
|
||||
| ERROR | `when: delayed` without `start_in` |
|
||||
| ERROR | `start_in` set but `when` is not `delayed` |
|
||||
| ERROR | `parallel` integer not in range 2–200 |
|
||||
| ERROR | `parallel` map form missing `matrix` key |
|
||||
| ERROR | `retry` integer not in range 0–2 |
|
||||
| ERROR | `retry.max` not in range 0–2 |
|
||||
| ERROR | `retry.when` contains an invalid failure type |
|
||||
| ERROR | `allow_failure` is not a boolean or a map with `exit_codes` |
|
||||
| ERROR | `interruptible` is not a boolean |
|
||||
| ERROR | `trigger` job also has `script` |
|
||||
| ERROR | `trigger` map missing `project` or `include` |
|
||||
| ERROR | `coverage` is not a regex pattern wrapped in `/` |
|
||||
| ERROR | `release` missing required `tag_name` |
|
||||
| ERROR | `environment.url` set without `environment.name` |
|
||||
| ERROR | `environment.action` is not one of `start`, `stop`, `prepare`, `verify`, `access` |
|
||||
| ERROR | `artifacts.when` is not `on_success`, `on_failure`, or `always` |
|
||||
| ERROR | `artifacts.expose_as` set without `artifacts.paths` |
|
||||
| ERROR | `cache.when` is not `on_success`, `on_failure`, or `always` |
|
||||
| ERROR | `cache.policy` is not `pull`, `push`, or `pull-push` |
|
||||
| ERROR | `rules[*].when` is not one of the valid `when` values |
|
||||
| ERROR | `image` map form missing `name` key |
|
||||
| ERROR | `inherit.default` / `inherit.variables` is not a boolean or list |
|
||||
| WARNING | `pages` job `artifacts.paths` does not include `public` |
|
||||
| ID | Severity | Rule |
|
||||
|----|----------|------|
|
||||
| GL008 | ERROR | `when` is not one of `on_success`, `on_failure`, `always`, `manual`, `delayed`, `never` |
|
||||
| GL009 | ERROR | `when: delayed` without `start_in` |
|
||||
| GL010 | ERROR | `start_in` set but `when` is not `delayed` |
|
||||
| GL011 | ERROR | `parallel` integer not in range 2–200, or map form missing `matrix` key |
|
||||
| GL012 | ERROR | `retry` integer not in range 0–2, or `retry.max` out of range |
|
||||
| GL013 | ERROR | `retry.when` contains an unrecognised failure type |
|
||||
| GL014 | ERROR | `allow_failure` is not a boolean or a map with `exit_codes` |
|
||||
| GL015 | ERROR | `interruptible` is not a boolean |
|
||||
| GL016 | ERROR | `trigger` job also has `script` |
|
||||
| GL017 | ERROR | `trigger` map missing `project` or `include` |
|
||||
| GL018 | ERROR | `coverage` is not a regex pattern wrapped in `/` |
|
||||
| GL019 | ERROR | `release` missing required `tag_name`, or is not a map |
|
||||
| GL020 | ERROR | `environment.url` set without `environment.name`, or invalid `environment.action` |
|
||||
| GL021 | ERROR | `artifacts.when` invalid, or `artifacts.expose_as` set without `artifacts.paths` |
|
||||
| GL022 | WARNING | `pages` job `artifacts.paths` does not include `public` |
|
||||
| GL023 | ERROR | `cache.when` or `cache.policy` has an invalid value |
|
||||
| GL024 | ERROR | `rules[*].when` is not one of the valid `when` values |
|
||||
| GL025 | ERROR | `image` map form missing `name` key |
|
||||
| GL026 | ERROR | `inherit.default` / `inherit.variables` is not a boolean or list |
|
||||
|
||||
### Cross-job graph
|
||||
|
||||
| Severity | Rule |
|
||||
|----------|------|
|
||||
| ERROR | `needs:` references a job that does not exist |
|
||||
| ERROR | `needs:` references a job in a later stage |
|
||||
| ERROR | Circular dependency detected in `needs:` graph |
|
||||
| ERROR | `dependencies:` references a job that does not exist |
|
||||
| ERROR | `dependencies:` references a job in the same or a later stage |
|
||||
| WARNING | `extends:` references an unknown base job (resolver warning; extends chain skipped for that job) |
|
||||
| ERROR | Cycle detected in `extends:` graph |
|
||||
| ID | Severity | Rule |
|
||||
|----|----------|------|
|
||||
| GL027 | ERROR/WARNING | `needs:` references a job that does not exist (WARNING when `optional: true`) |
|
||||
| GL028 | ERROR | `needs:` references a job in a later stage |
|
||||
| GL029 | ERROR | Circular dependency detected in `needs:` graph |
|
||||
| GL030 | ERROR | `dependencies:` references a job that does not exist |
|
||||
| GL031 | ERROR | `dependencies:` references a job in the same or a later stage |
|
||||
|
||||
### Expression validation
|
||||
|
||||
| ID | Severity | Rule |
|
||||
|----|----------|------|
|
||||
| GL032 | WARNING | `rules:if:` references `$VAR` not declared in `variables:` (pipeline, job, or `workflow:rules:variables:`) — may be a false positive for variables set in GitLab CI/CD project settings |
|
||||
|
||||
### Hidden jobs (templates)
|
||||
|
||||
|
||||
+26
-3
@@ -4,7 +4,7 @@ This document tracks planned improvements to `glint`. Items are grouped by theme
|
||||
|
||||
---
|
||||
|
||||
## Context-aware validation — ✓ single-context shipped in v0.2.0
|
||||
## Context-aware validation — ✓ single-context shipped in v0.2.0; expression evaluator hardened post-v0.2.0
|
||||
|
||||
Single-context simulation is fully implemented. Pass `--branch`, `--tag`, `--source`, or `--var` to either `glint check` or `glint graph`; jobs are evaluated and shown as active / manual / skipped.
|
||||
|
||||
@@ -16,6 +16,16 @@ glint check --source merge_request_event --var CI_MERGE_REQUEST_TARGET_BRANCH_NA
|
||||
glint graph tree --branch main .gitlab-ci.yml # tree annotated with [skipped] / [manual]
|
||||
```
|
||||
|
||||
**Shipped post-v0.2.0 (unreleased)**
|
||||
|
||||
- ✓ **`workflow:rules:variables:` propagation** — variables defined on the matching `workflow:rules:` entry are injected into the evaluation context before job `rules:if:` expressions are evaluated. Pipeline-level `variables:` defaults are also available. Priority chain (highest wins): `--var` > shortcuts > workflow-rule vars > pipeline defaults.
|
||||
- ✓ **Expression evaluator: multi-line expressions** — newlines in block-scalar and folded YAML `if:` values are now treated as whitespace; `||` / `&&` on a continuation line evaluate correctly.
|
||||
- ✓ **Expression evaluator: `${VAR}` curly-brace syntax** — `${CI_COMMIT_BRANCH}` is equivalent to `$CI_COMMIT_BRANCH` everywhere.
|
||||
- ✓ **Expression evaluator: regex flags** — `/pattern/i`, `/pattern/m`, `/pattern/s` are now supported; `i` maps to `(?i)` in Go's regexp.
|
||||
- ✓ **Expression evaluator: variable as regex RHS** — `$BRANCH =~ $PATTERN` where `$PATTERN` holds a `/regex/` string is evaluated correctly.
|
||||
- ✓ **Expression evaluator: bare `true` / `false` keywords** — treated as the strings `"true"` / `"false"` matching GitLab CI's own behaviour; `$GATEWAY_ENABLED == true` now evaluates correctly.
|
||||
- ✓ **Expression evaluator: integer literals** — `$COUNT == 4`, `$ENABLED == 1`, `$DISABLED == 0` compare as decimal strings.
|
||||
|
||||
**Remaining work**
|
||||
|
||||
- **Multi-context simulation** — run multiple contexts in one invocation and print a comparison table:
|
||||
@@ -23,7 +33,7 @@ glint graph tree --branch main .gitlab-ci.yml # tree annotated with [skipped]
|
||||
glint check --context branch=main --context branch=develop --context tag=v1.0.0 .gitlab-ci.yml
|
||||
```
|
||||
- **Context-scoped linting** — skip `needs:`/`dependencies:` cross-checks for jobs that are statically unreachable in the given context
|
||||
- **`rules:changes:` evaluation** — path glob evaluation against the local git tree (expression evaluator priority 5)
|
||||
- **`rules:changes:` evaluation** — path glob evaluation against the local git tree
|
||||
|
||||
---
|
||||
|
||||
@@ -47,7 +57,7 @@ The current rule set covers the most common sources of broken pipelines. These a
|
||||
## Include resolution
|
||||
|
||||
- ~~**`include: local:`** full resolution~~ — ✓ shipped in v0.2.0; local files are read from disk, recursively resolved, and merged before linting
|
||||
- **`include: remote:`** (URL) — fetch and merge plain HTTP/HTTPS URLs (no auth required)
|
||||
- ~~**`include: remote:`** (URL)~~ — ✓ shipped post-v0.2.0; plain HTTPS URLs are fetched (unauthenticated), parsed, and merged; sub-includes are resolved recursively; unreachable URLs emit `[WARNING]` and linting continues
|
||||
- **Recursive include depth limit** — guard against include cycles across files
|
||||
- **Offline mode / cache** — persist fetched remote templates to a local cache directory; `--offline` flag to skip network calls and use only cached copies
|
||||
- **`include: inputs:`** — substitute CI component input values into fetched templates before merging, so component-scoped jobs get their correct `stage:` and keyword values
|
||||
@@ -70,6 +80,7 @@ Right now the only output is plain-text findings. Structured output enables inte
|
||||
The SVG renderer and terminal tree cover the basic layout. These would bring it closer to GitLab's full interactive view.
|
||||
|
||||
- ~~**Terminal job tree**~~ — ✓ shipped in v0.2.0 as `glint graph tree`; stages as branches, jobs as leaves, context-aware annotations
|
||||
- ~~**`glint graph includes` shows jobs per file**~~ — ✓ shipped post-v0.2.0; each include node shows the jobs it defines as dashed-arrow rounded nodes in a distinct style
|
||||
- **Multi-job connector accuracy** — draw one connector per job pair rather than one per stage pair in classic mode, so pipelines with uneven columns look correct
|
||||
- **Job tooltip / detail panel** — embed a hidden `<title>` and `<desc>` per chip so SVG viewers show `stage`, `when`, `image`, and `needs` on hover
|
||||
- **`when: on_failure` visual distinction** — dashed border or distinct icon for failure-path jobs
|
||||
@@ -79,6 +90,18 @@ The SVG renderer and terminal tree cover the basic layout. These would bring it
|
||||
|
||||
---
|
||||
|
||||
## Findings quality — ✓ file and line numbers shipped post-v0.2.0
|
||||
|
||||
~~**File and line numbers on findings**~~ — ✓ shipped post-v0.2.0; every `[ERROR]` / `[WARNING]` now includes the source file and exact line of the job key (e.g. `job "deploy" (src/deploy.yml:14): …`). Works across local includes, remote project templates, and fetched component templates.
|
||||
|
||||
**Remaining improvements**
|
||||
|
||||
- ~~**`needs: optional: true` false-positive errors**~~ — ✓ shipped post-v0.2.0; optional missing needs are downgraded to `[WARNING]`
|
||||
- ~~**`extends:` jobs with missing script false errors**~~ — ✓ shipped post-v0.2.0; jobs using `extends:` that have no `script` after resolution emit `[WARNING]` (the script may come from an unfetchable remote base)
|
||||
- **`rules:if:` static reachability** — report when a job's entire `rules:` block can never evaluate to `when: on_success` given the declared pipeline variables (pure static, no context required)
|
||||
|
||||
---
|
||||
|
||||
## CI / editor integration
|
||||
|
||||
- **GitLab CI template** — a `.gitlab-ci.yml` snippet that runs `glint` as a pipeline-validation job before the real pipeline executes; publishable to the GitLab CI/CD Catalog
|
||||
|
||||
@@ -41,10 +41,14 @@ tasks:
|
||||
ignore_error: true
|
||||
- cmd: ./{{.BINARY}} check testdata/keywords_invalid.yml
|
||||
ignore_error: true
|
||||
- cmd: ./{{.BINARY}} check testdata/includes_remote.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check testdata/includes_project.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check testdata/includes_component.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check testdata/script_multiline.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check testdata/context_rules.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check --branch main testdata/context_rules.yml
|
||||
@@ -55,6 +59,22 @@ tasks:
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check --tag v1.0.0 testdata/context_rules.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check testdata/rules_if_expr.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check --branch main testdata/rules_if_expr.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check --branch feat/x testdata/rules_if_expr.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check testdata/workflow_vars.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check --branch main testdata/workflow_vars.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check --branch develop testdata/workflow_vars.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check --branch feat/x testdata/workflow_vars.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check testdata/variable_refs.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check samba-testdata/.gitlab-ci.yml
|
||||
ignore_error: false
|
||||
- cmd: ./{{.BINARY}} check samba-testdata/.gitlab-ci-coverage.yml
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 106 KiB |
@@ -147,6 +147,7 @@ Examples:
|
||||
|
||||
ctx := cicontext.New(*branch, *tag, *source, vars)
|
||||
if !ctx.IsEmpty() {
|
||||
enrichContext(ctx, p)
|
||||
printContext(p, ctx)
|
||||
}
|
||||
|
||||
@@ -272,6 +273,9 @@ Examples:
|
||||
resolver.Resolve(p) //nolint:errcheck
|
||||
|
||||
ctx := cicontext.New(*branch, *tag, *source, vars)
|
||||
if !ctx.IsEmpty() {
|
||||
enrichContext(ctx, p)
|
||||
}
|
||||
|
||||
switch mode {
|
||||
case "default":
|
||||
@@ -300,6 +304,24 @@ Examples:
|
||||
}
|
||||
}
|
||||
|
||||
// enrichContext injects pipeline-level variable defaults and then
|
||||
// workflow-rule-generated variables into ctx before job evaluation.
|
||||
// Injection respects pinned variables (--branch/--tag/--source/--var always win).
|
||||
func enrichContext(ctx *cicontext.Context, p *model.Pipeline) {
|
||||
// Pipeline variables: injected as defaults (lowest priority).
|
||||
for k, v := range cicontext.ExtractStringVars(p.Variables) {
|
||||
ctx.Inject(k, v)
|
||||
}
|
||||
// Workflow rules: evaluate to find which rule matches, then inject its variables.
|
||||
runs, ruleVars := cicontext.EvalWorkflow(p, ctx)
|
||||
if !runs {
|
||||
fmt.Fprintln(os.Stderr, "[WARNING] workflow:rules: pipeline would not start for this context")
|
||||
}
|
||||
for k, v := range ruleVars {
|
||||
ctx.Inject(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func printContext(p *model.Pipeline, ctx *cicontext.Context) {
|
||||
fmt.Printf("Context: %s\n\n", ctx.Summary())
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ import "strings"
|
||||
// pipeline evaluation (rules:if:, only:, except:, workflow:rules:).
|
||||
// Variables are keyed by their name without the leading $.
|
||||
type Context struct {
|
||||
Vars map[string]string
|
||||
Vars map[string]string
|
||||
pinned map[string]bool // vars set via --var or shortcuts; never overwritten by Inject
|
||||
}
|
||||
|
||||
// New builds a Context from high-level shortcut values and optional KEY=VALUE
|
||||
@@ -17,46 +18,55 @@ type Context struct {
|
||||
// preserving the existing linting behaviour when no context flags are given.
|
||||
//
|
||||
// Override priority (highest wins): extraVars > branch/tag/source shortcuts.
|
||||
// Both shortcut-derived and extraVar variables are pinned — they will not be
|
||||
// overwritten by Inject (used for pipeline-level and workflow-rule variables).
|
||||
func New(branch, tag, source string, extraVars []string) *Context {
|
||||
if branch == "" && tag == "" && source == "" && len(extraVars) == 0 {
|
||||
return &Context{}
|
||||
}
|
||||
|
||||
vars := make(map[string]string)
|
||||
pinned := make(map[string]bool)
|
||||
|
||||
pin := func(k, v string) {
|
||||
vars[k] = v
|
||||
pinned[k] = true
|
||||
}
|
||||
|
||||
if branch != "" {
|
||||
vars["CI_COMMIT_BRANCH"] = branch
|
||||
vars["CI_COMMIT_REF_NAME"] = branch
|
||||
vars["CI_COMMIT_REF_SLUG"] = slugify(branch)
|
||||
pin("CI_COMMIT_BRANCH", branch)
|
||||
pin("CI_COMMIT_REF_NAME", branch)
|
||||
pin("CI_COMMIT_REF_SLUG", slugify(branch))
|
||||
if source == "" {
|
||||
source = "push"
|
||||
}
|
||||
}
|
||||
if tag != "" {
|
||||
vars["CI_COMMIT_TAG"] = tag
|
||||
vars["CI_COMMIT_REF_NAME"] = tag
|
||||
vars["CI_COMMIT_REF_SLUG"] = slugify(tag)
|
||||
delete(vars, "CI_COMMIT_BRANCH") // tag pushes have no branch variable
|
||||
pin("CI_COMMIT_TAG", tag)
|
||||
pin("CI_COMMIT_REF_NAME", tag)
|
||||
pin("CI_COMMIT_REF_SLUG", slugify(tag))
|
||||
delete(vars, "CI_COMMIT_BRANCH")
|
||||
delete(pinned, "CI_COMMIT_BRANCH")
|
||||
if source == "" {
|
||||
source = "push"
|
||||
}
|
||||
}
|
||||
if source != "" {
|
||||
vars["CI_PIPELINE_SOURCE"] = source
|
||||
pin("CI_PIPELINE_SOURCE", source)
|
||||
}
|
||||
if _, ok := vars["CI_DEFAULT_BRANCH"]; !ok {
|
||||
vars["CI_DEFAULT_BRANCH"] = "main"
|
||||
}
|
||||
|
||||
// KEY=VALUE overrides win over shortcuts.
|
||||
// KEY=VALUE overrides win over shortcuts and everything else.
|
||||
for _, kv := range extraVars {
|
||||
k, v, ok := strings.Cut(kv, "=")
|
||||
if ok {
|
||||
vars[k] = v
|
||||
pin(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
return &Context{Vars: vars}
|
||||
return &Context{Vars: vars, pinned: pinned}
|
||||
}
|
||||
|
||||
// IsEmpty reports whether no variables have been set (no context flags given).
|
||||
@@ -73,6 +83,21 @@ func (c *Context) Get(key string) string {
|
||||
return c.Vars[key]
|
||||
}
|
||||
|
||||
// Inject sets key=value only if key is not already pinned (i.e. not set via
|
||||
// --branch / --tag / --source / --var). Used to inject pipeline-level variable
|
||||
// defaults and workflow-rule variables without overriding explicit user input.
|
||||
// Calling Inject in order from lowest-priority to highest-priority source
|
||||
// ensures later calls win over earlier ones.
|
||||
func (c *Context) Inject(key, value string) {
|
||||
if c.pinned[key] {
|
||||
return
|
||||
}
|
||||
if c.Vars == nil {
|
||||
c.Vars = make(map[string]string)
|
||||
}
|
||||
c.Vars[key] = value
|
||||
}
|
||||
|
||||
// Summary returns a short human-readable description of the context for CLI output.
|
||||
func (c *Context) Summary() string {
|
||||
if c.IsEmpty() {
|
||||
@@ -90,6 +115,28 @@ func (c *Context) Summary() string {
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
// ExtractStringVars converts a map[string]any variable block (as used by
|
||||
// Pipeline.Variables and Rule.Variables) to a flat map[string]string.
|
||||
// Plain string values are used directly. Extended {value: "..."} map form
|
||||
// uses the "value" key. Other forms are skipped.
|
||||
func ExtractStringVars(m map[string]any) map[string]string {
|
||||
if len(m) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(m))
|
||||
for k, v := range m {
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
out[k] = val
|
||||
case map[string]any:
|
||||
if s, ok := val["value"].(string); ok {
|
||||
out[k] = s
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// slugify converts a ref name to its GitLab slug form:
|
||||
// lowercased, non-alphanumeric characters replaced with '-', leading/trailing '-' removed.
|
||||
func slugify(s string) string {
|
||||
|
||||
+147
-19
@@ -9,12 +9,15 @@ import (
|
||||
// variable resolver.
|
||||
//
|
||||
// Supported:
|
||||
// - Variable references: $VAR_NAME
|
||||
// - Variable references: $VAR_NAME or ${VAR_NAME}
|
||||
// - String literals: "value" or 'value'
|
||||
// - Null keyword: null
|
||||
// - Comparison: == != =~ !~
|
||||
// - Boolean: && || !
|
||||
// - Grouping: ( )
|
||||
// - Regex flags: /pattern/i (case-insensitive), /pattern/m, /pattern/s
|
||||
// - Multi-line: newlines between tokens are treated as whitespace
|
||||
// - Variable regex RHS: $VAR =~ $PATTERN when $PATTERN holds a /regex/ string
|
||||
//
|
||||
// Regex patterns use Go's regexp syntax, which covers the common RE2 subset
|
||||
// used by GitLab CI. Unsupported or unparseable expressions fall back to true
|
||||
@@ -56,8 +59,13 @@ func (p *exprParser) consume(tok string) bool {
|
||||
}
|
||||
|
||||
func (p *exprParser) skipWS() {
|
||||
for p.pos < len(p.s) && (p.s[p.pos] == ' ' || p.s[p.pos] == '\t') {
|
||||
p.pos++
|
||||
for p.pos < len(p.s) {
|
||||
b := p.s[p.pos]
|
||||
if b == ' ' || b == '\t' || b == '\n' || b == '\r' {
|
||||
p.pos++
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,11 +75,11 @@ func (p *exprParser) skipWS() {
|
||||
// and_expr → not_expr ( '&&' not_expr )*
|
||||
// not_expr → '!' not_expr | primary
|
||||
// primary → '(' or_expr ')' | comparison
|
||||
// comparison → value ( op value | regex_op regex )? | value
|
||||
// value → '$' ident | '"' … '"' | "'" … "'" | 'null'
|
||||
// comparison → value ( op value | regex_op regex_rhs )?
|
||||
// value → '$' '{' ident '}' | '$' ident | '"' … '"' | "'" … "'" | 'null'
|
||||
// op → '==' | '!='
|
||||
// regex_op → '=~' | '!~'
|
||||
// regex → '/' … '/'
|
||||
// regex_rhs → '/' … '/' flags? | '$' ident (where ident value is '/…/flags')
|
||||
|
||||
func (p *exprParser) parseOr() (bool, bool) {
|
||||
left, ok := p.parseAnd()
|
||||
@@ -165,8 +173,11 @@ func (p *exprParser) parseComparison() (bool, bool) {
|
||||
|
||||
case p.consume("=~"):
|
||||
p.skipWS()
|
||||
pat, ok := p.parseRegexLiteral()
|
||||
if !ok {
|
||||
pat, patOk, permissive := p.parseRegexRHS()
|
||||
if permissive {
|
||||
return true, true
|
||||
}
|
||||
if !patOk {
|
||||
return false, false
|
||||
}
|
||||
re, err := regexp.Compile(pat)
|
||||
@@ -177,8 +188,11 @@ func (p *exprParser) parseComparison() (bool, bool) {
|
||||
|
||||
case p.consume("!~"):
|
||||
p.skipWS()
|
||||
pat, ok := p.parseRegexLiteral()
|
||||
if !ok {
|
||||
pat, patOk, permissive := p.parseRegexRHS()
|
||||
if permissive {
|
||||
return true, true
|
||||
}
|
||||
if !patOk {
|
||||
return false, false
|
||||
}
|
||||
re, err := regexp.Compile(pat)
|
||||
@@ -192,13 +206,49 @@ func (p *exprParser) parseComparison() (bool, bool) {
|
||||
return leftStr != "", true
|
||||
}
|
||||
|
||||
// parseValue reads $VAR, "string", 'string', or null.
|
||||
// null and undefined variables both produce an empty string.
|
||||
// parseRegexRHS parses the right-hand side of =~ / !~ operators.
|
||||
// Returns (pattern, ok, permissive):
|
||||
// - /regex/flags literal → (pattern, true, false)
|
||||
// - $VAR whose value is /regex/flags → (pattern, true, false)
|
||||
// - $VAR whose value is empty or not a /regex/ → ("", false, true) — caller uses permissive true
|
||||
// - parse error → ("", false, false)
|
||||
func (p *exprParser) parseRegexRHS() (pat string, ok bool, permissive bool) {
|
||||
if p.peek() == '/' {
|
||||
pat, ok = p.parseRegexLiteral()
|
||||
return pat, ok, false
|
||||
}
|
||||
if p.peek() == '$' {
|
||||
varVal, varOk := p.parseValue()
|
||||
if !varOk {
|
||||
return "", false, false
|
||||
}
|
||||
pat, ok = extractRegexFromString(varVal)
|
||||
if !ok {
|
||||
return "", false, true // variable is not a /regex/ value → permissive
|
||||
}
|
||||
return pat, true, false
|
||||
}
|
||||
return "", false, false
|
||||
}
|
||||
|
||||
// parseValue reads $VAR, ${VAR}, "string", 'string', null, true, false, or an
|
||||
// integer literal. null and undefined variables both produce an empty string.
|
||||
// true/false and integers produce their string representations (GitLab CI
|
||||
// compares all values as strings).
|
||||
func (p *exprParser) parseValue() (string, bool) {
|
||||
p.skipWS()
|
||||
|
||||
if p.peek() == '$' {
|
||||
p.pos++ // consume '$'
|
||||
if p.peek() == '{' {
|
||||
p.pos++ // consume '{'
|
||||
name := p.parseIdent()
|
||||
if name == "" || p.peek() != '}' {
|
||||
return "", false
|
||||
}
|
||||
p.pos++ // consume '}'
|
||||
return p.vars(name), true
|
||||
}
|
||||
name := p.parseIdent()
|
||||
if name == "" {
|
||||
return "", false
|
||||
@@ -206,12 +256,18 @@ func (p *exprParser) parseValue() (string, bool) {
|
||||
return p.vars(name), true
|
||||
}
|
||||
|
||||
// null keyword — must not be a prefix of a longer identifier.
|
||||
if p.startsWith("null") {
|
||||
end := p.pos + 4
|
||||
if end >= len(p.s) || !isIdentByte(p.s[end]) {
|
||||
p.pos += 4
|
||||
return "", true // null → empty string
|
||||
// Keywords and string literals must not be prefixes of longer identifiers.
|
||||
for _, kw := range []struct{ tok, val string }{
|
||||
{"null", ""},
|
||||
{"true", "true"},
|
||||
{"false", "false"},
|
||||
} {
|
||||
if p.startsWith(kw.tok) {
|
||||
end := p.pos + len(kw.tok)
|
||||
if end >= len(p.s) || !isIdentByte(p.s[end]) {
|
||||
p.pos += len(kw.tok)
|
||||
return kw.val, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,6 +275,15 @@ func (p *exprParser) parseValue() (string, bool) {
|
||||
return p.parseStringLiteral()
|
||||
}
|
||||
|
||||
// Integer literal — returned as its decimal string for string comparison.
|
||||
if p.peek() >= '0' && p.peek() <= '9' {
|
||||
start := p.pos
|
||||
for p.pos < len(p.s) && p.s[p.pos] >= '0' && p.s[p.pos] <= '9' {
|
||||
p.pos++
|
||||
}
|
||||
return p.s[start:p.pos], true
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -261,7 +326,8 @@ func (p *exprParser) parseRegexLiteral() (string, bool) {
|
||||
b := p.s[p.pos]
|
||||
if b == '/' {
|
||||
p.pos++ // consume closing '/'
|
||||
return sb.String(), true
|
||||
flags := p.parseRegexFlags()
|
||||
return applyRegexFlags(flags, sb.String()), true
|
||||
}
|
||||
if b == '\\' && p.pos+1 < len(p.s) {
|
||||
p.pos++
|
||||
@@ -275,6 +341,68 @@ func (p *exprParser) parseRegexLiteral() (string, bool) {
|
||||
return "", false // unterminated regex
|
||||
}
|
||||
|
||||
// parseRegexFlags reads zero or more regex flag letters (i, m, s) after the
|
||||
// closing '/'. Unknown letters are consumed but ignored.
|
||||
func (p *exprParser) parseRegexFlags() string {
|
||||
start := p.pos
|
||||
for p.pos < len(p.s) && isIdentByte(p.s[p.pos]) {
|
||||
p.pos++
|
||||
}
|
||||
return p.s[start:p.pos]
|
||||
}
|
||||
|
||||
// applyRegexFlags prepends Go regexp flag groups to pattern (e.g. (?i) for 'i').
|
||||
// Unknown flags are silently ignored.
|
||||
func applyRegexFlags(flags, pattern string) string {
|
||||
if flags == "" {
|
||||
return pattern
|
||||
}
|
||||
var prefix strings.Builder
|
||||
for _, f := range flags {
|
||||
switch f {
|
||||
case 'i':
|
||||
prefix.WriteString("(?i)")
|
||||
case 'm':
|
||||
prefix.WriteString("(?m)")
|
||||
case 's':
|
||||
prefix.WriteString("(?s)")
|
||||
}
|
||||
}
|
||||
return prefix.String() + pattern
|
||||
}
|
||||
|
||||
// extractRegexFromString parses a /pattern/flags string (typically from a CI
|
||||
// variable) and returns a Go regexp pattern with flags applied.
|
||||
func extractRegexFromString(s string) (string, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if len(s) == 0 || s[0] != '/' {
|
||||
return "", false
|
||||
}
|
||||
var sb strings.Builder
|
||||
i := 1
|
||||
for i < len(s) {
|
||||
b := s[i]
|
||||
if b == '/' {
|
||||
i++ // past closing '/'
|
||||
var flags strings.Builder
|
||||
for i < len(s) && isIdentByte(s[i]) {
|
||||
flags.WriteByte(s[i])
|
||||
i++
|
||||
}
|
||||
return applyRegexFlags(flags.String(), sb.String()), true
|
||||
}
|
||||
if b == '\\' && i+1 < len(s) {
|
||||
i++
|
||||
sb.WriteByte('\\')
|
||||
sb.WriteByte(s[i])
|
||||
} else {
|
||||
sb.WriteByte(b)
|
||||
}
|
||||
i++
|
||||
}
|
||||
return "", false // unterminated
|
||||
}
|
||||
|
||||
func isIdentByte(b byte) bool {
|
||||
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') || b == '_'
|
||||
}
|
||||
|
||||
@@ -5,10 +5,14 @@ import "testing"
|
||||
func TestEvalIf(t *testing.T) {
|
||||
vars := func(key string) string {
|
||||
m := map[string]string{
|
||||
"CI_COMMIT_BRANCH": "develop",
|
||||
"CI_COMMIT_TAG": "",
|
||||
"CI_COMMIT_BRANCH": "develop",
|
||||
"CI_COMMIT_TAG": "",
|
||||
"CI_PIPELINE_SOURCE": "push",
|
||||
"DEPLOY_ENV": "staging",
|
||||
"DEPLOY_ENV": "staging",
|
||||
"BRANCH_PATTERN": "/^dev/",
|
||||
"BRANCH_PATTERN_CI": "/^DEV/i",
|
||||
"EMPTY_PATTERN": "",
|
||||
"PLAIN_PATTERN": "develop",
|
||||
}
|
||||
return m[key]
|
||||
}
|
||||
@@ -69,6 +73,50 @@ func TestEvalIf(t *testing.T) {
|
||||
{"extra spaces", ` $CI_COMMIT_BRANCH == "develop" `, true},
|
||||
{"tabs", "$CI_COMMIT_BRANCH\t==\t\"develop\"", true},
|
||||
|
||||
// ── Multi-line expressions (newlines between tokens) ──────────────────
|
||||
{"multiline or true", "$CI_COMMIT_BRANCH == \"develop\" ||\n$CI_COMMIT_TAG != null", true},
|
||||
{"multiline or false", "$CI_COMMIT_BRANCH == \"main\" ||\n$CI_COMMIT_TAG != null", false},
|
||||
{"multiline and true", "$CI_COMMIT_BRANCH == \"develop\" &&\n$CI_PIPELINE_SOURCE == \"push\"", true},
|
||||
{"multiline and false", "$CI_COMMIT_BRANCH == \"main\" &&\n$CI_PIPELINE_SOURCE == \"push\"", false},
|
||||
{"multiline with crlf", "$CI_COMMIT_BRANCH == \"develop\" ||\r\n$CI_COMMIT_TAG != null", true},
|
||||
|
||||
// ── ${VAR} curly-brace syntax ─────────────────────────────────────────
|
||||
{"curly var eq match", `${CI_COMMIT_BRANCH} == "develop"`, true},
|
||||
{"curly var eq no match", `${CI_COMMIT_BRANCH} == "main"`, false},
|
||||
{"curly var truthiness", `${CI_COMMIT_BRANCH}`, true},
|
||||
{"curly var falsy", `${CI_COMMIT_TAG}`, false},
|
||||
{"curly var neq null", `${CI_COMMIT_BRANCH} != null`, true},
|
||||
{"curly mixed", `${CI_COMMIT_BRANCH} == "develop" && $CI_PIPELINE_SOURCE == "push"`, true},
|
||||
|
||||
// ── Regex flags (/pattern/i etc.) ─────────────────────────────────────
|
||||
{"regex flag i match", `$CI_COMMIT_BRANCH =~ /^DEV/i`, true},
|
||||
{"regex flag i no match", `$CI_COMMIT_BRANCH =~ /^MAIN/i`, false},
|
||||
{"regex flag i not match", `$CI_COMMIT_BRANCH !~ /^MAIN/i`, true},
|
||||
{"regex no flag case sensitive", `$CI_COMMIT_BRANCH =~ /^DEV/`, false},
|
||||
{"regex flag i version tag", `$CI_PIPELINE_SOURCE =~ /^PUSH$/i`, true},
|
||||
|
||||
// ── Variable on right side of =~ ──────────────────────────────────────
|
||||
{"var regex rhs match", `$CI_COMMIT_BRANCH =~ $BRANCH_PATTERN`, true},
|
||||
{"var regex rhs no match", `$CI_PIPELINE_SOURCE =~ $BRANCH_PATTERN`, false},
|
||||
{"var regex rhs ci flag match", `$CI_COMMIT_BRANCH =~ $BRANCH_PATTERN_CI`, true},
|
||||
{"var regex rhs empty permissive", `$CI_COMMIT_BRANCH =~ $EMPTY_PATTERN`, true},
|
||||
{"var regex rhs plain permissive", `$CI_COMMIT_BRANCH =~ $PLAIN_PATTERN`, true},
|
||||
{"var regex rhs not match", `$CI_COMMIT_BRANCH !~ $BRANCH_PATTERN`, false},
|
||||
|
||||
// ── Bare true/false keywords ─────────────────────────────────────────
|
||||
// GitLab CI treats true/false as the string values "true"/"false".
|
||||
{"bare true match", `$CI_PIPELINE_SOURCE == true`, false}, // "push" != "true"
|
||||
{"bare false match", `$CI_COMMIT_TAG == false`, false}, // "" != "false"
|
||||
{"bare true var set to true", `$DEPLOY_ENV == true`, false}, // "staging" != "true"
|
||||
{"bare false neq", `$CI_COMMIT_BRANCH != false`, true}, // "develop" != "false"
|
||||
{"bare true in compound", `$CI_COMMIT_BRANCH != null && $CI_COMMIT_TAG == false`, false},
|
||||
|
||||
// ── Integer literals ──────────────────────────────────────────────────
|
||||
// Compared as decimal strings (GitLab CI converts integers to strings).
|
||||
{"int eq match", `$CI_PIPELINE_SOURCE != 0`, true}, // "push" != "0"
|
||||
{"int eq no match", `$CI_COMMIT_TAG == 0`, false}, // "" != "0"
|
||||
{"int in compound", `$CI_COMMIT_BRANCH != null && $CI_COMMIT_BRANCH != 0`, true},
|
||||
|
||||
// ── Permissive fallback ───────────────────────────────────────────────
|
||||
{"unparseable returns true", `this is not valid syntax %%%`, true},
|
||||
{"empty expr returns true", ``, true},
|
||||
|
||||
@@ -28,13 +28,17 @@ func (s JobState) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// EvalWorkflow returns false when the pipeline's workflow:rules block would
|
||||
// prevent any pipeline from starting in the given context.
|
||||
// Returns true when ctx is empty, when there is no workflow block, or when no
|
||||
// rule is configured.
|
||||
func EvalWorkflow(p *model.Pipeline, ctx *Context) bool {
|
||||
// EvalWorkflow evaluates the pipeline's workflow:rules block against ctx.
|
||||
// Returns (runs, ruleVars):
|
||||
// - runs=false means the pipeline would not start for this context.
|
||||
// - ruleVars holds any variables: defined on the matching rule; inject these
|
||||
// into the context so job rules can reference them.
|
||||
//
|
||||
// Returns (true, nil) when ctx is empty, when there is no workflow block, or
|
||||
// when no rules are configured.
|
||||
func EvalWorkflow(p *model.Pipeline, ctx *Context) (bool, map[string]string) {
|
||||
if ctx.IsEmpty() || p.Workflow == nil || len(p.Workflow.Rules) == 0 {
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
vars := ctx.Get
|
||||
for _, rule := range p.Workflow.Rules {
|
||||
@@ -45,9 +49,9 @@ func EvalWorkflow(p *model.Pipeline, ctx *Context) bool {
|
||||
if when == "" {
|
||||
when = "always"
|
||||
}
|
||||
return when != "never"
|
||||
return when != "never", ExtractStringVars(rule.Variables)
|
||||
}
|
||||
return false // no rule matched → pipeline does not run
|
||||
return false, nil // no rule matched → pipeline does not run
|
||||
}
|
||||
|
||||
// EvalJob returns the effective JobState for job in the given context.
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
package cicontext
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.k3nny.fr/glint/internal/model"
|
||||
)
|
||||
|
||||
func TestEvalWorkflow(t *testing.T) {
|
||||
makePipeline := func(rules []model.Rule) *model.Pipeline {
|
||||
return &model.Pipeline{Workflow: &model.Workflow{Rules: rules}}
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
rules []model.Rule
|
||||
branch string
|
||||
wantRuns bool
|
||||
wantVars map[string]string
|
||||
}{
|
||||
{
|
||||
name: "no workflow block",
|
||||
rules: nil,
|
||||
branch: "main",
|
||||
wantRuns: true,
|
||||
wantVars: nil,
|
||||
},
|
||||
{
|
||||
name: "matching rule runs always",
|
||||
rules: []model.Rule{
|
||||
{If: `$CI_COMMIT_BRANCH == "main"`, When: "always"},
|
||||
},
|
||||
branch: "main",
|
||||
wantRuns: true,
|
||||
},
|
||||
{
|
||||
name: "matching rule when never",
|
||||
rules: []model.Rule{
|
||||
{If: `$CI_COMMIT_BRANCH == "main"`, When: "never"},
|
||||
},
|
||||
branch: "main",
|
||||
wantRuns: false,
|
||||
},
|
||||
{
|
||||
name: "no rule matched",
|
||||
rules: []model.Rule{
|
||||
{If: `$CI_COMMIT_BRANCH == "main"`},
|
||||
},
|
||||
branch: "develop",
|
||||
wantRuns: false,
|
||||
},
|
||||
{
|
||||
name: "matching rule with variables",
|
||||
rules: []model.Rule{
|
||||
{
|
||||
If: `$CI_COMMIT_BRANCH == "main"`,
|
||||
When: "always",
|
||||
Variables: map[string]any{
|
||||
"DEPLOY_TARGET": "production",
|
||||
"ENVIRONMENT": "prod",
|
||||
},
|
||||
},
|
||||
{When: "always"},
|
||||
},
|
||||
branch: "main",
|
||||
wantRuns: true,
|
||||
wantVars: map[string]string{
|
||||
"DEPLOY_TARGET": "production",
|
||||
"ENVIRONMENT": "prod",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "fallback rule with different variables",
|
||||
rules: []model.Rule{
|
||||
{If: `$CI_COMMIT_BRANCH == "main"`, Variables: map[string]any{"DEPLOY_TARGET": "production"}},
|
||||
{When: "always", Variables: map[string]any{"DEPLOY_TARGET": "staging"}},
|
||||
},
|
||||
branch: "develop",
|
||||
wantRuns: true,
|
||||
wantVars: map[string]string{"DEPLOY_TARGET": "staging"},
|
||||
},
|
||||
{
|
||||
name: "empty context always runs",
|
||||
rules: []model.Rule{
|
||||
{If: `$CI_COMMIT_BRANCH == "main"`, When: "never"},
|
||||
},
|
||||
branch: "", // no context
|
||||
wantRuns: true,
|
||||
wantVars: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var p *model.Pipeline
|
||||
if tc.rules == nil {
|
||||
p = &model.Pipeline{}
|
||||
} else {
|
||||
p = makePipeline(tc.rules)
|
||||
}
|
||||
ctx := New(tc.branch, "", "", nil)
|
||||
runs, vars := EvalWorkflow(p, ctx)
|
||||
if runs != tc.wantRuns {
|
||||
t.Errorf("EvalWorkflow runs = %v, want %v", runs, tc.wantRuns)
|
||||
}
|
||||
for k, want := range tc.wantVars {
|
||||
if got := vars[k]; got != want {
|
||||
t.Errorf("EvalWorkflow vars[%q] = %q, want %q", k, got, want)
|
||||
}
|
||||
}
|
||||
if len(vars) != len(tc.wantVars) {
|
||||
t.Errorf("EvalWorkflow returned %d vars, want %d; got %v", len(vars), len(tc.wantVars), vars)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextInject(t *testing.T) {
|
||||
t.Run("inject does not overwrite pinned var", func(t *testing.T) {
|
||||
ctx := New("main", "", "", []string{"DEPLOY_TARGET=override"})
|
||||
ctx.Inject("DEPLOY_TARGET", "workflow-value")
|
||||
if got := ctx.Get("DEPLOY_TARGET"); got != "override" {
|
||||
t.Errorf("Inject overwrote pinned var: got %q, want %q", got, "override")
|
||||
}
|
||||
})
|
||||
t.Run("inject does not overwrite shortcut var", func(t *testing.T) {
|
||||
ctx := New("main", "", "", nil)
|
||||
ctx.Inject("CI_COMMIT_BRANCH", "other")
|
||||
if got := ctx.Get("CI_COMMIT_BRANCH"); got != "main" {
|
||||
t.Errorf("Inject overwrote shortcut var: got %q, want %q", got, "main")
|
||||
}
|
||||
})
|
||||
t.Run("inject sets new variable", func(t *testing.T) {
|
||||
ctx := New("main", "", "", nil)
|
||||
ctx.Inject("DEPLOY_TARGET", "production")
|
||||
if got := ctx.Get("DEPLOY_TARGET"); got != "production" {
|
||||
t.Errorf("Inject did not set variable: got %q", got)
|
||||
}
|
||||
})
|
||||
t.Run("inject later call overrides earlier call", func(t *testing.T) {
|
||||
ctx := New("main", "", "", nil)
|
||||
ctx.Inject("DEPLOY_TARGET", "pipeline-default")
|
||||
ctx.Inject("DEPLOY_TARGET", "workflow-override")
|
||||
if got := ctx.Get("DEPLOY_TARGET"); got != "workflow-override" {
|
||||
t.Errorf("second Inject did not win: got %q, want %q", got, "workflow-override")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestExtractStringVars(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in map[string]any
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "plain strings",
|
||||
in: map[string]any{"A": "hello", "B": "world"},
|
||||
want: map[string]string{"A": "hello", "B": "world"},
|
||||
},
|
||||
{
|
||||
name: "extended value form",
|
||||
in: map[string]any{
|
||||
"KEY": map[string]any{"value": "extended", "description": "some desc"},
|
||||
},
|
||||
want: map[string]string{"KEY": "extended"},
|
||||
},
|
||||
{
|
||||
name: "mixed forms",
|
||||
in: map[string]any{
|
||||
"PLAIN": "str",
|
||||
"COMPLEX": map[string]any{"value": "val"},
|
||||
},
|
||||
want: map[string]string{"PLAIN": "str", "COMPLEX": "val"},
|
||||
},
|
||||
{
|
||||
name: "nil map",
|
||||
in: nil,
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := ExtractStringVars(tc.in)
|
||||
for k, want := range tc.want {
|
||||
if got[k] != want {
|
||||
t.Errorf("ExtractStringVars[%q] = %q, want %q", k, got[k], want)
|
||||
}
|
||||
}
|
||||
if len(got) != len(tc.want) {
|
||||
t.Errorf("ExtractStringVars returned %d entries, want %d", len(got), len(tc.want))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkflowVarsJobEval verifies the end-to-end flow: workflow rule injects
|
||||
// DEPLOY_TARGET, which is then used in a job's rules:if: expression.
|
||||
func TestWorkflowVarsJobEval(t *testing.T) {
|
||||
rules := []model.Rule{
|
||||
{If: `$CI_COMMIT_BRANCH == "main"`, Variables: map[string]any{"DEPLOY_TARGET": "production"}},
|
||||
{When: "always", Variables: map[string]any{"DEPLOY_TARGET": "staging"}},
|
||||
}
|
||||
p := &model.Pipeline{
|
||||
Workflow: &model.Workflow{Rules: rules},
|
||||
Jobs: map[string]model.Job{
|
||||
"deploy-prod": {
|
||||
Rules: []model.Rule{
|
||||
{If: `$DEPLOY_TARGET == "production"`, When: "on_success"},
|
||||
{When: "never"},
|
||||
},
|
||||
},
|
||||
"deploy-staging": {
|
||||
Rules: []model.Rule{
|
||||
{If: `$DEPLOY_TARGET == "staging"`, When: "on_success"},
|
||||
{When: "never"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
branch string
|
||||
wantProd JobState
|
||||
wantStaging JobState
|
||||
}{
|
||||
{"main", JobActive, JobSkipped},
|
||||
{"develop", JobSkipped, JobActive},
|
||||
} {
|
||||
ctx := New(tc.branch, "", "", nil)
|
||||
_, ruleVars := EvalWorkflow(p, ctx)
|
||||
for k, v := range ruleVars {
|
||||
ctx.Inject(k, v)
|
||||
}
|
||||
if got := EvalJob(p.Jobs["deploy-prod"], ctx); got != tc.wantProd {
|
||||
t.Errorf("branch=%q deploy-prod = %v, want %v", tc.branch, got, tc.wantProd)
|
||||
}
|
||||
if got := EvalJob(p.Jobs["deploy-staging"], ctx); got != tc.wantStaging {
|
||||
t.Errorf("branch=%q deploy-staging = %v, want %v", tc.branch, got, tc.wantStaging)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,6 +142,24 @@ func (cfg GitLabConfig) FetchFile(project, filePath, ref string) ([]byte, error)
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// FetchURL downloads the content at a plain HTTPS URL without authentication.
|
||||
// Used for include: remote: entries which are public by definition.
|
||||
func FetchURL(rawURL string) ([]byte, error) {
|
||||
resp, err := http.Get(rawURL) //nolint:noctx
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GET %s: %w", rawURL, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading body: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("GET %s: status %d", rawURL, resp.StatusCode)
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, v := range values {
|
||||
if v != "" {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.k3nny.fr/glint/internal/fetcher"
|
||||
@@ -11,6 +12,8 @@ import (
|
||||
)
|
||||
|
||||
// Includes returns a Mermaid flowchart of the full include dependency tree.
|
||||
// Each node shows the jobs defined directly in that file, connected with
|
||||
// dashed arrows (solid arrows represent the include hierarchy itself).
|
||||
// It recurses into project:, component:, and local: includes to expose
|
||||
// transitive dependencies. Includes that cannot be fetched are shown but not expanded.
|
||||
func Includes(sourcePath string, rawIncludes []any, cfg fetcher.GitLabConfig) string {
|
||||
@@ -19,7 +22,12 @@ func Includes(sourcePath string, rawIncludes []any, cfg fetcher.GitLabConfig) st
|
||||
cfg: cfg,
|
||||
baseDir: filepath.Dir(sourcePath),
|
||||
}
|
||||
root := &treeNode{id: "root", label: mermaidLabel(sourcePath), class: "main"}
|
||||
root := &treeNode{
|
||||
id: "root",
|
||||
label: mermaidLabel(sourcePath),
|
||||
class: "main",
|
||||
jobs: directJobs(sourcePath),
|
||||
}
|
||||
b.buildChildren(root, rawIncludes)
|
||||
return renderTree(root)
|
||||
}
|
||||
@@ -29,6 +37,7 @@ type treeNode struct {
|
||||
label string
|
||||
class string
|
||||
children []*treeNode
|
||||
jobs []string // job names defined directly in this file
|
||||
}
|
||||
|
||||
// treeBuilder accumulates state while recursively traversing include entries.
|
||||
@@ -105,7 +114,9 @@ func (b *treeBuilder) parseMap(m map[string]any) []*treeNode {
|
||||
return []*treeNode{node}
|
||||
}
|
||||
if remote, ok := m["remote"].(string); ok {
|
||||
return []*treeNode{{id: b.nextID(), label: "remote:<br>" + mermaidLabel(remote), class: "remote"}}
|
||||
node := &treeNode{id: b.nextID(), label: "remote:<br>" + mermaidLabel(remote), class: "remote"}
|
||||
b.recurseRemote(node, remote)
|
||||
return []*treeNode{node}
|
||||
}
|
||||
if tmpl, ok := m["template"].(string); ok {
|
||||
return []*treeNode{{id: b.nextID(), label: "template:<br>" + mermaidLabel(tmpl), class: "template"}}
|
||||
@@ -126,7 +137,11 @@ func (b *treeBuilder) recurseLocal(node *treeNode, path string) {
|
||||
return
|
||||
}
|
||||
p, err := model.ParseBytes(data)
|
||||
if err != nil || len(p.Include) == 0 {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
node.jobs = jobNames(p)
|
||||
if len(p.Include) == 0 {
|
||||
return
|
||||
}
|
||||
orig := b.baseDir
|
||||
@@ -147,7 +162,33 @@ func (b *treeBuilder) recurseProject(node *treeNode, project, filePath, ref stri
|
||||
return
|
||||
}
|
||||
p, err := model.ParseBytes(data)
|
||||
if err != nil || len(p.Include) == 0 {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
node.jobs = jobNames(p)
|
||||
if len(p.Include) == 0 {
|
||||
return
|
||||
}
|
||||
b.buildChildren(node, p.Include)
|
||||
}
|
||||
|
||||
func (b *treeBuilder) recurseRemote(node *treeNode, rawURL string) {
|
||||
key := "remote:" + rawURL
|
||||
if b.visited[key] {
|
||||
return
|
||||
}
|
||||
b.visited[key] = true
|
||||
|
||||
data, err := fetcher.FetchURL(rawURL)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p, err := model.ParseBytes(data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
node.jobs = jobNames(p)
|
||||
if len(p.Include) == 0 {
|
||||
return
|
||||
}
|
||||
b.buildChildren(node, p.Include)
|
||||
@@ -172,7 +213,11 @@ func (b *treeBuilder) recurseComponent(node *treeNode, ref string) {
|
||||
return
|
||||
}
|
||||
p, err := model.ParseBytes(data)
|
||||
if err != nil || len(p.Include) == 0 {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
node.jobs = jobNames(p)
|
||||
if len(p.Include) == 0 {
|
||||
return
|
||||
}
|
||||
b.buildChildren(node, p.Include)
|
||||
@@ -193,11 +238,19 @@ func renderTree(root *treeNode) string {
|
||||
w(" classDef local fill:#428fdc,stroke:#1068bf,color:#fff")
|
||||
w(" classDef remote fill:#868686,stroke:#686868,color:#fff")
|
||||
w(" classDef template fill:#fc6d26,stroke:#e56b1f,color:#fff")
|
||||
w(" classDef job fill:#f5f5ff,stroke:#7175a0,color:#333")
|
||||
w("")
|
||||
|
||||
jobCounter := 0
|
||||
var emit func(n *treeNode)
|
||||
emit = func(n *treeNode) {
|
||||
wf(" %s[\"%s\"]:::%s", n.id, n.label, n.class)
|
||||
for _, jobName := range n.jobs {
|
||||
jobCounter++
|
||||
jobID := fmt.Sprintf("job%d", jobCounter)
|
||||
wf(" %s(\"%s\"):::job", jobID, mermaidLabel(jobName))
|
||||
wf(" %s -.-> %s", n.id, jobID)
|
||||
}
|
||||
for _, child := range n.children {
|
||||
emit(child)
|
||||
wf(" %s --> %s", n.id, child.id)
|
||||
@@ -208,6 +261,29 @@ func renderTree(root *treeNode) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// directJobs parses a single pipeline file (without include resolution) and
|
||||
// returns the sorted list of job names defined directly in it.
|
||||
func directJobs(path string) []string {
|
||||
p, err := model.Parse(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return jobNames(p)
|
||||
}
|
||||
|
||||
// jobNames returns a sorted slice of all job names defined in p.
|
||||
func jobNames(p *model.Pipeline) []string {
|
||||
if len(p.Jobs) == 0 {
|
||||
return nil
|
||||
}
|
||||
names := make([]string, 0, len(p.Jobs))
|
||||
for name := range p.Jobs {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
return names
|
||||
}
|
||||
|
||||
// parseComponentRef parses a CI/CD component reference of the form
|
||||
// <host>/<project-path>/<component-name>@<version>.
|
||||
func parseComponentRef(ref string) (host, project, component, version string, err error) {
|
||||
|
||||
@@ -23,7 +23,10 @@ func checkDependencies(p *model.Pipeline) []Finding {
|
||||
if !exists {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleUnknownDependency,
|
||||
Job: name,
|
||||
File: job.File,
|
||||
Line: job.Line,
|
||||
Message: fmt.Sprintf("'dependencies' references unknown job %q", dep),
|
||||
})
|
||||
continue
|
||||
@@ -33,7 +36,10 @@ func checkDependencies(p *model.Pipeline) []Finding {
|
||||
if depHasStage && depIdx >= jobStageIdx {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleDependencyStage,
|
||||
Job: name,
|
||||
File: job.File,
|
||||
Line: job.Line,
|
||||
Message: fmt.Sprintf("'dependencies' job %q must be in an earlier stage (in %q, current job is in %q)", dep, depJob.Stage, job.Stage),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -105,6 +105,7 @@ func checkWhen(name string, job model.Job) []Finding {
|
||||
if job.When != "" && !validJobWhen[job.When] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidWhen,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'when' has invalid value %q; valid: on_success, on_failure, always, manual, delayed, never", job.When),
|
||||
})
|
||||
@@ -112,6 +113,7 @@ func checkWhen(name string, job model.Job) []Finding {
|
||||
if job.When == "delayed" && job.StartIn == "" {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleDelayedNoStartIn,
|
||||
Job: name,
|
||||
Message: "'when: delayed' requires 'start_in' (e.g. 'start_in: 30 minutes')",
|
||||
})
|
||||
@@ -119,6 +121,7 @@ func checkWhen(name string, job model.Job) []Finding {
|
||||
if job.When != "delayed" && job.StartIn != "" {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleStartInNoDelayed,
|
||||
Job: name,
|
||||
Message: "'start_in' is only valid when 'when: delayed'",
|
||||
})
|
||||
@@ -135,6 +138,7 @@ func checkParallel(name string, job model.Job) []Finding {
|
||||
if v < 2 || v > 200 {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidParallel,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'parallel' must be between 2 and 200, got %d", v),
|
||||
}}
|
||||
@@ -143,6 +147,7 @@ func checkParallel(name string, job model.Job) []Finding {
|
||||
if _, ok := v["matrix"]; !ok {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidParallel,
|
||||
Job: name,
|
||||
Message: "'parallel' map form must have a 'matrix' key",
|
||||
}}
|
||||
@@ -150,6 +155,7 @@ func checkParallel(name string, job model.Job) []Finding {
|
||||
default:
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidParallel,
|
||||
Job: name,
|
||||
Message: "'parallel' must be an integer (2–200) or a map with 'matrix'",
|
||||
}}
|
||||
@@ -166,6 +172,7 @@ func checkRetry(name string, job model.Job) []Finding {
|
||||
if v < 0 || v > 2 {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidRetry,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'retry' must be 0, 1, or 2; got %d", v),
|
||||
}}
|
||||
@@ -176,6 +183,7 @@ func checkRetry(name string, job model.Job) []Finding {
|
||||
if n, ok := maxVal.(int); ok && (n < 0 || n > 2) {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidRetry,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'retry.max' must be 0, 1, or 2; got %d", n),
|
||||
})
|
||||
@@ -188,6 +196,7 @@ func checkRetry(name string, job model.Job) []Finding {
|
||||
default:
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidRetry,
|
||||
Job: name,
|
||||
Message: "'retry' must be an integer (0–2) or a map with 'max'/'when'",
|
||||
}}
|
||||
@@ -201,6 +210,7 @@ func validateRetryWhen(name string, val any) []Finding {
|
||||
if !validRetryWhen[s] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidRetryWhen,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'retry.when' has invalid value %q", s),
|
||||
})
|
||||
@@ -230,6 +240,7 @@ func checkAllowFailure(name string, job model.Job) []Finding {
|
||||
if _, ok := v["exit_codes"]; !ok {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidAllowFailure,
|
||||
Job: name,
|
||||
Message: "'allow_failure' map form must contain 'exit_codes'",
|
||||
}}
|
||||
@@ -238,6 +249,7 @@ func checkAllowFailure(name string, job model.Job) []Finding {
|
||||
_ = v
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidAllowFailure,
|
||||
Job: name,
|
||||
Message: "'allow_failure' must be a boolean or a map with 'exit_codes'",
|
||||
}}
|
||||
@@ -252,6 +264,7 @@ func checkInterruptible(name string, job model.Job) []Finding {
|
||||
if _, ok := job.Interruptible.(bool); !ok {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidInterruptible,
|
||||
Job: name,
|
||||
Message: "'interruptible' must be a boolean",
|
||||
}}
|
||||
@@ -267,6 +280,7 @@ func checkTrigger(name string, job model.Job) []Finding {
|
||||
if scriptNonEmpty(job.Script) {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleTriggerWithScript,
|
||||
Job: name,
|
||||
Message: "jobs with 'trigger' cannot use 'script'",
|
||||
})
|
||||
@@ -277,6 +291,7 @@ func checkTrigger(name string, job model.Job) []Finding {
|
||||
if !hasProject && !hasInclude {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidTrigger,
|
||||
Job: name,
|
||||
Message: "'trigger' map must specify 'project' or 'include'",
|
||||
})
|
||||
@@ -294,6 +309,7 @@ func checkCoverage(name string, job model.Job) []Finding {
|
||||
if !coveragePattern.MatchString(job.Coverage) {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidCoverage,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'coverage' must be a regex pattern wrapped in '/' (e.g. '/\\d+\\.?\\d*%%/'), got %q", job.Coverage),
|
||||
}}
|
||||
@@ -309,6 +325,7 @@ func checkRelease(name string, job model.Job) []Finding {
|
||||
if !ok {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidRelease,
|
||||
Job: name,
|
||||
Message: "'release' must be a map",
|
||||
}}
|
||||
@@ -317,6 +334,7 @@ func checkRelease(name string, job model.Job) []Finding {
|
||||
if !exists || tagName == "" || tagName == nil {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidRelease,
|
||||
Job: name,
|
||||
Message: "'release' requires 'tag_name'",
|
||||
}}
|
||||
@@ -339,6 +357,7 @@ func checkEnvironment(name string, job model.Job) []Finding {
|
||||
if (envName == nil || envName == "") && hasURL {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidEnvironment,
|
||||
Job: name,
|
||||
Message: "'environment.url' requires 'environment.name' to be set",
|
||||
})
|
||||
@@ -346,6 +365,7 @@ func checkEnvironment(name string, job model.Job) []Finding {
|
||||
if action, ok := m["action"].(string); ok && !validEnvironmentAction[action] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidEnvironment,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'environment.action' has invalid value %q; valid: start, stop, prepare, verify, access", action),
|
||||
})
|
||||
@@ -365,6 +385,7 @@ func checkArtifacts(name string, job model.Job) []Finding {
|
||||
if w, ok := m["when"].(string); ok && !validArtifactsWhen[w] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidArtifacts,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'artifacts.when' has invalid value %q; valid: on_success, on_failure, always", w),
|
||||
})
|
||||
@@ -374,6 +395,7 @@ func checkArtifacts(name string, job model.Job) []Finding {
|
||||
if paths == nil {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidArtifacts,
|
||||
Job: name,
|
||||
Message: "'artifacts.expose_as' requires 'artifacts.paths'",
|
||||
})
|
||||
@@ -392,6 +414,7 @@ func checkArtifacts(name string, job model.Job) []Finding {
|
||||
if !found {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Warning,
|
||||
Rule: RulePagesPublic,
|
||||
Job: name,
|
||||
Message: "the 'pages' job should include 'public' in 'artifacts.paths' for GitLab Pages to deploy",
|
||||
})
|
||||
@@ -421,6 +444,7 @@ func checkCache(name string, job model.Job) []Finding {
|
||||
if w, ok := m["when"].(string); ok && !validCacheWhen[w] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidCache,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'cache.when' has invalid value %q; valid: on_success, on_failure, always", w),
|
||||
})
|
||||
@@ -428,6 +452,7 @@ func checkCache(name string, job model.Job) []Finding {
|
||||
if p, ok := m["policy"].(string); ok && !validCachePolicy[p] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidCache,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'cache.policy' has invalid value %q; valid: pull, push, pull-push", p),
|
||||
})
|
||||
@@ -442,6 +467,7 @@ func checkRules(name string, job model.Job) []Finding {
|
||||
if rule.When != "" && !validRuleWhen[rule.When] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidRulesWhen,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("rules[%d].when has invalid value %q; valid: on_success, on_failure, always, manual, delayed, never", i, rule.When),
|
||||
})
|
||||
@@ -462,6 +488,7 @@ func checkImage(name string, job model.Job) []Finding {
|
||||
if imgName == nil || imgName == "" {
|
||||
return []Finding{{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidImage,
|
||||
Job: name,
|
||||
Message: "'image' map form requires a 'name' key",
|
||||
}}
|
||||
@@ -489,6 +516,7 @@ func checkInherit(name string, job model.Job) []Finding {
|
||||
default:
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleInvalidInherit,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("'inherit.%s' must be a boolean or a list of names", key),
|
||||
})
|
||||
|
||||
@@ -16,13 +16,31 @@ const (
|
||||
|
||||
type Finding struct {
|
||||
Severity Severity
|
||||
Rule string // stable rule ID, e.g. "GL003" — see rules.go
|
||||
Job string // empty for pipeline-level findings
|
||||
File string // source file where the finding originates
|
||||
Line int // line number in File (0 = unknown)
|
||||
Message string
|
||||
}
|
||||
|
||||
func (f Finding) String() string {
|
||||
loc := ""
|
||||
if f.File != "" {
|
||||
if f.Line > 0 {
|
||||
loc = fmt.Sprintf(" (%s:%d)", f.File, f.Line)
|
||||
} else {
|
||||
loc = fmt.Sprintf(" (%s)", f.File)
|
||||
}
|
||||
}
|
||||
ruleStr := ""
|
||||
if f.Rule != "" {
|
||||
ruleStr = " " + f.Rule
|
||||
}
|
||||
if f.Job != "" {
|
||||
return fmt.Sprintf("[%s] job %q: %s", f.Severity, f.Job, f.Message)
|
||||
return fmt.Sprintf("[%s] job %q%s%s: %s", f.Severity, f.Job, loc, ruleStr, f.Message)
|
||||
}
|
||||
if loc != "" || ruleStr != "" {
|
||||
return fmt.Sprintf("[%s]%s%s: %s", f.Severity, loc, ruleStr, f.Message)
|
||||
}
|
||||
return fmt.Sprintf("[%s] %s", f.Severity, f.Message)
|
||||
}
|
||||
@@ -35,6 +53,7 @@ func Lint(p *model.Pipeline) []Finding {
|
||||
findings = append(findings, checkJobs(p)...)
|
||||
findings = append(findings, checkNeeds(p)...)
|
||||
findings = append(findings, checkDependencies(p)...)
|
||||
findings = append(findings, checkVariableRefs(p)...)
|
||||
return findings
|
||||
}
|
||||
|
||||
@@ -43,6 +62,8 @@ func checkStages(p *model.Pipeline) []Finding {
|
||||
if len(p.Stages) == 0 {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Warning,
|
||||
Rule: RuleNoStages,
|
||||
File: p.SourceFile,
|
||||
Message: "no stages defined; GitLab will use default stages (build, test, deploy)",
|
||||
})
|
||||
}
|
||||
@@ -58,6 +79,8 @@ func checkWorkflow(p *model.Pipeline) []Finding {
|
||||
if rule.When != "" && !validWorkflowRuleWhen[rule.When] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleWorkflowWhen,
|
||||
File: p.SourceFile,
|
||||
Message: fmt.Sprintf("workflow.rules[%d].when has invalid value %q; valid: always, never", i, rule.When),
|
||||
})
|
||||
}
|
||||
@@ -88,10 +111,17 @@ func checkJob(name string, job model.Job, stageSet map[string]bool) []Finding {
|
||||
|
||||
// After extends resolution, a job with no script/run is an error.
|
||||
// Exceptions: trigger jobs, pages jobs (use pages: keyword), and template jobs.
|
||||
// When the job has extends:, the script may come from a base that couldn't be
|
||||
// fetched (e.g. a remote include without a token), so downgrade to warning.
|
||||
hasScript := scriptNonEmpty(job.Script) || job.Run != nil
|
||||
if !isTemplate && !isTrigger && job.Pages == nil && !hasScript {
|
||||
sev := Error
|
||||
if job.Extends != nil {
|
||||
sev = Warning
|
||||
}
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Severity: sev,
|
||||
Rule: RuleMissingScript,
|
||||
Job: name,
|
||||
Message: "missing required field 'script' (or 'run')",
|
||||
})
|
||||
@@ -103,6 +133,7 @@ func checkJob(name string, job model.Job, stageSet map[string]bool) []Finding {
|
||||
if job.Stage != "" && !strings.Contains(job.Stage, "$[[") && len(stageSet) > 0 && !stageSet[job.Stage] {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleUnknownStage,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("stage %q is not defined in 'stages'", job.Stage),
|
||||
})
|
||||
@@ -112,6 +143,7 @@ func checkJob(name string, job model.Job, stageSet map[string]bool) []Finding {
|
||||
if job.Only != nil && len(job.Rules) > 0 {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleOnlyRulesConflict,
|
||||
Job: name,
|
||||
Message: "'only' and 'rules' cannot be used together",
|
||||
})
|
||||
@@ -121,6 +153,7 @@ func checkJob(name string, job model.Job, stageSet map[string]bool) []Finding {
|
||||
if job.Except != nil && len(job.Rules) > 0 {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleExceptRulesConflict,
|
||||
Job: name,
|
||||
Message: "'except' and 'rules' cannot be used together",
|
||||
})
|
||||
@@ -130,6 +163,7 @@ func checkJob(name string, job model.Job, stageSet map[string]bool) []Finding {
|
||||
if job.Only != nil || job.Except != nil {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Warning,
|
||||
Rule: RuleDeprecatedOnly,
|
||||
Job: name,
|
||||
Message: "'only'/'except' are deprecated; prefer 'rules'",
|
||||
})
|
||||
@@ -137,6 +171,13 @@ func checkJob(name string, job model.Job, stageSet map[string]bool) []Finding {
|
||||
|
||||
findings = append(findings, checkJobKeywords(name, job)...)
|
||||
|
||||
// Attach source location to every job-scoped finding collected above.
|
||||
for i := range findings {
|
||||
if findings[i].Job != "" && findings[i].File == "" {
|
||||
findings[i].File = job.File
|
||||
findings[i].Line = job.Line
|
||||
}
|
||||
}
|
||||
return findings
|
||||
}
|
||||
|
||||
|
||||
+45
-19
@@ -6,6 +6,12 @@ import (
|
||||
"git.k3nny.fr/glint/internal/model"
|
||||
)
|
||||
|
||||
// needEntry is a parsed element from a job's needs: list.
|
||||
type needEntry struct {
|
||||
job string
|
||||
optional bool // true when the needs entry carries optional: true
|
||||
}
|
||||
|
||||
func checkNeeds(p *model.Pipeline) []Finding {
|
||||
var findings []Finding
|
||||
|
||||
@@ -15,7 +21,7 @@ func checkNeeds(p *model.Pipeline) []Finding {
|
||||
stageIndex[s] = i
|
||||
}
|
||||
|
||||
// needsGraph maps each job to the list of jobs it depends on.
|
||||
// needsGraph maps each job to the jobs it depends on (existing jobs only).
|
||||
// Used for cycle detection after individual checks.
|
||||
needsGraph := make(map[string][]string)
|
||||
|
||||
@@ -24,32 +30,47 @@ func checkNeeds(p *model.Pipeline) []Finding {
|
||||
continue
|
||||
}
|
||||
|
||||
neededNames := parseNeedJobNames(job.Needs)
|
||||
needsGraph[name] = neededNames
|
||||
|
||||
entries := parseNeedEntries(job.Needs)
|
||||
jobStageIdx, jobHasStage := stageIndex[job.Stage]
|
||||
|
||||
for _, needed := range neededNames {
|
||||
neededJob, exists := p.Jobs[needed]
|
||||
for _, entry := range entries {
|
||||
neededJob, exists := p.Jobs[entry.job]
|
||||
if !exists {
|
||||
// optional: true means GitLab CI will silently skip the
|
||||
// dependency when the job is absent (e.g. from a conditional
|
||||
// include). Downgrade to warning so users are informed without
|
||||
// failing the lint.
|
||||
sev := Error
|
||||
if entry.optional {
|
||||
sev = Warning
|
||||
}
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Severity: sev,
|
||||
Rule: RuleNeedsUnknown,
|
||||
Job: name,
|
||||
Message: fmt.Sprintf("needs unknown job %q", needed),
|
||||
File: job.File,
|
||||
Line: job.Line,
|
||||
Message: fmt.Sprintf("needs unknown job %q", entry.job),
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
// Add to the cycle-detection graph only when the dep exists.
|
||||
needsGraph[name] = append(needsGraph[name], entry.job)
|
||||
|
||||
// A job cannot need a job in a later stage.
|
||||
if len(p.Stages) > 0 && jobHasStage && neededJob.Stage != "" {
|
||||
neededStageIdx, neededHasStage := stageIndex[neededJob.Stage]
|
||||
if neededHasStage && neededStageIdx > jobStageIdx {
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleNeedsStageOrder,
|
||||
Job: name,
|
||||
File: job.File,
|
||||
Line: job.Line,
|
||||
Message: fmt.Sprintf(
|
||||
"needs %q which is in a later stage (%q after %q)",
|
||||
needed, neededJob.Stage, job.Stage,
|
||||
entry.job, neededJob.Stage, job.Stage,
|
||||
),
|
||||
})
|
||||
}
|
||||
@@ -57,32 +78,33 @@ func checkNeeds(p *model.Pipeline) []Finding {
|
||||
}
|
||||
}
|
||||
|
||||
findings = append(findings, detectNeedsCycles(needsGraph)...)
|
||||
findings = append(findings, detectNeedsCycles(needsGraph, p.Jobs)...)
|
||||
return findings
|
||||
}
|
||||
|
||||
// parseNeedJobNames extracts job names from a needs: list.
|
||||
// Each element is either a plain string or a map with a "job" key.
|
||||
// Cross-pipeline needs (maps with a "pipeline" key) are skipped.
|
||||
func parseNeedJobNames(needs []any) []string {
|
||||
var names []string
|
||||
// parseNeedEntries extracts needs entries from a needs: list, preserving the
|
||||
// optional flag. Each element is a plain string (job name) or a map with a
|
||||
// "job" key. Cross-pipeline needs (maps with a "pipeline" key) are skipped.
|
||||
func parseNeedEntries(needs []any) []needEntry {
|
||||
var entries []needEntry
|
||||
for _, n := range needs {
|
||||
switch v := n.(type) {
|
||||
case string:
|
||||
names = append(names, v)
|
||||
entries = append(entries, needEntry{job: v})
|
||||
case map[string]any:
|
||||
if _, crossPipeline := v["pipeline"]; crossPipeline {
|
||||
continue
|
||||
}
|
||||
if job, ok := v["job"].(string); ok {
|
||||
names = append(names, job)
|
||||
optional, _ := v["optional"].(bool)
|
||||
entries = append(entries, needEntry{job: job, optional: optional})
|
||||
}
|
||||
}
|
||||
}
|
||||
return names
|
||||
return entries
|
||||
}
|
||||
|
||||
func detectNeedsCycles(graph map[string][]string) []Finding {
|
||||
func detectNeedsCycles(graph map[string][]string, jobs map[string]model.Job) []Finding {
|
||||
const (
|
||||
unvisited = 0
|
||||
visiting = 1
|
||||
@@ -101,9 +123,13 @@ func detectNeedsCycles(graph map[string][]string) []Finding {
|
||||
case visiting:
|
||||
if !reported[name] {
|
||||
reported[name] = true
|
||||
j := jobs[name]
|
||||
findings = append(findings, Finding{
|
||||
Severity: Error,
|
||||
Rule: RuleNeedsCycle,
|
||||
Job: name,
|
||||
File: j.File,
|
||||
Line: j.Line,
|
||||
Message: fmt.Sprintf("circular dependency in needs: %v → %s", path, name),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package linter
|
||||
|
||||
// Rule ID constants. Each ID is stable across versions and uniquely identifies
|
||||
// one lint check. Use these when filtering output, writing suppression rules,
|
||||
// or referencing a check in documentation.
|
||||
//
|
||||
// Prefix GL = Glint / GitLab CI lint.
|
||||
// Numbering is sequential by category; gaps may appear as rules are added.
|
||||
const (
|
||||
// ── Pipeline-level ──────────────────────────────────────────────────────
|
||||
|
||||
// GL001: no stages: block defined; GitLab falls back to default stages.
|
||||
RuleNoStages = "GL001"
|
||||
|
||||
// GL002: workflow.rules[n].when has an invalid value (only always/never allowed).
|
||||
RuleWorkflowWhen = "GL002"
|
||||
|
||||
// ── Job structure ────────────────────────────────────────────────────────
|
||||
|
||||
// GL003: job is missing a required script: (or run:) field.
|
||||
RuleMissingScript = "GL003"
|
||||
|
||||
// GL004: job references a stage not declared in stages:.
|
||||
RuleUnknownStage = "GL004"
|
||||
|
||||
// GL005: only: and rules: used together on the same job.
|
||||
RuleOnlyRulesConflict = "GL005"
|
||||
|
||||
// GL006: except: and rules: used together on the same job.
|
||||
RuleExceptRulesConflict = "GL006"
|
||||
|
||||
// GL007: only:/except: used (deprecated; prefer rules:).
|
||||
RuleDeprecatedOnly = "GL007"
|
||||
|
||||
// ── Keyword constraints ──────────────────────────────────────────────────
|
||||
|
||||
// GL008: when: has an invalid value.
|
||||
RuleInvalidWhen = "GL008"
|
||||
|
||||
// GL009: when: delayed without start_in:.
|
||||
RuleDelayedNoStartIn = "GL009"
|
||||
|
||||
// GL010: start_in: set when when: is not delayed.
|
||||
RuleStartInNoDelayed = "GL010"
|
||||
|
||||
// GL011: parallel: value is invalid (integer out of range or map missing matrix:).
|
||||
RuleInvalidParallel = "GL011"
|
||||
|
||||
// GL012: retry: integer is out of range 0–2, or retry: is neither int nor map.
|
||||
RuleInvalidRetry = "GL012"
|
||||
|
||||
// GL013: retry.when: contains an unrecognised failure type.
|
||||
RuleInvalidRetryWhen = "GL013"
|
||||
|
||||
// GL014: allow_failure: is not a boolean or a map with exit_codes:.
|
||||
RuleInvalidAllowFailure = "GL014"
|
||||
|
||||
// GL015: interruptible: is not a boolean.
|
||||
RuleInvalidInterruptible = "GL015"
|
||||
|
||||
// GL016: trigger: job also defines script: (mutually exclusive).
|
||||
RuleTriggerWithScript = "GL016"
|
||||
|
||||
// GL017: trigger: map does not specify project: or include:.
|
||||
RuleInvalidTrigger = "GL017"
|
||||
|
||||
// GL018: coverage: is not a regex pattern wrapped in /.
|
||||
RuleInvalidCoverage = "GL018"
|
||||
|
||||
// GL019: release: is missing required tag_name:, or is not a map.
|
||||
RuleInvalidRelease = "GL019"
|
||||
|
||||
// GL020: environment: has an invalid url/action configuration.
|
||||
RuleInvalidEnvironment = "GL020"
|
||||
|
||||
// GL021: artifacts: has an invalid when/expose_as configuration.
|
||||
RuleInvalidArtifacts = "GL021"
|
||||
|
||||
// GL022: pages job artifacts.paths does not include public/.
|
||||
RulePagesPublic = "GL022"
|
||||
|
||||
// GL023: cache: has an invalid when/policy value.
|
||||
RuleInvalidCache = "GL023"
|
||||
|
||||
// GL024: rules[n].when has an invalid value.
|
||||
RuleInvalidRulesWhen = "GL024"
|
||||
|
||||
// GL025: image: map form is missing a name: key.
|
||||
RuleInvalidImage = "GL025"
|
||||
|
||||
// GL026: inherit.default or inherit.variables is not a boolean or list.
|
||||
RuleInvalidInherit = "GL026"
|
||||
|
||||
// ── Cross-job graph ──────────────────────────────────────────────────────
|
||||
|
||||
// GL027: needs: references a job that does not exist in the pipeline.
|
||||
RuleNeedsUnknown = "GL027"
|
||||
|
||||
// GL028: needs: references a job in a later stage than the current job.
|
||||
RuleNeedsStageOrder = "GL028"
|
||||
|
||||
// GL029: circular dependency detected in the needs: graph.
|
||||
RuleNeedsCycle = "GL029"
|
||||
|
||||
// GL030: dependencies: references a job that does not exist.
|
||||
RuleUnknownDependency = "GL030"
|
||||
|
||||
// GL031: dependencies: references a job in the same or a later stage.
|
||||
RuleDependencyStage = "GL031"
|
||||
|
||||
// ── Expression validation ────────────────────────────────────────────────
|
||||
|
||||
// GL032: rules:if: references a variable not declared in pipeline variables:,
|
||||
// the job's own variables:, or any workflow:rules:variables: block.
|
||||
// May be a false positive for variables set in GitLab CI/CD project settings.
|
||||
RuleUndeclaredVariable = "GL032"
|
||||
)
|
||||
@@ -0,0 +1,158 @@
|
||||
package linter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.k3nny.fr/glint/internal/model"
|
||||
)
|
||||
|
||||
// predefinedVarPrefixes lists GitLab-maintained variable namespaces that are
|
||||
// always available without an explicit declaration in variables: blocks.
|
||||
var predefinedVarPrefixes = []string{
|
||||
"CI_", // most predefined CI variables (CI_COMMIT_BRANCH, CI_JOB_ID, …)
|
||||
"GITLAB_", // user/project metadata (GITLAB_USER_ID, GITLAB_FEATURES, …)
|
||||
"FF_", // GitLab feature flags
|
||||
"RUNNER_", // runner-level variables
|
||||
"TRIGGER_", // trigger token variables passed from upstream pipelines
|
||||
"CHAT_", // ChatOps variables
|
||||
}
|
||||
|
||||
func isPredefinedVar(name string) bool {
|
||||
for _, prefix := range predefinedVarPrefixes {
|
||||
if strings.HasPrefix(name, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// extractIfVars returns every variable name referenced in a rules:if: expression.
|
||||
// String literals are skipped so that dollar signs inside quoted values are
|
||||
// not mistaken for variable references.
|
||||
func extractIfVars(expr string) []string {
|
||||
var names []string
|
||||
i := 0
|
||||
for i < len(expr) {
|
||||
switch expr[i] {
|
||||
case '"', '\'':
|
||||
quote := expr[i]
|
||||
i++
|
||||
for i < len(expr) && expr[i] != quote {
|
||||
if expr[i] == '\\' {
|
||||
i++ // skip escaped character
|
||||
}
|
||||
i++
|
||||
}
|
||||
if i < len(expr) {
|
||||
i++ // consume closing quote
|
||||
}
|
||||
case '$':
|
||||
i++ // consume '$'
|
||||
if i < len(expr) && expr[i] == '{' {
|
||||
i++ // consume '{'
|
||||
start := i
|
||||
for i < len(expr) && isVarNameByte(expr[i]) {
|
||||
i++
|
||||
}
|
||||
if i < len(expr) && expr[i] == '}' && i > start {
|
||||
names = append(names, expr[start:i])
|
||||
i++ // consume '}'
|
||||
}
|
||||
} else {
|
||||
start := i
|
||||
for i < len(expr) && isVarNameByte(expr[i]) {
|
||||
i++
|
||||
}
|
||||
if i > start {
|
||||
names = append(names, expr[start:i])
|
||||
}
|
||||
}
|
||||
default:
|
||||
i++
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func isVarNameByte(b byte) bool {
|
||||
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') || b == '_'
|
||||
}
|
||||
|
||||
// checkVariableRefs warns when a rules:if: expression references a variable that
|
||||
// is not declared in pipeline variables:, the job's own variables:, or any
|
||||
// workflow:rules:variables: block. Predefined GitLab CI variables (CI_*, GITLAB_*,
|
||||
// …) are always exempt. Variables set in GitLab CI/CD project settings are
|
||||
// invisible to glint, so the finding is a WARNING rather than an error.
|
||||
func checkVariableRefs(p *model.Pipeline) []Finding {
|
||||
pipelineVars := make(map[string]bool, len(p.Variables))
|
||||
for k := range p.Variables {
|
||||
pipelineVars[k] = true
|
||||
}
|
||||
|
||||
// Union of all variables any workflow rule might inject into the context.
|
||||
workflowRuleVars := make(map[string]bool)
|
||||
if p.Workflow != nil {
|
||||
for _, rule := range p.Workflow.Rules {
|
||||
for k := range rule.Variables {
|
||||
workflowRuleVars[k] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var findings []Finding
|
||||
|
||||
// Check workflow rules:if: expressions.
|
||||
if p.Workflow != nil {
|
||||
seen := make(map[string]bool)
|
||||
for i, rule := range p.Workflow.Rules {
|
||||
if rule.If == "" {
|
||||
continue
|
||||
}
|
||||
for _, varName := range extractIfVars(rule.If) {
|
||||
if isPredefinedVar(varName) || pipelineVars[varName] || seen[varName] {
|
||||
continue
|
||||
}
|
||||
seen[varName] = true
|
||||
findings = append(findings, Finding{
|
||||
Severity: Warning,
|
||||
Rule: RuleUndeclaredVariable,
|
||||
File: p.SourceFile,
|
||||
Message: fmt.Sprintf("workflow.rules[%d].if: $%s is not declared in pipeline variables:", i, varName),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check each job's rules:if: expressions.
|
||||
for name, job := range p.Jobs {
|
||||
jobVars := make(map[string]bool, len(job.Variables))
|
||||
for k := range job.Variables {
|
||||
jobVars[k] = true
|
||||
}
|
||||
|
||||
// Deduplicate per (job, varName): report each undeclared variable once per job.
|
||||
seen := make(map[string]bool)
|
||||
for i, rule := range job.Rules {
|
||||
if rule.If == "" {
|
||||
continue
|
||||
}
|
||||
for _, varName := range extractIfVars(rule.If) {
|
||||
if isPredefinedVar(varName) || pipelineVars[varName] || jobVars[varName] || workflowRuleVars[varName] || seen[varName] {
|
||||
continue
|
||||
}
|
||||
seen[varName] = true
|
||||
findings = append(findings, Finding{
|
||||
Severity: Warning,
|
||||
Rule: RuleUndeclaredVariable,
|
||||
Job: name,
|
||||
File: job.File,
|
||||
Line: job.Line,
|
||||
Message: fmt.Sprintf("rules[%d].if: $%s is not declared in pipeline or job variables:", i, varName),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return findings
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package linter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.k3nny.fr/glint/internal/model"
|
||||
)
|
||||
|
||||
func TestExtractIfVars(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
expr string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "simple variable",
|
||||
expr: `$MY_VAR == "value"`,
|
||||
want: []string{"MY_VAR"},
|
||||
},
|
||||
{
|
||||
name: "curly brace syntax",
|
||||
expr: `${MY_VAR} != null`,
|
||||
want: []string{"MY_VAR"},
|
||||
},
|
||||
{
|
||||
name: "multiple variables",
|
||||
expr: `$BRANCH == "main" && $DEPLOY_ENV == "prod"`,
|
||||
want: []string{"BRANCH", "DEPLOY_ENV"},
|
||||
},
|
||||
{
|
||||
name: "dollar sign inside string literal is skipped",
|
||||
expr: `$REAL_VAR == "$not_a_var"`,
|
||||
want: []string{"REAL_VAR"},
|
||||
},
|
||||
{
|
||||
name: "no variables",
|
||||
expr: `"main" == "main"`,
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "regex rhs variable",
|
||||
expr: `$BRANCH =~ $PATTERN`,
|
||||
want: []string{"BRANCH", "PATTERN"},
|
||||
},
|
||||
{
|
||||
name: "multiline expression",
|
||||
expr: "$BRANCH == \"main\" ||\n$BRANCH == \"develop\"",
|
||||
want: []string{"BRANCH", "BRANCH"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := extractIfVars(tc.expr)
|
||||
if len(got) != len(tc.want) {
|
||||
t.Fatalf("extractIfVars(%q) = %v, want %v", tc.expr, got, tc.want)
|
||||
}
|
||||
for i, v := range got {
|
||||
if v != tc.want[i] {
|
||||
t.Errorf("[%d] got %q, want %q", i, v, tc.want[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPredefinedVar(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"CI_COMMIT_BRANCH", true},
|
||||
{"CI_JOB_TOKEN", true},
|
||||
{"GITLAB_USER_ID", true},
|
||||
{"GITLAB_FEATURES", true},
|
||||
{"FF_SOME_FLAG", true},
|
||||
{"RUNNER_ID", true},
|
||||
{"TRIGGER_PAYLOAD", true},
|
||||
{"CHAT_INPUT", true},
|
||||
{"MY_CUSTOM_VAR", false},
|
||||
{"DEPLOY_ENV", false},
|
||||
{"FEATURE_ENABLED", false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.input, func(t *testing.T) {
|
||||
if got := isPredefinedVar(tc.input); got != tc.want {
|
||||
t.Errorf("isPredefinedVar(%q) = %v, want %v", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckVariableRefs(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
pipeline *model.Pipeline
|
||||
wantWarnings int
|
||||
}{
|
||||
{
|
||||
name: "declared pipeline variable — no warning",
|
||||
pipeline: &model.Pipeline{
|
||||
Variables: map[string]any{"MY_VAR": "value"},
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {Rules: []model.Rule{{If: `$MY_VAR == "value"`}}},
|
||||
},
|
||||
},
|
||||
wantWarnings: 0,
|
||||
},
|
||||
{
|
||||
name: "predefined CI variable — no warning",
|
||||
pipeline: &model.Pipeline{
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {Rules: []model.Rule{{If: `$CI_COMMIT_BRANCH == "main"`}}},
|
||||
},
|
||||
},
|
||||
wantWarnings: 0,
|
||||
},
|
||||
{
|
||||
name: "undeclared variable — one warning",
|
||||
pipeline: &model.Pipeline{
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {Rules: []model.Rule{{If: `$UNDEFINED_VAR == "yes"`}}},
|
||||
},
|
||||
},
|
||||
wantWarnings: 1,
|
||||
},
|
||||
{
|
||||
name: "same undeclared var in multiple rules — one warning per job",
|
||||
pipeline: &model.Pipeline{
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {
|
||||
Rules: []model.Rule{
|
||||
{If: `$UNDEFINED_VAR == "yes"`},
|
||||
{If: `$UNDEFINED_VAR == "no"`},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantWarnings: 1,
|
||||
},
|
||||
{
|
||||
name: "job-level variable — no warning",
|
||||
pipeline: &model.Pipeline{
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {
|
||||
Variables: map[string]any{"LOCAL_VAR": "value"},
|
||||
Rules: []model.Rule{{If: `$LOCAL_VAR == "value"`}},
|
||||
},
|
||||
},
|
||||
},
|
||||
wantWarnings: 0,
|
||||
},
|
||||
{
|
||||
name: "workflow rule variable available to job rules — no warning",
|
||||
pipeline: &model.Pipeline{
|
||||
Workflow: &model.Workflow{
|
||||
Rules: []model.Rule{
|
||||
{
|
||||
If: `$CI_COMMIT_BRANCH == "main"`,
|
||||
Variables: map[string]any{"DEPLOY_ENV": "production"},
|
||||
},
|
||||
},
|
||||
},
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {Rules: []model.Rule{{If: `$DEPLOY_ENV == "production"`}}},
|
||||
},
|
||||
},
|
||||
wantWarnings: 0,
|
||||
},
|
||||
{
|
||||
name: "undeclared variable in workflow rules:if",
|
||||
pipeline: &model.Pipeline{
|
||||
Workflow: &model.Workflow{
|
||||
Rules: []model.Rule{{If: `$UNDECLARED == "main"`}},
|
||||
},
|
||||
Jobs: map[string]model.Job{},
|
||||
},
|
||||
wantWarnings: 1,
|
||||
},
|
||||
{
|
||||
name: "two jobs each with a different undeclared variable — two warnings",
|
||||
pipeline: &model.Pipeline{
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {Rules: []model.Rule{{If: `$UNDEF_A == "x"`}}},
|
||||
"job-b": {Rules: []model.Rule{{If: `$UNDEF_B == "y"`}}},
|
||||
},
|
||||
},
|
||||
wantWarnings: 2,
|
||||
},
|
||||
{
|
||||
name: "no rules — no warnings",
|
||||
pipeline: &model.Pipeline{
|
||||
Jobs: map[string]model.Job{
|
||||
"job-a": {Script: "echo ok"},
|
||||
},
|
||||
},
|
||||
wantWarnings: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
findings := checkVariableRefs(tc.pipeline)
|
||||
count := 0
|
||||
for _, f := range findings {
|
||||
if f.Severity == Warning && f.Rule == RuleUndeclaredVariable {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != tc.wantWarnings {
|
||||
t.Errorf("got %d GL032 warnings, want %d; findings: %v", count, tc.wantWarnings, findings)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -13,14 +13,22 @@ func Parse(path string) (*Pipeline, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading file: %w", err)
|
||||
}
|
||||
return ParseBytes(data)
|
||||
p, err := ParseBytes(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.SourceFile = path
|
||||
p.SetJobOrigin(path)
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// ParseBytes parses YAML from an in-memory byte slice.
|
||||
func ParseBytes(data []byte) (*Pipeline, error) {
|
||||
// First pass: decode into a raw map to extract job keys.
|
||||
var raw map[string]yaml.Node
|
||||
if err := yaml.Unmarshal(data, &raw); err != nil {
|
||||
// First pass: parse into a yaml.Node document to extract job keys with
|
||||
// their exact source line numbers (key nodes carry the line, value nodes
|
||||
// carry the body we decode into Job / map[string]any).
|
||||
var doc yaml.Node
|
||||
if err := yaml.Unmarshal(data, &doc); err != nil {
|
||||
return nil, fmt.Errorf("parsing YAML: %w", err)
|
||||
}
|
||||
|
||||
@@ -32,21 +40,36 @@ func ParseBytes(data []byte) (*Pipeline, error) {
|
||||
|
||||
p.Jobs = make(map[string]Job)
|
||||
p.RawJobs = make(map[string]map[string]any)
|
||||
for key, node := range raw {
|
||||
|
||||
if doc.Kind != yaml.DocumentNode || len(doc.Content) == 0 {
|
||||
return p, nil
|
||||
}
|
||||
root := doc.Content[0]
|
||||
if root.Kind != yaml.MappingNode {
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Walk root mapping in key/value pairs.
|
||||
for i := 0; i+1 < len(root.Content); i += 2 {
|
||||
keyNode := root.Content[i]
|
||||
valNode := root.Content[i+1]
|
||||
key := keyNode.Value
|
||||
if ReservedKeys[key] {
|
||||
continue
|
||||
}
|
||||
|
||||
var rawMap map[string]any
|
||||
if err := node.Decode(&rawMap); err != nil {
|
||||
if err := valNode.Decode(&rawMap); err != nil {
|
||||
return nil, fmt.Errorf("parsing raw job %q: %w", key, err)
|
||||
}
|
||||
p.RawJobs[key] = rawMap
|
||||
|
||||
var j Job
|
||||
if err := node.Decode(&j); err != nil {
|
||||
if err := valNode.Decode(&j); err != nil {
|
||||
return nil, fmt.Errorf("parsing job %q: %w", key, err)
|
||||
}
|
||||
j.Name = key
|
||||
j.Line = keyNode.Line // exact line of the job name key
|
||||
p.Jobs[key] = j
|
||||
}
|
||||
|
||||
|
||||
+32
-17
@@ -3,20 +3,32 @@ package model
|
||||
// Pipeline represents the top-level structure of a .gitlab-ci.yml file.
|
||||
// Unknown top-level keys are collected into Jobs.
|
||||
type Pipeline struct {
|
||||
Stages []string `yaml:"stages"`
|
||||
Variables map[string]string `yaml:"variables"`
|
||||
Default *DefaultConfig `yaml:"default"`
|
||||
Include []any `yaml:"include"`
|
||||
Workflow *Workflow `yaml:"workflow"`
|
||||
SourceFile string // path of the root pipeline file; set by Parse
|
||||
Stages []string `yaml:"stages"`
|
||||
Variables map[string]any `yaml:"variables"` // string or {value,description,options} map
|
||||
Default *DefaultConfig `yaml:"default"`
|
||||
Include []any `yaml:"include"`
|
||||
Workflow *Workflow `yaml:"workflow"`
|
||||
// Jobs holds every non-reserved top-level key (i.e. job definitions).
|
||||
Jobs map[string]Job `yaml:"-"`
|
||||
RawJobs map[string]map[string]any `yaml:"-"` // pre-resolution raw maps, used by the resolver
|
||||
Jobs map[string]Job `yaml:"-"`
|
||||
RawJobs map[string]map[string]any `yaml:"-"` // pre-resolution raw maps, used by the resolver
|
||||
}
|
||||
|
||||
// SetJobOrigin sets the File field on all jobs that don't already have one.
|
||||
// Called after ParseBytes to record which file each job came from.
|
||||
func (p *Pipeline) SetJobOrigin(file string) {
|
||||
for name, j := range p.Jobs {
|
||||
if j.File == "" {
|
||||
j.File = file
|
||||
}
|
||||
p.Jobs[name] = j
|
||||
}
|
||||
}
|
||||
|
||||
type DefaultConfig struct {
|
||||
Image string `yaml:"image"`
|
||||
BeforeScript []string `yaml:"before_script"`
|
||||
AfterScript []string `yaml:"after_script"`
|
||||
Image any `yaml:"image"` // string or {name,pull_policy,...} map
|
||||
BeforeScript any `yaml:"before_script"` // []string or string (block scalar)
|
||||
AfterScript any `yaml:"after_script"` // []string or string
|
||||
Cache any `yaml:"cache"`
|
||||
Artifacts any `yaml:"artifacts"`
|
||||
Retry any `yaml:"retry"`
|
||||
@@ -29,7 +41,9 @@ type Workflow struct {
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
Name string // set by parser, not from YAML
|
||||
Name string // set by parser, not from YAML
|
||||
File string // source file; set by Parse / resolver
|
||||
Line int // line of the job key in its source file; set by parser
|
||||
Stage string `yaml:"stage"`
|
||||
Script any `yaml:"script"` // []string or string (block scalar)
|
||||
Run any `yaml:"run"` // alternative to script (CI steps)
|
||||
@@ -37,8 +51,8 @@ type Job struct {
|
||||
AfterScript any `yaml:"after_script"` // []string or string
|
||||
Image any `yaml:"image"`
|
||||
Services []any `yaml:"services"`
|
||||
Variables map[string]string `yaml:"variables"`
|
||||
Rules []Rule `yaml:"rules"`
|
||||
Variables map[string]any `yaml:"variables"` // string or {value,description,options} map
|
||||
Rules []Rule `yaml:"rules"`
|
||||
Only any `yaml:"only"`
|
||||
Except any `yaml:"except"`
|
||||
Needs []any `yaml:"needs"`
|
||||
@@ -66,10 +80,11 @@ type Job struct {
|
||||
}
|
||||
|
||||
type Rule struct {
|
||||
If string `yaml:"if"`
|
||||
When string `yaml:"when"`
|
||||
Changes []string `yaml:"changes"`
|
||||
Exists []string `yaml:"exists"`
|
||||
If string `yaml:"if"`
|
||||
When string `yaml:"when"`
|
||||
Changes any `yaml:"changes"` // []string or {paths,compare_to} map
|
||||
Exists any `yaml:"exists"` // []string or map form
|
||||
Variables map[string]any `yaml:"variables"` // set/override variables when rule matches (GitLab CI 15.0+)
|
||||
}
|
||||
|
||||
// ReservedKeys are top-level GitLab CI keys that are NOT job definitions.
|
||||
|
||||
@@ -90,7 +90,14 @@ func resolveIncludes(p *model.Pipeline, includes []any, cfg fetcher.GitLabConfig
|
||||
continue
|
||||
}
|
||||
|
||||
// remote, template — resolved by GitLab at runtime, skip silently.
|
||||
if remote, _ := entry["remote"].(string); remote != "" {
|
||||
w, ew := resolveRemoteInclude(p, remote, cfg, rootDir, visited)
|
||||
warnings = append(warnings, w...)
|
||||
extWarnings = append(extWarnings, ew...)
|
||||
continue
|
||||
}
|
||||
|
||||
// template — resolved by GitLab at runtime, skip silently.
|
||||
}
|
||||
return warnings, extWarnings
|
||||
}
|
||||
@@ -117,6 +124,7 @@ func resolveLocalInclude(p *model.Pipeline, rawPath string, cfg fetcher.GitLabCo
|
||||
if err != nil {
|
||||
return []IncludeWarning{{Label: label, Err: fmt.Errorf("parsing YAML: %w", err)}}, nil
|
||||
}
|
||||
included.SetJobOrigin(absPath)
|
||||
|
||||
// Recursively resolve the included file's own includes first, merging
|
||||
// everything into `included` before we merge it into the parent `p`.
|
||||
@@ -132,6 +140,39 @@ func resolveLocalInclude(p *model.Pipeline, rawPath string, cfg fetcher.GitLabCo
|
||||
return warnings, extWarnings
|
||||
}
|
||||
|
||||
// resolveRemoteInclude fetches a plain HTTPS URL, parses it as CI YAML, and
|
||||
// merges it into p. Sub-includes of the fetched file are resolved recursively.
|
||||
func resolveRemoteInclude(p *model.Pipeline, rawURL string, cfg fetcher.GitLabConfig, rootDir string, visited map[string]bool) ([]IncludeWarning, []ExtendWarning) {
|
||||
label := "remote " + rawURL
|
||||
|
||||
if visited[rawURL] {
|
||||
return nil, nil
|
||||
}
|
||||
visited[rawURL] = true
|
||||
|
||||
data, err := fetcher.FetchURL(rawURL)
|
||||
if err != nil {
|
||||
return []IncludeWarning{{Label: label, Err: err}}, nil
|
||||
}
|
||||
|
||||
included, err := model.ParseBytes(data)
|
||||
if err != nil {
|
||||
return []IncludeWarning{{Label: label, Err: fmt.Errorf("parsing YAML: %w", err)}}, nil
|
||||
}
|
||||
included.SetJobOrigin(rawURL)
|
||||
|
||||
var warnings []IncludeWarning
|
||||
var extWarnings []ExtendWarning
|
||||
if len(included.Include) > 0 {
|
||||
w, ew := resolveIncludes(included, included.Include, cfg, rootDir, visited)
|
||||
warnings = append(warnings, w...)
|
||||
extWarnings = append(extWarnings, ew...)
|
||||
}
|
||||
|
||||
mergeIncluded(p, included)
|
||||
return warnings, extWarnings
|
||||
}
|
||||
|
||||
// resolveProjectInclude fetches all files listed under a single project: entry
|
||||
// and merges them into p.
|
||||
func resolveProjectInclude(p *model.Pipeline, entry map[string]any, project string, cfg fetcher.GitLabConfig, rootDir string, visited map[string]bool) ([]IncludeWarning, []ExtendWarning) {
|
||||
@@ -161,6 +202,7 @@ func resolveProjectInclude(p *model.Pipeline, entry map[string]any, project stri
|
||||
warnings = append(warnings, IncludeWarning{Label: label, Err: fmt.Errorf("parsing YAML: %w", err)})
|
||||
continue
|
||||
}
|
||||
included.SetJobOrigin(label)
|
||||
|
||||
if len(included.Include) > 0 {
|
||||
w, ew := resolveIncludes(included, included.Include, cfg, rootDir, visited)
|
||||
@@ -200,6 +242,7 @@ func resolveComponentInclude(p *model.Pipeline, ref string, cfg fetcher.GitLabCo
|
||||
if err != nil {
|
||||
return IncludeWarning{Label: label, Err: fmt.Errorf("parsing component YAML: %w", err)}, nil, true
|
||||
}
|
||||
included.SetJobOrigin(label)
|
||||
|
||||
var extWarnings []ExtendWarning
|
||||
if len(included.Include) > 0 {
|
||||
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
---
|
||||
# Exercises include: remote: URL fetching and sub-include recursion.
|
||||
# The remote file is a real public GitLab CI template; it may contain its own
|
||||
# includes which should also be resolved. Exit code is 0 (warnings allowed).
|
||||
stages:
|
||||
- test
|
||||
|
||||
include:
|
||||
- remote: https://gitlab.com/gitlab-org/gitlab/-/raw/master/lib/gitlab/ci/templates/Bash.gitlab-ci.yml
|
||||
|
||||
local-job:
|
||||
stage: test
|
||||
script:
|
||||
- echo "local job alongside remote include"
|
||||
Vendored
+9
@@ -38,3 +38,12 @@ missing-needs-job:
|
||||
- echo "bad"
|
||||
needs:
|
||||
- nonexistent-job # ERROR: job doesn't exist
|
||||
|
||||
# optional: true — missing dep should be WARNING not ERROR
|
||||
optional-needs-job:
|
||||
stage: build
|
||||
script:
|
||||
- echo "I depend on something that may not exist"
|
||||
needs:
|
||||
- job: nonexistent-optional-job
|
||||
optional: true
|
||||
|
||||
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
|
||||
Vendored
+87
@@ -0,0 +1,87 @@
|
||||
---
|
||||
# Exercises multi-line script patterns and extended variable declarations.
|
||||
# Ref: https://docs.gitlab.com/ci/yaml/script/#split-long-commands
|
||||
# Ref: https://docs.gitlab.com/ee/ci/yaml/#variablesdescription
|
||||
# All patterns here must parse cleanly (exit 0).
|
||||
|
||||
stages:
|
||||
- build
|
||||
- test
|
||||
- deploy
|
||||
|
||||
# Pipeline-level variables: plain strings and extended {value, description} map form.
|
||||
variables:
|
||||
PLAIN_VAR: "hello"
|
||||
DEPLOY_ENV:
|
||||
value: "staging"
|
||||
description: "The deployment target. Set to staging or production."
|
||||
RETRIES:
|
||||
value: "3"
|
||||
description: "Number of retry attempts."
|
||||
options:
|
||||
- "1"
|
||||
- "3"
|
||||
- "5"
|
||||
|
||||
default:
|
||||
# image in map form (name + pull_policy)
|
||||
image:
|
||||
name: alpine:latest
|
||||
pull_policy: if-not-present
|
||||
# before_script as a block scalar (not a list)
|
||||
before_script:
|
||||
- apk add --no-cache curl git
|
||||
|
||||
build-literal-block:
|
||||
stage: build
|
||||
# script items using literal block scalar (|)
|
||||
script:
|
||||
- |
|
||||
if [[ "$DEPLOY_ENV" == "production" ]]; then
|
||||
echo "Production build"
|
||||
else
|
||||
echo "Non-production build"
|
||||
fi
|
||||
- echo "Build step done"
|
||||
|
||||
build-folded-block:
|
||||
stage: build
|
||||
# script items using folded block scalar (>)
|
||||
script:
|
||||
- >
|
||||
apt-get update -qq &&
|
||||
apt-get install -y curl wget
|
||||
- echo "Packages installed"
|
||||
before_script:
|
||||
- |
|
||||
echo "Job-level before_script"
|
||||
echo "Using literal block scalar"
|
||||
|
||||
test-job:
|
||||
stage: test
|
||||
script:
|
||||
- echo "Running tests"
|
||||
- |
|
||||
set -e
|
||||
go test ./...
|
||||
echo "Tests passed"
|
||||
# Job-level variable with extended form
|
||||
variables:
|
||||
TEST_FLAG:
|
||||
value: "true"
|
||||
description: "Enable verbose test output"
|
||||
# rules.changes in map form (GitLab 15.3+)
|
||||
rules:
|
||||
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
|
||||
changes:
|
||||
paths:
|
||||
- "**/*.go"
|
||||
compare_to: "main"
|
||||
when: on_success
|
||||
- when: on_success
|
||||
|
||||
deploy-job:
|
||||
stage: deploy
|
||||
script:
|
||||
- echo "Deploying to $DEPLOY_ENV"
|
||||
when: manual
|
||||
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
---
|
||||
# variable_refs.yml
|
||||
# Verifies that GL032 does not fire for variables that are declared or predefined.
|
||||
# Covers: pipeline variables:, job variables:, workflow:rules:variables:, and CI_* prefixes.
|
||||
# Expected: exits 0 (no errors; no GL032 warnings).
|
||||
|
||||
stages:
|
||||
- build
|
||||
- deploy
|
||||
|
||||
variables:
|
||||
DEPLOY_TARGET: "staging"
|
||||
FEATURE_FLAG: "false"
|
||||
|
||||
workflow:
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "main"'
|
||||
variables:
|
||||
DEPLOY_TARGET: "production"
|
||||
- when: always
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script: make build
|
||||
rules:
|
||||
# pipeline-level variable — GL032 must not fire
|
||||
- if: '$DEPLOY_TARGET == "staging"'
|
||||
when: on_success
|
||||
# predefined CI variable — GL032 must not fire
|
||||
- if: '$CI_COMMIT_BRANCH =~ /^feat\/.+/'
|
||||
when: manual
|
||||
|
||||
deploy:
|
||||
stage: deploy
|
||||
script: make deploy
|
||||
variables:
|
||||
DEPLOY_REGION: "us-east-1"
|
||||
rules:
|
||||
# pipeline-level variable — no warning
|
||||
- if: '$FEATURE_FLAG == "true"'
|
||||
when: never
|
||||
# workflow-rule-injected variable — no warning
|
||||
- if: '$DEPLOY_TARGET == "production"'
|
||||
when: on_success
|
||||
# job-level variable — no warning
|
||||
- if: '$DEPLOY_REGION == "us-east-1"'
|
||||
when: on_success
|
||||
# predefined GITLAB_ variable — no warning
|
||||
- if: '$GITLAB_USER_LOGIN == "bot"'
|
||||
when: never
|
||||
- when: manual
|
||||
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
---
|
||||
# workflow_vars.yml
|
||||
# Exercises workflow:rules:variables: injection.
|
||||
# The matching workflow rule sets DEPLOY_TARGET; job rules use it.
|
||||
#
|
||||
# Expected behaviour per context:
|
||||
# (no context) → all jobs active (no context evaluation)
|
||||
# --branch main → deploy-prod active, deploy-staging skipped
|
||||
# --branch develop → deploy-prod skipped, deploy-staging active
|
||||
# --branch feat/x → deploy-prod skipped, deploy-staging skipped (manual)
|
||||
|
||||
stages:
|
||||
- build
|
||||
- deploy
|
||||
|
||||
variables:
|
||||
DEPLOY_TARGET:
|
||||
value: ""
|
||||
description: "Deployment target — set by workflow rules"
|
||||
|
||||
workflow:
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "main"'
|
||||
variables:
|
||||
DEPLOY_TARGET: production
|
||||
- if: '$CI_COMMIT_BRANCH == "develop"'
|
||||
variables:
|
||||
DEPLOY_TARGET: staging
|
||||
- when: always
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script: make build
|
||||
rules:
|
||||
- when: always
|
||||
|
||||
deploy-prod:
|
||||
stage: deploy
|
||||
script: make deploy ENV=production
|
||||
rules:
|
||||
- if: '$DEPLOY_TARGET == "production"'
|
||||
when: on_success
|
||||
- when: never
|
||||
|
||||
deploy-staging:
|
||||
stage: deploy
|
||||
script: make deploy ENV=staging
|
||||
rules:
|
||||
- if: '$DEPLOY_TARGET == "staging"'
|
||||
when: on_success
|
||||
- when: never
|
||||
Reference in New Issue
Block a user