refactor(action): separate retrieving the old report
This makes the code easier to read
This commit is contained in:
parent
09d8ea071d
commit
72a3513f46
3 changed files with 61 additions and 54 deletions
59
action.yml
59
action.yml
|
@ -77,59 +77,10 @@ runs:
|
|||
JOB_NAME: ${{ inputs.job-name }}
|
||||
PRIVATE_REPO: ${{ inputs.private-repo-workaround }}
|
||||
run: |
|
||||
. "${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 [ "$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"
|
||||
if "$GITHUB_ACTION_PATH/retrieve-old-report.sh" && [ -f old-report.json ]; then
|
||||
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
|
||||
# If we have a previous report compare against it
|
||||
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"
|
||||
"$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
|
||||
else
|
||||
log 'Reporting on sizes'
|
||||
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json
|
||||
fi
|
||||
|
||||
log 'Reporting on sizes'
|
||||
"$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json
|
||||
|
|
56
retrieve-old-report.sh
Executable file
56
retrieve-old-report.sh
Executable 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
0
utils.sh
Normal file → Executable file
Loading…
Reference in a new issue