image: maven:3.8.6-openjdk-11

definitions:
  steps:
    - step: &build-step
        name: SonarQube analysis
        script:
          - pipe: sonarsource/sonarqube-scan:2.0.1
            variables:
              SONAR_HOST_URL: ${SONAR_HOST_URL} # Get the value from the repository/workspace variable.
              SONAR_TOKEN: ${SONAR_TOKEN} # Get the value from the repository/workspace variable. You shouldn't set secret in clear text here.
        artifacts:
          - sonar_results.txt
              
    - step: &OWASPScan
        name: OWASP Dependency-Check
        script:
          - apt-get update && apt-get install -y wget unzip
          - wget https://github.com/jeremylong/DependencyCheck/releases/download/v7.1.1/dependency-check-7.1.1-release.zip
          - unzip dependency-check-7.1.1-release.zip
          - ./dependency-check/bin/dependency-check.sh --project my-app --scan . --format ALL
        artifacts:
          - dependency-check-report.html

    - step: &SeleniumScan
        name: Run Selenium IDE Tests
        services:
          - docker  # Running in Docker is required for Selenium Runner
        ports:
        - "4444:4444"
        caches:
          - maven
        script:
          # Install Selenium IDE Runner via npm (Node.js)
          - apt-get update && apt-get install -y curl
          - curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
          - apt-get install -y nodejs
          - npm install -g selenium-side-runner
          - sleep 10
          # Run the .side file test (replace 'tests.side' with your actual file)
          - selenium-side-runner -s http://localhost:4444/wd/hub -c "browserName=chrome" ./tests/selenium/datafy_web.side -- debug
          # Capture Selenium logs or test results for emailing
          - cat test-results.log > selenium-logs.txt  # This file will be created by selenium-side-runner
        artifacts:
          - selenium-logs.txt

    - step: &NodeInstall
        name: Install Node.js and npm
        script:
          # Install Node.js and npm
          - sed -i 's/jessie/buster/g' /etc/apt/sources.list
          - apt-get update
          - apt-get install -y curl
          - curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
          - apt-get install -y nodejs
    - step: &EmailResults
        name: Email Results
        script:
          # Install mailutils
          - apt-get update && apt-get install -y mailutils
          # Email SonarQube, OWASP, and Selenium test results
          - echo "SonarQube Results - Datafy" | mail -s "SonarQube Results" jaco@overdrive.co.za
          - cat dependency-check-report.html | mail -s "OWASP Dependency Check Results" jaco@overdrive.co.za
          - cat selenium-logs.txt | mail -s "Selenium Test Results" jaco@overdrive.co.za
    - step: &SaveResults
        name: Save Results to Supabase
        script:
          # Install curl for Supabase API interaction
          - apt-get update && apt-get install -y curl jq

          # Read test results from the files
          - SONAR_RESULTS="" #$(cat sonar_results.txt)
          - OWASP_RESULTS=$(cat owasp_results.txt)
          - SELENIUM_RESULTS="" #$(cat selenium_results.txt)

          # Get the repository name from Bitbucket environment variables
          - REPO_NAME=$(echo $BITBUCKET_REPO_FULL_NAME)

          # Construct JSON payload for Supabase API
          - |
            JSON_PAYLOAD=$(jq -n \
              --arg sonar "$SONAR_RESULTS" \
              --arg owasp "$OWASP_RESULTS" \
              --arg selenium "$SELENIUM_RESULTS" \
              --arg repo "$REPO_NAME" \
                '{ sonar_results: $sonar, owasp_results: $owasp, selenium_results: $selenium, repo_name: $repo }')

          # Insert results into Supabase using REST API
          - |
            curl -X POST https://supabase.appmonitor.co.za/rest/v1/test_results \
              -H "apikey: $SUPABASE_API_KEY" \
              -H "Authorization: Bearer $SUPABASE_SERVICE_ROLE_KEY" \
              -H "Content-Type: application/json" \
              -d "$JSON_PAYLOAD"

  caches:
    sonar: ~/.sonar      

clone:
  depth: full

pipelines:
  branches:
    '{main}':
      - step: *build-step
      - step: *OWASPScan
      #- step: *SeleniumScan
      #- step: *NodeInstall
      #- step: *EmailResults
      - step: *SaveResults