From 10fd3b2450e951fcd1d1f64f1fb02240d174a9de Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Wed, 21 Aug 2024 10:38:11 +0200 Subject: [PATCH] download 10 days of workflow history and use Github paging --- .../github-download-workflow-artifacts.sh | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/configuration/github-download-workflow-artifacts.sh b/configuration/github-download-workflow-artifacts.sh index 249cce3f768..ee612a9ef53 100755 --- a/configuration/github-download-workflow-artifacts.sh +++ b/configuration/github-download-workflow-artifacts.sh @@ -14,13 +14,22 @@ BRANCH="${1}" BEARER_TOKEN="${2}" UNIX_TIME=$( date +%s ) UNIX_DATE=$( date --iso-8601=second ) -UNIX_TIME_YESTERDAY=$(( UNIX_TIME - 2*24*3600 )) # look back two days in time +UNIX_TIME_YESTERDAY=$(( UNIX_TIME - 10*24*3600 )) # look back ten days in time, trying to catch even re-runs of older jobs DATE_YESTERDAY=$( date --iso-8601=second -d @${UNIX_TIME_YESTERDAY} ) -# Get the artifacts URL of the last workflow run triggered by a branch push for ${BRANCH}: -LAST_WORKFLOW_FOR_BRANCH=$( curl --silent -L -H 'Authorization: Bearer '${BEARER_TOKEN} https://api.github.com/repos/SAP/sailing-analytics/actions/runs?created=${DATE_YESTERDAY/+/%2B}..${UNIX_DATE/+/%2B}\&per_page=100 2>/dev/null | jq -r '.workflow_runs | map(select(.status == "completed" and .name == "release" and ((.head_branch | startswith("'${BRANCH}'")) or (.head_branch | startswith("releases/'${BRANCH}'"))))) | sort_by(.updated_at) | reverse | .[0]' ) -ARTIFACTS_URL=$( echo "${LAST_WORKFLOW_FOR_BRANCH}" | jq -r '.artifacts_url' ) -CONCLUSION=$( echo "${LAST_WORKFLOW_FOR_BRANCH}" | jq -r '.conclusion' ) -ARTIFACTS_JSON=$( curl --silent -H 'Authorization: Bearer '${BEARER_TOKEN} "${ARTIFACTS_URL}" ) +HEADERS_FILE=$( mktemp headersXXXXX ) +NEXT_PAGE="https://api.github.com/repos/SAP/sailing-analytics/actions/runs?created=${DATE_YESTERDAY/+/%2B}..${UNIX_DATE/+/%2B}&per_page=100" +ARTIFACTS_JSON="" +# Now go through the pages as long as we have a non-empty NEXT_PAGE URL and no valid ARTIFACTS_JSON +while [ -z "${ARTIFACTS_JSON}" -a -n "${NEXT_PAGE}" ]; do + echo "Trying page ${NEXT_PAGE} ..." + # Get the artifacts URL of the last workflow run triggered by a branch push for ${BRANCH}: + LAST_WORKFLOW_FOR_BRANCH=$( curl -D "${HEADERS_FILE}" --silent -L -H 'Authorization: Bearer '${BEARER_TOKEN} "${NEXT_PAGE}" 2>/dev/null | jq -r '.workflow_runs | map(select(.status == "completed" and .name == "release" and ((.head_branch | startswith("'${BRANCH}'")) or (.head_branch | startswith("releases/'${BRANCH}'"))))) | sort_by(.updated_at) | reverse | .[0]' ) + NEXT_PAGE=$( grep "^link: " "${HEADERS_FILE}" | sed -e 's/^.*<\([^>]*\)>; rel="next".*$/\1/' ) + ARTIFACTS_URL=$( echo "${LAST_WORKFLOW_FOR_BRANCH}" | jq -r '.artifacts_url' ) + CONCLUSION=$( echo "${LAST_WORKFLOW_FOR_BRANCH}" | jq -r '.conclusion' ) + ARTIFACTS_JSON=$( curl --silent -H 'Authorization: Bearer '${BEARER_TOKEN} "${ARTIFACTS_URL}" ) +done +rm "${HEADERS_FILE}" if [ -z "${ARTIFACTS_JSON}" ]; then echo "Workflow run or artifacts not found" exit 1