2024-12-19 21:56:16 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2024-12-18 22:20:27 +01:00
|
|
|
. "${GITHUB_ACTION_PATH}/utils.sh"
|
|
|
|
|
2024-12-19 21:56:16 +01:00
|
|
|
repo_info() {
|
|
|
|
curl -X GET \
|
|
|
|
-H "Authorization: token $GITHUB_TOKEN" \
|
|
|
|
-H 'accept: application/json' \
|
|
|
|
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY"
|
|
|
|
}
|
|
|
|
|
|
|
|
in_private_repo() {
|
|
|
|
test "$(repo_info | jq --raw-output '.private')" = 'true'
|
|
|
|
}
|
|
|
|
|
|
|
|
default_branch() {
|
|
|
|
repo_info | jq --raw-output '.default_branch'
|
|
|
|
}
|
|
|
|
|
|
|
|
# USAGE: base_report_url <BASE_BRANCH>
|
2024-12-18 22:20:27 +01:00
|
|
|
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" \
|
2024-12-19 21:56:16 +01:00
|
|
|
--arg head_branch "$1" \
|
2024-12-18 22:20:27 +01:00
|
|
|
'[.workflow_runs[] | select(.name == $name and .head_branch == $head_branch)] | first | .url'
|
|
|
|
}
|
2024-12-19 21:56:16 +01:00
|
|
|
|
2024-12-18 22:20:27 +01:00
|
|
|
# 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'
|
|
|
|
}
|
|
|
|
|
2024-12-19 21:56:16 +01:00
|
|
|
# If a base branch is not provided, use the default branch
|
2024-12-19 23:12:40 +01:00
|
|
|
base_branch=${BASE_BRANCH:-$(default_branch)}
|
2024-12-18 22:20:27 +01:00
|
|
|
|
2024-12-19 21:56:16 +01:00
|
|
|
if [ "$(in_private_repo)" != 'true' ] && [ "$JOB_NAME" ]; then
|
|
|
|
url=$(base_report_url "$base_branch")
|
2024-12-18 22:20:27 +01:00
|
|
|
|
|
|
|
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
|
2024-12-19 21:56:16 +01:00
|
|
|
log "Reporting on sizes and comparing to sizes in $base_branch"
|
2024-12-18 22:20:27 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
2024-12-19 21:56:16 +01:00
|
|
|
error "Failed to find previous report, expected at: $report_url"
|
2024-12-18 22:20:27 +01:00
|
|
|
fi
|
|
|
|
|
2024-12-19 21:56:16 +01:00
|
|
|
warn "Couldn't retrieve old report:"
|
|
|
|
warn ' This usuially happens when running on private repos'
|
|
|
|
warn ' or when job-name is not set.'
|
|
|
|
warn
|
|
|
|
warn ' See the README for more details'
|
|
|
|
|
|
|
|
error "Falling back to slow method (checkout $base_branch and generate the report)"
|
|
|
|
|
|
|
|
old=$(mktemp -d)
|
|
|
|
group "Download files from $base_branch"
|
|
|
|
curl -X 'GET' \
|
|
|
|
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/archive/$base_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
|