parameters:
  - name:  ServiceDirectory
    type: string
    default: ''
  - name:  Image
    type: string
    default: ''
  - name:  GoVersion
    type: string
    default: ''
  - name:  TestProxy
    type: boolean
    default: false
  - name: EnvVars
    type: object
    default: {}
  - name: TestRunTime
    type: string
    default: '600s'

steps:
  - task: Powershell@2
    displayName: Build
    env:
      GO111MODULE: 'on'
    inputs:
      targetType: filePath
      pwsh: true
      filePath: eng/scripts/build.ps1
      arguments: -filter '${{ parameters.ServiceDirectory }}'

  - task: Powershell@2
    displayName: Vet
    env:
      GO111MODULE: 'on'
    inputs:
      targetType: filePath
      pwsh: true
      filePath: eng/scripts/build.ps1
      arguments: -vet -skipBuild -filter '${{ parameters.ServiceDirectory }}'

  - pwsh: |
      go install github.com/jstemmer/go-junit-report@v0.9.1
      go install github.com/axw/gocov/gocov@v1.1.0
      go install github.com/AlekSi/gocov-xml@v1.0.0
      go install github.com/matm/gocov-html@v0.0.0-20200509184451-71874e2e203b
    displayName: "Install Coverage and Junit Dependencies"

  - ${{ if eq(parameters.TestProxy, true) }}:
    - template: /eng/common/testproxy/test-proxy-tool.yml

  - task: PowerShell@2
    displayName: 'Run Tests'
    inputs:
      targetType: 'filePath'
      filePath: ./eng/scripts/run_tests.ps1
      arguments: '${{ parameters.ServiceDirectory }} ${{ parameters.TestRunTime }}'
      pwsh: true
    env:
      GO111MODULE: 'on'
      PROXY_CERT: $(Build.SourcesDirectory)/eng/common/testproxy/dotnet-devcert.crt
      ${{ insert }}: ${{ parameters.EnvVars }}

  - task: PowerShell@2
    displayName: 'Build Performance Tests'
    inputs:
      targetType: 'filePath'
      filePath: ./eng/scripts/Build_Perf.ps1
      arguments: '${{ parameters.ServiceDirectory }}'
      pwsh: true

  - ${{ if eq(parameters.TestProxy, true) }}:
    - pwsh: |
        # $(Build.SourcesDirectory)/test-proxy.log is the hardcoded output log location for the test-proxy-tool.yml
        cat $(Build.SourcesDirectory)/test-proxy.log
      displayName: 'Dump Test Proxy logs'
      condition: succeededOrFailed()

  - task: AzureArtifacts.manifest-generator-task.manifest-generator-task.ManifestGeneratorTask@0
    displayName: 'Generate BOM'
    condition: and(succeededOrFailed(), eq(variables['generate.bom'], 'true'))
    inputs:
      BuildDropPath: $(Build.ArtifactStagingDirectory)

  - task: Powershell@2
    inputs:
      filePath: $(Build.SourcesDirectory)/eng/common/scripts/Save-Package-Properties.ps1
      arguments: >
        -ServiceDirectory ${{parameters.ServiceDirectory}}
        -OutDirectory $(Build.ArtifactStagingDirectory)/PackageInfo
      pwsh: true
      workingDirectory: $(Pipeline.Workspace)
    displayName: Dump Package properties
    condition: succeeded()

  - template: /eng/common/pipelines/templates/steps/set-default-branch.yml
  
  - task: Powershell@2
    inputs:
      filePath: $(Build.SourcesDirectory)/eng/scripts/Create-ApiReview.ps1
      arguments: >
        -ServiceDirectory ${{parameters.ServiceDirectory}}
        -OutPath $(Build.ArtifactStagingDirectory)
        -ApiviewUri "$(azuresdk-apiview-uri)"
        -ApiKey "$(azuresdk-apiview-apikey)"
        -ApiLabel "Auto Review - $(Build.SourceVersion)"
        -SourceBranch $(Build.SourceBranchName)
        -DefaultBranch $(DefaultBranch)
        -ConfigFileDir $(Build.ArtifactStagingDirectory)/PackageInfo
      pwsh: true
      workingDirectory: $(Pipeline.Workspace)
    displayName: Create API review for Go
    continueOnError: true
    condition: and(succeeded(), ne(variables['Skip.CreateApiReview'], 'true') , ne(variables['Build.Reason'],'PullRequest'), eq(variables['System.TeamProject'], 'internal'))

  - template: /eng/common/pipelines/templates/steps/publish-artifact.yml
    parameters:
      ArtifactPath: '$(Build.ArtifactStagingDirectory)/_manifest'
      ArtifactName: 'manifest'
      CustomCondition: and(succeededOrFailed(), eq(variables['generate.bom'], 'true'))

  - task: PublishTestResults@2
    condition: succeededOrFailed()
    inputs:
      testRunner: JUnit
      testResultsFiles: '$(Build.SourcesDirectory)/sdk/${{parameters.ServiceDirectory}}/report.xml'
      testRunTitle: 'Go ${{ parameters.GoVersion }} on ${{ parameters.Image }}'
      failTaskOnFailedTests: true

  - task: PublishCodeCoverageResults@1
    condition: succeededOrFailed()
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: './coverage.xml'
      additionalCodeCoverageFiles: './coverage.html'
