#!/bin/bash
# smoke_test_status_reporter.sh: Upload result of a Bamboo or other CI job to GitHub Status API
# @copyright Highly Confidential and Proprietary Zoox Information. Copyright (c) Zoox.
#
# Date Created: December 4, 2019
# Author: trawat
#
# This script searches the firmware directory (using ZOOX_FIRMWARE_ROOT) for files matching
# the filename "create_bamboo_sentinel_file.sh"
#
# Because the files should be in well-named directories for specific ECU test suites, we can
# use these paths to determine which ECUs are included in testing.
#
# Inside the Bamboo jobs that run the tests, these scripts create sentinel files in the following
# location: $bamboo_build_working_directory/
#
# Once we know which ECUs may have sentinel files created, we can check the directory above for each
# file and if all exist post a success message to GitHub for the PR that triggered the test or a
# failure message otherwise

# Context (What is the string the Bamboo CI webhooks trigger the jobs as)
# Used to report on and update a specific context in GitHub Pull Requests
context="Firmware Tests"

# Run find and process the output into an array
directory_paths=($(find ${ZOOX_FIRMWARE_ROOT} -name "create_bamboo_sentinel_file.sh"))

# Determine the names of the directories which contain the "create_bamboo_sentinel_file.sh"
# Note: the names are like "emu" or "bmu" not /zoox/driving/firmware/validation/emu
test_directories=()
for path in "${directory_paths[@]}"; do
    test_directories+=($(echo $path | rev | cut -d'/' -f2- | rev | xargs basename))
done

# Convenience var for Python virtual environment
virt="gsu_env"
zoox_pypi="https://pypi.zooxlabs.com/simple"
public_pypi="https://pypi.python.org/simple"

# Check for virtual environment
if [ ! -d $virt ]; then
    # requires Python 3.6 to be installed somewhere on host OS
    virtualenv -p $(which python3) $virt --python=python3.6
fi

# If any other virtual environment is active, deactivate it (this isn't likely in CI/CD pipeline
# but it might be true if this script is running on someone's local system. Just to be safe.)
deactivate 2> /dev/null

# Activate virtual environment
. $virt/bin/activate

# Install gitsmoke package (force re-install)
pip install gitsmoke --index-url $zoox_pypi --extra-index-url $public_pypi

# (step 2)
# Determine Bamboo Status by checking for sentinel files
checks_passed=0
for test_directory in "${test_directories[@]}"; do
    if [ -f "$bamboo_build_working_directory/${test_directory}_github.sentinel" ]; then
        ((++checks_passed))
    fi
done

# If the checks count is not equal to the number of sentinel file scripts (sentinel files that
# should be present) then the checks overall have failed. Otherwise success.
if [ $checks_passed -eq ${#test_directories[@]} ]; then
    status="success"
else
    status="failure"
fi

description="$checks_passed/${#test_directories[@]} checks of $context Passed on Build #$bamboo_buildNumber"
# Use Gitsmoke CLI with user/script supplied context to upload result to GitHub Pull Request status
gitsmoke -r $status -s $bamboo_repository_revision_number -u $bamboo_buildResultsUrl -c \
    "$context" -d "$description"
