From 5ef2baacdcc6544bbe9c9915151ab9ae946c773b Mon Sep 17 00:00:00 2001 From: Mohit Gaur <56885276+Mohit-Gaur@users.noreply.github.com> Date: Thu, 24 Jul 2025 21:11:17 +0530 Subject: [PATCH] fix: update check-workflows-use-hashes to use github error format (#2875) # What does this PR do? Updates the script `scripts/check-workflows-use-hashes.sh` to improve error reporting by adopting GitHub Actions error annotation format. * Updated the script to use GitHub Actions error annotation format (`::error file={name},line={line},col={col}::{message}`) making error messages more actionable and easier to locate in workflows. * Modified the script to include line numbers for `uses:` references by using `grep -n` and extracting line numbers, improving the precision of error reporting. Closes #2778 ## Test Plan - Violation check - Created test file with mixed SHA/non-SHA actions ``` echo 'uses: actions/checkout@v4' > test-workflow.yml echo 'uses: actions/upload-artifact@main' >> test-workflow.yml ``` Result: Correctly detected violations with precise line numbers ``` ./scripts/check-workflows-use-hashes.sh Output: ::error file=test-workflow.yml,line=14::uses non-SHA action ref: uses: actions/checkout@v4 ::error file=test-workflow.yml,line=20::uses non-SHA action ref: uses: actions/upload-artifact@main ``` - Verified existing project workflows pass ``` ./scripts/check-workflows-use-hashes.sh # Result: Exit code 0 (all workflows properly SHA-pinned) ``` --- scripts/check-workflows-use-hashes.sh | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/scripts/check-workflows-use-hashes.sh b/scripts/check-workflows-use-hashes.sh index d508ce843..8ab12d661 100755 --- a/scripts/check-workflows-use-hashes.sh +++ b/scripts/check-workflows-use-hashes.sh @@ -12,21 +12,23 @@ set -euo pipefail failed=0 # Find all workflow YAML files + +# Use GitHub Actions error format +# ::error file={name},line={line},col={col}::{message} + for file in $(find .github/workflows/ -type f \( -name "*.yml" -o -name "*.yaml" \)); do IFS=$'\n' - # Grep for `uses:` lines that look like actions - for line in $(grep -E '^.*uses:[^@]+@[^ ]+' "$file"); do - # Extract the ref part after the last @ + # Get line numbers for each 'uses:' + while IFS= read -r match; do + line_num=$(echo "$match" | cut -d: -f1) + line=$(echo "$match" | cut -d: -f2-) ref=$(echo "$line" | sed -E 's/.*@([A-Za-z0-9._-]+).*/\1/') - # Check if ref is a 40-character hex string (full SHA). - # - # Note: strictly speaking, this could also be a tag or branch name, but - # we'd have to pull this info from the remote. Meh. if ! [[ $ref =~ ^[0-9a-fA-F]{40}$ ]]; then - echo "ERROR: $file uses non-SHA action ref: $line" + # Output in GitHub Actions annotation format + echo "::error file=$file,line=$line_num::uses non-SHA action ref: $line" failed=1 fi - done + done < <(grep -n -E '^.*uses:[^@]+@[^ ]+' "$file") done exit $failed