diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 2bf4368e..966847b8 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -113,11 +113,22 @@ jobs: f.write('body< + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.head_repository.full_name != github.repository + runs-on: ubuntu-latest + steps: + - name: Determine artifact name + id: meta + run: | + WF="${{ github.event.workflow_run.name }}" + if [ "$WF" = "Coverage" ]; then + echo "artifact=coverage-comment" >> "$GITHUB_OUTPUT" + echo "marker=" >> "$GITHUB_OUTPUT" + elif [ "$WF" = "Metrics Gate" ]; then + echo "artifact=metrics-comment" >> "$GITHUB_OUTPUT" + echo "marker=" >> "$GITHUB_OUTPUT" + else + echo "Unknown workflow: $WF" + exit 1 + fi + + - name: Get PR number + id: pr + uses: actions/github-script@v7 + with: + script: | + const result = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head: `${context.payload.workflow_run.head_repository.owner.login}:${context.payload.workflow_run.head_branch}`, + }); + if (result.data.length > 0) { + core.setOutput('number', result.data[0].number); + } else { + core.setFailed('Could not find PR for this workflow run'); + } + + - name: Download artifact + id: download + continue-on-error: true + uses: actions/download-artifact@v4 + with: + name: ${{ steps.meta.outputs.artifact }} + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + path: /tmp/comment + + - name: Find comment file + if: steps.download.outcome == 'success' + id: file + run: | + FILE=$(find /tmp/comment -name '*.md' | head -1) + echo "path=${FILE}" >> "$GITHUB_OUTPUT" + + - name: Find existing comment + if: steps.download.outcome == 'success' + uses: peter-evans/find-comment@v3 + id: fc + with: + issue-number: ${{ steps.pr.outputs.number }} + comment-author: 'github-actions[bot]' + body-includes: ${{ steps.meta.outputs.marker }} + + - name: Create or update comment + if: steps.download.outcome == 'success' + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.fc.outputs.comment-id }} + issue-number: ${{ steps.pr.outputs.number }} + body-path: ${{ steps.file.outputs.path }} + edit-mode: replace diff --git a/.github/workflows/metrics.yml b/.github/workflows/metrics.yml index 85169c01..f43e3d09 100644 --- a/.github/workflows/metrics.yml +++ b/.github/workflows/metrics.yml @@ -603,9 +603,17 @@ jobs: echo "gate_fail=${GATE_FAIL}" >> "$GITHUB_OUTPUT" + - name: Save comment as artifact (for fork PRs) + uses: actions/upload-artifact@v4 + with: + name: metrics-comment + path: /tmp/metrics_comment.md + retention-days: 1 + - name: Find existing metrics comment uses: peter-evans/find-comment@v3 id: fc + if: github.event.pull_request.head.repo.full_name == github.repository with: issue-number: ${{ github.event.pull_request.number }} comment-author: 'github-actions[bot]' @@ -613,6 +621,7 @@ jobs: - name: Create or update metrics comment uses: peter-evans/create-or-update-comment@v4 + if: github.event.pull_request.head.repo.full_name == github.repository with: comment-id: ${{ steps.fc.outputs.comment-id }} issue-number: ${{ github.event.pull_request.number }} diff --git a/.github/workflows/screenshot.yml b/.github/workflows/screenshot.yml index 310ebc5e..84bb3c63 100644 --- a/.github/workflows/screenshot.yml +++ b/.github/workflows/screenshot.yml @@ -22,7 +22,8 @@ jobs: steps: - uses: actions/checkout@v4 with: - ref: ${{ github.head_ref }} + repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }} + ref: ${{ github.event.pull_request.head.ref || github.ref }} fetch-depth: 0 - name: Build screenshot Docker image @@ -51,6 +52,7 @@ jobs: # Push screenshots to a ref so we can embed them in the PR comment - name: Push screenshots to ref + if: github.event.pull_request.head.repo.full_name == github.repository id: push_ref run: | REF_NAME="screenshots/pr-${{ github.event.pull_request.number || 'manual' }}" @@ -69,9 +71,10 @@ jobs: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" # Return to PR branch - git checkout "${{ github.head_ref }}" 2>/dev/null || git checkout "${{ github.sha }}" + git checkout "${{ github.event.pull_request.head.ref }}" 2>/dev/null || git checkout "${{ github.sha }}" - name: Generate PR comment body + if: github.event.pull_request.head.repo.full_name == github.repository id: comment run: | REF="${{ steps.push_ref.outputs.ref }}" @@ -143,7 +146,7 @@ jobs: - name: Find existing screenshot comment uses: peter-evans/find-comment@v3 id: fc - if: github.event_name == 'pull_request' + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository with: issue-number: ${{ github.event.pull_request.number }} comment-author: 'github-actions[bot]' @@ -151,7 +154,7 @@ jobs: - name: Create or update screenshot comment uses: peter-evans/create-or-update-comment@v4 - if: github.event_name == 'pull_request' + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository with: comment-id: ${{ steps.fc.outputs.comment-id }} issue-number: ${{ github.event.pull_request.number }}