name: Release
trigger: none
pr: none

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

parameters:
  - name: publishPackage
    displayName: 🚀 Publish Package
    type: boolean
    default: false

variables:
  ARTIFACT_NAME_WHEEL: wheel
  architecture: x64
  python.version: '3.8'
  TeamName: lsprotocol

extends:
  template: azure-pipelines/MicroBuild.1ES.Official.yml@MicroBuildTemplate
  parameters:
    sdl:
      sourceAnalysisPool: VSEngSS-MicroBuild2022-1ES
    pool:
      name: AzurePipelines-EO
      demands:
        - ImageOverride -equals 1ESPT-Ubuntu22.04
      os: Linux
    customBuildTags:
      - ES365AIMigrationTooling
    stages:
      - stage: Build
        displayName: Build
        jobs:
          - job: Build

            templateContext:
              outputs:
                - output: pipelineArtifact
                  targetPath: $(Build.StagingDirectory)/dist
                  sbomBuildDropPath: $(Build.StagingDirectory)/dist
                  artifactName: $(ARTIFACT_NAME_WHEEL)

            steps:
              - checkout: self
                fetchDepth: 1
                fetchTags: false

              - task: UsePythonVersion@0
                inputs:
                  versionSpec: '$(python.version)'
                  architecture: '$(architecture)'
                displayName: 'Use Python $(python.version) $(architecture)'

              - script: python -m pip install nox
                displayName: Install nox

              - script: python -m nox --session build_python_package
                displayName: Build package (sdist and wheels)

              - powershell: |
                  python -m pip install toml-cli
                  $releaseVersion = & toml get --toml-path packages/python/pyproject.toml project.version
                  echo "releaseVersion: $releaseVersion"
                  echo "##vso[task.setvariable variable=releaseVersion;isOutput=true]$releaseVersion"
                displayName: Get release version
                name: getReleaseVersionStep

              - script: ls -al packages/python/dist

              - task: CopyFiles@2
                displayName: Copy wheel and tarball
                inputs:
                  sourceFolder: packages/python/dist
                  targetFolder: $(Build.StagingDirectory)/dist
                  contents: |
                    lsprotocol-$(getReleaseVersionStep.releaseVersion)-py3-none-any.whl
                    lsprotocol-$(getReleaseVersionStep.releaseVersion).tar.gz

      - stage: CreateTag
        displayName: Create Tag
        dependsOn: Build
        variables:
          releaseVersion: $[ stageDependencies.Build.Build.outputs['getReleaseVersionStep.releaseVersion'] ]
        jobs:
          - job: CreateTag
            steps:
              - checkout: self
                fetchDepth: 1
                fetchTags: false
                persistCredentials: true

              - script: |
                  git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
                  git config user.name "Azure Piplines"
                  git fetch --depth 1 origin $(Build.SourceBranchName)
                  git tag -a $(releaseVersion) -m "Release $(releaseVersion)" origin/$(Build.SourceBranchName)
                  git push origin $(releaseVersion)
                displayName: Create git tag

      - stage: CreateRelease
        displayName: Create GitHub Release
        dependsOn:
          - Build
          - CreateTag
        variables:
          releaseVersion: $[ stageDependencies.Build.Build.outputs['getReleaseVersionStep.releaseVersion'] ]
        jobs:
          - job: CreateRelease
            templateContext:
              type: releaseJob
              isProduction: true
              inputs:
                - input: pipelineArtifact
                  artifactName: $(ARTIFACT_NAME_WHEEL)
                  targetPath: $(Build.StagingDirectory)/dist
            steps:
              - task: GitHubRelease@1 #https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/github-release-v1?view=azure-pipelines
                displayName: Create GitHub Release
                inputs:
                  gitHubConnection: GitHub-lsprotocol
                  repositoryName: microsoft/lsprotocol
                  action: create
                  target: $(Build.SourceBranchName)
                  title: $(releaseVersion)
                  tag: $(releaseVersion)
                  tagSource: userSpecifiedTag
                  isDraft: true
                  addChangeLog: false
                  assets: $(Build.StagingDirectory)/dist/*

      - stage: WaitForValidation
        dependsOn: CreateRelease
        condition: and(succeeded(), ${{ parameters.publishPackage }})
        jobs:
          - job: wait_for_validation
            displayName: Wait for manual validation
            pool: server
            steps:
              - task: ManualValidation@0
                timeoutInMinutes: 1440 # task times out in 1 day
                inputs:
                  notifyUsers: erikd@microsoft.com
                  instructions: Please test the latest draft release and then publish it.
                  onTimeout: reject

      - stage: Release
        dependsOn: WaitForValidation
        jobs:
          - job: PublishToPyPi
            displayName: Release to PyPi

            pool:
              name: VSEngSS-MicroBuild2022-1ES # This pool is required to have the certs needed to publish to PyPi using ESRP.
              os: windows
              image: server2022-microbuildVS2022-1es

            templateContext:
              type: releaseJob
              isProduction: true
              inputs:
                - input: pipelineArtifact
                  artifactName: $(ARTIFACT_NAME_WHEEL)
                  targetPath: $(Build.StagingDirectory)/dist

            steps:
              - template: MicroBuild.Publish.yml@MicroBuildTemplate
                parameters:
                  intent: PackageDistribution
                  contentType: PyPi
                  contentSource: Folder
                  folderLocation: $(Build.StagingDirectory)/dist
                  waitForReleaseCompletion: true
                  owners: erikd@microsoft.com
                  approvers: grwheele@microsoft.com
