File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+ set -euo pipefail
3+
4+ WORKFLOWS_DIR=" ${1:- .} /.github/workflows"
5+
6+ echo " Scanning workflows in: $WORKFLOWS_DIR "
7+
8+ # Recursively grep workflow files for actions not pinned to SHA-1
9+ grep -Prno --include=" *.yml" --include=" *.yaml" ' uses:\s*([^/]+)/([^@]+)@([^[:space:]]+)' " ${WORKFLOWS_DIR} " | \
10+ while IFS=: read -r file _line_num uses_line; do
11+ echo -n " $file - "
12+ # Extract owner/repo/version
13+ if [[ " $uses_line " =~ uses:\ ([^/]+)/([^@]+)@ ([^[:space:]]+) ]]; then
14+ owner=" ${BASH_REMATCH[1]} "
15+ repo=" ${BASH_REMATCH[2]} "
16+ version=" ${BASH_REMATCH[3]} "
17+ action=" $owner /$repo "
18+ echo -n " $owner /$repo : "
19+ else
20+ echo " Failed to parse line: $uses_line [FAIL]"
21+ continue
22+ fi
23+
24+ # Skip if already pinned to SHA
25+ if [[ " $version " =~ ^[0-9a-f]{40}$ ]]; then
26+ echo " SHA-1 pinned: $version [OK]"
27+ continue
28+ else
29+ echo -n " Tag pinned: $version [WARNING], "
30+ fi
31+
32+ api_url=" https://api.github.com/repos/$owner /$repo /git/ref/tags/$version "
33+
34+ # Get full SHA
35+ sha=$( curl -s " $api_url " | jq -r ' .object.sha' )
36+ if [[ " $sha " == " null" || -z " $sha " ]]; then
37+ echo " Could not resolve $action @$version [FAIL]"
38+ continue
39+ fi
40+
41+ echo " Replacing $version → $sha [OK]"
42+
43+ # Precise sed replacement: match 'uses:' literally and append comment
44+ sed -i.bak " s|uses:\s*$action @$version |uses: $action @$sha # $version |g" " $file "
45+ done
You can’t perform that action at this time.
0 commit comments