refactor!(action): detect when in private branches

This removes the need to use the private-repo-workaround input.
This commit is contained in:
Jalil David Salamé Messina 2024-12-19 21:56:16 +01:00
parent bff5365c60
commit 39f58ee538
Signed by: jalil
GPG key ID: F016B9E770737A0B
5 changed files with 125 additions and 69 deletions

View file

@ -1,6 +1,23 @@
#!/bin/sh
. "${GITHUB_ACTION_PATH}/utils.sh"
# USAGE: base_report_url
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>
base_report_url() {
curl -X 'GET' \
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/tasks" \
@ -8,9 +25,10 @@ base_report_url() {
-H 'accept: application/json' |
jq --raw-output \
--arg name "$JOB_NAME" \
--arg head_branch "$HEAD_BRANCH" \
--arg head_branch "$1" \
'[.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}' \
@ -19,23 +37,11 @@ has_report() {
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"
# If a base branch is not provided, use the default branch
base_branch=${BASE_BRANCH-$(default_branch)}
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)
if [ "$(in_private_repo)" != 'true' ] && [ "$JOB_NAME" ]; then
url=$(base_report_url "$base_branch")
log "Found previous run at: $url"
report_url="$url/artifacts/$ARTIFACT_NAME"
@ -46,11 +52,28 @@ elif [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then
"$report_url" \
-H "Authorization: token $GITHUB_TOKEN" |
gunzip >old-report.json
log "Reporting on sizes and comparing to sizes in $HEAD_BRANCH"
log "Reporting on sizes and comparing to sizes in $base_branch"
exit 0
else
log "Failed to find previous report, expected at: $report_url"
fi
error "Failed to find previous report, expected at: $report_url"
fi
exit 1
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