trigger: none
pr: none

parameters:
- name: DevPackage
  default: false
  values:
  - false
  - true

variables:
- group: Release Secrets for GitHub

resources:
  repositories:
  - repository: azure-sdk-for-java
    type: github
    name: Azure/azure-sdk-for-java
    endpoint: azure
    ref: main

jobs:
- job: Generate_SDK

  timeoutInMinutes: 60

  variables:
  - template: /eng/pipelines/variables/globals.yml
  - template: /eng/pipelines/variables/image.yml
  - name: PullRequestTitleSuffix
    ${{ if eq(parameters.DevPackage, 'true') }}:
      value: " DEV"
    ${{ else }}:
      value: ""

  pool:
    name: $(LINUXPOOL)
    image: $(LINUXVMIMAGE)
    os: linux

  steps:
  - checkout: self
    submodules: true

  - checkout: azure-sdk-for-java

  - task: NodeTool@0
    displayName: 'Install Node.js $(NodeVersion)'
    inputs:
      versionSpec: '$(NodeVersion)'

  - task: PowerShell@2
    retryCountOnTaskFailure: 1
    condition: and(succeeded(), eq('${{ parameters.DevPackage }}', 'true'))
    displayName: 'Build'
    inputs:
      pwsh: true
      filePath: $(Build.SourcesDirectory)/autorest.java/Build-TypeSpec.ps1
      workingDirectory: $(Build.SourcesDirectory)/autorest.java

  - script: |
      npm install -g @azure-tools/typespec-client-generator-cli
    displayName: 'Install tsp-client'

  - task: PowerShell@2
    displayName: 'Get Package Version'
    inputs:
      targetType: 'inline'
      script: |
        $PACKAGE_VERSION = node -p -e "require('./typespec-extension/package.json').version"
        Write-Host("##vso[task.setvariable variable=PackageVersion]$PACKAGE_VERSION")
      workingDirectory: $(Build.SourcesDirectory)/autorest.java

  - script: |
      python3 ./eng/sdk/sync_sdk.py --sdk-root=$(Build.SourcesDirectory)/azure-sdk-for-java --package-json-path=$(Build.SourcesDirectory)/autorest.java/typespec-extension/package.json --dev-package=${{ parameters.DevPackage }}
    displayName: 'Generate SDK'
    workingDirectory: $(Build.SourcesDirectory)/autorest.java

  - template: /eng/common/pipelines/templates/steps/create-pull-request.yml@azure-sdk-for-java
    parameters:
      WorkingDirectory: $(Build.SourcesDirectory)/azure-sdk-for-java
      ScriptDirectory: $(Build.SourcesDirectory)/azure-sdk-for-java/eng/common/scripts
      RepoName: azure-sdk-for-java
      BaseBranchName: 'refs/heads/main'
      PRBranchName: typespec-java-generation-$(Build.BuildId)
      CommitMsg: '[Automation] Generate SDK based on TypeSpec $(PackageVersion)$(PullRequestTitleSuffix)'
      PRTitle: '[Automation] Generate SDK based on TypeSpec $(PackageVersion)$(PullRequestTitleSuffix)'
      PRLabels: 'DPG'
      OpenAsDraft: 'true'
