diff-shades comment #3453
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
| name: diff-shades comment | |
| on: | |
| workflow_run: | |
| workflows: [diff-shades] | |
| types: [completed] | |
| permissions: {} | |
| env: | |
| PIP_UPLOADED_PRIOR_TO: P2D | |
| jobs: | |
| comment: | |
| runs-on: ubuntu-latest | |
| # We want to comment even if there were failed files or the stable style changed | |
| # That would cause the main workflow to "fail" | |
| if: | |
| github.event.workflow_run.event == 'pull_request' && | |
| contains(fromJSON('["success", "failure"]'), github.event.workflow_run.conclusion) | |
| permissions: | |
| pull-requests: write # Needed to comment on PR | |
| steps: | |
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | |
| with: | |
| persist-credentials: false | |
| - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | |
| id: artifacts | |
| with: | |
| merge-multiple: true | |
| pattern: ".*.pr-comment.md" | |
| path: ${{ runner.temp }}/diff-shades-artifacts | |
| github-token: ${{ github.token }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Validate downloaded comment artifacts | |
| id: comment-artifacts | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| artifact_dir="${RUNNER_TEMP}/diff-shades-artifacts" | |
| preview_artifact="${artifact_dir}/.preview.pr-comment.md" | |
| stable_artifact="${artifact_dir}/.stable.pr-comment.md" | |
| if [ ! -d "$artifact_dir" ]; then | |
| echo "::error::Artifact directory does not exist: ${artifact_dir}" | |
| exit 1 | |
| fi | |
| while IFS= read -r -d '' entry; do | |
| if [ "$(dirname "$entry")" != "$artifact_dir" ]; then | |
| echo "::error::Unexpected nested artifact path: ${entry}" | |
| exit 1 | |
| fi | |
| case "$(basename "$entry")" in | |
| .preview.pr-comment.md|.stable.pr-comment.md) ;; | |
| *) | |
| echo "::error::Unexpected artifact path: ${entry}" | |
| exit 1 | |
| ;; | |
| esac | |
| if [ ! -f "$entry" ] || [ -L "$entry" ]; then | |
| echo "::error::Artifact must be a regular file: ${entry}" | |
| exit 1 | |
| fi | |
| done < <(find "$artifact_dir" -mindepth 1 -print0) | |
| for artifact in "$preview_artifact" "$stable_artifact"; do | |
| if [ ! -f "$artifact" ] || [ -L "$artifact" ]; then | |
| echo "::error::Missing expected artifact file: ${artifact}" | |
| exit 1 | |
| fi | |
| done | |
| { | |
| echo "preview=${preview_artifact}" | |
| echo "stable=${stable_artifact}" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Set up Python | |
| uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 | |
| with: | |
| python-version: "3.15" | |
| allow-prereleases: true | |
| - name: Install dependencies | |
| run: python -m pip install --group diff-shades-comment | |
| - name: Get PR number | |
| id: pr | |
| run: | |
| echo pr=$(gh pr list --search $sha --json number --jq ".[0].number") >> | |
| "$GITHUB_OUTPUT" | |
| env: | |
| GITHUB_TOKEN: ${{ github.token }} | |
| sha: ${{ github.event.workflow_run.head_sha }} | |
| - name: Get details from initial workflow run | |
| id: metadata | |
| run: | | |
| python scripts/diff_shades_gha_helper.py comment-details \ | |
| "$pr" "$run_id" "$preview_artifact" "$stable_artifact" | |
| env: | |
| GITHUB_TOKEN: ${{ github.token }} | |
| pr: ${{ steps.pr.outputs.pr }} | |
| run_id: ${{ github.event.workflow_run.id }} | |
| preview_artifact: ${{ steps.comment-artifacts.outputs.preview }} | |
| stable_artifact: ${{ steps.comment-artifacts.outputs.stable }} | |
| - name: Try to find pre-existing PR comment | |
| id: find-comment | |
| uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad # v4.0.0 | |
| with: | |
| issue-number: ${{ steps.pr.outputs.pr }} | |
| comment-author: "github-actions[bot]" | |
| body-includes: "diff-shades" | |
| - name: Create or update PR comment | |
| uses: peter-evans/create-or-update-comment@e8674b075228eee787fea43ef493e45ece1004c9 # v5.0.0 | |
| with: | |
| comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
| issue-number: ${{ steps.pr.outputs.pr }} | |
| body: ${{ steps.metadata.outputs.comment-body }} | |
| edit-mode: replace |