-
Notifications
You must be signed in to change notification settings - Fork 206
155 lines (140 loc) · 6.22 KB
/
CompareScreenshotComment.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
name: Screenshot compare comment
on:
workflow_run:
workflows:
- CompareScreenshot
types:
- completed
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref }}-${{ github.event.workflow_run.id }}
cancel-in-progress: true
permissions: { }
jobs:
Comment-CompareScreenshot:
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
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:
- uses: dawidd6/action-download-artifact@246dbf436b23d7c49e21a7ab8204ca9ecd1fe615 # v2.27.0
with:
name: pr
run_id: ${{ github.event.workflow_run.id }}
- id: get-pull-request-number
name: Get pull request number
shell: bash
run: |
echo "pull_request_number=$(cat NR)" >> "$GITHUB_OUTPUT"
- name: main checkout
id: checkout-main
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0
with:
ref: main
- id: switch-companion-branch
env:
BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }}
run: |
# orphan means it will create no history branch
git branch -D "$BRANCH_NAME" || true
git checkout --orphan "$BRANCH_NAME"
git rm -rf .
- uses: dawidd6/action-download-artifact@246dbf436b23d7c49e21a7ab8204ca9ecd1fe615 # v2.27.0
with:
run_id: ${{ github.event.workflow_run.id }}
name: screenshot-diff
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: companion_${{ github.event.workflow_run.head_branch }}
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
if: steps.check-if-there-are-valid-files.outputs.exist_valid_files == 'true'
env:
BRANCH_NAME: companion_${{ github.event.workflow_run.head_branch }}
shell: bash
run: |
# 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_./-]+$")
delimiter="$(openssl rand -hex 8)"
{
echo "reports<<${delimiter}"
# Create markdown table header
echo "Snapshot diff report"
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')
urlPart="${BRANCH_NAME//#/%23}/${file//#/%23}"
echo "| [$fileName](https://github.com/${{ github.repository }}/blob/$urlPart) | ![](https://github.com/${{ github.repository }}/blob/$urlPart?raw=true) |" >> "$GITHUB_OUTPUT"
done
echo "${delimiter}" >> "$GITHUB_OUTPUT"
- name: Find Comment
uses: peter-evans/find-comment@a54c31d7fa095754bfef525c0c8e5e5674c4b4b1 # v2
id: fc
if: steps.generate-diff-reports.outputs.reports != ''
with:
issue-number: ${{ steps.get-pull-request-number.outputs.pull_request_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@c6c9a1a66007646a28c153e2a8580a5bad27bcfa # v3
if: steps.generate-diff-reports.outputs.reports != ''
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ steps.get-pull-request-number.outputs.pull_request_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 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
# For testing purpose, delete branch if it's older than 1 second
echo "branch: $branch now_timestamp: $now_timestamp last_commit_date_timestamp: $last_commit_date_timestamp"
if [ $((now_timestamp - last_commit_date_timestamp)) -gt 1 ]; then
# Comment out for demonstration purpose
echo "Deleting $branch"
# git push origin --delete "$branch"
fi
done