#!/usr/bin/env bash
#
# just the beginnings of an integration testing script
# more of a way to generate logs to eyeball for gross
# errors than anything really automated.
#

# TODO(rfagen): add a test that verifies a protected branch can be updated by a non-fast forward base branch

set -o pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

function usage {
  echo " "
  echo "usage: $0 [--help|--create|--update|--delete]"
  echo " "
  echo "where:"
  echo "        --help    displays this"
  echo "        --create  runs only the 'create' tests"
  echo "        --update  runs only the 'update' tests"
  echo "        --delete  runs only the 'delete' tests"
  echo " "
  echo "        no options will run all three sets of tests"
  echo " "
  exit 1
}

function mock_create {
    info_message "but first create some branches to $1"
    RETC=0
    ./manage_protected_branch.py create feature/bar --owner ci-testing | grep This
    RETC+=$?
    ./manage_protected_branch.py create develop/bar --owner ci-testing | grep This
    RETC+=$?
    ./manage_protected_branch.py create release/bar --owner ci-testing | grep This
    RETC+=$?
    if [[ $RETC -ne 0 ]]; then
      echo -e "$RED Something went wrong... ($RETC)  $NC"
      exit
    fi
    info_message "ok. done with setup"
  }

CREATE=0
UPDATE=0
DELETE=0

#no options defaults to running all three sets
if [[ $# -eq 0 ]]; then
  CREATE=1
  UPDATE=1
  DELETE=1
elif [[ $# -gt 1 ]]; then
  usage
else
  case $1 in
    --help)
      usage
      ;;
    --create)
      CREATE=1
      ;;
    --update)
      UPDATE=1
      ;;
    --delete)
      DELETE=1
      ;;
    *)
      echo -e $RED Unrecognized option: $* $NC
      usage
      ;;
  esac
fi

function horizontal_line {
echo "==============================================="
echo
}

function pass_message {
  echo -en $GREEN
  echo -n "This should pass: $*"
  echo -e $NC
}

function fail_message {
  echo -en $RED
  echo -n "This should 'fail': $*"
  echo -e $NC
}

function dry_message {
  echo -en $YELLOW
  echo -n "This is a dry-run: $*"
  echo -e $NC
}

function info_message {
  echo -en $YELLOW
  echo -n "... $*"
  echo -e $NC
}

echo "Clean up the branches and remotes"
echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch develop/bar > /dev/null 2>&1
echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch develop/plugh > /dev/null 2>&1
echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch feature/bar > /dev/null 2>&1
echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch release/bar > /dev/null 2>&1
echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch release/20.99 > /dev/null 2>&1
git remote remove del_old_prot_repo > /dev/null 2>&1

if [[ $CREATE -eq 1 ]]; then
  info_message "Run the manage_protected_branch.py create tests"
  horizontal_line
  fail_message - not a protected branch prefix
  echo "@@@ running @@@ ./manage_protected_branch.py create justabranch --owner ci-testing"
  ./manage_protected_branch.py create justabranch --owner ci-testing
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./manage_protected_branch.py create develop/bar --owner ci-testing"
  ./manage_protected_branch.py create develop/bar --owner ci-testing
  horizontal_line
  fail_message - update not create
  echo "@@@ running @@@ ./manage_protected_branch.py create develop/bar --owner ci-testing"
  ./manage_protected_branch.py create develop/bar --owner ci-testing
  horizontal_line
  fail_message - create no base branch
  echo "@@@ running @@@ ./manage_protected_branch.py create develop/plugh --owner ci-testing --base feature/bar"
  ./manage_protected_branch.py create develop/plugh --owner ci-testing --base feature/bar
  horizontal_line
  fail_message - no such owner
  echo "@@@ running @@@ ./manage_protected_branch.py create feature/bar --owner arglebargle"
  ./manage_protected_branch.py create feature/bar --owner arglebargle
  horizontal_line
  fail_message - no such owner + debug info
  echo "@@@ running @@@ ./manage_protected_branch.py create feature/bar --owner arglebargle --debug"
  ./manage_protected_branch.py create feature/bar --owner arglebargle --debug
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./manage_protected_branch.py create feature/bar --owner ci-testing"
  ./manage_protected_branch.py create feature/bar --owner ci-testing
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./manage_protected_branch.py create develop/plugh --owner ci-testing --base feature/bar"
  ./manage_protected_branch.py create develop/plugh --owner ci-testing --base feature/bar
  horizontal_line
  fail_message - update not create, debug info
  echo "@@@ running @@@ ./manage_protected_branch.py create feature/bar --owner ci-testing --debug"
  ./manage_protected_branch.py create feature/bar --owner ci-testing --debug
  horizontal_line
  dry_message - update not create fail
  echo "@@@ running @@@ ./manage_protected_branch.py create feature/bar --owner ci-testing --dry-run"
  ./manage_protected_branch.py create feature/bar --owner ci-testing --dry-run
  horizontal_line
  dry_message - pass
  echo "@@@ running @@@ ./manage_protected_branch.py create feature/plugh --owner ci-testing  --dry-run"
  ./manage_protected_branch.py create feature/plugh --owner ci-testing  --dry-run
  horizontal_line
  fail_message - update not create
  echo "@@@ running @@@ ./manage_protected_branch.py create feature/bar --owner ci-testing --repo driving"
  ./manage_protected_branch.py create feature/bar --owner ci-testing --repo driving
  horizontal_line
  pass_message - a restricted branch
  echo "@@@ running @@@ ./manage_protected_branch.py create release/20.99 --owner ci-testing  --do-not-preserve-statuses"
  ./manage_protected_branch.py create release/bar --owner ci-testing --do-not-preserve-statuses
  horizontal_line
fi

if [[ $UPDATE -eq 1 ]]; then
  info_message "Run the manage_protected_branch.py update tests"
  if [[ $CREATE -eq 0 ]]; then
    mock_create update
  fi
  pass_message
  echo "@@@ running @@@ ./manage_protected_branch.py update feature/bar --owner ci-testing "
  ./manage_protected_branch.py update feature/bar --owner ci-testing
  horizontal_line
  pass_message - debug info
  echo "@@@ running @@@ ./manage_protected_branch.py update feature/bar --owner ci-testing  --debug"
  ./manage_protected_branch.py update feature/bar --owner ci-testing  --debug
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./manage_protected_branch.py update develop/bar --owner ci-testing --base feature/bar "
  ./manage_protected_branch.py update develop/bar --owner ci-testing --base feature/bar
  horizontal_line
  pass_message - only code review
  echo "@@@ running @@@ ./manage_protected_branch.py update release/bar --owner ci-testing  --do-not-preserve-statuses"
  ./manage_protected_branch.py update release/bar --owner ci-testing --do-not-preserve-statuses
  horizontal_line
fi

if [[ $DELETE -eq 1 ]]; then
  info_message "Run the delete_old_protected_branches.py tests"
  if [[ $CREATE -eq 0 ]]; then
    if [[ $UPDATE -eq 0 ]]; then
      mock_create delete
    fi
  fi
  pass_message - no-op
  echo "@@@ running @@@ ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch develop/bar --dry-run"
  echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch develop/bar --dry-run
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch develop/bar"
  echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch develop/bar
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch feature/bar"
  echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch feature/bar
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch release/bar"
  echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving --branch release/bar
  horizontal_line
  pass_message
  echo "@@@ running @@@ ./delete_old_protected_branches.py --owner ci-testing --repo driving "
  echo "y" | ./delete_old_protected_branches.py --owner ci-testing --repo driving
  horizontal_line
fi
