Skip to content

Commit bab10c6

Browse files
Copilotleofang
andcommitted
Fix GitHub API call for fetching PR preview folders
Co-authored-by: leofang <[email protected]>
1 parent c44f63a commit bab10c6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

ci/cleanup-pr-previews

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,28 @@ echo -e "${GREEN}[INFO]${NC} All prerequisites satisfied"
104104
echo -e "${YELLOW}[INFO]${NC} Fetching PR preview folders from gh-pages branch..."
105105

106106
# Get the list of pr-XXXXX folders from gh-pages branch
107-
PR_FOLDERS=$(gh api repos/"${REPOSITORY}"/contents/docs/pr-preview \
108-
--header "Accept: application/vnd.github+json" \
109-
--jq '.[] | select(.type == "dir" and (.name | test("^pr-[0-9]+$"))) | .name' \
110-
--field ref=gh-pages 2>/dev/null || true)
107+
echo -e "${YELLOW}[INFO]${NC} Fetching PR preview folders from gh-pages branch..."
108+
109+
# Try to get the contents of docs/pr-preview directory
110+
API_RESPONSE=$(gh api repos/"${REPOSITORY}"/contents/docs/pr-preview --field ref=gh-pages 2>/dev/null || echo "ERROR")
111+
112+
if [[ "$API_RESPONSE" == "ERROR" ]] || echo "$API_RESPONSE" | jq -e '.message' >/dev/null 2>&1; then
113+
# API call failed or returned an error message
114+
if echo "$API_RESPONSE" | jq -e '.message' >/dev/null 2>&1; then
115+
ERROR_MSG=$(echo "$API_RESPONSE" | jq -r '.message')
116+
echo -e "${RED}[ERROR]${NC} GitHub API error: ${ERROR_MSG}" >&2
117+
else
118+
echo -e "${RED}[ERROR]${NC} Failed to fetch PR preview folders from gh-pages branch" >&2
119+
fi
120+
echo -e "${YELLOW}[INFO]${NC} This could be because:" >&2
121+
echo -e " - The gh-pages branch doesn't exist" >&2
122+
echo -e " - The docs/pr-preview directory doesn't exist in gh-pages" >&2
123+
echo -e " - Authentication issues with GH_TOKEN" >&2
124+
exit 1
125+
fi
126+
127+
# Parse the response to get folder names
128+
PR_FOLDERS=$(echo "$API_RESPONSE" | jq -r '.[] | select(.type == "dir" and (.name | test("^pr-[0-9]+$"))) | .name' 2>/dev/null || true)
111129

112130
if [[ -z "$PR_FOLDERS" ]]; then
113131
echo -e "${YELLOW}[INFO]${NC} No PR preview folders found in gh-pages branch"

0 commit comments

Comments
 (0)