Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 427c9b3

Browse files
EricccTaiwancharliechiou
andcommitted
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 <charlie910417@gmail.com> Change-Id: I8688fdb093e88ed95326b911f198c974cd615ad4
1 parent 5223a1d commit 427c9b3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎scripts/check-repo.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ 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+
API_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/commits"
91+
92+
# Try to use cached GitHub credentials from GitHub CLI
93+
# https://docs.github.com/en/get-started/git-basics/caching-your-github-credentials-in-git
94+
if command -v gh >/dev/null 2>&1; then
95+
TOKEN=$(gh auth token 2>/dev/null)
96+
if [ -n "$TOKEN" ]; then
97+
response=$(curl -sSL -H "Authorization: token $TOKEN" "$API_URL")
98+
fi
99+
fi
100+
101+
# If response is empty (i.e. token not available or failed), use unauthenticated request.
102+
if [ -z "$response" ]; then
103+
response=$(curl -sSL "$API_URL")
104+
fi
105+
106+
# Extract the latest commit SHA from the JSON response
107+
upstream_hash=$(echo "$response" | grep -m 1 '"sha":' | sed -E 's/.*"sha": "([^"]+)".*/1円/')
108+
fi
109+
88110
if [ -z "$upstream_hash" ]; then
89111
throw "Failed to retrieve upstream commit hash from GitHub.\n"
90112
fi

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /