Skip to content

Commit 489a111

Browse files
authored
build: validate PR metadata before use in coverage publish workflow
PR-URL: #12343 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cf7b40a commit 489a111

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

.github/workflows/publish_coverage_pr.yml

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,29 @@ jobs:
6767
if: steps.download-metadata.outcome == 'success'
6868
run: |
6969
pr_number=$(cat pr-metadata/pr_number)
70+
71+
# Validate that the PR number is a positive integer; otherwise, abort:
72+
if ! [[ "$pr_number" =~ ^[0-9]+$ ]]; then
73+
echo "Invalid PR number: not a positive integer."
74+
exit 1
75+
fi
7076
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
71-
{
72-
echo 'report<<EOF'
73-
cat pr-metadata/report
74-
echo 'EOF'
75-
} >> $GITHUB_OUTPUT
7677
7778
# Post report as comment to PR:
7879
- name: 'Post report as comment to PR'
7980
if: steps.download-metadata.outcome == 'success'
8081
# Pin action to full length commit SHA
8182
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
83+
env:
84+
PR_NUMBER: ${{ steps.pr-metadata.outputs.pr_number }}
8285
with:
8386
github-token: ${{ secrets.STDLIB_BOT_PAT_REPO_WRITE }}
8487
script: |
85-
const prNumber = parseInt('${{ steps.pr-metadata.outputs.pr_number }}');
88+
const fs = require( 'fs' );
89+
90+
const prNumber = parseInt( process.env.PR_NUMBER, 10 );
91+
const report = fs.readFileSync( 'pr-metadata/report', 'utf8' );
92+
8693
const { data: comments } = await github.rest.issues.listComments({
8794
'issue_number': prNumber,
8895
'owner': context.repo.owner,
@@ -95,14 +102,14 @@ jobs:
95102
'owner': context.repo.owner,
96103
'repo': context.repo.repo,
97104
'comment_id': botComment.id,
98-
'body': `${{ steps.pr-metadata.outputs.report }}`
105+
'body': report
99106
});
100107
} else {
101108
await github.rest.issues.createComment({
102109
'issue_number': prNumber,
103110
'owner': context.repo.owner,
104111
'repo': context.repo.repo,
105-
'body': `${{ steps.pr-metadata.outputs.report }}`
112+
'body': report
106113
});
107114
}
108115
@@ -145,11 +152,13 @@ jobs:
145152
# Checkout coverage repository branch for PR:
146153
- name: 'Checkout coverage repository branch'
147154
if: steps.download-coverage.outcome == 'success'
155+
env:
156+
PR_NUMBER: ${{ steps.pr-metadata.outputs.pr_number }}
148157
run: |
149158
cd ./www-test-code-coverage
150-
BRANCH_NAME="pr-${{ steps.pr-metadata.outputs.pr_number }}"
151-
git fetch origin $BRANCH_NAME || true
152-
git checkout $BRANCH_NAME || git checkout -b $BRANCH_NAME
159+
BRANCH_NAME="pr-$PR_NUMBER"
160+
git fetch origin "$BRANCH_NAME" || true
161+
git checkout "$BRANCH_NAME" || git checkout -b "$BRANCH_NAME"
153162
154163
# Remove all directories except .github and .git from branch:
155164
find . -mindepth 1 -maxdepth 1 -type d -not -name '.github' -not -name '.git' -exec git rm -rf {} + || true
@@ -166,12 +175,11 @@ jobs:
166175
commit_timestamp=$(date -u +"%Y-%m-%d %H:%M:%S")
167176
168177
# Append coverage to ndjson files:
169-
files=$(find ./artifacts -name 'index.html')
170-
for file in $files; do
171-
file=${file//artifacts/www-test-code-coverage}
172-
coverage=$(echo -n '['; grep -oP "(?<=class='fraction'>)[0-9]+/[0-9]+" $file | awk -F/ '{ if ($2 != 0) print $1 "," $2 "," ($1/$2)*100; else print $1 "," $2 ",100" }' | tr '\n' ',' | sed 's/,$//'; echo -n ",\"$commit_sha\",\"$commit_timestamp\"]")
173-
echo $coverage >> $(dirname $file)/coverage.ndjson
174-
done
178+
while IFS= read -r -d '' file; do
179+
file="${file//artifacts/www-test-code-coverage}"
180+
coverage=$(echo -n '['; grep -oP "(?<=class='fraction'>)[0-9]+/[0-9]+" "$file" | awk -F/ '{ if ($2 != 0) print $1 "," $2 "," ($1/$2)*100; else print $1 "," $2 ",100" }' | tr '\n' ',' | sed 's/,$//'; echo -n ",\"$commit_sha\",\"$commit_timestamp\"]")
181+
echo "$coverage" >> "$(dirname "$file")/coverage.ndjson"
182+
done < <(find ./artifacts -name 'index.html' -print0)
175183
else
176184
echo "The artifacts directory does not exist."
177185
fi
@@ -194,11 +202,12 @@ jobs:
194202
env:
195203
REPO_GITHUB_TOKEN: ${{ secrets.STDLIB_BOT_PAT_REPO_WRITE }}
196204
USER_NAME: stdlib-bot
205+
PR_NUMBER: ${{ steps.pr-metadata.outputs.pr_number }}
197206
run: |
198207
cd ./www-test-code-coverage
199-
BRANCH_NAME="pr-${{ steps.pr-metadata.outputs.pr_number }}"
208+
BRANCH_NAME="pr-$PR_NUMBER"
200209
git config --local user.email "82920195+stdlib-bot@users.noreply.github.com"
201210
git config --local user.name "stdlib-bot"
202211
git add .
203212
git commit -m "Update artifacts" || exit 0
204-
git push "https://$USER_NAME:$REPO_GITHUB_TOKEN@github.com/stdlib-js/www-test-code-coverage.git" $BRANCH_NAME
213+
git push "https://$USER_NAME:$REPO_GITHUB_TOKEN@github.com/stdlib-js/www-test-code-coverage.git" "$BRANCH_NAME"

0 commit comments

Comments
 (0)