-
Notifications
You must be signed in to change notification settings - Fork 1
174 lines (148 loc) · 5.63 KB
/
Copy pathvisual_diff_results.yml
File metadata and controls
174 lines (148 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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