diff --git a/scripts/check-required-contexts.sh b/scripts/check-required-contexts.sh index 0a52d5c..41ffe16 100644 --- a/scripts/check-required-contexts.sh +++ b/scripts/check-required-contexts.sh @@ -157,6 +157,12 @@ function localTargetError(uses, fullWf) { : onBlock && typeof onBlock === "object" ? "workflow_call" in onBlock : false; if (!callable) return `'${uses}' does not declare on.workflow_call, so GitHub cannot run it as a reusable workflow`; + // The caller passes with.context, and GitHub refuses an input the called workflow does not + // declare, so the target must declare on.workflow_call.inputs.context as a string input. + const call = onBlock && typeof onBlock === "object" && !Array.isArray(onBlock) ? onBlock.workflow_call : undefined; + const ctxInput = call && typeof call === "object" ? call.inputs?.context : undefined; + if (!ctxInput || typeof ctxInput !== "object" || ctxInput.type !== "string") + return `'${uses}' does not declare on.workflow_call.inputs.context with type: string, so GitHub refuses the caller's with.context`; return null; } diff --git a/tests/workflows/squabble_gate_test.sh b/tests/workflows/squabble_gate_test.sh index c75d0d4..d18eedb 100644 --- a/tests/workflows/squabble_gate_test.sh +++ b/tests/workflows/squabble_gate_test.sh @@ -343,12 +343,27 @@ fresh_local() { # fresh_local fi } LOCAL_TARGET='./.github/workflows/ci-gate.yml' +GOOD_CALL=$'on:\n workflow_call:\n inputs:\n context: {type: string}' +fresh_local "$LOCAL_TARGET" "$GOOD_CALL" +assert_rc 0 'status mapping: a separate caller whose local target declares workflow_call with a string context input passes' check_status +fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:\n inputs:\n context: {type: string, required: true}\n workflow_dispatch:' +assert_rc 0 'status mapping: a required string context input beside another trigger passes' check_status +# The caller passes with.context; GitHub refuses an input the called workflow does not declare. fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:' -assert_rc 0 'status mapping: a separate caller whose local target declares workflow_call passes' check_status -fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:\n inputs:\n context: {type: string}' -assert_rc 0 'status mapping: a local target with workflow_call inputs passes' check_status +assert_rc 1 'status mapping: a local target whose workflow_call declares no inputs fails' check_status +fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:' +check_status >"$WORK/status-msg" 2>&1 || true +assert_rc 0 'status mapping message: names the missing context input' grep -qF 'inputs.context with type: string' "$WORK/status-msg" fresh_local "$LOCAL_TARGET" 'on: [workflow_call]' -assert_rc 0 'status mapping: a local target with a workflow_call list passes' check_status +assert_rc 1 'status mapping: a workflow_call list (which cannot declare inputs) fails' check_status +fresh_local "$LOCAL_TARGET" 'on: workflow_call' +assert_rc 1 'status mapping: a bare workflow_call string (which cannot declare inputs) fails' check_status +fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:\n inputs:\n other: {type: string}' +assert_rc 1 'status mapping: inputs without context fails' check_status +fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:\n inputs:\n context: {type: number}' +assert_rc 1 'status mapping: a non-string context input fails' check_status +fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:\n inputs:\n context:' +assert_rc 1 'status mapping: a context input with no type fails' check_status fresh_local "$LOCAL_TARGET" $'on:\n push:' assert_rc 1 'status mapping: a local target without workflow_call fails' check_status fresh_local "$LOCAL_TARGET" $'on:\n push:' @@ -359,14 +374,14 @@ assert_rc 1 'status mapping: a missing local target file fails' check_status fresh_local "$LOCAL_TARGET" - check_status >"$WORK/status-msg" 2>&1 || true assert_rc 0 'status mapping message: names the missing target file' grep -qF 'names a file that does not exist' "$WORK/status-msg" -fresh_local './.github/workflows/../workflows/ci-gate.yml' $'on:\n workflow_call:' +fresh_local './.github/workflows/../workflows/ci-gate.yml' "$GOOD_CALL" assert_rc 1 'status mapping: a local target path with .. fails even though the file exists' check_status -fresh_local './.github/workflows/../workflows/ci-gate.yml' $'on:\n workflow_call:' +fresh_local './.github/workflows/../workflows/ci-gate.yml' "$GOOD_CALL" check_status >"$WORK/status-msg" 2>&1 || true assert_rc 0 'status mapping message: names the path traversal' grep -qF "contains '..' path traversal" "$WORK/status-msg" -fresh_local '/etc/ci-gate.yml' $'on:\n workflow_call:' +fresh_local '/etc/ci-gate.yml' "$GOOD_CALL" assert_rc 1 'status mapping: an absolute local target fails' check_status -fresh_local "$LOCAL_TARGET" $'on:\n workflow_call:' +fresh_local "$LOCAL_TARGET" "$GOOD_CALL" sed -i 's|^ uses: .*| uses: ./.github/workflows/ci-gate.yml|' "$status_tree/.github/workflows/gate-caller.yml" printf ' other:\n uses: ./../outside/ci-gate.yml\n with: {context: "CI/gate"}\n' >> "$status_tree/.github/workflows/gate-caller.yml" assert_rc 1 'status mapping: a second, bad local job fails beside a valid local job' check_status