trigger:
  branches:
    include:
      - tsc-compat-debug
      - refs/tags/office-ui-fabric-react_v*

variables:
  - name: PackageName
    value: office-ui-fabric-react
  - name: CompatRepoUrl
    value: https://github.com/Microsoft/ui-fabric-ts-validation.git
  - name: NodeVersion
    value: '10.x'
  - name: VmImage
    value: 'Ubuntu 16.04'

jobs:
  - job: Build
    pool:
      vmImage: $(VmImage)
    timeoutInMinutes: 60
    steps:
      - checkout: self
        clean: true

      - task: NodeTool@0
        displayName: 'Use Node $(NodeVersion)'
        inputs:
          versionSpec: '$(NodeVersion)'
          checkLatest: true

      # Install Rush repo dependencies
      - script: |
          node common/scripts/install-run-rush.js install --bypass-policy
        displayName: Run Rush install in office-ui-fabric-react repository

      # Build package in Rush repo
      - script: |
          node common/scripts/install-run-rush.js build --to $(PackageName)
        displayName: Run Rush build --to $(PackageName)

      # Run npm pack to simulate installation from feed
      - script: |
          npm pack ./packages/$(PackageName)
        displayName: Run npm pack $(PackageName)

      # Save ReleaseId into a txt artifact file
      - script: |
          echo *.tgz | grep -oP '(?<=office-ui-fabric-react-).*(?=.tgz)' >> $(Build.StagingDirectory)/compatMap.txt
        name: setReleaseIdStep
        displayName: Save ReleaseId into a txt artifact file

      # Rename built package tarball to reference later
      - script: |
          mv *.tgz $(Build.StagingDirectory)/$(PackageName).tgz
        displayName: Move $(PackageName).tgz to Build.StagingDirectory

      # Publish built library for validation job
      - task: PublishBuildArtifacts@1
        displayName: Publish Build Artifacts
        inputs:
          pathtoPublish: $(Build.StagingDirectory)
          artifactName: compatMapDrop

  - job: Compatibility
    dependsOn: Build
    condition: succeeded()
    timeoutInMinutes: 60
    pool:
      vmImage: $(VmImage)
    strategy:
      maxParallel: 1
      matrix:
        'TypeScript 2.4':
          TypeScriptVersion: 2.4
        'TypeScript 2.7':
          TypeScriptVersion: 2.7
        'TypeScript 2.8':
          TypeScriptVersion: 2.8
        'TypeScript 2.9':
          TypeScriptVersion: 2.9
        'TypeScript 3.0':
          TypeScriptVersion: 3.0
        'TypeScript 3.1':
          TypeScriptVersion: 3.1
        'TypeScript 3.2':
          TypeScriptVersion: 3.2
        'TypeScript 3.3':
          TypeScriptVersion: 3.3
        'TypeScript 3.4':
          TypeScriptVersion: 3.4
    steps:
      - checkout: none
      - task: NodeTool@0
        displayName: 'Use Node $(NodeVersion)'
        inputs:
          versionSpec: '$(NodeVersion)'
          checkLatest: true

      # Download build library
      - task: DownloadBuildArtifacts@0
        displayName: 'Download Build Artifacts'
        inputs:
          artifactName: compatMapDrop
          downloadPath: $(Build.StagingDirectory)

      # Clone repository containing versioned TypeScript sample apps
      - script: |
          git clone $(CompatRepoUrl) $(Build.StagingDirectory)/__tests__
        displayName: Clone compatibility repository

      # Install repository's dependencies
      - script: |
          node common/scripts/install-run-rush.js install
        displayName: Run Rush install in compatibility repository
        workingDirectory: $(Build.StagingDirectory)/__tests__

      # Install built package tarball in sample app
      - script: |
          npm install $(Build.StagingDirectory)/compatMapDrop/$(PackageName).tgz
        displayName: Install $(PackageName) in TypeScript $(TypeScriptVersion) test app
        workingDirectory: $(Build.StagingDirectory)/__tests__/tests/$(TypeScriptVersion)

      # Build sample app consuming Fluent UI React to test TypeScript version compatibility
      - script: |
          node common/scripts/install-run-rush.js test --to $(TypeScriptVersion)-office-ui-fabric-react-test
        displayName: Build TypeScript $(TypeScriptVersion) test app
        workingDirectory: $(Build.StagingDirectory)/__tests__

      # Write successful ts versions to compatMap.txt
      - script: |
          echo $(TypeScriptVersion) >> $(Build.StagingDirectory)/compatMapDrop/compatMap.txt
        displayName: Write successful ts versions to compatMap.txt

      # Publish built library for validation job
      - task: PublishBuildArtifacts@1
        displayName: Publish Build Artifacts
        inputs:
          pathtoPublish: $(Build.StagingDirectory)/compatMapDrop
          artifactName: compatMapDrop

  - job: InvokePOSTAPI
    dependsOn: Compatibility
    condition: always()
    timeoutInMinutes: 60
    pool:
      vmImage: $(VmImage)
    steps:
      # Download build library
      - task: DownloadBuildArtifacts@0
        displayName: 'Download Build Artifacts'
        inputs:
          artifactName: compatMapDrop
          downloadPath: $(Build.StagingDirectory)

      # Invoke POST API with json body
      # The artifact file compatMap.txt which has the TS compatibility folows the below format,
      # where the first line is the OUFR release and the subsequent lines are the TS versions supported
      #  6.182.1
      #  2.8
      #  2.9
      #  3.0
      #  3.1
      #  3.2
      #  3.3
      #  3.4
      # It is then concatenated into a format like 2.8, 2.9, 3.0, 3.1, 3.2, 3.3, 3.4
      # and the fabricweb backend API is invoked which writes these entrie to an Azure table
      - powershell: $i = 0;
          $releaseVersion = "";
          $compatibleTsVersions = "";
          $compatMapArray = Get-Content $env:BUILD_STAGINGDIRECTORY/compatMapDrop/compatMap.txt;
          foreach($line in $compatMapArray) {
          if($i -match 0) {
          $releaseVersion = $line;
          } else {
          if($i -match 1) {
          $compatibleTsVersions += $line;
          } else {
          $compatibleTsVersions += ", ";
          $compatibleTsVersions += $line;
          }
          }
          $i++;
          }
          Write-Host $releaseVersion;
          Write-Host $compatibleTsVersions;

          $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]";
          $headers.Add("AuthToken", "$(releaseVersionVariable)");
          $APIUrl = "https://fabricweb.azurewebsites.net/oufr/registerNewWithTsCompat?version=$releaseVersion&compatibleTsVersions=$compatibleTsVersions";
          $response = Invoke-RestMethod -Uri $APIUrl -Method Get -Headers $headers;
          Write-Host $response;
        displayName: Invoke POST API with json body
