diff --git a/scripts/check-lock-sync.sh b/scripts/check-lock-sync.sh index bd51fa4..82dfd9a 100755 --- a/scripts/check-lock-sync.sh +++ b/scripts/check-lock-sync.sh @@ -249,10 +249,19 @@ FILENAME == lockfile { } # ---------- pass 2: the workflow YAML ---------- -FNR == 1 { wf = FILENAME } +FNR == 1 { wf = FILENAME; stepind = -1 } { line = $0 sub(/[[:space:]]+#.*$/, "", line) # strip trailing comment + # Track whether this line sits inside a job's `steps:` block, by + # indentation: a `uses:` there is a step action, whatever its path looks + # like (an action directory may be named tool.yml). Only a `uses:` outside + # every `steps:` block is a job-level reusable-workflow call. + if (line ~ /[^[:space:]]/) { + match(line, /^[[:space:]]*/); ind = RLENGTH + if (stepind >= 0 && ind <= stepind) stepind = -1 + if (line ~ /^[[:space:]]*steps:/) stepind = ind + } if (line ~ /^[[:space:]]*-?[[:space:]]*uses:[[:space:]]*.+$/) { raw = line sub(/^[[:space:]]*-?[[:space:]]*uses:[[:space:]]*/, "", raw) @@ -264,7 +273,7 @@ FNR == 1 { wf = FILENAME } # Clause 5's exception needs to know HOW a ref is used: as the target of a # job-level reusable-workflow call, or as an action. if (n != "") { - if (raw ~ /\/\.github\/workflows\/[^\/@]+\.ya?ml@/) reusetgt[ck(n)] = 1 + if (stepind < 0 && line !~ /^[[:space:]]*-/ && raw ~ /\/\.github\/workflows\/[^\/@]+\.ya?ml@/) reusetgt[ck(n)] = 1 else actiontgt[ck(n)] = 1 } } @@ -374,7 +383,11 @@ END { k = ck(s) # Only the target of job-level reusable calls, never an action and never a # nested uses: of another record (see the header, clause 5). - if ((k in reusetgt) && !(k in actiontgt) && index(wantsrc[k], " dependencies:") == 0) continue + # The record's key must itself be pinned to that same SHA: a mutable key + # (@v1) or a key at a different SHA is not "its own SHA". + keyref = substr(s, index(s, "@") + 1) + if (fullsha(keyref) && tolower(keyref) == tolower(selfsha[s]) && + (k in reusetgt) && !(k in actiontgt) && index(wantsrc[k], " dependencies:") == 0) continue nself++ selflist = selflist sprintf("\n %s (ref: %s)", s, selfsha[s]) } diff --git a/tests/workflows/check_lock_sync_test.sh b/tests/workflows/check_lock_sync_test.sh index 16349db..51fb55f 100644 --- a/tests/workflows/check_lock_sync_test.sh +++ b/tests/workflows/check_lock_sync_test.sh @@ -363,3 +363,61 @@ sha_key sed -i "s#Owner/Repo/.github/workflows/check.yml@#Owner/Repo/.github/actions/check@#" "$work/current/ci.yml" self_ref expect 1 'SELF-SHA REFS' 'a path outside .github/workflows is not a reusable workflow' + +# The exception needs the key itself pinned to the same full SHA as ref:. +fresh +reusable_only +self_ref +expect 1 'SELF-SHA REFS' 'a mutable key (@v1) with a SHA ref is not covered by the exception' +fresh +reusable_only +sha_key +sed -i "s/ref: 'v1'/ref: '2222222222222222222222222222222222222222'/" "$work/current/actions.lock" +expect 1 'SELF-SHA REFS' 'a SHA key whose ref names a different SHA is not covered' + +# Job-level vs step position is read from the YAML, not inferred from the +# path: a step action may live in a directory named like a workflow file. +fresh +cat > "$work/current/ci.yml" <<'YML' +name: Test +on: pull_request +jobs: + local: + runs-on: ubuntu-latest + steps: + - uses: Owner/Repo/.github/workflows/tool.yml@v1 +YML +sha_key +self_ref +expect 1 'SELF-SHA REFS' 'a step action with a workflow-shaped path is not a reusable call' +fresh +cat > "$work/current/ci.yml" <<'YML' +name: Test +on: pull_request +jobs: + local: + runs-on: ubuntu-latest + steps: + - name: build + uses: Owner/Repo/.github/workflows/tool.yml@v1 + reusable: + uses: Owner/Repo/.github/workflows/check.yml@v1 +YML +sha_key +self_ref +expect 1 'SELF-SHA REFS' 'a non-first step key with a workflow-shaped path still counts as an action' +fresh +cat > "$work/current/ci.yml" <<'YML' +name: Test +on: pull_request +jobs: + local: + runs-on: ubuntu-latest + steps: + - run: echo build + reusable: + uses: Owner/Repo/.github/workflows/check.yml@v1 +YML +sha_key +self_ref +expect 0 'OK' 'a job-level call after another job with steps is still a reusable call'