trigger: none
pr:
  branches:
    include:
    - main
    - release/*

jobs:
################################################################################
- job: check_run_pipeline
################################################################################
  displayName: Check pipeline preconditions (changes ARE NOT in either edgelet, docs, or mqtt folder)
  pool:
    name: $(pool.linux.name)
    demands:
    - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
  steps:
  - bash: |
      git log -m -1 --name-only --first-parent --pretty="" | egrep -i -v '^(edgelet|doc|mqtt)'
      if [[ $? == 0 ]]; then
        echo "Detected changes outside of edgelet, docs and mqtt folders"
        echo "##vso[task.setvariable variable=RUN_PIPELINE;isOutput=true]TRUE"
      fi
    displayName: Check changes in sources
    name: check_files

################################################################################
- job: linux_amd64
################################################################################
  displayName: Linux amd64
  dependsOn: check_run_pipeline
  condition: eq(dependencies.check_run_pipeline.outputs['check_files.RUN_PIPELINE'], 'true')
  pool:
    name: $(pool.linux.name)
    demands:
    - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
  steps:
  - script: scripts/linux/installPrereqs.sh
    displayName: Install Prerequisites
  - script: |
      dotnet test \
        --configuration $(configuration) \
        --filter 'Category=Unit' \
        --logger trx \
    displayName: Build and run unit tests
  - task: PublishTestResults@2
    displayName: Publish Test Results
    inputs:
      testRunner: VSTest
      testResultsFiles: '**/TestResults/*.trx'
    condition: succeededOrFailed()

################################################################################
- job: code_coverage
################################################################################
  displayName: Code coverage
  dependsOn: check_run_pipeline
  condition: eq(dependencies.check_run_pipeline.outputs['check_files.RUN_PIPELINE'], 'true')
  variables:
    coverage.goal: 58
  pool:
    name: $(pool.linux.name)
    demands:
    - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
  steps:
  - script: scripts/linux/installPrereqs.sh
    displayName: Install Prerequisites
  - script: |
      dotnet test \
        --logger trx \
        --results-directory '$(Agent.TempDirectory)' \
        --filter 'Category=Unit' \
        --collect 'xplat code coverage' \
        --settings CodeCoverage.runsettings
    displayName: Run unit tests with code coverage
  - script: |
      reportgenerator \
        -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml \
        -targetdir:$(Build.SourcesDirectory) \
        -reporttypes:Cobertura
    displayName: Generate coverage report
    condition: succeededOrFailed()
  - task: PublishCodeCoverageResults@2
    displayName: Publish coverage report
    inputs:
      summaryFileLocation: '$(Build.SourcesDirectory)/Cobertura.xml'
    condition: succeededOrFailed()
  - task: BuildQualityChecks@9
    displayName: 'Check build quality'
    inputs:
      checkCoverage: true
      coverageFailOption: fixed
      coverageType: lines
      coverageThreshold: $(coverage.goal)
    condition: succeededOrFailed()
