#!/bin/bash set -eu TEST_REFERENCE_SUBDIR="${TEST_REFERENCE_SUBDIR:-test/reference}" REPO=pixel-test-reference GITHUB_BASE="${GITHUB_BASE:-cockpit-project/cockpit}" GITHUB_REPOSITORY="${GITHUB_BASE%/*}/${REPO}" CLONE_REMOTE="https://github.com/${GITHUB_REPOSITORY}" PUSH_REMOTE="git@github.com:${GITHUB_REPOSITORY}" message() { [ "${V-}" != 0 ] || printf " %-8s %s\n" "$1" "$2" } cmd_init() { git submodule add -b empty "$CLONE_REMOTE" "$TEST_REFERENCE_SUBDIR" } cmd_update() { git submodule update --init -- "$TEST_REFERENCE_SUBDIR" || ( echo "" echo "Updating test/reference has failed, maybe because of" echo "local changes that have been accidentally made while" echo "it was out of date." echo "" echo "If you want to throw away these local changes, run" echo "" echo " $ ./test/common/pixel-tests reset" echo "" exit 1 ) } cmd_pull() { cmd_update } cmd_status() { cmd_update ( cd "$TEST_REFERENCE_SUBDIR" git rm --force --cached --quiet '*.png' git add *.png if git diff-index --name-status --cached --exit-code HEAD; then echo No changes fi ) } cmd_push() { cmd_update ( cd "$TEST_REFERENCE_SUBDIR" git rm --force --cached --quiet '*.png' git add *.png if ! git diff-index --name-status --cached --exit-code HEAD; then git fetch origin empty:empty git reset --soft empty git commit --quiet -m "$(date)" else echo No changes fi tag="sha-$(git rev-parse HEAD)" [ $(git tag -l "$tag") ] || git tag "$tag" HEAD --no-sign git push "$PUSH_REMOTE" "$tag" ) git add "$TEST_REFERENCE_SUBDIR" if [ -n "$(git status --porcelain "$TEST_REFERENCE_SUBDIR")" ]; then echo "" echo "The test/reference link has changed. The next step is to commit and" echo "push this change, just like any other change to a file." echo "" echo "The change has already been added with 'git add', so you could now" echo "amend your current HEAD commit with it like this:" echo "" echo " $ git commit --amend" echo "" echo "Then the HEAD commit can be pushed like normally. There is nothing" echo "special about committing and pushing a change to test/reference." fi } cmd_reset() { rm -rf "$TEST_REFERENCE_SUBDIR" cmd_update } pixel_test_logs_urls() { urls=() prs=() for arg in "${@:-}"; do if [[ "$arg" == http* ]]; then urls+=("$arg") else prs+=("$arg") fi done if [ ${#urls[@]} -gt 0 ]; then echo "${urls[@]}" return fi revisions=() remote=$(git remote get-url upstream || git remote get-url origin) repo=$(echo $remote | sed -re 's,git@github.com:|https://github.com/,,' -e 's,\.git$,,') if [ ${#prs[@]} -eq 0 ]; then revisions+=("$(git rev-parse "@{upstream}")") else for pr in "${prs[@]}"; do revisions+=("$(curl -s "https://api.github.com/repos/$repo/pulls/$pr" | python3 -c " import json import sys print(json.load(sys.stdin)['head']['sha']) ")") done fi for revision in "${revisions[@]}"; do context=$(cat test/reference-image) curl -s "https://api.github.com/repos/$repo/statuses/$revision?per_page=100" | python3 -c " import json import sys seen = set() for s in json.load(sys.stdin): c = s['context'] if 'firefox' in c or 'devel' in c: continue if c.split('/')[0] == sys.argv[1] and s['target_url']: url=s['target_url'] if url not in seen: seen.add(url) print(url) " "$context" done } cmd_fetch() { read -ra urls <<<"$(pixel_test_logs_urls "${@:-}")" if [ "${#urls[@]}" -eq 0 ]; then echo >&2 "Can't find test results for $(cat test/reference-image), sorry." exit 1 elif [ "${#urls[@]}" -ne ${#@} ]; then echo "Something went wrong fetching the links, aborting early" exit 1 fi cmd_update for url in "${urls[@]}"; do url=${url/\/log.html/} echo "Fetching new pixel test references from $url" pixels=$(curl -s "$url/index.html" | grep '[^=><"]*-pixels.png' -o | uniq) for f in ${pixels}; do echo "$f" curl -s --output-dir test/reference/ -O "$url/$f" done done } cmd_help() { cat <