- Notifications
You must be signed in to change notification settings - Fork1k
feat: add GitHub Action for automatic docs preview comments#20254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
david-fraley wants to merge3 commits intomainChoose a base branch fromdf/docs-preview-comment-action
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+93 −0
Draft
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions.github/workflows/docs-preview-comment.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Automatically comment on PRs with doc preview links when docs/ files are changed | ||
name: Docs Preview Comment | ||
on: | ||
# zizmor: ignore[dangerous-triggers] We explicitly want to run on pull_request_target for write permissions. | ||
pull_request_target: | ||
types: [opened, synchronize, reopened] | ||
paths: | ||
- 'docs/**' | ||
permissions: | ||
pull-requests: write | ||
contents: read | ||
jobs: | ||
comment-preview-links: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Harden Runner | ||
uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1 | ||
with: | ||
egress-policy: audit | ||
- name: Checkout PR | ||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
with: | ||
ref: ${{ github.event.pull_request.head.sha }} | ||
fetch-depth: 0 | ||
persist-credentials: false | ||
- name: Get changed docs files | ||
id: changed-files | ||
env: | ||
BASE_REF: ${{ github.event.pull_request.base.ref }} | ||
HEAD_REF: ${{ github.event.pull_request.head.ref }} | ||
run: | | ||
# Get the list of changed files in docs/ | ||
git fetch origin "$BASE_REF" | ||
CHANGED_DOCS=$(git diff --name-only "origin/$BASE_REF...HEAD" | grep '^docs/' | grep '\.md$' || true) | ||
if [ -z "$CHANGED_DOCS" ]; then | ||
echo "No markdown files changed in docs/" | ||
echo "has_changes=false" >> "$GITHUB_OUTPUT" | ||
exit 0 | ||
fi | ||
echo "has_changes=true" >> "$GITHUB_OUTPUT" | ||
# URI-encode the branch name | ||
ENCODED_BRANCH=$(echo -n "$HEAD_REF" | jq -sRr @uri) | ||
# Build the comment with preview links | ||
COMMENT="## 📚 Documentation Preview Links\n\n" | ||
COMMENT+="The following documentation pages have been modified and are available for preview:\n\n" | ||
while IFS= read -r file; do | ||
# Remove 'docs/' prefix and '.md' suffix | ||
DOC_PATH="${file#docs/}" | ||
DOC_PATH="${DOC_PATH%.md}" | ||
# Build the preview URL | ||
PREVIEW_URL="https://coder.com/docs/@${ENCODED_BRANCH}/${DOC_PATH}" | ||
# Add to comment | ||
COMMENT+="- [\`${file}\`](${PREVIEW_URL})\n" | ||
done <<< "$CHANGED_DOCS" | ||
COMMENT+="\n---\n" | ||
COMMENT+="_Preview links are generated for branch: \`${HEAD_REF}\`_" | ||
# Save comment to file for next step | ||
echo -e "$COMMENT" > comment.txt | ||
echo "Generated comment:" | ||
cat comment.txt | ||
- name: Find existing comment | ||
if: steps.changed-files.outputs.has_changes == 'true' | ||
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0 | ||
id: find-comment | ||
with: | ||
issue-number: ${{ github.event.pull_request.number }} | ||
comment-author: 'github-actions[bot]' | ||
body-includes: '## 📚 Documentation Preview Links' | ||
- name: Create or update comment | ||
if: steps.changed-files.outputs.has_changes == 'true' | ||
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0 | ||
with: | ||
comment-id: ${{ steps.find-comment.outputs.comment-id }} | ||
issue-number: ${{ github.event.pull_request.number }} | ||
body-path: comment.txt | ||
edit-mode: replace |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.