image: itwadic/php7.4-apache2-git-ssh:latest

definitions:
  steps:
    - step: &php-syntax-check
        name: PHP Syntax Check
        script:
          - echo "Checking PHP syntax on changed files..."
          # Fetch the branch to compare against
          - git fetch origin $BITBUCKET_BRANCH
          # Find changed PHP files
          - git diff --name-only --diff-filter=ACMRT HEAD~1 HEAD > changed_files.txt

          # Run syntax check on changed files
          - |
            if [ -s changed_files.txt ]; then
            while read file; do
              # Check if the file has a .php extension
              if [[ $file == *.php ]]; then
                php -l "$file";
              fi
            done < changed_files.txt | tee /tmp/lint_results.log;

            if grep -q "Errors parsing" /tmp/lint_results.log; then
              echo "PHP syntax check failed.";

              # Send Slack notification
              curl -X POST -H 'Content-type: application/json' \
                --data '{"text":"🚨 *PHP Syntax Error* - The PHP syntax check has failed for the branch *'$BITBUCKET_BRANCH'*. \n *Commit Link:* https://bitbucket.org/scopetechnologies/tracescope-plugin/commits/'$BITBUCKET_COMMIT' \n *Build Link:* https://bitbucket.org/scopetechnologies/tracescope-plugin/pipelines/results/'$BITBUCKET_BUILD_NUMBER' "}' $SLACK_WEBHOOK_URL;

              exit 1;
            else
              echo "PHP syntax check passed.";
            fi
            else
              echo "No PHP files changed.";
            fi
    - step: &deploy-qa
          name: Deploy on QA
          deployment: test
          script:
            - echo "Starting deployment to QA server..."
            - set -e  # Exit immediately if a command exits with a non-zero status
            - curl ifconfig.me
            - | 
              ssh -o StrictHostKeyChecking=no $QA_USER@$QA_SERVER "
              cd /var/www/html/rsoc/qa_RS_OC_1/roofscopeoc &&
              git checkout develop &&
              git pull origin develop
              "
            - |
                # Send Slack notification
                curl -X POST -H 'Content-type: application/json' \
                  --data '{"text":"Tracescope-Plugin:  Branch *'$BITBUCKET_BRANCH'* has been deployed on QA. \n *Commit Link:* https://bitbucket.org/scopetechnologies/tracescope-plugin/commits/'$BITBUCKET_COMMIT' \n *Build Link:* https://bitbucket.org/scopetechnologies/tracescope-plugin/pipelines/results/'$BITBUCKET_BUILD_NUMBER' "}' $SLACK_WEBHOOK_URL;
    - step: &deploy-production
          name: Deploy on Production
          deployment: production
          script:
            - echo "Starting deployment to Production server..."
            - set -e  # Exit immediately if a command exits with a non-zero status
            - | 
              ssh -o StrictHostKeyChecking=no $PROD_USER@$PROD_SERVER "
              cd /var/www/html/plugin/tracescope/ && sudo git pull
              "
            - |
                # Send Slack notification
                curl -X POST -H 'Content-type: application/json' \
                  --data '{"text":"Tracescope-Plugin: Branch *'$BITBUCKET_BRANCH'* has been deployed on Production. \n *Commit Link:* https://bitbucket.org/scopetechnologies/tracescope-plugin/commits/'$BITBUCKET_COMMIT' \n *Build Link:* https://bitbucket.org/scopetechnologies/tracescope-plugin/pipelines/results/'$BITBUCKET_BUILD_NUMBER' "}' $SLACK_WEBHOOK_URL;

pipelines:
  default:
    - step: *php-syntax-check
  branches:
    develop:
      - step: *php-syntax-check
      - step: *deploy-qa
    cherrypickMergeBranch:
      - step: *php-syntax-check
      - step: *deploy-production
