# Pipeline for Salesforce deployment check & MegaLinter check
#
# This pipeline is triggered when a commit is added to a Pull Request
#
# This means the Pull Request should already exist before commiting your work or you'll have to push an empty commit
# to trigger the pipeline.
#
# Setup:
#   - Go to Pipelines
#   - Select or create a new pipeline (you may have to Create new -> Existing Azure Pipelines YAML file)
#   - Edit this new pipeline
#   - Add your variables in the "variables" section and tick "Keep this value secret"
#   - Go to Repos -> Branches and lock your majors branches

#  /!\ This part is only for PR pipelines

#   - Go to Pipelines and select your pipeline then Edit it
#   - Open the menu ⋮ and select Triggers
#   - Tick "Override the YAML continuous integration trigger from here" and select "Disable continuous integration"

#   - Now go to Repos -> Branches and create a "Branch policy" for each of your major branches
#   - In Build Validation, click + and then select your Build pipeline (even if it is prefilled, it's buggy and you
#     need to manually select it again) and keep the default settings for the other options then save.
#         This causes the pipeline to be triggered on Pull Requests even if we previously disabled it.

name: Check Pull Request

# Mandatory variables, used by jobs
variables:
  - name: FORCE_COLOR
    value: "1"
  - name: BRANCH_NAME
    value: $[replace(variables['System.PullRequest.TargetBranch'], 'refs/heads/', '')]

jobs:
  # Simulate SFDX deployment
  - job: DeploymentCheck
    timeoutInMinutes: 150
    pool:
      vmImage: ubuntu-latest
    steps:
      # Checkout repo
      - checkout: self
        fetchDepth: 0
        persistCredentials: true
        displayName: Git Checkout

      # Setup Node.js
      - task: UseNode@1
        inputs:
          version: ">=20.0.0"
        displayName: "Use Node.js"

      # Install SFDX & Dependencies
      - script: |
          npm install @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
        displayName: "Install SFDX & plugins"

      # Login & check deployment to PR target branch related org (configuration: https://hardisgroupcom.github.io/sfdx-hardis/salesforce-ci-cd-setup-auth/ )
      - script: |
          sf hardis:auth:login
          sf hardis:project:deploy:smart --check
        env:
          SFDX_CLIENT_ID_INTEGRATION: $(SFDX_CLIENT_ID_INTEGRATION)
          SFDX_CLIENT_KEY_INTEGRATION: $(SFDX_CLIENT_KEY_INTEGRATION)
          CI_COMMIT_REF_NAME: $(BRANCH_NAME)
          CONFIG_BRANCH: $(BRANCH_NAME)
          ORG_ALIAS: $(BRANCH_NAME)
          CI: "true"
          SFDX_DEPLOY_WAIT_MINUTES: 150
          SLACK_TOKEN: $(SLACK_TOKEN)
          SLACK_CHANNEL_ID: $(SLACK_CHANNEL_ID)
          NOTIF_EMAIL_ADDRESS: $(NOTIF_EMAIL_ADDRESS)
          GENERIC_TICKETING_PROVIDER_REGEX: $(GENERIC_TICKETING_PROVIDER_REGEX)
          GENERIC_TICKETING_PROVIDER_URL_BUILDER: $(GENERIC_TICKETING_PROVIDER_URL_BUILDER)
          JIRA_HOST: $(JIRA_HOST)
          JIRA_EMAIL: $(JIRA_EMAIL)
          JIRA_TOKEN: $(JIRA_TOKEN)
          JIRA_PAT: $(JIRA_HOST)
          JIRA_TICKET_REGEX: $(JIRA_TICKET_REGEX)
          OPENAI_API_KEY: $(OPENAI_API_KEY)
          SFDX_AUTH_URL_TECHNICAL_ORG: $(SFDX_AUTH_URL_TECHNICAL_ORG)
          SYSTEM_ACCESSTOKEN: $(System.AccessToken)
          CI_SFDX_HARDIS_AZURE_TOKEN: $(System.AccessToken)
          SYSTEM_COLLECTIONURI: $(System.CollectionUri)
          SYSTEM_TEAMPROJECT: $(System.TeamProject)
          SYSTEM_JOB_DISPLAY_NAME: $(System.JobDisplayName)
          SYSTEM_JOB_ID: $(System.JobId)
          SYSTEM_PULLREQUEST_PULLREQUESTID: $(System.PullRequest.PullRequestId)
          BUILD_REPOSITORY_ID: $(Build.Repository.ID)
          BUILD_REPOSITORYNAME: $(Build.Repository.Name)
          BUILD_SOURCEBRANCHNAME: $(Build.SourceBranchName)
          BUILD_BUILD_ID: $(Build.BuildId)
          SFDX_DISABLE_FLOW_DIFF: false # Set to true to disable Flow doc during CI/CD setup
        displayName: "Simulate deploy to org"

  # Run MegaLinter to detect linting and security issues
  # https://megalinter.io/latest/flavors/salesforce/
  # Config is editable in .mega-linter.yml
  - job: MegaLinter
    pool:
      vmImage: ubuntu-latest
    steps:
      # Pull MegaLinter docker image
      - script: docker pull oxsecurity/megalinter-salesforce:latest
        displayName: Pull MegaLinter
      # Run MegaLinter
      - script: |
          docker run -v $(System.DefaultWorkingDirectory):/tmp/lint \
            --env-file <(env | grep -e SYSTEM_ -e BUILD_ -e TF_ -e AGENT_) \
            -e CI=true \
            -e SYSTEM_ACCESSTOKEN=$(System.AccessToken) \
            -e GIT_AUTHORIZATION_BEARER=$(System.AccessToken) \
            oxsecurity/megalinter-salesforce:latest
        displayName: Run MegaLinter
      # Publish Megalinter reports
      - publish: $(System.DefaultWorkingDirectory)/megalinter-reports/
        condition: succeededOrFailed()
        artifact: megalinter-reports
        displayName: Publish reports
