2323 git fetch origin pull/${{ github.event.pull_request.number }}/head:pr-${{ github.event.pull_request.number }}
2424 git checkout pr-${{ github.event.pull_request.number }}
2525
26- - name : Install Fern and CML
27- run : |
28- npm install -g fern-api@latest
29- npm install -g @dvcorg/cml
30-
31- - name : Install Chrome for Puppeteer
32- run : npx puppeteer browsers install chrome@141.0.7390.54
26+ - name : Install Fern
27+ run : npm install -g fern-api@latest
3328
3429 - name : Generate preview URL
3530 id : generate-docs
@@ -42,105 +37,43 @@ jobs:
4237 echo "preview_url=$URL" >> $GITHUB_OUTPUT
4338 echo "Preview URL: $URL"
4439
45- - name : Run fern docs diff for changed MDX files
46- id : docs-diff
40+ - name : Get page links for changed MDX files
41+ id : page-links
4742 env :
4843 FERN_TOKEN : ${{ secrets.FERN_TOKEN }}
4944 run : |
5045 PREVIEW_URL="${{ steps.generate-docs.outputs.preview_url }}"
5146 CHANGED_FILES=$(git diff --name-only origin/main...HEAD -- '*.mdx' 2>/dev/null || echo "")
5247
5348 if [ -z "$CHANGED_FILES" ] || [ -z "$PREVIEW_URL" ]; then
54- echo "has_diffs=false" >> $GITHUB_OUTPUT
55- exit 0
49+ echo "page_links=" >> $GITHUB_OUTPUT; exit 0
5650 fi
5751
58- # Convert newlines to space-separated list for the command
59- FILES_LIST=$(echo "$CHANGED_FILES" | tr '\n' ' ')
60-
61- # Create output directory
62- mkdir -p .fern/diff
63-
64- # Run fern docs diff and capture output
65- # Progress messages go to stderr, JSON goes to stdout
66- # We capture everything to a temp file, then extract just the JSON
67- npx fern-api@latest docs diff "$PREVIEW_URL" $FILES_LIST --output .fern/diff > .fern/diff/output.txt 2>&1 || true
52+ BASE_URL=$(echo "$PREVIEW_URL" | grep -oP 'https?://[^/]+')
6853
69- # Extract just the JSON part (starts with { and ends with })
70- # The JSON is the last thing output, so we find the line starting with { and take everything from there
71- sed -n '/^{/,$p' .fern/diff/output.txt > .fern/diff/diff.json
54+ FILES_PARAM=$(echo "$CHANGED_FILES" | tr '\n' ',' | sed 's/,$//')
55+ RESPONSE=$(curl -sf -H "FERN_TOKEN: $FERN_TOKEN" "${PREVIEW_URL}/api/fern-docs/get-slug-for-file?files=${FILES_PARAM}" 2>/dev/null) || {
56+ echo "page_links=" >> $GITHUB_OUTPUT; exit 0
57+ }
7258
73- # Debug: show what we captured
74- echo "=== Raw output ==="
75- cat .fern/diff/output.txt
76- echo "=== Extracted JSON ==="
77- cat .fern/diff/diff.json
59+ PAGE_LINKS=$(echo "$RESPONSE" | jq -r --arg url "$BASE_URL" \
60+ '.mappings[] | select(.slug != null) | "- [\(.slug)](\($url)/\(.slug))"')
7861
79- # Check if diff JSON file exists and has valid content
80- if [ -f ".fern/diff/diff.json" ] && grep -q '"diffs"' .fern/diff/diff.json 2>/dev/null; then
81- echo "has_diffs=true" >> $GITHUB_OUTPUT
62+ if [ -n "$PAGE_LINKS" ]; then
63+ { echo "page_links<<EOF"; echo "$PAGE_LINKS"; echo "EOF"; } >> $GITHUB_OUTPUT
8264 else
83- echo "has_diffs=false" >> $GITHUB_OUTPUT
84- echo "No diffs found or diff.json is invalid"
65+ echo "page_links=" >> $GITHUB_OUTPUT
8566 fi
8667
87- - name : Upload diff images as artifacts
88- if : steps.docs-diff.outputs.has_diffs == 'true'
89- uses : actions/upload-artifact@v4
90- with :
91- name : docs-diff-images
92- path : .fern/diff/*.png
93- retention-days : 7
94-
95- - name : Upload images and create comment
96- if : steps.docs-diff.outputs.has_diffs == 'true'
97- env :
98- REPO_TOKEN : ${{ secrets.GITHUB_TOKEN }}
99- PREVIEW_URL : ${{ steps.generate-docs.outputs.preview_url }}
68+ - name : Create comment content
10069 run : |
101- # Parse diff.json and upload images using cml
102- PREVIEW_URL="${{ steps.generate-docs.outputs.preview_url }}"
103- BASE_URL=$(echo "$PREVIEW_URL" | grep -oP 'https?://[^/]+')
104-
105- echo ":herb: **Preview your docs:** <${PREVIEW_URL}>" > comment.md
106- echo "" >> comment.md
107- echo "### Visual changes detected:" >> comment.md
108- echo "" >> comment.md
70+ echo ":herb: **Preview your docs:** <${{ steps.generate-docs.outputs.preview_url }}>" > comment.md
10971
110- # Process each diff entry
111- jq -c '.diffs[]' .fern/diff/diff.json | while read -r diff; do
112- FILE=$(echo "$diff" | jq -r '.file')
113- SLUG=$(echo "$diff" | jq -r '.slug')
114- COMPARISON=$(echo "$diff" | jq -r '.comparison')
115- CHANGE_PERCENT=$(echo "$diff" | jq -r '.changePercent // "N/A"')
116- IS_NEW_PAGE=$(echo "$diff" | jq -r '.isNewPage')
117-
118- echo "#### \`${FILE}\`" >> comment.md
119- echo "" >> comment.md
120- echo "**Page:** [${SLUG}](${BASE_URL}/${SLUG}) | **Change:** ${CHANGE_PERCENT}%" >> comment.md
121- if [ "$IS_NEW_PAGE" = "true" ]; then
122- echo " | *New page*" >> comment.md
123- fi
72+ if [ -n "${{ steps.page-links.outputs.page_links }}" ]; then
12473 echo "" >> comment.md
125-
126- # Upload image using cml and get URL
127- if [ -f "$COMPARISON" ]; then
128- IMAGE_URL=$(cml publish "$COMPARISON" 2>/dev/null || echo "")
129- if [ -n "$IMAGE_URL" ]; then
130- echo "" >> comment.md
131- else
132- echo "*Comparison image available in [workflow artifacts](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})*" >> comment.md
133- fi
134- fi
135- echo "" >> comment.md
136- done
137-
138- cat comment.md
139-
140- - name : Create comment without diffs
141- if : steps.docs-diff.outputs.has_diffs != 'true'
142- run : |
143- echo ":herb: **Preview your docs:** <${{ steps.generate-docs.outputs.preview_url }}>" > comment.md
74+ echo "Here are the markdown pages you've updated:" >> comment.md
75+ echo "${{ steps.page-links.outputs.page_links }}" >> comment.md
76+ fi
14477
14578 - name : Post PR comment
14679 uses : thollander/actions-comment-pull-request@v2.4.3
0 commit comments