Skip to content

Commit 9563d40

Browse files
authored
Merge pull request #281 from EricccTaiwan/rest-api
Introduce REST API fallback
2 parents eb54e6f + 427c9b3 commit 9563d40

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

scripts/check-repo.sh

+22
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)