Skip to content

Commit 3395343

Browse files
Introduce REST API fallback
Switch to using the GitHub REST API as a fallback when HTML parsing fails to retrieve the commit hash from the GitHub commits page. This ensures that the script continues to work even under rate limit conditions and improves reliability. Optionally, authentication can be used to further mitigate rate limits. Co-authored-by: Po-Ying Chiu <[email protected]> Change-Id: I248793f475525af192944f892641169ee3320517
1 parent 5223a1d commit 3395343

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

scripts/check-repo.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ upstream_hash=$(
8585

8686
rm -f "$temp_file"
8787

88+
# If HTML parsing fails, fallback to using GitHub REST API
89+
if [ -z "$upstream_hash" ]; then
90+
echo "HTML retrieval failed, falling back to GitHub REST API..."
91+
API_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/commits"
92+
93+
# Try to use cached GitHub credentials from GitHub CLI
94+
# https://docs.github.com/en/get-started/git-basics/caching-your-github-credentials-in-git
95+
if command -v gh >/dev/null 2>&1; then
96+
TOKEN=$(gh auth token 2>/dev/null)
97+
if [ -n "$TOKEN" ]; then
98+
echo "Using token from GitHub CLI..."
99+
response=$(curl -sSL -H "Authorization: token $TOKEN" "$API_URL")
100+
fi
101+
fi
102+
103+
# If response is empty (i.e. token not available or failed), use unauthenticated request.
104+
if [ -z "$response" ]; then
105+
response=$(curl -sSL "$API_URL")
106+
fi
107+
108+
# Extract the latest commit SHA from the JSON response
109+
upstream_hash=$(echo "$response" | grep -m 1 '"sha":' | sed -E 's/.*"sha": "([^"]+)".*/\1/')
110+
fi
111+
88112
if [ -z "$upstream_hash" ]; then
89113
throw "Failed to retrieve upstream commit hash from GitHub.\n"
90114
fi

0 commit comments

Comments
 (0)