pr:
  - master
  - website-content

trigger: none

variables:
  - group: fabric-variables

pool: 'fluentbuild'

steps:
  - template: azure-pipelines.tools.yml

  - script: |
      yarn
    displayName: yarn

  # @fluentui/perf-test needs build and bundle steps
  - script: |
      yarn lage build --scope @fluentui/perf-test --scope perf-test --no-deps --verbose --no-cache --grouped
    displayName: build, bundle @fluentui v0
    env:
      BACKFILL_CACHE_PROVIDER: 'azure-blob'
      BACKFILL_CACHE_PROVIDER_OPTIONS: '{"connectionString":"$(BACKFILL_CONNECTION_STRING)", "container":"$(BACKFILL_CONTAINER)"}'

  # Fluent perf-test must run before Fabric perf-test until they are consolidated.
  # @fluentui/perf-test Baseline
  - script: |
      yarn perf:test:base
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
    workingDirectory: packages/fluentui/perf-test
    displayName: Perf Test Base (Fluent)

  # @fluentui/perf-test PR
  - script: |
      yarn perf:test
    condition: ne(variables['Build.SourceBranch'], 'refs/heads/master')
    workingDirectory: packages/fluentui/perf-test
    displayName: Perf Test (Fluent)

  # Fabric: perf-test will build, bundle, and run-perftest
  - script: |
      yarn just perf-test
    workingDirectory: apps/perf-test
    displayName: Perf Test

  - task: AzureUpload@1
    displayName: Upload Perf Test Result to PR deploy site (Fluent)
    inputs:
      SourcePath: 'packages/fluentui/perf-test/dist'
      azureSubscription: 'UI Fabric (bac044cf-49e1-4843-8dda-1ce9662606c8)'
      storage: fabricweb
      ContainerName: '$web'
      BlobPrefix: 'pr-deploy-site/$(Build.SourceBranch)/perf-test/fluentui'

  - task: AzureUpload@1
    displayName: Upload Perf Test Result to PR deploy site
    inputs:
      SourcePath: 'apps/perf-test/dist'
      azureSubscription: 'UI Fabric (bac044cf-49e1-4843-8dda-1ce9662606c8)'
      storage: fabricweb
      ContainerName: '$web'
      BlobPrefix: 'pr-deploy-site/$(Build.SourceBranch)/perf-test'

  - task: GithubPRComment@0
    displayName: 'Post Perf Results to Github Pull Request'
    inputs:
      githubOwner: microsoft
      githubRepo: 'fluentui'
      blobFilePath: '$(Build.SourcesDirectory)/$(PerfCommentFilePath)'
      status: '$(PerfCommentStatus)'
      uniqueId: 'perfComment9423'

  - task: GithubPRStatus@0
    displayName: 'Update Github Pull Request Status'
    inputs:
      githubOwner: microsoft
      githubRepo: 'fluentui'
      githubContext: 'Pull Request Deployed Site'
      githubDescription: 'Click "Details" to go to the Deployed Site'
      githubTargetLink: 'http://fabricweb.z5.web.core.windows.net/pr-deploy-site/$(Build.SourceBranch)/'

  - script: |
      yarn stats:build
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
    displayName: Bundle Statistics (master only)

  - script: |
      yarn perf
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
    displayName: Performance Tests (master only)

  # HEADS UP: also see tag-version-prefix in fluentui-publish.js
  - script: |
      yarn stats:save --tag=`git tag --points-at HEAD | grep ^@fluentui/react-northstar_v | grep -o 'northstar_v.*'`
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
    displayName: Save Statistics to DB (master only)
    env:
      STATS_URI: $(STATS_URI)

  - task: DeleteFiles@1
    inputs:
      SourceFolder: $(Build.SourcesDirectory)
      Contents: '**/*'
    condition: always()
