#
# Copyright Super iPaaS Integration LLC, an IBM Company 2024
#
echo "Checking PR is merged or not"
source "${ONE_PIPELINE_PATH}"/tools/get_repo_params

APP_TOKEN_PATH="./app-token"
PR_URL=$(get_env PR_URL "")
read -r APP_REPO_NAME APP_REPO_OWNER APP_SCM_TYPE APP_API_URL < <(get_repo_params "$(load_repo app-repo url)" "$APP_TOKEN_PATH")
curl -L -H "Authorization: Bearer $(cat ${APP_TOKEN_PATH})" -H "Accept: application/vnd.github+json" "${APP_API_URL}/repos/${APP_REPO_OWNER}/${APP_REPO_NAME}/branches/$branch/protection" > "$branch_protection_content"
response=$(curl -s -o /dev/null -w "%{http_code}" -X GET \
            --url "${PR_URL}/merge" \
            --header "Authorization: Bearer $(cat "${APP_TOKEN_PATH}")")

if [ "$response" -eq 204 ]; then
  echo "Pull request #$PR_NUMBER is merged."
  exit 1
elif [ "$response" -eq 404 ]; then
  echo "Pull request #$PR_NUMBER is not merged."
else
  echo "Failed to check merge status. HTTP status code: $response"
  exit 2
fi
            
      
