-
Notifications
You must be signed in to change notification settings - Fork 1
158 lines (136 loc) · 5.85 KB
/
test.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
name: Test
on:
pull_request:
merge_group:
types: [checks_requested]
push:
branches: [ main ]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
changes:
name: Check for Android changes
runs-on: ubuntu-latest
permissions:
pull-requests: read
outputs:
android: ${{ steps.filter.outputs.android }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
android:
- 'android/**'
- '.github/**'
screenshots:
name: Screenshot tests
needs: changes
if: ${{ needs.changes.outputs.android == 'true' }}
runs-on: ubuntu-latest
permissions: write-all
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
with:
gradle-version: wrapper
- name: Run tests
id: screenshot-tests
run: |
cd android
./gradlew verifyPaparazziDebug --continue --quiet
- name: Create companion branch and comment content
id: create-companion-branch
# Don't run on merge queue branches
if: ${{ !startsWith(github.ref, 'refs/heads/gh-readonly-queue/') && failure() }}
shell: bash
env:
PR_NUMBER: ${{ github.event.number }}
run: |
# Name for companion branch
COMPANION_BRANCH=companion/${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
git branch -D "$COMPANION_BRANCH" || true
git checkout "$COMPANION_BRANCH" || git checkout -B "$COMPANION_BRANCH"
# Find all the files starting with delta-*.png
files_to_add=$(find . -type f -name "delta-*.png")
# Check for invalid file names and add only valid ones
for file in $files_to_add; do
git add -f "$file"
done
# Don't commit any other files
git restore .
# TODO: Use Github app instead of personal access token
git config --global user.name GuardianAndroid
git config --global user.email [email protected]
git commit -m "Add screenshot diff"
git push origin HEAD:"$COMPANION_BRANCH" -f
delimiter="$(openssl rand -hex 8)"
{
echo "reports<<${delimiter}"
# Create markdown table header
echo "**Screenshot tests failed**<br/>${#files_to_add[@]} failures. Please check the failure diffs:"
echo "| Image |"
echo "|-------|"
} >> "$GITHUB_OUTPUT"
# Iterate over the files and create table rows
for file in $files_to_add; do
# Get the file name, strip down to only test class and function name
fileName=$(basename "$file" | sed 's/^[^_]*_//')
echo "| $fileName ![](https://github.com/${{ github.repository }}/blob/$COMPANION_BRANCH/$file?raw=true) |" >> "$GITHUB_OUTPUT"
done
echo "${delimiter}" >> "$GITHUB_OUTPUT"
echo "BRANCH IS ${GITHUB_REF}"
- name: Find Comment
uses: peter-evans/find-comment@v3
id: find-comment
# Don't run on merge queue branches
if: ${{ always() && !startsWith(github.ref, 'refs/heads/gh-readonly-queue/') && github.ref != 'refs/heads/main' }}
with:
issue-number: ${{ github.event.number }}
comment-author: 'github-actions[bot]'
body-includes: Screenshot tests failed
- name: Add or update comment on PR
uses: peter-evans/create-or-update-comment@v4
# Don't run on merge queue branches, only run if screenshot tests failed and companion branch created
if: ${{ failure() && !startsWith(github.ref, 'refs/heads/gh-readonly-queue/') && github.ref != 'refs/heads/main' && steps.screenshot-tests.outcome != 'success' && steps.create-companion-branch.outcome == 'success' }}
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.number }}
body: ${{ steps.create-companion-branch.outputs.reports }}
edit-mode: replace
- name: Resolve comment on PR
uses: peter-evans/create-or-update-comment@v4
# Don't run on merge queue branches, only run if screenshot tests passed and comment exists
if: ${{ success() && !startsWith(github.ref, 'refs/heads/gh-readonly-queue/') && github.ref != 'refs/heads/main' && success() && steps.find-comment.outputs.comment-id != '' }}
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.number }}
body: |
**Screenshot tests failed**
Errors resolved.
edit-mode: replace
- name: Cleanup outdated companion branches
if: ${{ always() }}
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)
echo "branch: $branch now_timestamp: $now_timestamp last_commit_date_timestamp: $last_commit_date_timestamp"
# For testing purpose, delete branch if it's older than 1 second
# if [ $((now_timestamp - last_commit_date_timestamp)) -gt 1 ]; then
# 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