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

Some refactoring of gh_releases #166

Merged
merged 1 commit into from Nov 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 21 additions & 17 deletions overlays/github-latest-release/usr/local/bin/gh_releases
Expand Up @@ -7,38 +7,45 @@ tmp_dir=/tmp/gh_releases
rm -rf $tmp_dir
mkdir -p $tmp_dir

fatal() {
echo -e "\n[FATAL] $1" 1>&2
exit 1
}
fatal() { echo -e "\n[FATAL] $@" 1>&2; exit 1; }
warning() { echo -e "[WARNING] $@" 1>&2; }

usage() {
echo "Usage: gh_releases.sh <user>/<repo>" 1>&2
echo "Note: requires GITHUB_USER and GITHUB_USER_TOKEN" 1>&2
echo " environment variables to be set." 1>&2
echo "For debugging, set DEBUG=y." 1>&2
cat >&2 <<EOF
Usage: $(basename $0) <user>/<repo>

Note: Setting GITHUB_USER and GITHUB_USER_TOKEN environment variables are
recommended. If not set, multipage results may be unreliable.

For debugging, set DEBUG=y.
EOF
}

if [[ -z "$repo_path" ]]; then
usage
fatal "user/repo not provided!"
fi
if [[ -z "$GITHUB_USER" ]]; then
usage
fatal "GITHUB_USER not set!"
warn="GITHUB_USER not set!"
fi
if [[ -z "$GITHUB_USER_TOKEN" ]]; then
usage
fatal "GITHUB_USER_TOKEN not set!"
warn="$warn GITHUB_USER_TOKEN not set!"
fi
if [[ -n "$GITHUB_USER" ]] && [[ -n "$GITHUB_USER_TOKEN" ]]; then
USER="-u $GITHUB_USER:$GITHUB_USER_TOKEN"
else
warning $warn "Authentication won't be used."
USER=""
fi

echo -n "Fetching releases from github for \"$repo_path\"... " 1>&2
echo -n "Fetching releases from github for '$repo_path'... " 1>&2

get_page() {
url=$1
key=$2
page=$3
tmp_file=$(mktemp $tmp_dir/XXXX.tmp)
curl -u "$GITHUB_USER:$GITHUB_USER_TOKEN" -b /tmp/cookies.txt -c /tmp/cookies.txt -s "${url}?page=${page}&per_page=100" > $tmp_file || true
curl $USER -b /tmp/cookies.txt -c /tmp/cookies.txt -s "${url}?page=${page}&per_page=100" > $tmp_file || true
if grep "Bad credentials" $tmp_file >/dev/null ; then
fatal "Bad GitHub credentials"
else
Expand All @@ -52,7 +59,6 @@ get_all_pages() {
key=$2
declare -i page=0
last_page="$(get_page "$url" "$key" "$page")"
touch /tmp/gh_releases

while [[ -n "$last_page" ]]
do
Expand All @@ -62,11 +68,9 @@ get_all_pages() {
done
}


get_all_pages "https://api.github.com/repos/${repo_path}/releases" "tag_name"
get_all_pages "https://api.github.com/repos/${repo_path}/tags" "name"

echo "Done!" 1>&2
cat $tmp_dir/releases | sort --version-sort --unique
[[ -z $DEBUG ]] && rm -rf $tmp_dir