-
Notifications
You must be signed in to change notification settings - Fork 19
226 lines (192 loc) · 8.19 KB
/
roborazzi-compare-screenshot.yml
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Adapted from https://github.com/takahirom/roborazzi-compare-on-github-comment-sample/blob/main/.github/workflows/CompareScreenshot.yml
name: CompareScreenshot
on:
pull_request:
permissions:
contents: read
jobs:
compare-with-base-branch:
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read # for clone
actions: write # for upload-artifact
steps:
- name: Harden Runner
uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2
with:
egress-policy: audit
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Set up Build
uses: ./.github/actions/setup-build
with:
setup-gradle: true
setup-protoc: true
- name: Download screenshots from base branch
uses: dawidd6/action-download-artifact@80620a5d27ce0ae443b965134db88467fc607b43 # v7
continue-on-error: true
with:
name: screenshots
workflow: roborazzi-record-screenshot.yml
branch: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref || github.event.repository.default_branch }}
- name: Compare Screenshots
id: compare-screenshot-test
run: |
./gradlew compareRoborazziDebug --stacktrace
- uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
if: ${{ always() }}
with:
name: diff-vs-base-branch
path: |
**/build/outputs/roborazzi
**/build/reports/roborazzi
retention-days: 30
- name: Save PR number
if: ${{ github.event_name == 'pull_request' }}
run: |
mkdir -p ./pr
echo ${{ github.event.number }} > ./pr/NR
- uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: pr
path: pr/
Update-Comment:
needs: compare-with-base-branch
timeout-minutes: 2
permissions:
actions: read # for downloading artifacts
contents: write # for pushing screenshot-diff to companion branch
pull-requests: write # for creating a comment on pull requests
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2
with:
egress-policy: audit
- name: Checkout base
id: checkout-base
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ github.event.pull_request.base.ref }}
- name: Switch to companion branch
id: switch-companion-branch
env:
BRANCH_NAME: roborazzi_companion_${{ github.event.pull_request.head.ref }}
run: |
# orphan means it will create no history branch
git branch -D "$BRANCH_NAME" || true
git checkout --orphan "$BRANCH_NAME"
git rm -rf .
- name: Download diff
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: diff-vs-base-branch
path: screenshot-diff
- id: check-if-there-are-valid-files
name: Check if there are valid files
shell: bash
run: |
# Find all the files ending with _compare.png
mapfile -t files_to_add < <(find . -type f -name "*_compare.png")
# Check for invalid file names and add only valid ones
exist_valid_files="false"
for file in "${files_to_add[@]}"; do
if [[ $file =~ ^[a-zA-Z0-9_./-]+$ ]]; then
exist_valid_files="true"
break
fi
done
echo "exist_valid_files=$exist_valid_files" >> "$GITHUB_OUTPUT"
- id: push-screenshot-diff
shell: bash
if: steps.check-if-there-are-valid-files.outputs.exist_valid_files == 'true'
env:
BRANCH_NAME: roborazzi_companion_${{ github.event.pull_request.head.ref }}
run: |
# Find all the files ending with _compare.png
files_to_add=$(find . -type f -name "*_compare.png")
# Check for invalid file names and add only valid ones
for file in $files_to_add; do
if [[ "$file" =~ ^[a-zA-Z0-9_./-]+$ ]]; then
git add "$file"
fi
done
git config --global user.name ScreenshotBot
git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com
git commit -m "Add screenshot diff"
git push origin HEAD:"$BRANCH_NAME" -f
- id: generate-diff-reports
name: Generate diff reports
env:
BRANCH_NAME: roborazzi_companion_${{ github.event.pull_request.head.ref }}
shell: bash
run: |
delimiter="$(openssl rand -hex 8)"
{
echo "reports<<${delimiter}"
# Create markdown table header
echo "Snapshot diff report vs base branch: ${{ github.event.pull_request.base.ref }}"
echo "Last updated: $(TZ='America/Los_Angeles' date), Sha: ${{ github.event.pull_request.head.sha }}"
} >> "$GITHUB_OUTPUT"
if [[ ${{ steps.check-if-there-are-valid-files.outputs.exist_valid_files }} == 'true' ]]; then
# Find all the files ending with _compare.png in roborazzi folder
files=$(find . -type f -name "*_compare.png" | grep "roborazzi/" | grep -E "^[a-zA-Z0-9_./-]+$")
{
echo "| File name | Image |"
echo "|-------|-------|"
} >> "$GITHUB_OUTPUT"
# Iterate over the files and create table rows
for file in $files; do
# Get the file name and insert newlines every 20 characters
fileName=$(basename "$file" | sed -r 's/(.{20})/\1<br>/g')
echo "| [$fileName](https://github.com/${{ github.repository }}/blob/$BRANCH_NAME/$file) | ![](https://github.com/${{ github.repository }}/blob/$BRANCH_NAME/$file?raw=true) |" >> "$GITHUB_OUTPUT"
done
### Else there were no differences
else
{
echo "No differences detected"
} >> "$GITHUB_OUTPUT"
fi
echo "${delimiter}" >> "$GITHUB_OUTPUT"
- name: Find Comment
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
id: fc
if: steps.generate-diff-reports.outputs.reports != ''
with:
issue-number: ${{ github.event.number }}
comment-author: 'github-actions[bot]'
body-includes: Snapshot diff report
- name: Add or update comment on PR
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
if: steps.generate-diff-reports.outputs.reports != ''
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.number }}
body: ${{ steps.generate-diff-reports.outputs.reports }}
edit-mode: replace
- name: Cleanup outdated companion branches
run: |
# Find outdated companion branches with last commit date
git branch -r --format="%(refname:lstrip=3)" | grep roborazzi_companion_ | while read -r branch; do
last_commit_date_timestamp=$(git log -1 --format=%ct "origin/$branch")
now_timestamp=$(date +%s)
# Delete branch if it's older than 1 month
if [ $((now_timestamp - last_commit_date_timestamp)) -gt 2592000 ]; then
echo "Deleting $branch"
git push origin --delete "$branch"
fi
done