nix-flake-outputs-size/retrieve-old-report.sh

57 lines
1.6 KiB
Bash
Raw Normal View History

. "${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