diff --git a/action.yml b/action.yml index f2741f7..3d4a9d4 100644 --- a/action.yml +++ b/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 - 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 diff --git a/retrieve-old-report.sh b/retrieve-old-report.sh new file mode 100755 index 0000000..362be97 --- /dev/null +++ b/retrieve-old-report.sh @@ -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 +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 diff --git a/utils.sh b/utils.sh old mode 100644 new mode 100755