refactor(action): separate retrieving the old report

This makes the code easier to read
This commit is contained in:
Jalil David Salamé Messina 2024-12-18 22:20:27 +01:00
parent 09d8ea071d
commit 72a3513f46
Signed by: jalil
GPG key ID: F016B9E770737A0B
3 changed files with 61 additions and 54 deletions

View file

@ -77,59 +77,10 @@ runs:
JOB_NAME: ${{ inputs.job-name }} JOB_NAME: ${{ inputs.job-name }}
PRIVATE_REPO: ${{ inputs.private-repo-workaround }} PRIVATE_REPO: ${{ inputs.private-repo-workaround }}
run: | run: |
. "${GITHUB_ACTION_PATH}/utils.sh" if "$GITHUB_ACTION_PATH/retrieve-old-report.sh" && [ -f old-report.json ]; then
log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH"
# USAGE: base_report_url
base_report_url() {
curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/tasks" \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'accept: application/json' |
jq --raw-output \
--arg name "$JOB_NAME" \
--arg head_branch "$HEAD_BRANCH" \
'[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .url'
}
# USAGE: has_report <REPORT_URL>
has_report() {
http_code=$(curl -X 'GET' -o /dev/null --silent -Iw '%{http_code}' \
"$1" -H "Authorization: token $GITHUB_TOKEN")
log "Got code $http_code for $1"
test "$http_code" = '200'
}
if [ "$PRIVATE_REPO" = 'true' ]; then
log "In a private repo, downloading $HEAD_BRANCH to build the old report"
old=$(mktemp -d)
curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/archive/$HEAD_BRANCH.tar.gz" \
-H "Authorization: token $GITHUB_TOKEN" |
tar -zvx --strip-components=1 -C "$old"
(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh") > old-report.json
log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH"
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json
exit 0 else
# If we have a previous report compare against it log 'Reporting on sizes'
elif [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json
url=$(base_report_url)
log "Found previous run at: $url"
report_url="$url/artifacts/$ARTIFACT_NAME"
if has_report "$report_url"; then
log 'Found previous report, downloading...'
curl -X 'GET' \
"$report_url" \
-H "Authorization: token $GITHUB_TOKEN" |
gunzip > old-report.json
log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH"
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json
exit 0
else
log "Failed to find previous report, expected at: $report_url"
fi
fi fi
log 'Reporting on sizes'
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json

56
retrieve-old-report.sh Executable file
View file

@ -0,0 +1,56 @@
. "${GITHUB_ACTION_PATH}/utils.sh"
# USAGE: base_report_url
base_report_url() {
curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/tasks" \
-H "Authorization: token $GITHUB_TOKEN" \
-H 'accept: application/json' |
jq --raw-output \
--arg name "$JOB_NAME" \
--arg head_branch "$HEAD_BRANCH" \
'[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .url'
}
# USAGE: has_report <REPORT_URL>
has_report() {
http_code=$(curl -X 'GET' -o /dev/null --silent -Iw '%{http_code}' \
"$1" -H "Authorization: token $GITHUB_TOKEN")
log "Got code $http_code for $1"
test "$http_code" = '200'
}
# If we have a previous report compare against it
if [ "$PRIVATE_REPO" = 'true' ]; then
log "In a private repo, downloading $HEAD_BRANCH to build the old report"
old=$(mktemp -d)
group 'Downloaded files'
curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/archive/$HEAD_BRANCH.tar.gz" \
-H "Authorization: token $GITHUB_TOKEN" |
tar -zvx --strip-components=1 -C "$old"
endgroup
(cd "$old" && "$GITHUB_ACTION_PATH/create-report.sh") >old-report.json
exit 0
elif [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then
url=$(base_report_url)
log "Found previous run at: $url"
report_url="$url/artifacts/$ARTIFACT_NAME"
if has_report "$report_url"; then
log 'Found previous report, downloading...'
curl -X 'GET' \
"$report_url" \
-H "Authorization: token $GITHUB_TOKEN" |
gunzip >old-report.json
log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH"
exit 0
else
log "Failed to find previous report, expected at: $report_url"
fi
fi
exit 1

0
utils.sh Normal file → Executable file
View file