|
| 1 | +# Automatically comment on PRs with doc preview links when docs/ files are changed |
| 2 | +name:Docs Preview Comment |
| 3 | + |
| 4 | +on: |
| 5 | +# zizmor: ignore[dangerous-triggers] We explicitly want to run on pull_request_target for write permissions. |
| 6 | +pull_request_target: |
| 7 | +types:[opened, synchronize, reopened] |
| 8 | +paths: |
| 9 | + -'docs/**' |
| 10 | + |
| 11 | +permissions: |
| 12 | +pull-requests:write |
| 13 | +contents:read |
| 14 | + |
| 15 | +jobs: |
| 16 | +comment-preview-links: |
| 17 | +runs-on:ubuntu-latest |
| 18 | +steps: |
| 19 | + -name:Harden Runner |
| 20 | +uses:step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a# v2.13.1 |
| 21 | +with: |
| 22 | +egress-policy:audit |
| 23 | + |
| 24 | + -name:Checkout PR |
| 25 | +uses:actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683# v4.2.2 |
| 26 | +with: |
| 27 | +ref:${{ github.event.pull_request.head.sha }} |
| 28 | +fetch-depth:0 |
| 29 | + |
| 30 | + -name:Get changed docs files |
| 31 | +id:changed-files |
| 32 | +run:| |
| 33 | + # Get the list of changed files in docs/ |
| 34 | + git fetch origin ${{ github.event.pull_request.base.ref }} |
| 35 | + CHANGED_DOCS=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }}...HEAD | grep '^docs/' | grep '\.md$' || true) |
| 36 | +
|
| 37 | + if [ -z "$CHANGED_DOCS" ]; then |
| 38 | + echo "No markdown files changed in docs/" |
| 39 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 40 | + exit 0 |
| 41 | + fi |
| 42 | +
|
| 43 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + # URI-encode the branch name |
| 46 | + BRANCH_NAME="${{ github.event.pull_request.head.ref }}" |
| 47 | + ENCODED_BRANCH=$(echo -n "$BRANCH_NAME" | jq -sRr @uri) |
| 48 | +
|
| 49 | + # Build the comment with preview links |
| 50 | + COMMENT="## 📚 Documentation Preview Links\n\n" |
| 51 | + COMMENT+="The following documentation pages have been modified and are available for preview:\n\n" |
| 52 | +
|
| 53 | + while IFS= read -r file; do |
| 54 | + # Remove 'docs/' prefix and '.md' suffix |
| 55 | + DOC_PATH="${file#docs/}" |
| 56 | + DOC_PATH="${DOC_PATH%.md}" |
| 57 | +
|
| 58 | + # Build the preview URL |
| 59 | + PREVIEW_URL="https://coder.com/docs/@${ENCODED_BRANCH}/${DOC_PATH}" |
| 60 | +
|
| 61 | + # Add to comment |
| 62 | + COMMENT+="- [\`${file}\`](${PREVIEW_URL})\n" |
| 63 | + done <<< "$CHANGED_DOCS" |
| 64 | +
|
| 65 | + COMMENT+="\n---\n" |
| 66 | + COMMENT+="_Preview links are generated for branch: \`${BRANCH_NAME}\`_" |
| 67 | +
|
| 68 | + # Save comment to file for next step |
| 69 | + echo -e "$COMMENT" > comment.txt |
| 70 | +
|
| 71 | + echo "Generated comment:" |
| 72 | + cat comment.txt |
| 73 | +
|
| 74 | + -name:Find existing comment |
| 75 | +if:steps.changed-files.outputs.has_changes == 'true' |
| 76 | +uses:peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e# v3.1.0 |
| 77 | +id:find-comment |
| 78 | +with: |
| 79 | +issue-number:${{ github.event.pull_request.number }} |
| 80 | +comment-author:'github-actions[bot]' |
| 81 | +body-includes:'## 📚 Documentation Preview Links' |
| 82 | + |
| 83 | + -name:Create or update comment |
| 84 | +if:steps.changed-files.outputs.has_changes == 'true' |
| 85 | +uses:peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043# v4.0.0 |
| 86 | +with: |
| 87 | +comment-id:${{ steps.find-comment.outputs.comment-id }} |
| 88 | +issue-number:${{ github.event.pull_request.number }} |
| 89 | +body-path:comment.txt |
| 90 | +edit-mode:replace |