# =============================================================================
# This pipeline publishes NuGet packages. Manually triggered only.
# - "Internal": pushes unsigned packages to the internal TeamsSDKPreviews feed.
# - "Public": signs (Authenticode + NuGet) and pushes to nuget.org (requires approval).
# Version is determined by Nerdbank.GitVersioning (nbgv) from version.json.
#
# MIGRATED TO 1ES OFFICIAL PIPELINE TEMPLATE: This pipeline now extends from
# 1ES.Official.PipelineTemplate to ensure compliance with M365 security requirements.
# =============================================================================

resources:
  repositories:
  - repository: 1esPipelines
    type: git
    name: 1ESPipelineTemplates/1ESPipelineTemplates
    ref: refs/tags/release

trigger: none

pr: none

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

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'

extends:
  template: v1/1ES.Official.PipelineTemplate.yml@1esPipelines
  parameters:
    pool:
      name: Azure-Pipelines-1ESPT-ExDShared
      image: ubuntu-22.04
      os: linux

    sdl:
      sourceAnalysisPool:
        name: Azure-Pipelines-1ESPT-ExDShared
        image: windows-2022
        os: windows

    # Required for M365PT tracking and drift management
    customBuildTags:
    - ES365AIMigrationTooling

    stages:

    - ${{ if eq(parameters.publishType, 'Internal') }}:
      - stage: Build_Test_Pack_Push_Internal
        displayName: 'Build, Test, Pack, and Push (Internal)'
        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'

            - pwsh: |
                $nugetConfig = @"
                <?xml version="1.0" encoding="utf-8"?>
                <configuration>
                  <packageSources>
                    <clear />
                    <add key="TeamsSDKPreviews" value="https://pkgs.dev.azure.com/DomoreexpGithub/Github_Pipelines/_packaging/TeamsSDKPreviews/nuget/v3/index.json" />
                  </packageSources>
                </configuration>
                "@
                $nugetConfig | Out-File -FilePath "$(Build.SourcesDirectory)/nuget.config" -Encoding utf8
              displayName: 'Create nuget.config'

            - task: NuGetAuthenticate@1
              displayName: 'Authenticate with NuGet feeds'

            - 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

            - task: DotNetCoreCLI@2
              displayName: 'Pack'
              inputs:
                command: 'pack'
                packagesToPack: 'Libraries/**/*.csproj'
                nobuild: true
                configuration: '$(buildConfiguration)'
                outputDir: '$(Build.ArtifactStagingDirectory)'
                buildProperties: 'SymbolPackageFormat=snupkg'

            - task: 1ES.PublishNuget@1
              displayName: 'Push NuGet Packages to Internal Feed'
              inputs:
                useDotNetTask: false
                packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg'
                packageParentPath: '$(Build.ArtifactStagingDirectory)'
                publishVstsFeed: '$(System.TeamProject)/TeamsSDKPreviews'
                nuGetFeedType: internal
                allowPackageConflicts: false  # Allow duplicate versions for preview testing
                publishPackageMetadata: true
                retryCountOnTaskFailure: 2

            - task: 1ES.PublishPipelineArtifact@1
              displayName: 'Publish NuGet Packages as Pipeline Artifact'
              inputs:
                targetPath: '$(Build.ArtifactStagingDirectory)'
                artifactName: 'Packages'

    - ${{ if eq(parameters.publishType, 'Public') }}:
      - stage: Build_Test_Sign_Pack
        displayName: 'Build, Test, Sign, and Pack (Public)'
        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'

            - pwsh: |
                $nugetConfig = @"
                <?xml version="1.0" encoding="utf-8"?>
                <configuration>
                  <packageSources>
                    <clear />
                    <add key="TeamsSDKPreviews" value="https://pkgs.dev.azure.com/DomoreexpGithub/Github_Pipelines/_packaging/TeamsSDKPreviews/nuget/v3/index.json" />
                  </packageSources>
                </configuration>
                "@
                $nugetConfig | Out-File -FilePath "$(Build.SourcesDirectory)/nuget.config" -Encoding utf8
              displayName: 'Create nuget.config'

            - task: NuGetAuthenticate@1
              displayName: 'Authenticate with NuGet feeds'

            - 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: .azdo/templates/sign-and-pack.yaml@self

            - task: 1ES.PublishPipelineArtifact@1
              displayName: 'Publish NuGet Packages as Pipeline Artifact'
              inputs:
                targetPath: '$(Build.ArtifactStagingDirectory)'
                artifactName: 'Packages'

    - ${{ if eq(parameters.publishType, 'Public') }}:
      - stage: PushToNuGet
        displayName: 'Push 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: Packages

                - task: 1ES.PublishNuget@1
                  displayName: 'Push Packages to nuget.org'
                  inputs:
                    useDotNetTask: false
                    packagesToPush: '$(Pipeline.Workspace)/Packages/*.nupkg'
                    packageParentPath: '$(Pipeline.Workspace)/Packages'
                    nuGetFeedType: external
                    publishFeedCredentials: 'Microsoft.Teams.*'
                    publishPackageMetadata: true
                    retryCountOnTaskFailure: 2
