Skip to content

Commit

Permalink
Show screenshot diffs in pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed May 22, 2024
1 parent 608f035 commit b22263a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- master
pull_request:
env:
resourceRunID: ${{ github.run_id }}-${{ github.run_number }}

jobs:
Build:
Expand Down Expand Up @@ -72,7 +74,8 @@ jobs:
set -x
cp extendedSample/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/emulator\(AVD\)\ -\ ${{ matrix.emulatorApi }}/* screenshotsToCompare${{ matrix.emulatorApi }}
set +x
./screenShotCompare.sh
echo ${{ env.resourceRunID }}
./screenShotCompare.sh ${{ env.resourceRunID }}
- name: Archive screenshots diffs ${{ matrix.emulatorApi }}
if: ${{ always() }}
uses: actions/upload-artifact@v4
Expand Down
46 changes: 45 additions & 1 deletion screenShotCompare.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
#!/bin/bash

diffFiles=./screenshotDiffs
mkdir $diffFiles
#cp app/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/emulator\(AVD\)\ -\ 9/* screenshotsToCompare
set -x
./git-diff-image/install.sh
GIT_DIFF_IMAGE_OUTPUT_DIR=$diffFiles git diff-image

source scripts/lib.sh

PR=$(echo "$GITHUB_REF_NAME" | sed "s/\// /" | awk '{print $1}')
echo pr=$PR
brew install jq

echo "delete all old comments, starting with Screenshot differs:$emulatorApi"
oldComments=$(curl_gh -X GET https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/"$PR"/comments | jq '.[] | (.id |tostring) + "|" + (.user.login | test("github-actions") | tostring) + "|" + (.body | test("Screenshot differs:'$emulatorApi'.*") | tostring)' | grep "true|true" | tr -d "\"" | cut -f1 -d"|")
echo "comments=$comments"
echo "$oldComments" | while read comment; do
echo "delete comment=$comment"
curl_gh -X DELETE https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/comments/"$comment"
done

pushd $diffFiles
body=""
COUNTER=0
ls -la

# ignore an error, when no files where found https://unix.stackexchange.com/a/723909/201876
setopt no_nomatch
for f in *.png; do
if [[ ${f} == "*.png" ]]
then
echo "nothing found"
else
(( COUNTER++ ))

newName="$1-${f}"
mv "${f}" "$newName"
echo "==> Uploaded screenshot $newName"
curl -i -F "file=@$newName" https://www.mxtracks.info/github
echo "==> Add screenshot comment $PR"
body="$body ${f}![screenshot](https://www.mxtracks.info/github/uploads/$newName) <br/><br/>"
fi
done

if [ ! "$body" == "" ]; then
curl_gh -X POST https://api.github.com/repos/"$GITHUB_REPOSITORY"/issues/$PR/comments -d "{ \"body\" : \"Screenshot differs:$emulatorApi $COUNTER <br/><br/> $body \" }"
fi

popd 1>/dev/null

# set error when diffs are there
[ "$(ls -A $diffFiles)" ] && exit 1 || exit 0
18 changes: 18 additions & 0 deletions scripts/lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

## This file is intended to be sourced by other scripts

function err() {
echo >&2 "$@"
}

function curl_gh() {
if [[ -n "$GITHUB_TOKEN" ]]; then
curl \
--silent \
--header "Authorization: token $GITHUB_TOKEN" \
"$@"
else
err "WARNING: No GITHUB_TOKEN found. Skipping API call"
fi
}

0 comments on commit b22263a

Please sign in to comment.