parameters:
  - name: ServiceDirectory
    type: string
    default: 'ml'
  - name: Artifacts
    type: object
    default:
    - name: azure-ai-ml
      safeName: azureaiml
      skipVerifyChangeLog: true
  - name: SamplesRepo
    type: string
    default: 'https://github.com/azure/azureml-examples'

jobs:
  - job: 'Build_Upload_PR'
    displayName: "Build, Upload, and PR Azure ML Changeset"
    timeoutInMinutes: 90
    variables:
      - name: StorageAccountName
        value: 'azuresdkartifacts'
      - template: /eng/pipelines/templates/variables/globals.yml

    pool:
      name: azsdk-pool

    steps:
    - template: /eng/pipelines/templates/steps/resolve-package-targeting.yml
      parameters:
        BuildTargetingString: "azure-*"

    - script: |
        echo "##vso[build.addbuildtag]Scheduled"
      displayName: 'Tag scheduled builds'
      condition: and(eq(variables['Build.SourceBranchName'], variables['DefaultBranch']), eq(variables['Build.Reason'],'Schedule'))

    - task: UsePythonVersion@0
      displayName: 'Use Python $(PythonVersion)'
      inputs:
        versionSpec: $(PythonVersion)

    - script: |
        python -m pip install eng/tools/azure-sdk-tools[build]
        python -m pip install azure-identity
        python -m pip install azure-storage-blob
      displayName: 'Prep Environment'

    - pwsh: |
        sdk_build -d "$(Build.ArtifactStagingDirectory)" --repo="$(Build.SourcesDirectory)" --service="${{parameters.ServiceDirectory}}" "$(TargetingString)"
      displayName: Generate Packages

    - pwsh: |
        Get-ChildItem "$(Build.ArtifactStagingDirectory)" -R
      displayName: Output Staging Directory

    - pwsh: |
        git clone --depth 1 ${{ parameters.SamplesRepo }} $(Agent.BuildDirectory)/ml
        Write-Host "##vso[task.setvariable variable=mlrepo;]$(Agent.BuildDirectory)/ml"
      displayName: Clone the ML Samples Repo, Set Variables

    - pwsh: |
        Get-ChildItem "$(mlrepo)" -R
      displayName: Output Staging Directory

    - pwsh: |
        cat "$(mlrepo)/sdk/python/setup.sh"
      displayName: Display setup.sh pre-update

    - task: AzurePowerShell@5
      displayName: Generate Samples Repo Changes
      inputs:
        azureSubscription: 'Azure SDK Artifacts'
        azurePowerShellVersion: LatestVersion
        pwsh: true
        ScriptType: InlineScript
        Inline: |
          python scripts/devops_tasks/generate_ml_sample_update.py --ml-repo "$(mlrepo)" --ml-wheel-folder "$(Build.ArtifactStagingDirectory)/azure-ai-ml" --build-id "$(Build.BuildId)" --storage-account-name "$(StorageAccountName)";
          exit $LASTEXITCODE;

    - pwsh: |
        cat "$(mlrepo)/sdk/python/setup.sh"
      displayName: Display setup.sh post-update

    - template: /eng/common/pipelines/templates/steps/create-pull-request.yml
      parameters:
        RepoName: azureml-examples
        RepoOwner: Azure
        PRBranchName: test-ml-sdk-version-${{ parameters.ServiceDirectory }}-$(Build.BuildId)
        PROwner: Azure
        BaseBranchName: main
        CommitMsg: "Update sdk/python/setup.sh to target fresh built azure-ai-ml wheel."
        PRTitle: "SDK Samples Run generated from $(Build.BuildId)"
        WorkingDirectory: $(mlrepo)
        PRBody: "Check azureml samples against azure-sdk build $(Build.BuildId)."
