# =============================================================================
# This pipeline publishes preview packages. Manually triggered only.
# - "Internal": pushes unsigned packages to the internal TeamsSDKPreviews feed.
# - "Public": signs (Authenticode + NuGet) and pushes to nuget.org (requires approval).
# =============================================================================

trigger: none

pr: none

parameters:
- name: publishType
  displayName: 'Publish Type'
  type: string
  default: 'Internal'
  values:
  - Internal
  - Public

pool:
  vmImage: 'ubuntu-22.04'

variables:
  buildConfiguration: 'Release'
  folderPath: '$(Build.SourcesDirectory)'
  ${{ if eq(parameters.publishType, 'Public') }}:
    appRegistrationTenantId: 'cdc5aeea-15c5-4db6-b079-fcadd2505dc2'
    authenticodeSignId: '2d5c4ab9-0b7e-4f60-bb92-70322df77b94'
    nugetSignId: 'a94a770a-9a7b-4888-a3ea-24584b851e49'

stages:

- ${{ if eq(parameters.publishType, 'Internal') }}:
  - stage: Build_Test_Pack_Push_Internal
    displayName: 'Build, Test, Pack, and Push (Internal Preview)'
    jobs:
    - job: BuildTestPackPush
      displayName: 'Build, Test, Pack, and Push to Internal Feed'
      steps:
        - checkout: self

        - task: UseDotNet@2
          displayName: 'Use .NET 8'
          inputs:
            packageType: 'sdk'
            version: '8.0.x'

        - task: UseDotNet@2
          displayName: 'Use .NET 10'
          inputs:
            packageType: 'sdk'
            version: '10.0.x'

        - script: dotnet restore
          displayName: 'Restore'

        - script: dotnet build --no-restore --configuration $(buildConfiguration)
          displayName: 'Build'

        - script: dotnet test --no-build --verbosity normal --logger trx --configuration $(buildConfiguration)
          displayName: 'Test'

        - task: PublishTestResults@2
          displayName: 'Publish Test Results'
          condition: succeededOrFailed()
          inputs:
            testResultsFormat: 'VSTest'
            testResultsFiles: '**/*.trx'
            mergeTestResults: true

        - script: dotnet pack --no-build -o $(Build.ArtifactStagingDirectory) /p:SymbolPackageFormat=snupkg --configuration $(buildConfiguration)
          displayName: 'Pack'

        - task: NuGetCommand@2
          displayName: 'Push NuGet Packages to Internal Feed'
          inputs:
            command: push
            packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg'
            nuGetFeedType: internal
            publishVstsFeed: '$(System.TeamProject)/TeamsSDKPreviews'

        - task: PublishPipelineArtifact@1
          displayName: 'Publish NuGet Packages as Pipeline Artifact'
          inputs:
            targetPath: '$(Build.ArtifactStagingDirectory)'
            artifact: 'PreviewPackages'
            publishLocation: 'pipeline'

- ${{ if eq(parameters.publishType, 'Public') }}:
  - stage: Build_Test_Sign_Pack
    displayName: 'Build, Test, Sign, and Pack (Public Preview)'
    jobs:
    - job: BuildTestSignPack
      displayName: 'Build, Test, Sign, and Pack'
      steps:
        - checkout: self

        - task: UseDotNet@2
          displayName: 'Use .NET 8'
          inputs:
            packageType: 'sdk'
            version: '8.0.x'

        - task: UseDotNet@2
          displayName: 'Use .NET 10'
          inputs:
            packageType: 'sdk'
            version: '10.0.x'

        - script: dotnet restore
          displayName: 'Restore'

        - script: dotnet build --no-restore --configuration $(buildConfiguration)
          displayName: 'Build'

        - script: dotnet test --no-build --verbosity normal --logger trx --configuration $(buildConfiguration)
          displayName: 'Test'

        - task: PublishTestResults@2
          displayName: 'Publish Test Results'
          condition: succeededOrFailed()
          inputs:
            testResultsFormat: 'VSTest'
            testResultsFiles: '**/*.trx'
            mergeTestResults: true

        - template: templates/sign-and-pack.yaml

        - task: PublishPipelineArtifact@1
          displayName: 'Publish NuGet Packages as Pipeline Artifact'
          inputs:
            targetPath: '$(Build.ArtifactStagingDirectory)'
            artifact: 'PreviewPackages'
            publishLocation: 'pipeline'

- ${{ if eq(parameters.publishType, 'Public') }}:
  - stage: PushToNuGet
    displayName: 'Push Preview Packages to nuget.org'
    dependsOn: Build_Test_Sign_Pack
    jobs:
    - deployment: PushPackages
      displayName: 'Manual Approval Required to Push Packages'
      environment:
        name: 'teams-net-publish'
      strategy:
        runOnce:
          deploy:
            steps:
            - download: current
              artifact: PreviewPackages

            - task: NuGetCommand@2
              displayName: 'Push NuGet Packages'
              inputs:
                command: push
                packagesToPush: '$(Pipeline.Workspace)/PreviewPackages/*.nupkg'
                nuGetFeedType: external
                publishFeedCredentials: 'Microsoft.Teams.*'
