Visual Diff Results #161
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: Visual Diff Results | |
| on: | |
| workflow_run: | |
| workflows: [Visual Diff] | |
| types: | |
| - completed | |
| jobs: | |
| check-artifact: | |
| name: Check for results artifact | |
| runs-on: ubuntu-latest | |
| permissions: | |
| actions: read | |
| outputs: | |
| found: ${{ steps.check.outputs.found }} | |
| steps: | |
| - name: Check for "results" artifact | |
| id: check | |
| uses: actions/github-script@v8 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: context.payload.workflow_run.id, | |
| }); | |
| const match = allArtifacts.data.artifacts.find(a => a.name === 'results'); | |
| if (match) { | |
| core.setOutput('found', 'true'); | |
| } else { | |
| core.setOutput('found', 'false'); | |
| console.log('No artifact uploaded.'); | |
| } | |
| comment: | |
| name: Comment on Pull Request | |
| needs: check-artifact | |
| if: ${{ needs.check-artifact.outputs.found == 'true' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| actions: read | |
| pull-requests: write | |
| env: | |
| MARKER: '<!-- visual-diff-results -->' | |
| steps: | |
| - name: Download artifact | |
| uses: actions/github-script@v8 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: context.payload.workflow_run.id, | |
| }); | |
| let matchArtifact = allArtifacts.data.artifacts.find((artifact) => { | |
| return artifact.name === 'results'; | |
| }); | |
| let download = await github.rest.actions.downloadArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: matchArtifact.id, | |
| archive_format: 'zip', | |
| }); | |
| const temp = '${{ runner.temp }}/artifacts'; | |
| if (!fs.existsSync(temp)){ | |
| fs.mkdirSync(temp); | |
| } | |
| fs.writeFileSync(path.join(temp, 'results.zip'), Buffer.from(download.data)); | |
| - name: Unzip artifact | |
| run: unzip "${{ runner.temp }}/artifacts/results.zip" -d "${{ runner.temp }}/artifacts" | |
| - name: Extract Details | |
| uses: actions/github-script@v8 | |
| id: data | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const temp = '${{ runner.temp }}/artifacts'; | |
| const report = fs.readFileSync(path.join(temp, 'report.md'), "utf8"); | |
| const issue_number = Number(fs.readFileSync(path.join(temp, 'pr_number'))); | |
| const exit_code = Number(fs.readFileSync(path.join(temp, 'exit_code'))); | |
| const approved = fs.readFileSync(path.join(temp, 'approved'), "utf8"); | |
| const url = fs.readFileSync(path.join(temp, 'artifact_url'), "utf8"); | |
| const rhett = { report, issue_number, exit_code, approved, url }; | |
| console.log(rhett); | |
| return rhett; | |
| - name: Decode Output | |
| id: decoded | |
| run: | | |
| JSON='${{ steps.data.outputs.result }}' | |
| ISSUE_NUMBER="$(echo "$JSON" | jq -r '.issue_number')" | |
| EXIT_CODE="$(echo "$JSON" | jq -r '.exit_code')" | |
| APPROVED="$(echo "$JSON" | jq -r '.approved')" | |
| URL="$(echo "$JSON" | jq -r '.url')" | |
| REPORT="$(echo "$JSON" | jq -r '.report')" | |
| { | |
| echo "issue_number=$ISSUE_NUMBER" | |
| echo "exit_code=$EXIT_CODE" | |
| echo "approved=$APPROVED" | |
| echo "url=$URL" | |
| echo "report<<EOF" | |
| echo "$REPORT" | |
| echo "EOF" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Show Output | |
| run: | | |
| echo "issue_number:" | |
| echo "${{ steps.decoded.outputs.issue_number }}" | |
| echo "exit_code:" | |
| echo "${{ steps.decoded.outputs.exit_code }}" | |
| echo "approved:" | |
| echo "${{ steps.decoded.outputs.approved }}" | |
| echo "url:" | |
| echo "${{ steps.decoded.outputs.url }}" | |
| echo "report:" | |
| echo "${{ steps.decoded.outputs.report }}" | |
| - name: Approval Results | |
| id: approval-results | |
| run: | | |
| exitCode="${{ steps.decoded.outputs.exit_code }}" | |
| approved="${{ steps.decoded.outputs.approved }}" | |
| echo "visual-differ exit code: $exitCode" | |
| echo "approve visual diff label present: $approved" | |
| if [ "$exitCode" != "0" ] && [ "$approved" == "true" ]; then | |
| APPROVAL_RESULTS="### ✅ Visual Diff Approved" | |
| else | |
| APPROVAL_RESULTS="" | |
| fi | |
| echo "results=$APPROVAL_RESULTS" >> "$GITHUB_OUTPUT" | |
| - name: Find Previous Comment | |
| uses: peter-evans/find-comment@v4 | |
| id: find | |
| with: | |
| issue-number: ${{ steps.decoded.outputs.issue_number }} | |
| body-includes: ${{ env.MARKER }} | |
| - name: Create or Update Comment | |
| uses: peter-evans/create-or-update-comment@v5 | |
| with: | |
| comment-id: ${{ steps.find.outputs.comment-id }} | |
| issue-number: ${{ steps.decoded.outputs.issue_number }} | |
| body: | | |
| ${{ steps.approval-results.outputs.results }} | |
| ${{ steps.decoded.outputs.report }} | |
| Download the [results](${{ steps.decoded.outputs.url }}). | |
| ${{ env.MARKER }} | |
| edit-mode: replace |