#!/bin/bash

SOURCE_TEST_REPORT_FOLDER=$1
DESTINATION_TEST_REPORT_FILE=$2
REWRITE_TEST_TARGET=$3

if [ $# -ne 3 ]; then
  echo "Usage: $0 SOURCE_TEST_REPORT_FOLDER DESTINATION_TEST_REPORT_FILE REWRITE_TEST_TARGET"
  exit
fi

if [ -z "$SOURCE_TEST_REPORT_FOLDER" ]; then
  echo "No source report folder set, exiting..."
  exit 1
fi

if [ -z "$DESTINATION_TEST_REPORT_FILE" ]; then
  echo "No destination report filename, exiting..."
  exit 1
fi

if [ -z "$REWRITE_TEST_TARGET" ]; then
  echo "No test target for rewriting set, exiting..."
  exit 1
fi

echo "Combining test report files from source folder..."
# shellcheck disable=SC2066
for file in "$SOURCE_TEST_REPORT_FOLDER"/*; do
  echo "Appending $file ..."
  cat "$file" >> "$DESTINATION_TEST_REPORT_FILE"
done

echo "Filtering combined test report file..."

# The following jq command takes a go test JSON report file generated by gotestsum and only takes those lines where
# the Test variable was defined and the Action was either pass or fail. Then, it prints those lines in
# the following format: {"test": "<test-name>", :"<test-platform-name>": "<test-result>"}

# shellcheck disable=SC2002
if [[ -f "$DESTINATION_TEST_REPORT_FILE" ]]; then
  filtered_report=$(cat "$DESTINATION_TEST_REPORT_FILE" | jq -c "select( (.Action == \"pass\" or .Action == \"fail\") and .Test != null ) | {\"test\": .Test, \"$REWRITE_TEST_TARGET\": .Action}")
else
  filtered_report="{}"
fi

echo "$filtered_report" > "$DESTINATION_TEST_REPORT_FILE"
