Skip to content

Commit 00032f4

Browse files
fix(docs): preserve preview pages and clear stale comments
Signed-off-by: svcnemo-autobot <svcnemo-autobot@nvidia.com>
1 parent e03a2fb commit 00032f4

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

‎.github/workflows/fern-docs-preview.yml‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ jobs:
104104
with:
105105
ref: ${{ github.sha }}
106106
path: pr-source
107+
fetch-depth: 0
107108
persist-credentials: false
108109

109110
- name: Prepare preview docs
@@ -116,9 +117,20 @@ jobs:
116117
test -z "$(find "pr-source/$ROOT" -type l -print -quit)"
117118
mkdir -p "$RUNNER_TEMP/fern-preview/$(dirname "$ROOT")"
118119
cp -R "trusted-source/$ROOT" "$RUNNER_TEMP/fern-preview/$ROOT"
120+
# Keep newer main pages as fallbacks, but honor explicit PR deletions.
121+
BASE=$(git -C pr-source merge-base HEAD refs/remotes/origin/main)
122+
git -C pr-source diff --no-renames --diff-filter=D --name-only -z \
123+
"$BASE" HEAD -- "$ROOT/" > "$RUNNER_TEMP/fern-deleted-paths"
124+
while IFS= read -r -d '' path; do
125+
case "$path" in
126+
*/.*|*/components/*) continue ;;
127+
esac
128+
case "$path" in
129+
*.md|*.mdx|*.png|*.jpg|*.jpeg|*.gif|*.svg|*.webp|*.ico|*.pdf)
130+
rm -f -- "$RUNNER_TEMP/fern-preview/$path" ;;
131+
esac
132+
done < "$RUNNER_TEMP/fern-deleted-paths"
119133
# Navigation, components and tooling stay on the trusted branch.
120-
find "$RUNNER_TEMP/fern-preview/$ROOT" -regextype posix-extended -type f \
121-
-regex "$CONTENT" ! -path '*/components/*' -delete
122134
(cd pr-source && find "$ROOT" -regextype posix-extended -type f \
123135
-regex "$CONTENT" ! -path '*/.*' ! -path '*/components/*' \
124136
-exec cp --parents -- '{}' "$RUNNER_TEMP/fern-preview/" \;)
@@ -175,3 +187,31 @@ jobs:
175187
else
176188
gh api "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" -f body="$BODY"
177189
fi
190+
191+
clear-preview:
192+
needs: changed-files
193+
if: >-
194+
github.repository == 'NVIDIA-NeMo/Megatron-Bridge' &&
195+
needs.changed-files.outputs.docs == 'false'
196+
runs-on: ubuntu-latest
197+
permissions:
198+
pull-requests: write
199+
steps:
200+
- name: Remove stale preview comments after docs changes are reverted
201+
env:
202+
GH_TOKEN: ${{ github.token }}
203+
run: |
204+
set -euo pipefail
205+
[[ "$GITHUB_REF" =~ ^refs/heads/pull-request/([1-9][0-9]*)$ ]]
206+
PR_NUMBER=${BASH_REMATCH[1]}
207+
gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" | jq -e \
208+
--arg repo "$GITHUB_REPOSITORY" --arg sha "$GITHUB_SHA" --argjson number "$PR_NUMBER" \
209+
'.number == $number and .state == "open" and .head.sha == $sha and
210+
.base.repo.full_name == $repo'
211+
gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" \
212+
--jq '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- preview-docs -->"))) | .id' \
213+
> "$RUNNER_TEMP/fern-preview-comments"
214+
while IFS= read -r comment; do
215+
[[ "$comment" =~ ^[1-9][0-9]*$ ]]
216+
gh api "repos/$GITHUB_REPOSITORY/issues/comments/$comment" -X DELETE
217+
done < "$RUNNER_TEMP/fern-preview-comments"

0 commit comments

Comments
 (0)