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

Tweak gh_releases #287

Open
wants to merge 4 commits into
base: 18.x
Choose a base branch
from
Open
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: 44 additions & 11 deletions overlays/turnkey.d/github-latest-release/usr/local/bin/gh_releases
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@

[[ -n $DEBUG ]] && set -x

repo_path="$1" # user/repo_name
tmp_dir=/tmp/gh_releases
rm -rf $tmp_dir
mkdir -p $tmp_dir

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

usage() {
cat >&2 <<EOF
Usage: $(basename $0) <user>/<repo>
Usage: $(basename "$0") [OPTIONS] <user>/<repo>

Options:
-h|--help show this help and exit
-a|--all show all (stable) versions, not just the most recent
-p|--pre-release
include alpha, beta and rc versions

Env Vars:
# used for github api
GITHUB_USER
GITHUB_USER_TOKEN

# retain temp files and set x if set
# retain temp files and set x
DEBUG

# omit tags or releases in output respectively if set
Expand All @@ -31,6 +36,27 @@ Note: Setting GITHUB_USER and GITHUB_USER_TOKEN environment variables are
EOF
}

unset show_all pre_release repo_path
while [[ "$#" -gt 0 ]]; do
case $1 in
-a|--all)
show_all=true;;
-p|--pre-release)
pre_release=true;;
-h|--help)
usage
exit;;
*)
if [[ -z "$repo_path" ]]; then
repo_path="$1"
else
usage
fatal "Unknown option: $1"
fi;;
esac
shift
done

if [[ -z "$repo_path" ]]; then
usage
fatal "user/repo not provided!"
Expand All @@ -44,7 +70,7 @@ 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."
warning "$warn Authentication won't be used."
USER=""
fi

Expand All @@ -62,12 +88,12 @@ get_page() {
key=$2
page=$3
tmp_file=$(mktemp $tmp_dir/XXXX.tmp)
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
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
grep -oP "\"$key\": \"\\K(.*)(?=\")" $tmp_file
rm -f $tmp_file
grep -oP "\"$key\": \"\\K(.*)(?=\")" "$tmp_file"
rm -f "$tmp_file"
fi
}

Expand All @@ -89,5 +115,12 @@ get_all_pages() {
[[ -z $NO_TAGS ]] && get_all_pages "https://api.github.com/repos/${repo_path}/tags" "name"

echo "Done!" 1>&2
cat $tmp_dir/releases | sort --version-sort --unique
versions=$(cat $tmp_dir/releases | sort --version-sort --unique)
if [[ -z "$pre_release" ]]; then
versions=$(grep -vi 'alpha\|beta\|rc' <<<"$versions")
fi
if [[ -z "$show_all" ]]; then
versions=$(tail -1 <<<"$versions")
fi
echo "$versions"
[[ -z $DEBUG ]] && rm -rf $tmp_dir