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 reinstall=false
token="" token=""
DEBUG=false DEBUG=false
organization=mastodon
repository=mastodon
function usage() { function usage() {
echo "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 " -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 " -r|--reinstall Allow to reinstall the same tag."
echo " -n|--no-backup Don't do a backup of the database before the upgrade." 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 ] while [ $# -gt 0 ]
do do
case "${1}" in case "${1}" in
"-h"|"-?"|"--help") "-h"|"-?"|"--help")
shift shift
usage usage
exit 0 exit 0
;; ;;
"-d"|"--debug") "-d"|"--debug")
shift shift
export DEBUG=true export DEBUG=true
;; ;;
"-t"|"--tag") "-t"|"--tag")
shift shift
tag="${1}" tag="${1}"
shift shift
;; ;;
"-T"|"--token") "-T"|"--token")
shift shift
token="${1}" token="${1}"
shift shift
;; ;;
'-D'|'--dummy'|'--dry-run') '-D'|'--dummy'|'--dry-run')
shift shift
dummy=true dummy=true
;; ;;
'-n'|'--no-backup') '-n'|'--no-backup')
shift shift
no_backup=true no_backup=true
;; ;;
'-r'|'--reinstall') '-r'|'--reinstall')
shift shift
reinstall=true reinstall=true
;; ;;
'-o'|'--organization')
shift
organization="${1}"
shift
;;
'-R'|'--repository')
shift
repository="${1}"
shift
;;
*) *)
message "Unknown parameter '$1'" p message "Unknown parameter '$1'" p
shift shift
@ -61,11 +75,12 @@ do
done done
if [ -z "${token}" ]; then if [ -z "${token}" ]; then
echo "You must provide a Github token" auth_params=""
exit 4 else
auth_params=(-H "Authorization: Bearer ${token}")
fi fi
if [ "${tag}" == "latest" ]; then 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 if [ "${DEBUG}" == "true" ]; then
echo "Latest tag is '${tag}'" echo "Latest tag is '${tag}'"
fi fi
@ -73,12 +88,12 @@ else
if [ "${DEBUG}" == "true" ]; then if [ "${DEBUG}" == "true" ]; then
echo "Looking for tag '${tag}'..." echo "Looking for tag '${tag}'..."
fi 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) releases_file=$(mktemp /tmp/tmp.XXXX)
while [ -z "${next_page}" ] while [ -z "${next_page}" ]
do do
tmpfile=$(mktemp /tmp/tmp.XXXXX) 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}" echo "${releases}" >> "${releases_file}"
next_page=$(grep '< link: ' "${tmpfile}" | sed 's/< link: //g' | sed 's/, /\n/g' | grep 'rel="next"' | grep -o 'https://[^>]*') next_page=$(grep '< link: ' "${tmpfile}" | sed 's/< link: //g' | sed 's/, /\n/g' | grep 'rel="next"' | grep -o 'https://[^>]*')
done done