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

jobs:
################################################################################
  - job: check_run_pipeline
################################################################################
    displayName: Check pipeline preconditions (changes ARE in builds or edgelet)
    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 '^(rust-toolchain\.toml|builds|edgelet)'
          if [[ $? == 0 ]]; then
            echo "Detected changes inside builds or edgelet 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: echo "##vso[task.setvariable variable=NO_VALGRIND;]true"
        displayName: Set env variables
      - bash: scripts/linux/generic-rust/install.sh --project-root "edgelet"
        displayName: Install Rust
      - script: |
          source $HOME/.cargo/env
          cd edgelet
          make \
            CONNECT_MANAGEMENT_URI=unix:///var/run/iotedge/mgmt.sock \
            CONNECT_WORKLOAD_URI=unix:///var/run/iotedge/workload.sock \
            LISTEN_MANAGEMENT_URI=fd://aziot-edged.mgmt.socket \
            LISTEN_WORKLOAD_URI=fd://aziot-edged.workload.socket \
            release
        displayName: build
      - bash: edgelet/build/linux/test.sh
        displayName: Test

################################################################################
  - job: linux_arm32v7
################################################################################
    displayName: Linux arm32v7
    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
    variables:
      IOTEDGE_HOMEDIR: /tmp
    steps:
      - bash: 'echo "##vso[task.setvariable variable=PATH;]${CARGO_HOME:-"$HOME/.cargo"}/bin:$PATH"'
        displayName: Modify path
      - bash: |
          BASE_VERSION=`cat $BUILD_SOURCESDIRECTORY/edgelet/version.txt`
          VERSION="$BASE_VERSION$BUILD_BUILDNUMBER"
          echo "##vso[task.setvariable variable=VERSION;]$VERSION"
        displayName: Set Version
      - bash: scripts/linux/generic-rust/install.sh --project-root "edgelet"
        displayName: Install Rust
      - script: "cargo install cross --version ^0.2 --locked"
        displayName: "Install cross"
      - script: "cross build --target armv7-unknown-linux-gnueabihf"
        displayName: armv7-unknown-linux-gnueabihf build
        workingDirectory: $(Build.SourcesDirectory)/edgelet
      - script: "cross test --target armv7-unknown-linux-gnueabihf"
        displayName: armv7-unknown-linux-gnueabihf test
        workingDirectory: $(Build.SourcesDirectory)/edgelet

################################################################################
  - job: linux_arm64v8
################################################################################
    displayName: Linux arm64v8
    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
    variables:
      IOTEDGE_HOMEDIR: /tmp
    steps:
      - bash: 'echo "##vso[task.setvariable variable=PATH;]$HOME/.cargo/bin:$PATH"'
        displayName: Modify path
      - bash: |
          BASE_VERSION=`cat $BUILD_SOURCESDIRECTORY/edgelet/version.txt`
          VERSION="$BASE_VERSION$BUILD_BUILDNUMBER"
          echo "##vso[task.setvariable variable=VERSION;]$VERSION"
        displayName: Set Version
      - bash: scripts/linux/generic-rust/install.sh --project-root "edgelet"
        displayName: Install Rust
      - script: "cargo install cross --version ^0.2 --locked"
        displayName: "Install cross"
      - script: "cross build --target aarch64-unknown-linux-gnu"
        displayName: aarch64-unknown-linux-gnu build
        workingDirectory: $(Build.SourcesDirectory)/edgelet
      - script: "cross test --target aarch64-unknown-linux-gnu"
        displayName: aarch64-unknown-linux-gnu test
        workingDirectory: $(Build.SourcesDirectory)/edgelet

################################################################################
  - job: style_check
################################################################################
    displayName: Style Check
    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: echo "##vso[task.setvariable variable=NO_VALGRIND;]true"
        displayName: Set env variables
      - bash: scripts/linux/generic-rust/install.sh --project-root "edgelet"
        displayName: Install Rust
      - bash: scripts/linux/generic-rust/format.sh --project-root "edgelet"
        displayName: Format Code
      - bash: edgelet/build/linux/clippy.sh
        displayName: Clippy

################################################################################
  - 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: 30
    pool:
      name: $(pool.linux.name)
      demands:
        - ImageOverride -equals agent-aziotedge-ubuntu-22.04-msmoby
    steps:
      - script: scripts/linux/generic-rust/install.sh --project-root "edgelet"
        displayName: Install Rust
      - script: |
          . "$HOME/.cargo/env"
          cargo install --locked --version 0.26.1 cargo-tarpaulin
        workingDirectory: edgelet
        displayName: Install Cargo Tarpaulin
      - script: |
          . "$HOME/.cargo/env"
          cargo tarpaulin --out Xml --output-dir .
        displayName: Test
        workingDirectory: edgelet
      - task: PublishCodeCoverageResults@2
        displayName: Publish code coverage results
        inputs:
          summaryFileLocation: "edgelet/cobertura.xml"
      - task: BuildQualityChecks@9
        displayName: "Check build quality"
        inputs:
          checkCoverage: true
          coverageFailOption: fixed
          coverageType: lines
          coverageThreshold: $(coverage.goal)
