trigger: none
variables:
  - template: /eng/pipelines/templates/variables/globals.yml
jobs:
  - template: /eng/common/pipelines/templates/jobs/docindex.yml

  - job: UpdateDocsMsBuildConfig
    pool:
      vmImage: ubuntu-20.04
    timeoutInMinutes: 180
    variables:
      DocRepoLocation: $(Pipeline.Workspace)/docs
      DailyDocRepoLocation: $(Pipeline.Workspace)/daily
      DocRepoOwner: Azure
      DocRepoName: azure-docs-sdk-java
      DocValidationImageId: azuresdkimages.azurecr.io/javarefautocr:latest
    steps:
      # Sync docs repo onboarding files/folders
      - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
        parameters:
          SkipCheckoutNone: true
          Paths:
            - package.json
            - metadata/
            - docs-ref-mapping/reference-unified.yml
            - docs-ref-services/
          Repositories:
            - Name: $(DocRepoOwner)/$(DocRepoName)
              WorkingDirectory: $(DocRepoLocation)
      # Pull and build the docker image.
      - template: /eng/common/pipelines/templates/steps/docker-pull-image.yml
        parameters:
          ContainerRegistryClientId: $(azuresdkimages-cr-clientid)
          ContainerRegistryClientSecret: $(azuresdkimages-cr-clientsecret)
          ImageId: "$(DocValidationImageId)"
      # Call update docs ci script to onboard packages
      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Update-DocsMsPackages.ps1
          arguments: -DocRepoLocation $(DocRepoLocation) -ImageId '$(DocValidationImageId)'
        displayName: Update Docs Onboarding for main branch
        condition: and(succeeded(), or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Force.MainUpdate'], 'true')))

      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Service-Level-Readme-Automation.ps1
          arguments: >-
            -DocRepoLocation $(DocRepoLocation)
            -TenantId '$(opensource-aad-tenant-id)'
            -ClientId '$(opensource-aad-app-id)'
            -ClientSecret '$(opensource-aad-secret)'
        displayName: Generate Service Level Readme for main branch
        condition: and(succeeded(), or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Force.MainUpdate'], 'true')))

      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Update-DocsMsToc.ps1
          arguments: >-
            -DocRepoLocation $(DocRepoLocation)
            -OutputLocation $(DocRepoLocation)/docs-ref-mapping/reference-unified.yml
        displayName: Generate ToC for main branch
        condition: and(succeeded(), or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Force.MainUpdate'], 'true')))

      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Verify-RequiredDocsJsonMembers.ps1
          arguments: >-
            -DocRepoLocation $(DocRepoLocation)
        displayName: Verify Required Docs Json Members

      # Push changes to docs repo
      - template: /eng/common/pipelines/templates/steps/set-default-branch.yml
        parameters:
          WorkingDirectory: $(DocRepoLocation)

      - template: /eng/common/pipelines/templates/steps/git-push-changes.yml
        parameters:
          BaseRepoBranch: $(DefaultBranch)
          BaseRepoOwner: $(DocRepoOwner)
          CommitMsg: "Update docs CI configuration"
          TargetRepoName: $(DocRepoName)
          TargetRepoOwner: $(DocRepoOwner)
          WorkingDirectory: $(DocRepoLocation)

      # Prepare daily docs CI
      - template: /eng/common/pipelines/templates/steps/set-daily-docs-branch-name.yml
        parameters:
          DailyBranchVariableName: DailyDocsBranchName
      - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
        parameters:
          SkipCheckoutNone: true
          Paths:
            - package.json
            - metadata/
            - docs-ref-mapping/reference-unified.yml
            - docs-ref-services/
          Repositories:
            - Name: azure-sdk/$(DocRepoName)
              WorkingDirectory: $(DailyDocRepoLocation)

      # Docs daily updates is supposed to download packages from public feed repository, so we have to specify additional repositories in a POM or the profile.
      # Here is maven documentation: https://maven.apache.org/guides/mini/guide-multiple-repositories.html
      - powershell: |
          # Linux mvn `setting.xml` is sitting under path `~/.m2/setting.xml`
          Get-Command mvn
          if (!(Test-Path '~/.m2/')) {
            mkdir ~/.m2/
          }
          if (Test-Path '~/.m2/setting.xml') {
            Write-Host "'setting.xml' exists. Overwriting the file to support multiple repositories."
          }
          Copy-Item "./eng/repo-docs/docms/daily.update.setting.xml" -Destination "~/.m2/settings.xml"
        displayName: 'Configure mvn'
        workingDirectory: $(Build.SourcesDirectory)
      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Update-DocsMsPackages.ps1
          arguments: -DocRepoLocation $(DailyDocRepoLocation)
        displayName: Update Docs Onboarding for Daily docs
      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Service-Level-Readme-Automation.ps1
          arguments: >-
            -DocRepoLocation $(DailyDocRepoLocation)
            -TenantId '$(opensource-aad-tenant-id)'
            -ClientId '$(opensource-aad-app-id)'
            -ClientSecret '$(opensource-aad-secret)'
        displayName: Generate Service Level Readme for Daily docs
      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Update-DocsMsToc.ps1
          arguments: >-
            -DocRepoLocation $(DailyDocRepoLocation)
            -OutputLocation $(DailyDocRepoLocation)/docs-ref-mapping/reference-unified.yml
        displayName: Generate ToC for Daily docs

      - task: Powershell@2
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Verify-RequiredDocsJsonMembers.ps1
          arguments: >-
            -DocRepoLocation $(DocRepoLocation)
        displayName: Verify Required Docs Json Members

      - template: /eng/common/pipelines/templates/steps/git-push-changes.yml
        parameters:
          BaseRepoBranch: $(DailyDocsBranchName)
          BaseRepoOwner: $(DocRepoOwner)
          CommitMsg: "Update targeting packages based on release metadata. (Daily docs)"
          TargetRepoName: $(DocRepoName)
          TargetRepoOwner: azure-sdk
          WorkingDirectory: $(DailyDocRepoLocation)
          ScriptDirectory: $(Build.SourcesDirectory)/eng/common/scripts
          PushArgs: -f

      - task: PowerShell@2
        displayName: Queue Docs CI build
        inputs:
          pwsh: true
          filePath: eng/common/scripts/Queue-Pipeline.ps1
          arguments: >
            -Organization "apidrop"
            -Project "Content%20CI"
            -DefinitionId 3188
            -AuthToken "$(azuresdk-apidrop-devops-queue-build-pat)"
            -BuildParametersJson (@{ params = (Get-Content ./eng/dailydocsconfig.json -Raw) -replace '%%DailyDocsBranchName%%', "$(DailyDocsBranchName)" } | ConvertTo-Json)
