# ============================================================
# alcyon/identity - Bitbucket Pipelines
# Tests PHPUnit (Orchestra Testbench)
# ============================================================

image: php:8.2-cli-bookworm

definitions:
  caches:
    composer:
      key:
        files:
          - composer.json
      path: vendor

  steps:
    - step: &test-step
        name: Tests PHPUnit
        caches:
          - composer
        script:
          - apt-get update && apt-get install -y unzip libzip-dev libxml2-dev libcurl4-openssl-dev
          - docker-php-ext-install zip bcmath xml curl
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - export COMPOSER_ALLOW_SUPERUSER=1
          - composer install --prefer-dist --no-progress
          - vendor/bin/phpunit

    - step: &create-pr-step
        name: Create PR dev → master
        image: alpine:3.19
        script:
          - apk add --no-cache curl
          - |
            echo "Creation PR dev -> master..."
            HTTP_CODE=$(curl -s -o /tmp/pr_response.txt -w "%{http_code}" \
              -X POST \
              -u "nicephore.allaglo@jupitech.fr:$BB_APP_PASSWORD" \
              -H "Content-Type: application/json" \
              "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_FULL_NAME/pullrequests" \
              -d '{
                "title": "Merge dev (build #'"$BITBUCKET_BUILD_NUMBER"')",
                "source": {"branch": {"name": "dev"}},
                "destination": {"branch": {"name": "master"}},
                "description": "PR auto-generee apres succes des tests (pipeline #'"$BITBUCKET_BUILD_NUMBER"').\n\nCommit: '"$BITBUCKET_COMMIT"'",
                "close_source_branch": false
              }')
            if [ "$HTTP_CODE" = "201" ]; then
              echo "PR creee avec succes"
            elif [ "$HTTP_CODE" = "409" ]; then
              echo "PR deja existante - rien a faire"
            else
              echo "Erreur creation PR (HTTP $HTTP_CODE):"
              cat /tmp/pr_response.txt
              exit 1
            fi

    - step: &deploy-staging-step
        name: Deploy to Staging
        deployment: staging
        image: alpine:3.19
        script:
          - apk add --no-cache openssh-client git
          - mkdir -p ~/.ssh
          - echo "$STAGING_SSH_KEY" | base64 -d > ~/.ssh/id_rsa
          - chmod 600 ~/.ssh/id_rsa
          - ssh-keyscan -H $STAGING_SERVER_IP >> ~/.ssh/known_hosts
          - |
            ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no $STAGING_SSH_USER@$STAGING_SERVER_IP "
              DEPLOY_ROOT=/home/ceterisprime/public_html/app/connect
              SERVICE_DIR=\$DEPLOY_ROOT/identity

              if [ ! -d \$SERVICE_DIR/.git ]; then
                echo 'Premier deploiement: clone...'
                git clone --branch master https://jupitechfr:$BB_APP_PASSWORD@bitbucket.org/alcyonpartners/identity.git \$SERVICE_DIR
              else
                echo 'Mise a jour depuis master...'
                cd \$SERVICE_DIR
                git remote set-url origin https://jupitechfr:$BB_APP_PASSWORD@bitbucket.org/alcyonpartners/identity.git
                git fetch origin
                git reset --hard origin/master
                git remote set-url origin https://bitbucket.org/alcyonpartners/identity.git
              fi
              echo 'Identity package deploye'
            "

pipelines:
  default:
    - step: *test-step

  branches:
    dev:
      - step: *test-step
      - step: *create-pr-step
    master:
      - step: *deploy-staging-step

  custom:
    daily-tests:
      - step: *test-step
