Skip to content

Commit

Permalink
chore(script/update-packages): Make auto updates rate-limit aware
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJo2000 committed Jan 30, 2025
1 parent f70b91d commit bfe5130
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion scripts/bin/update-packages
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ TERMUX_PACKAGES_DIRECTORIES=$(jq --raw-output 'del(.pkg_format) | keys | .[]' "$
# shellcheck source=scripts/properties.sh
. "${TERMUX_SCRIPTDIR}/scripts/properties.sh"

# my GH_TOKEN for debugging, this is already passed in for the CI.
. "${TERMUX_SCRIPTDIR}/gh.env" || :

# Utility function to write error message to stderr.
# shellcheck source=scripts/updates/utils/termux_error_exit.sh
. "${TERMUX_SCRIPTDIR}"/scripts/updates/utils/termux_error_exit.sh
Expand Down Expand Up @@ -77,7 +80,7 @@ TERMUX_PACKAGES_DIRECTORIES=$(jq --raw-output 'del(.pkg_format) | keys | .[]' "$
# shellcheck source=scripts/updates/termux_pkg_auto_update.sh
. "${TERMUX_SCRIPTDIR}"/scripts/updates/termux_pkg_auto_update.sh

# Converts milliseconds to human-readable format.
# Converts milliseconds to human-readable format.
# Example: `ms_to_human_readable 123456789` => 34h 17m 36s 789ms
ms_to_human_readable() {
echo "$(($1/3600000))h $(($1%3600000/60000))m $(($1%60000/1000))s $(($1%1000))ms" | sed 's/0h //;s/0m //;s/0s //'
Expand All @@ -94,6 +97,15 @@ quiet() {
return $?
}

# Check GitHub rate limits
_gh_check_rate_limit() {
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/rate_limit | jq '.resources.core'
}

_update() {
export TERMUX_PKG_NAME
TERMUX_PKG_NAME="$(basename "$1")"
Expand All @@ -117,6 +129,10 @@ _update() {
. "${TERMUX_PKG_BUILDER_DIR}"/build.sh 2>/dev/null
set -e -u

[[ "$TERMUX_PKG_SRCURL" == *"github.com"* ]] && {
_gh_check_rate_limit
}

echo # Newline.
echo "INFO: Updating ${TERMUX_PKG_NAME} [Current version: ${TERMUX_PKG_VERSION}]"
termux_pkg_auto_update
Expand Down

0 comments on commit bfe5130

Please sign in to comment.