#!/bin/bash

set -eu
set -o pipefail

python3 -m squad.frontend

(./manage.py test --noinput -v 2 2>&1 || true) | tee .tests

echo "{
  \"job_id\": \"$TRAVIS_JOB_NUMBER\",
  \"job_url\": \"https://travis-ci.org/Linaro/squad/jobs/$TRAVIS_JOB_ID\",
  \"build_url\": \"https://travis-ci.org/Linaro/squad/builds/$TRAVIS_BUILD_ID\"
}" | jq '' > .metadata.json

SUBMIT_TO=${SUBMIT_TO:-https://qa-reports.linaro.org/api}

if [ "$TRAVIS_PULL_REQUEST" = "false" ] && [ "$TRAVIS_BRANCH" = "master" ]; then
  ./scripts/dogfood "${SUBMIT_TO}" qa/squad "$TESTENV" "$TRAVIS_BUILD_NUMBER"
fi

! grep -q '^FAILED ' .tests
