Skip to content

Commit 3d9ddee

Browse files
authored
chore: update preview workflow so it matches fern-api/docs (#104)
1 parent 2f8ac9c commit 3d9ddee

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

.github/workflows/preview-docs.yml

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: preview-docs
1+
name: Preview Docs
22

33
on: pull_request
44

@@ -11,6 +11,13 @@ jobs:
1111
steps:
1212
- name: Checkout repository
1313
uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0 # Fetch full history for git diff
16+
17+
- name: Checkout PR
18+
run: |
19+
git fetch origin pull/${{ github.event.pull_request.number }}/head:pr-${{ github.event.pull_request.number }}
20+
git checkout pr-${{ github.event.pull_request.number }}
1421
1522
- name: Setup Fern CLI
1623
uses: fern-api/setup-fern-cli@v1
@@ -23,12 +30,50 @@ jobs:
2330
OUTPUT=$(fern generate --docs --preview 2>&1) || true
2431
echo "$OUTPUT"
2532
URL=$(echo "$OUTPUT" | grep -oP 'Published docs to \K.*(?= \()')
33+
echo "preview_url=$URL" >> $GITHUB_OUTPUT
2634
echo "Preview URL: $URL"
27-
echo "🌿 Preview your docs: $URL" > preview_url.txt
2835
29-
- name: Comment URL in PR
36+
- name: Get page links for changed MDX files
37+
id: page-links
38+
env:
39+
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
40+
run: |
41+
PREVIEW_URL="${{ steps.generate-docs.outputs.preview_url }}"
42+
CHANGED_FILES=$(git diff --name-only origin/main...HEAD -- '*.mdx' 2>/dev/null || echo "")
43+
44+
if [ -z "$CHANGED_FILES" ] || [ -z "$PREVIEW_URL" ]; then
45+
echo "page_links=" >> $GITHUB_OUTPUT; exit 0
46+
fi
47+
48+
BASE_URL=$(echo "$PREVIEW_URL" | grep -oP 'https?://[^/]+')
49+
50+
FILES_PARAM=$(echo "$CHANGED_FILES" | tr '\n' ',' | sed 's/,$//')
51+
RESPONSE=$(curl -sf -H "FERN_TOKEN: $FERN_TOKEN" "${PREVIEW_URL}/api/fern-docs/get-slug-for-file?files=${FILES_PARAM}" 2>/dev/null) || {
52+
echo "page_links=" >> $GITHUB_OUTPUT; exit 0
53+
}
54+
55+
PAGE_LINKS=$(echo "$RESPONSE" | jq -r --arg url "$BASE_URL" \
56+
'.mappings[] | select(.slug != null) | "- [\(.slug)](\($url)/\(.slug))"')
57+
58+
if [ -n "$PAGE_LINKS" ]; then
59+
{ echo "page_links<<EOF"; echo "$PAGE_LINKS"; echo "EOF"; } >> $GITHUB_OUTPUT
60+
else
61+
echo "page_links=" >> $GITHUB_OUTPUT
62+
fi
63+
64+
- name: Create comment content
65+
run: |
66+
echo ":herb: **Preview your docs:** <${{ steps.generate-docs.outputs.preview_url }}>" > comment.md
67+
68+
if [ -n "${{ steps.page-links.outputs.page_links }}" ]; then
69+
echo "" >> comment.md
70+
echo "Here are the markdown pages you've updated:" >> comment.md
71+
echo "${{ steps.page-links.outputs.page_links }}" >> comment.md
72+
fi
73+
74+
- name: Post PR comment
3075
uses: thollander/actions-comment-pull-request@v2.4.3
3176
with:
32-
filePath: preview_url.txt
77+
filePath: comment.md
3378
comment_tag: preview-docs
3479
mode: upsert

0 commit comments

Comments
 (0)