Skip to content

Commit bc8f916

Browse files
committed
Improve error handling and formatting in GitHub Actions script
1 parent 7ada706 commit bc8f916

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

scripts/github-action.sh

+39-28
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
set -e -o pipefail
44

5-
type gh > /dev/null 2>&1 || { echo >&2 'ERROR: Script requires "gh" (see https://cli.github.com)'; exit 1; }
6-
type go-coverage-report > /dev/null 2>&1 || { echo >&2 'ERROR: Script requires "go-coverage-report" binary in PATH'; exit 1; }
5+
type gh >/dev/null 2>&1 || {
6+
echo >&2 'ERROR: Script requires "gh" (see https://cli.github.com)'
7+
exit 1
8+
}
9+
type go-coverage-report >/dev/null 2>&1 || {
10+
echo >&2 'ERROR: Script requires "go-coverage-report" binary in PATH'
11+
exit 1
12+
}
713

814
USAGE="$0: Execute go-coverage-report as GitHub action.
915
@@ -56,40 +62,46 @@ CHANGED_FILES_PATH=${CHANGED_FILES_PATH:-.github/outputs/all_modified_files.json
5662
SKIP_COMMENT=${SKIP_COMMENT:-false}
5763

5864
if [[ -z ${GITHUB_REPOSITORY+x} ]]; then
59-
echo "Missing github_repository argument"
60-
exit 1
65+
echo "Missing github_repository argument"
66+
exit 1
6167
fi
6268

6369
if [[ -z ${GITHUB_PULL_REQUEST_NUMBER+x} ]]; then
64-
echo "Missing github_pull_request_number argument"
65-
exit 1
70+
echo "Missing github_pull_request_number argument"
71+
exit 1
6672
fi
6773

6874
if [[ -z ${GITHUB_RUN_ID+x} ]]; then
69-
echo "Missing github_run_id argument"
70-
exit 1
75+
echo "Missing github_run_id argument"
76+
exit 1
7177
fi
7278

7379
if [[ -z ${GITHUB_OUTPUT+x} ]]; then
74-
echo "Missing GITHUB_OUTPUT environment variable"
75-
exit 1
80+
echo "Missing GITHUB_OUTPUT environment variable"
81+
exit 1
7682
fi
7783

7884
# If GITHUB_BASELINE_WORKFLOW_REF is defined, extract the workflow file path from it and use it instead of GITHUB_BASELINE_WORKFLOW
7985
if [[ -n ${GITHUB_BASELINE_WORKFLOW_REF+x} ]]; then
80-
GITHUB_BASELINE_WORKFLOW=$(basename "${GITHUB_BASELINE_WORKFLOW_REF%%@*}")
86+
GITHUB_BASELINE_WORKFLOW=$(basename "${GITHUB_BASELINE_WORKFLOW_REF%%@*}")
8187
fi
8288

8389
export GH_REPO="$GITHUB_REPOSITORY"
8490

85-
start_group(){
86-
echo "::group::$*"
87-
{ set -x; return; } 2>/dev/null
91+
start_group() {
92+
echo "::group::$*"
93+
{
94+
set -x
95+
return
96+
} 2>/dev/null
8897
}
8998

90-
end_group(){
91-
{ set +x; return; } 2>/dev/null
92-
echo "::endgroup::"
99+
end_group() {
100+
{
101+
set +x
102+
return
103+
} 2>/dev/null
104+
echo "::endgroup::"
93105
}
94106

95107
start_group "Download code coverage results from current run"
@@ -112,12 +124,12 @@ end_group
112124

113125
start_group "Compare code coverage results"
114126
go-coverage-report \
115-
-root="$ROOT_PACKAGE" \
116-
-trim="$TRIM_PACKAGE" \
117-
"$OLD_COVERAGE_PATH" \
118-
"$NEW_COVERAGE_PATH" \
119-
"$CHANGED_FILES_PATH" \
120-
> $COVERAGE_COMMENT_PATH
127+
-root="$ROOT_PACKAGE" \
128+
-trim="$TRIM_PACKAGE" \
129+
"$OLD_COVERAGE_PATH" \
130+
"$NEW_COVERAGE_PATH" \
131+
"$CHANGED_FILES_PATH" \
132+
>$COVERAGE_COMMENT_PATH
121133
end_group
122134

123135
if [ ! -s $COVERAGE_COMMENT_PATH ]; then
@@ -131,7 +143,7 @@ echo "Writing GitHub output parameter to \"$GITHUB_OUTPUT\""
131143
echo "coverage_report<<END_OF_COVERAGE_REPORT"
132144
cat "$COVERAGE_COMMENT_PATH"
133145
echo "END_OF_COVERAGE_REPORT"
134-
} >> "$GITHUB_OUTPUT"
146+
} >>"$GITHUB_OUTPUT"
135147

136148
if [ "$SKIP_COMMENT" = "true" ]; then
137149
echo "Skipping pull request comment (\$SKIP_COMMENT=true))"
@@ -142,10 +154,9 @@ start_group "Comment on pull request"
142154
COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${GITHUB_PULL_REQUEST_NUMBER}/comments" -q '.[] | select(.user.login=="github-actions[bot]" and (.body | test("Coverage Δ")) ) | .id' | head -n 1)
143155
if [ -z "$COMMENT_ID" ]; then
144156
echo "Creating new coverage report comment"
157+
gh pr comment "$GITHUB_PULL_REQUEST_NUMBER" --body-file="$COVERAGE_COMMENT_PATH"
145158
else
146-
echo "Replacing old coverage report comment"
147-
gh api -X DELETE "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}"
159+
echo "Updating old coverage report comment"
160+
gh api -X PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" -f body="$(cat "$COVERAGE_COMMENT_PATH")"
148161
fi
149-
150-
gh pr comment "$GITHUB_PULL_REQUEST_NUMBER" --body-file=$COVERAGE_COMMENT_PATH
151162
end_group

0 commit comments

Comments
 (0)