# You may:
# - Update manifest/package-skip-items.xml to filter elements to retrieve

# Doc & support: https://sfdx-hardis.cloudity.com/salesforce-monitoring-home/

image: node:20
definitions:
  services:
    docker:
      memory: 3072 # If you have crashes, play with this number

pipelines:
  default:
    ##############################################
    ### Sfdx Sources Backup + Push new commit ####
    ##############################################
    - step:
        name: Backup sfdx-hardis
        clone:
          depth: full
        script:
          # Install SF Cli & dependencies
          - npm install --no-cache @salesforce/cli --global
          - sf plugins install @salesforce/plugin-packaging
          - echo 'y' | sf plugins install sfdx-hardis
          - echo 'y' | sf plugins install sfdx-git-delta
          - sf version --verbose --json
          - export BRANCH_NAME=$(echo "$BITBUCKET_BRANCH" | sed 's/refs\/heads\///')
          - export CI_COMMIT_REF_NAME=$BRANCH_NAME
          - export CONFIG_BRANCH=$BRANCH_NAME
          - export ORG_ALIAS=$BRANCH_NAME
          - export FORCE_COLOR=1
          # Login
          - sf hardis:auth:login
          # Backup
          - sf hardis:org:monitor:backup
          # Commit & push
          - git status
          - git add --all
          - git commit -m "Org state on $(date -u +'%Y-%m-%d %H:%M') for $BRANCH_NAME [skip ci]" || echo "No changes to commit"
          - git push --set-upstream origin "$BRANCH_NAME"
        artifacts:
          - hardis-report/**

    - parallel:
        ######################
        ### Run Apex Tests ###
        ######################
        - step:
            name: Apex Tests sfdx-hardis
            script:
              # Install SF Cli & dependencies
              - npm install --no-cache @salesforce/cli --global
              - sf plugins install @salesforce/plugin-packaging
              - echo 'y' | sf plugins install sfdx-hardis
              - echo 'y' | sf plugins install sfdx-git-delta
              - sf version --verbose --json
              - export BRANCH_NAME=$(echo "$BITBUCKET_BRANCH" | sed 's/refs\/heads\///')
              - export CI_COMMIT_REF_NAME=$BRANCH_NAME
              - export CONFIG_BRANCH=$BRANCH_NAME
              - export ORG_ALIAS=$BRANCH_NAME
              - export FORCE_COLOR=1
              # Get latest commit of the branch
              - git pull origin "${BRANCH_NAME}"
              # Login
              - sf hardis:auth:login
              # Apex tests
              - sf hardis:org:test:apex
            artifacts:
              - hardis-report/**

        ############################################################
        ### Run MegaLinter to detect quality and security issues ###
        ############################################################
        - step:
            name: Run MegaLinter
            image: oxsecurity/megalinter-salesforce:latest

            script:
              # Get latest commit of the branch
              - export BRANCH_NAME=$(echo "$BITBUCKET_BRANCH" | sed 's/refs\/heads\///')
              - git pull origin "${BRANCH_NAME}"
              - export DEFAULT_WORKSPACE=$BITBUCKET_CLONE_DIR && bash /entrypoint.sh
            artifacts:
              - megalinter-reports/**

        # Other monitoring tools
        - step:
            name: Other Monitoring checks sfdx-hardis
            script:
              # Install SF Cli & dependencies
              - npm install --no-cache @salesforce/cli --global
              - sf plugins install @salesforce/plugin-packaging
              - echo 'y' | sf plugins install sfdx-hardis
              - echo 'y' | sf plugins install sfdx-git-delta
              - sf version --verbose --json
              - export BRANCH_NAME=$(echo "$BITBUCKET_BRANCH" | sed 's/refs\/heads\///')
              - export CI_COMMIT_REF_NAME=$BRANCH_NAME
              - export CONFIG_BRANCH=$BRANCH_NAME
              - export ORG_ALIAS=$BRANCH_NAME
              - export FORCE_COLOR=1
              # Get latest commit of the branch
              - git pull origin "${BRANCH_NAME}"
              # Login
              - sf hardis:auth:login
              # Other monitoring tools
              - sf hardis:org:monitor:all
            artifacts:
              - hardis-report/**
