Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pagination when requesting github API #1168

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions lib/releases.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
#!/bin/bash

function get_latest_release() {
set +x
# get last page
if [ -z "${GITHUB_TOKEN:-}" ]; then
release="$(curl -sL "${1}")" || ( set -x && exit 1 )
last_page="$(curl -s -I "${1}" | grep '^link:' | sed -e 's/^link:.*page=//g' -e 's/>.*$//g')"
else
release="$(curl -H "Authorization: token ${GITHUB_TOKEN}" -sL "${1}")" || ( set -x && exit 1 )
last_page="$(curl -s -I "${1}" -H "Authorization: token $GITHUB_TOKEN" | grep '^link:' | sed -e 's/^link:.*page=//g' -e 's/>.*$//g')"
fi
# This gets the latest release as vx.y.z or vx.y.z-rc.0, including any version with a suffix starting with - , for example -rc.0
# The order is exactly as released in Github.
# Downside is that selecting official releases only isn't possible, while pre-release
# selection is possible given specific enough prefix, like v1.3.0-pre
release_tag="$(echo "$release" | jq -r "[.[].tag_name | select( startswith(\"${2:-}\"))] | .[0]")"

if [[ "$release_tag" == "null" ]]; then
set -x
# default last page to 1
last_page="${last_page:-1}"

set +x

for current_page in $(seq 1 "$last_page"); do
tuminoid marked this conversation as resolved.
Show resolved Hide resolved

url="${1}?page=${current_page}"

if [ -z "${GITHUB_TOKEN:-}" ]; then
release="$(curl -sL "${url}")" || { set -x && exit 1; }
else
release="$(curl -H "Authorization: token ${GITHUB_TOKEN}" -sL "${url}")" || { set -x && exit 1; }
fi

# This gets the latest release as vx.y.z or vx.y.z-rc.0, including any version with a suffix starting with - , for example -rc.0
# The order is exactly as released in Github.
# Downside is that selecting official releases only isn't possible, while pre-release
# selection is possible given specific enough prefix, like v1.3.0-pre
release_tag="$(echo "${release}" | jq -r "[.[].tag_name | select( startswith(\"${2:-}\"))] | .[0]")"
# if release tag found
if [[ "${release_tag}" != "null" ]]; then
break
fi

done

set -x
# if not found
if [[ "${release_tag}" == "null" ]]; then
exit 1
fi
set -x
# shellcheck disable=SC2005
echo "$release_tag"
echo "${release_tag}"
}

# CAPM3, CAPI and BMO release path
CAPM3RELEASEPATH="{https://api.github.com/repos/${CAPM3_BASE_URL:-metal3-io/cluster-api-provider-metal3}/releases}"
CAPIRELEASEPATH="{https://api.github.com/repos/${CAPI_BASE_URL:-kubernetes-sigs/cluster-api}/releases?per_page=100}"
CAPIRELEASEPATH="{https://api.github.com/repos/${CAPI_BASE_URL:-kubernetes-sigs/cluster-api}/releases}"
BMORELEASEPATH="{https://api.github.com/repos/${BMO_BASE_URL:-metal3-io/baremetal-operator}/releases}"

# CAPM3, CAPI and BMO releases
Expand All @@ -49,13 +70,13 @@ BMOBRANCH="${BMOBRANCH:-${BMORELEASE}}"

# On first iteration, jq might not be installed
if [[ "$CAPIRELEASE" == "" ]]; then
command -v jq &> /dev/null && echo "Failed to fetch CAPI release from Github" && exit 1
command -v jq &>/dev/null && echo "Failed to fetch CAPI release from Github" && exit 1
fi

if [[ "$CAPM3RELEASE" == "" ]]; then
command -v jq &> /dev/null && echo "Failed to fetch CAPM3 release from Github" && exit 1
command -v jq &>/dev/null && echo "Failed to fetch CAPM3 release from Github" && exit 1
fi

if [[ "$BMORELEASE" == "" ]]; then
command -v jq &> /dev/null && echo "Failed to fetch BMO release from Github" && exit 1
command -v jq &>/dev/null && echo "Failed to fetch BMO release from Github" && exit 1
fi
Comment on lines +73 to 82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the space here? I think it is easier to read with the space so we should add it back.

Copy link
Member Author

@mboukhalfa mboukhalfa Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was auto reformatted by editor formatter! do we have coding style for that

Copy link
Member

@lentzi90 lentzi90 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuminoid any input on this? I'm inclined to approve it as just a matter of preference if there are no objections.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That space is completely up to personal taste. I tend to have the space there for readability, but it doesn't really matter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I'm putting lgtm then 🙂