diff --git a/tests/workflows/fixtures/workflow-linter/pin-fail-branch-quoted-with-comment/workflow.yml b/tests/workflows/fixtures/workflow-linter/pin-fail-branch-quoted-with-comment/workflow.yml new file mode 100644 index 0000000..083aabd --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/pin-fail-branch-quoted-with-comment/workflow.yml @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: + a: + uses: "org/repo/.github/workflows/x.yml@main" # 0123456789abcdef0123456789abcdef01234567 diff --git a/tests/workflows/fixtures/workflow-linter/pin-fail-flow-style/workflow.yml b/tests/workflows/fixtures/workflow-linter/pin-fail-flow-style/workflow.yml new file mode 100644 index 0000000..e5d3404 --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/pin-fail-flow-style/workflow.yml @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: {a: {uses: "org/repo/.github/workflows/x.yml@main"}} diff --git a/tests/workflows/fixtures/workflow-linter/pin-fail-short-sha/workflow.yml b/tests/workflows/fixtures/workflow-linter/pin-fail-short-sha/workflow.yml new file mode 100644 index 0000000..b38b714 --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/pin-fail-short-sha/workflow.yml @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: + a: + uses: org/repo/.github/workflows/x.yml@0123456789abcdef diff --git a/tests/workflows/fixtures/workflow-linter/pin-fail-tag/workflow.yml b/tests/workflows/fixtures/workflow-linter/pin-fail-tag/workflow.yml new file mode 100644 index 0000000..f566ce3 --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/pin-fail-tag/workflow.yml @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: + a: + uses: org/repo/.github/workflows/x.yml@v1 diff --git a/tests/workflows/fixtures/workflow-linter/pin-fail-unparseable-yaml/workflow.yml b/tests/workflows/fixtures/workflow-linter/pin-fail-unparseable-yaml/workflow.yml new file mode 100644 index 0000000..1c6b5bf --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/pin-fail-unparseable-yaml/workflow.yml @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: + a: [unclosed diff --git a/tests/workflows/fixtures/workflow-linter/pin-fail-uppercase-hex/workflow.yml b/tests/workflows/fixtures/workflow-linter/pin-fail-uppercase-hex/workflow.yml new file mode 100644 index 0000000..512f224 --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/pin-fail-uppercase-hex/workflow.yml @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: + a: + uses: org/repo/.github/workflows/x.yml@0123456789ABCDEF0123456789ABCDEF01234567 diff --git a/tests/workflows/fixtures/workflow-linter/pin-pass-sha-and-local/workflow.yml b/tests/workflows/fixtures/workflow-linter/pin-pass-sha-and-local/workflow.yml new file mode 100644 index 0000000..2d5a9e4 --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/pin-pass-sha-and-local/workflow.yml @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: + a: + uses: org/repo/.github/workflows/x.yml@0123456789abcdef0123456789abcdef01234567 + b: + uses: ./.github/workflows/local.yml + c: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7.0.1 diff --git a/tests/workflows/fixtures/workflow-linter/spdx-fail-identifier-after-code/workflow.yml b/tests/workflows/fixtures/workflow-linter/spdx-fail-identifier-after-code/workflow.yml new file mode 100644 index 0000000..ad3758b --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/spdx-fail-identifier-after-code/workflow.yml @@ -0,0 +1,6 @@ +name: t +# SPDX-License-Identifier: MPL-2.0 +on: push +permissions: + contents: read +jobs: {} diff --git a/tests/workflows/fixtures/workflow-linter/spdx-fail-no-identifier/workflow.yml b/tests/workflows/fixtures/workflow-linter/spdx-fail-no-identifier/workflow.yml new file mode 100644 index 0000000..b510475 --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/spdx-fail-no-identifier/workflow.yml @@ -0,0 +1,7 @@ +# This workflow is managed by gh actions-lock. +# just a comment +name: t +on: push +permissions: + contents: read +jobs: {} diff --git a/tests/workflows/fixtures/workflow-linter/spdx-pass-after-lock-header/workflow.yml b/tests/workflows/fixtures/workflow-linter/spdx-pass-after-lock-header/workflow.yml new file mode 100644 index 0000000..3e485ec --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/spdx-pass-after-lock-header/workflow.yml @@ -0,0 +1,8 @@ +# This workflow is managed by gh actions-lock. +# SPDX-License-Identifier: MPL-2.0 +# This workflow is managed by gh actions-lock. +name: t +on: push +permissions: + contents: read +jobs: {} diff --git a/tests/workflows/fixtures/workflow-linter/spdx-pass-line1/workflow.yml b/tests/workflows/fixtures/workflow-linter/spdx-pass-line1/workflow.yml new file mode 100644 index 0000000..6c568fe --- /dev/null +++ b/tests/workflows/fixtures/workflow-linter/spdx-pass-line1/workflow.yml @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: MPL-2.0 +name: t +on: push +permissions: + contents: read +jobs: {} diff --git a/tests/workflows/workflow_linter_test.sh b/tests/workflows/workflow_linter_test.sh new file mode 100755 index 0000000..749773d --- /dev/null +++ b/tests/workflows/workflow_linter_test.sh @@ -0,0 +1,151 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# +# Test: workflow-linter.yml firing and silence. +# +# Runs the real `run:` text of two steps of .github/workflows/workflow-linter.yml +# against fixture workflows and checks that each step +# * fires (exits non-zero and names the problem) on a bad workflow, and +# * stays silent (exits zero) on a good one. +# The step text is extracted from the workflow file at run time, so a change to +# the workflow is what is tested, not a copy of it. +# +# Steps covered: +# "Check SPDX Headers" (the header comment block is read, not line 1) +# "Check SHA-Pinned Actions" (reusable workflow calls must end in @<40 hex>; +# its `gh` calls are stubbed, no network is used) +# +# Fixture cases live in tests/workflows/fixtures/workflow-linter//workflow.yml. +# A case name starting with "spdx-" is run against the SPDX step, one starting +# with "pin-" against the pinning step; "-pass-" cases must exit 0, "-fail-" +# cases must exit non-zero. One extra case runs the pinning step with no `yq` on +# PATH and expects it to fail closed. +# +# Usage: tests/workflows/workflow_linter_test.sh [path/to/workflow-linter.yml] +# Needs: bash, awk, grep, and (for the pin- cases) yq on PATH. + +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +WORKFLOW="${1:-$HERE/../../.github/workflows/workflow-linter.yml}" +FIXTURES="$HERE/fixtures/workflow-linter" + +[ -f "$WORKFLOW" ] || { echo "workflow not found: $WORKFLOW" >&2; exit 2; } +[ -d "$FIXTURES" ] || { echo "fixtures not found: $FIXTURES" >&2; exit 2; } + +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +# Print the `run: |` block of the step whose name is $2 in workflow file $1. +extract_step() { + awk -v want="$2" ' + function indent(s) { match(s, /^ */); return RLENGTH } + !inrun && $0 ~ /^[[:space:]]*- name:/ { + n = $0; sub(/^[[:space:]]*- name:[[:space:]]*/, "", n) + gsub(/^["'\'']|["'\'']$/, "", n) + found = (n == want); next + } + found && !inrun && $0 ~ /^[[:space:]]+run:[[:space:]]*\|[[:space:]]*$/ { + inrun = 1; base = -1; next + } + inrun { + if ($0 ~ /^[[:space:]]*$/) { print ""; next } + if (base < 0) base = indent($0) + if (indent($0) < base) exit + print substr($0, base + 1) + } + ' "$1" +} + +extract_step "$WORKFLOW" "Check SPDX Headers" > "$TMP/spdx.sh" +extract_step "$WORKFLOW" "Check SHA-Pinned Actions" > "$TMP/pin.sh" +for s in spdx pin; do + [ -s "$TMP/$s.sh" ] || { echo "could not extract the $s step from $WORKFLOW" >&2; exit 2; } +done + +# `gh` stub: the pinning step installs and runs gh-actions-lock before its own +# check. Those calls are out of scope here and must not touch the network. +mkdir -p "$TMP/stub" +printf '#!/bin/sh\nexit 0\n' > "$TMP/stub/gh" +chmod +x "$TMP/stub/gh" + +BASH_BIN="$(command -v bash)" +PASS=0 +FAIL=0 + +# run_case