name: PR-$(System.PullRequest.PullRequestNumber)-$(Date:yyyyMMdd)$(Rev:.rr)
trigger:
  # Batch merge builds together while a merge build is running
  batch: true
  branches:
    include:
    - master
    - release*
    - feature*

pr:
  branches:
    include:
    - master
    - release*
    - feature*

resources:
  repositories:
  - repository: ComplianceRepo
    type: github
    endpoint: PowerShell
    name: PowerShell/compliance
    ref: master

variables:
  - name: repoFolder
    value: PowerShell

jobs:
- job: CI_Compliance
  displayName: CI Compliance

  pool:
    vmImage: windows-latest

  variables:
    - name: repoPath
      value: $(Agent.BuildDirectory)\$(repoFolder)

  steps:
  - checkout: self
    clean: true
    path: $(repoFolder)

  - checkout: ComplianceRepo

  - template: ci-compliance.yml@ComplianceRepo

- job: Linux_CI
  displayName: Markdown and Common Tests

  pool:
    vmImage: ubuntu-16.04

  variables:
    - name: repoPath
      value: $(Agent.BuildDirectory)/$(repoFolder)

  steps:
  - checkout: self
    clean: true
    path: $(repoFolder)

  - checkout: ComplianceRepo

  - powershell: |
      Get-ChildItem -Path env:
    displayName: Capture Environment
    condition: succeededOrFailed()

  - powershell: |
      Install-module Pester -Scope CurrentUser -Force -MaximumVersion 4.99
    displayName: Install Pester
    condition: succeededOrFailed()

  - bash: |
      curl -o- --progress-bar -L https://yarnpkg.com/install.sh | bash
    displayName: Bootstrap Yarn
    condition: succeededOrFailed()

  - bash: |
      sudo yarn global add markdown-spellcheck@0.11.0
    displayName: Install mdspell
    condition: succeededOrFailed()

  - bash: |
      mdspell '**/*.md' '!**/Pester/**/*.md' '!**/dotnet-tools/**/*.md' --ignore-numbers --ignore-acronyms --report --en-us;
    displayName: Test Spelling in Markdown
    condition: succeededOrFailed()
    workingDirectory: '$(repoPath)'

  - ${{ if not(contains(variables['SYSTEM.COLLECTIONURI'],'mscodehub')) }}:
    - pwsh: |
        Import-module ./build.psm1
        $path = Join-Path -Path $pwd -ChildPath './commonTestResults.xml'
        $results = invoke-pester -Script ./test/common -OutputFile $path -OutputFormat NUnitXml -PassThru
        Write-Host "##vso[results.publish type=NUnit;mergeResults=true;runTitle=Common Tests;publishRunAttachments=true;resultFiles=$path;]"
        if($results.TotalCount -eq 0 -or $results.FailedCount -gt 0)
        {
          throw "Markdown tests failed"
        }
      displayName: Run Common Tests
      condition: succeededOrFailed()
      workingDirectory: '$(repoPath)'

  - template: dailyBuildCompliance.yml@ComplianceRepo
    parameters:
      sourceScanPath: '$(repoPath)'
