|
| 1 | +#!/bin/env bash |
| 2 | + |
| 3 | +TARGET_BRANCH="${TARGET_BRANCH:-master}" |
| 4 | +TARGET_PATH="${TARGET_PATH:-openapi.json}" |
| 5 | +TOKEN="${TOKEN:-$ACTIONS_RUNTIME_TOKEN}" |
| 6 | + |
| 7 | +CURL="${CURL_COMMAND:-curl --silent}" |
| 8 | + |
| 9 | +function definedOrExit { |
| 10 | + if [[ -z "$1" ]]; then |
| 11 | + echo "$2" |
| 12 | + cat "$3" |
| 13 | + exit 1 |
| 14 | + fi |
| 15 | +} |
| 16 | + |
| 17 | +echo "Using curl as \"${CURL}\"" |
| 18 | + |
| 19 | +function call_curl { |
| 20 | + ${CURL} -H "Authorization: Bearer ${TOKEN}" "$@" |
| 21 | +} |
| 22 | + |
| 23 | +JOBS_URL="https://api.github.com/repos/RedHatInsights/notifications-ui-backend/actions/workflows/main.yml/runs?status=success&branch=${TARGET_BRANCH}&event=push" |
| 24 | + |
| 25 | +echo "Getting artifacts_url from ${JOBS_URL}" |
| 26 | +call_curl "${JOBS_URL}" > .returned |
| 27 | +ARTIFACTS_URL=$(jq --raw-output '.workflow_runs[0].artifacts_url | if . == null then "" else . end' < .returned) |
| 28 | +definedOrExit "${ARTIFACTS_URL}" "Unable to get artifacts_url" .returned |
| 29 | + |
| 30 | +echo "Getting archive download url from ${ARTIFACTS_URL}" |
| 31 | +call_curl "${ARTIFACTS_URL}" > .returned |
| 32 | +ARCHIVE_DOWNLOAD_URL=$(jq --raw-output '.artifacts[0].archive_download_url | if . == null then "" else . end' < .returned) |
| 33 | +definedOrExit "${ARCHIVE_DOWNLOAD_URL}" "Unable to get archive_download_url" .returned |
| 34 | + |
| 35 | +call_curl -i "${ARCHIVE_DOWNLOAD_URL}" > .returned |
| 36 | +echo "Getting download url from ${ARCHIVE_DOWNLOAD_URL}" |
| 37 | +DOWNLOAD_URL=$(grep -oP 'Location: \K.+' < .returned) |
| 38 | +definedOrExit "${DOWNLOAD_URL}" "Unable to get Location header with download url" .returned |
| 39 | +DOWNLOAD_URL=${DOWNLOAD_URL%$'\r'} |
| 40 | +rm -f .returned |
| 41 | + |
| 42 | +echo "Downloading artifact package from ${DOWNLOAD_URL}" |
| 43 | +call_curl "${DOWNLOAD_URL}" > openapi.json.zip |
| 44 | +mkdir -p .tmp |
| 45 | +unzip openapi.json.zip openapi.json -d .tmp |
| 46 | +mv .tmp/openapi.json "${TARGET_PATH}" |
| 47 | +rm -rf .tmp |
| 48 | +rm openapi.json.zip |
0 commit comments