happo / happo.io

Happo is a cross-browser screenshot testing service
https://happo.io
MIT License
196 stars 25 forks source link

Ability to list reports #52

Closed jtomaszewski closed 5 years ago

jtomaszewski commented 5 years ago

Instead of running Happo for every PR, we'd prefer to running it only once every night on a nightly CI build.

Thus, for example, we want to build a report every Monday-Friday night, that runs the happo for the current master, and compare it with the report from the previous day.

To do this, we need to query for all of the reports, in order to select with which report should we compare the new report.

How can we do it? In Happo API we don't see any way to list all reports atm.

trotzig commented 5 years ago

Hi @jtomaszewski! Adding an ability to query the API for a list of available reports is certainly possible, but I wonder if you could do things slightly differently. The sha part of a happo run <sha> is an arbitrary identifier, it doesn't have to be a commit hash. So you could for instance have a nightly job that did something like this:

TODAY=$(date +%Y-%m-%d)
YESTERDAY=$(date -d "yesterday 13:00" '+%Y-%m-%d')
npm run happo run $TODAY
npm run happo compare $YESTERDAY $TODAY

Would that work? If not, can you explain a little bit more in-depth how you envision the API endpoint to behave?

jtomaszewski commented 5 years ago

Yeah, the way you explained with using current date in report's SHA is exactly what I thought about 10min later and what I'm working on atm ;)

So I'll be able to do it in that way. I'm writing now a shell script that checks last ~3-4 days and see if a report is done under such SHA, if yes, then it makes the compare.

Although if there'd be existing script that could do it that would be nice. I think I'll be happy to share it with you guys later on here if you want.

P.S. The ability to make an API request to list the reports would be still nice though.

trotzig commented 5 years ago

Thanks for the update! I'll make a note about listing reports through the API, but I can't promise when it will happen. If you find yourself in a situation where you would be helped by such an API endpoint, let us know and we can reprioritize!

Closing this issue as it seems you've found a way to get things working. Feel free to reopen if you disagree!

jack-sf commented 5 years ago

Just following up. We've done a shell script like that for our nightly CircleCI builds:

#!/bin/sh

set -e

BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
HAPPO_BASEURL="https://happo.io/a/76/p/78"

has_report() {
  SHA=$1
  npm run happo has-report $SHA > /dev/null 2>&1
}

find_last_build() {
  n=1
  until [ ! $n -lt 4 ]
  do
    if [[ "$OSTYPE" == "darwin"* ]]; then
      DATE=$(gdate -d "-$n days" +%F)
    else
      DATE=$(date -d "-$n days" +%F)
    fi
    SHA="$BRANCH_NAME-$DATE"
    has_report $SHA && echo $SHA && return
    n=`expr $n + 1`
  done
}

NEXT_DATE=$(date +%F)
NEXT_SHA="$BRANCH_NAME-$NEXT_DATE"

npm run happo run $NEXT_SHA
NEXT_REPORT_URL="$HAPPO_BASEURL/report/$NEXT_SHA"

PREVIOUS_SHA=$(find_last_build)
PREVIOUS_REPORT_URL="$HAPPO_BASEURL/report/$PREVIOUS_SHA"
if ! test -n "$PREVIOUS_SHA"; then
  echo "Previous build couldn't been found. No compare request will be done on Happo."

  curl -X POST --data-urlencode "payload={\"channel\": \"#sf-web-gen2-notifs\",\
   \"username\": \"Happo.io\",\
   \"text\": \"New <$HAPPO_BASEURL|Happo.io> report <$NEXT_REPORT_URL|$NEXT_SHA> has been finished.\",\
   \"icon_emoji\": \":ghost:\"}" $SLACK_WEBHOOK_URL

  exit
fi

COMPARE_RESULT_FILE=$(mktemp)
npm run happo compare $PREVIOUS_SHA $NEXT_SHA | tee $COMPARE_RESULT_FILE
if [ "$?" != "0" ] && [ "$?" != "113" ]; then
  exit 1
fi
COMPARE_RESULT=$(cat $COMPARE_RESULT_FILE | sed "s/\"/'/g" | tail -n +5)

COMPARE_URL="$HAPPO_BASEURL/compare/$PREVIOUS_SHA/$NEXT_SHA"
curl -X POST --data-urlencode "payload={\"channel\": \"#sf-web-gen2-notifs\",\
   \"username\": \"Happo.io\",\
   \"text\": \"New <$HAPPO_BASEURL|Happo.io> report <$NEXT_REPORT_URL|$NEXT_SHA> has been finished \
and <$COMPARE_URL|compared> with <$PREVIOUS_REPORT_URL|$PREVIOUS_SHA>.\n\n$COMPARE_RESULT\",\
   \"icon_emoji\": \":ghost:\"}" $SLACK_WEBHOOK_URL

Maybe you want to put it somewhere as an example? Definitely could be useful to some other happo users ;)