|
| 1 | +name: Preview comment |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + pull-requests: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + comment: |
| 12 | + name: Post preview URL |
| 13 | + runs-on: ubuntu-latest |
| 14 | + # Only run on PRs from the same repo (not forks) |
| 15 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Extract deploy repo from make.jl |
| 22 | + id: deploy |
| 23 | + run: | |
| 24 | + # Looks for: deploy_repo = "github.com/owner/repo.git" |
| 25 | + DEPLOY_REPO=$(grep -oP 'deploy_repo\s*=\s*"github\.com/\K[^"]+' docs/make.jl | sed 's/\.git$//' || true) |
| 26 | +
|
| 27 | + if [ -n "$DEPLOY_REPO" ]; then |
| 28 | + echo "repo=$DEPLOY_REPO" >> "$GITHUB_OUTPUT" |
| 29 | + else |
| 30 | + # No cross-repo deployment found, fall back to the source repo |
| 31 | + echo "repo=${{ github.repository }}" >> "$GITHUB_OUTPUT" |
| 32 | + fi |
| 33 | +
|
| 34 | + - name: Comment preview URL |
| 35 | + uses: actions/github-script@v7 |
| 36 | + env: |
| 37 | + DEPLOY_REPO: ${{ steps.deploy.outputs.repo }} |
| 38 | + with: |
| 39 | + script: | |
| 40 | + const pr = context.issue.number; |
| 41 | + const [deployOwner, deployRepoName] = process.env.DEPLOY_REPO.split("/"); |
| 42 | +
|
| 43 | + const previewUrl = `https://${deployOwner}.github.io/${deployRepoName}/previews/PR${pr}/`; |
| 44 | + const marker = "<!-- documenter-preview-comment -->"; |
| 45 | + const body = [ |
| 46 | + marker, |
| 47 | + "### 📚 Documentation preview 🚀", |
| 48 | + "", |
| 49 | + `**Preview URL:** [${previewUrl}](${previewUrl})`, |
| 50 | + "", |
| 51 | + "> [!NOTE]", |
| 52 | + "> The preview will be available once the documentation build completes successfully, and will reflect the last successful build for this PR.", |
| 53 | + ].join("\n"); |
| 54 | +
|
| 55 | + const { data: comments } = await github.rest.issues.listComments({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + issue_number: pr, |
| 59 | + }); |
| 60 | +
|
| 61 | + const existing = comments.find((c) => c.body?.includes(marker)); |
| 62 | +
|
| 63 | + if (!existing) { |
| 64 | + await github.rest.issues.createComment({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + issue_number: pr, |
| 68 | + body, |
| 69 | + }); |
| 70 | + } |
0 commit comments