#!/bin/bash

jq --null-input --arg yaml "$1" '.content=$yaml' \
  | curl "https://git.ethitter.com/api/v4/ci/lint?include_merged_yaml=true" \
    --silent \
    --header 'Content-Type: application/json' \
    --header "PRIVATE-TOKEN: $GITLAB_API_PAT" \
    --data @- \
    -o result.json

echo "**********************************************************"
echo "MERGED YAML:"
jq --raw-output '.merged_yaml' < result.json

echo "**********************************************************"
echo "ERRORS:"
jq '.errors' < result.json

echo "**********************************************************"
echo "WARNINGS:"
jq '.warnings' < result.json

echo "**********************************************************"
echo "IS VALID:"
jq '.valid' < result.json

if [ "$(jq '.valid' < result.json)" = "true" ]; then
  exit 0
else
  exit 1
fi