Allow access without token for public repos and options for custom org and repo

This commit is contained in:
Antonio J. Delgado 2024-08-16 16:01:32 +03:00
parent e89441990b
commit 243b9b8b4f

View file

@ -6,6 +6,8 @@ no_backup=false
reinstall=false
token=""
DEBUG=false
organization=mastodon
repository=mastodon
function usage() {
echo "Usage:"
@ -17,42 +19,54 @@ function usage() {
echo " -D|--dummy|--dry-run Run in Dry-mode without actually updating but showing the commands to execute."
echo " -r|--reinstall Allow to reinstall the same tag."
echo " -n|--no-backup Don't do a backup of the database before the upgrade."
echo " -o|--organization Repository organization."
echo " -R|--repository Repository in the organization."
}
while [ $# -gt 0 ]
do
case "${1}" in
"-h"|"-?"|"--help")
shift
usage
exit 0
;;
shift
usage
exit 0
;;
"-d"|"--debug")
shift
export DEBUG=true
;;
shift
export DEBUG=true
;;
"-t"|"--tag")
shift
tag="${1}"
shift
;;
shift
tag="${1}"
shift
;;
"-T"|"--token")
shift
token="${1}"
shift
;;
shift
token="${1}"
shift
;;
'-D'|'--dummy'|'--dry-run')
shift
dummy=true
;;
shift
dummy=true
;;
'-n'|'--no-backup')
shift
no_backup=true
;;
shift
no_backup=true
;;
'-r'|'--reinstall')
shift
reinstall=true
;;
shift
reinstall=true
;;
'-o'|'--organization')
shift
organization="${1}"
shift
;;
'-R'|'--repository')
shift
repository="${1}"
shift
;;
*)
message "Unknown parameter '$1'" p
shift
@ -61,11 +75,12 @@ do
done
if [ -z "${token}" ]; then
echo "You must provide a Github token"
exit 4
auth_params=""
else
auth_params=(-H "Authorization: Bearer ${token}")
fi
if [ "${tag}" == "latest" ]; then
tag=$(curl -L -s -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${token}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/mastodon/mastodon/releases/latest | jq '.tag_name' | sed 's/"//g')
tag=$(curl -L -s -H "Accept: application/vnd.github+json" "${auth_params[@]}" -H "X-GitHub-Api-Version: 2022-11-28" "https://api.github.com/repos/${organization}/${repository}/releases/latest" | jq '.tag_name' | sed 's/"//g')
if [ "${DEBUG}" == "true" ]; then
echo "Latest tag is '${tag}'"
fi
@ -73,12 +88,12 @@ else
if [ "${DEBUG}" == "true" ]; then
echo "Looking for tag '${tag}'..."
fi
next_page=https://api.github.com/repos/mastodon/mastodon/releases
next_page="https://api.github.com/repos/${organization}/${repository}/releases"
releases_file=$(mktemp /tmp/tmp.XXXX)
while [ -z "${next_page}" ]
do
tmpfile=$(mktemp /tmp/tmp.XXXXX)
releases=$(curl -L -v -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${token}" -H "X-GitHub-Api-Version: 2022-11-28" "${next_page}" | jq '.[]|.tag_name' 2> "${tmpfile}")
releases=$(curl -L -v -H "Accept: application/vnd.github+json" "${auth_params[@]}" -H "X-GitHub-Api-Version: 2022-11-28" "${next_page}" | jq '.[]|.tag_name' 2> "${tmpfile}")
echo "${releases}" >> "${releases_file}"
next_page=$(grep '< link: ' "${tmpfile}" | sed 's/< link: //g' | sed 's/, /\n/g' | grep 'rel="next"' | grep -o 'https://[^>]*')
done