# Builds a pull request with 2 commit statuses and codeowner approval required
# https://docs.github.com/en/rest/reference/repos#create-a-commit-status
#
# Important: This is not using the GitHub Checks API (GitHub App required)
#
normal=$(tput sgr0)
highlight=$(tput setaf 2)

printf "$highlight"

cat << EOF

  ________            ____
 /_  __/ /_  ___     / __ \____ _      _____  _____
  / / / __ \/ _ \   / /_/ / __ \ | /| / / _ \/ ___/
 / / / / / /  __/  / ____/ /_/ / |/ |/ /  __/ /
/_/ /_/ /_/\___/  /_/    \____/|__/|__/\___/_/

EOF

printf "${normal}"

# 
#
#
 printf "${highlight} - Creating repo: ${normal}"
./create-repo-testrepo.sh | jq -r '.name'
./add-team-to-repo.sh
 printf "${highlight} - Creating docs/README: ${normal}"
./create-commit-readme.sh | jq -r ".content.html_url"
 printf "${highlight} - Creating CODEOWNERS: ${normal}"
./create-commit-codeowners.sh| jq -r ".content.html_url"
sleep 2
 printf "${highlight} - Creating new branch: ${normal}"
./create-branch-newbranch.sh | jq -r '.url'
 printf "${highlight} - Creating a commit on the new branch: ${normal}"
./create-commit-on-new-branch.sh | jq -r ".content.html_url"
 printf "${highlight} - Creating an issue: ${normal}"
./create-repo-issue.sh | jq -r '.html_url'
 printf "${highlight} - Creating a pull request: ${normal}"
./create-pull-request.sh | jq -r '.html_url'
# set the branch protection rules for main
 printf "${highlight} - Setting branch protection rules on default branch: ${normal}"
./set-branch-protection.sh | jq -r '.url'

 printf "${highlight} - Creating a release: ${normal}"
./create-release.sh  | jq -r '.url'
 printf "${highlight} - Adding a .gitattributes file to new branch: ${normal}"
./create-commit-gitattributes.sh | jq -r ".content.html_url"

 printf "${highlight} - Creating commit status for an optional check: ${normal}"
./create-commit-status.sh pending ci/commit-status-optional | jq -r '.url'
 printf "${highlight} - Creating commit status for an required check: ${normal}"
./create-commit-status.sh pending ci/commit-status-required | jq -r '.url'
 printf "${highlight} - Updating branch protection with required check: ${normal}"
./update-branch-protection-with-required-check.sh | jq -r '.url'

sleep 3

. .gh-api-examples.conf

clear
echo ------------------------------------------- 
if [ ${pr_approver_token} != "replace_with_a_PAT" ]; then
    echo "Do you want to create an approving review by a codeowner using token ${pr_approver_token}:"
    x=n
    read x
    if [ "$x" != "y" ]; then
        echo "Skip creating approval review by codeowner"
    elif [ "$x" == "y" ]; then
        printf "${highlight} - Creating an approving review for a pull request: ${normal}"
        ./create-approving-review-for-a-pull-request.sh | jq -r '.html_url'
    fi
fi

echo ------------------------------------------- 

# Ask the user if they want to run these two commands to update the check statuses

clear
echo -------------------------------------------
echo
echo "Press y to update the optional commit status to success" 
echo "Press any other key to skip this step"
echo
x=n
read x
if [ "$x" != "y" ]; then
    echo "Skipping updating the optional commit status"
elif [ "$x" == "y" ]; then
    printf "${highlight} - Updating status for optional commit-status-opitonal to success: ${normal}"
    ./create-commit-status.sh success ci/commit-status-optional | jq -r '.id'
fi

clear
x=n
echo
echo "Press y to update the required commit status to success" 
echo "Press any other key to skip this step"
echo
read x
if [ "$x" != "y" ]; then
    echo "Skipping updating ci/commit-status-required" 
elif [ "$x" == "y" ]; then
    printf "${highlight} - Updating status for required check to success: ${normal}"
    ./create-commit-status.sh success ci/commit-status-required | jq -r '.id'
fi


clear
x=n
echo
echo "Press y to create a check run" 
echo "Press any other key to skip this step"
echo
read x
if [ "$x" != "y" ]; then
    echo "Skip creating check run"
elif [ "$x" == "y" ]; then
    printf "${highlight} - Creating check run with annotation: ${normal}"
    check_run_id=$(./tiny-create-check-run-with-annotation.sh | jq -r '.id')
    printf "${highlight} - Created check run id: ${check_run_id} ${normal}"
fi

clear
x=n
echo
echo "Press y to update check run id ${check_run_id}" 
echo "Press any other key to skip this step"
echo
read x
if [ "$x" != "y" ]; then
    echo "Skip updating check run"
elif [ "$x" == "y" ]; then
    printf "${highlight} - Updating check run id: $check_run_id ${normal}"
    ./tiny-update-a-check-run.sh ${check_run_id}
fi


