jobs:
  - job: 'Linting'

    timeoutInMinutes: 180

    variables:
      - template: /eng/pipelines/templates/variables/globals.yml

    pool:
      name: azsdk-pool-mms-ubuntu-2004-general

    steps:
      # Skip sparse checkout for the `azure-sdk-for-<lang>-pr` private mirrored repositories
      # as we require the GitHub service connection to be loaded.
      - ${{ if not(contains(variables['Build.DefinitionName'], 'java-pr')) }}:
          - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
            parameters:
              Paths:
                - '**/*.xml'
                - '!sdk/**/test-recordings'
                - '!sdk/**/session-records'

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

      - task: PythonScript@0
        displayName: 'Set versions for source build'
        inputs:
          scriptPath: 'eng/versioning/set_versions.py'
          arguments: '--build-type client --pst'

      - task: PythonScript@0
        displayName: 'Update versions for source build'
        inputs:
          scriptPath: 'eng/versioning/update_versions.py'
          arguments: '--update-type library --build-type client --sr'

      - template: /eng/pipelines/templates/steps/generate-project-list-and-cache-maven-repository.yml
        parameters:
          Artifacts:
            - name: azure-core
              groupId: com.azure
              safeName: azurecore
          JobType: 'linting'

      # The only time generate_from_source_pom.py should be used to set the SparseCheckoutDirectories
      # is for FromSource runs or, in the case of code quality reports, a run that needs to build
      # everything using the latest source. It'll greedily set any service directories as it figures
      # out what libraries, their dependents and so on, that need to be in ClientFromSourcePom.xml
      - task: PythonScript@0
        displayName: 'Generate FromSource POM and directories for sparse checkout'
        inputs:
          scriptPath: 'eng/scripts/generate_from_source_pom.py'
          arguments: '--set-skip-linting-projects SkipLintingProjects --project-list $(ProjectList)'
          workingDirectory: '$(System.DefaultWorkingDirectory)'

      - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
        parameters:
          SkipCheckoutNone: true
          Paths: $(SparseCheckoutDirectories)

      - task: PowerShell@2
        inputs:
          filePath: $(System.DefaultWorkingDirectory)/eng/pipelines/scripts/Get-Linting-Commands.ps1
          arguments: >
            -BuildReason $(Build.Reason)
            -SourceBranch "HEAD"
            -LintingPipelineVariable "LintingGoals"
          pwsh: true
        displayName: 'Generate Linting Commands'

      - template: /eng/pipelines/templates/steps/install-reporting-tools.yml

      - task: Maven@3
        displayName: 'Build Projects'
        inputs:
          mavenPomFile: ClientFromSourcePom.xml
          goals: 'install'
          options: '$(DefaultOptions) -T 2C -DskipTests -Dgpg.skip -DtrimStackTrace=false -Dmaven.javadoc.skip=true -Dcodesnippet.skip=true -Dcheckstyle.skip -Dspotbugs.skip -Drevapi.skip=true -Djacoco.skip=true '
          mavenOptions: '$(MemoryOptions) $(LoggingOptions)'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: $(JavaBuildVersion)
          jdkArchitectureOption: 'x64'
          publishJUnitResults: false
        condition: and(succeeded(), ne(variables['LintingGoals'], ''))

      - task: Maven@3
        displayName: 'Run Linting'
        inputs:
          mavenPomFile: ClientFromSourcePom.xml
          goals: $(LintingGoals)
          # Skip failing on Checkstyle, RevApi, and Spotbugs errors.
          # This is being done as the error reporting will be captured, so if any error reports exist the build can be
          # failed later allowing all libraries to be validated.
          options: '$(DefaultOptions) -T 2C -Dcheckstyle.failOnViolation=false -Dcheckstyle.failsOnError=false -Dspotbugs.failOnError=false -Drevapi.failBuildOnProblemsFound=false -pl "!com.azure:azure-sdk-from-source,$(SkipLintingProjects)"'
          mavenOptions: '$(MemoryOptions) $(LoggingOptions)'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: $(JavaBuildVersion)
          jdkArchitectureOption: 'x64'
          publishJUnitResults: false
        condition: and(succeeded(), ne(variables['LintingGoals'], ''))

      - task: PowerShell@2
        inputs:
          filePath: $(System.DefaultWorkingDirectory)/eng/pipelines/scripts/Get-Linting-Reports.ps1
          arguments: -OutputDirectory $(Build.ArtifactStagingDirectory)/linting-reports
          pwsh: true
          workingDirectory: $(Pipeline.Workspace)
        displayName: 'Generate Linting Reports'
        condition: and(always(), ne(variables['LintingGoals'], ''))

      - publish: $(Build.ArtifactStagingDirectory)/linting-reports.zip
        displayName: 'Publish Linting Reports'
        artifact: linting-reports-$(System.StageName)-$(System.JobName)-$(System.JobAttempt)
        condition: and(always(), ne(variables['LintingGoals'], ''))

      - pwsh: |
          if ((Get-ChildItem -Path $(Build.ArtifactStagingDirectory)/linting-reports -Directory | Measure-Object).Count -gt 0) {
            Write-Host "Linting errors have been found, download the 'linting-reports.zip' artifact to see what was found."
            exit 1
          }
        displayName: 'Fail Pipeline if Linting Reports Exist'
        condition: and(succeeded(), ne(variables['LintingGoals'], ''))

      - task: PowerShell@2
        displayName: Clean up maven local cache
        inputs:
          pwsh: true
          filePath: $(Build.SourcesDirectory)/eng/scripts/Remove-Azure-Artifacts-From-Cache.ps1
          arguments: >
            -MavenCacheFolder $(MAVEN_CACHE_FOLDER)
        continueOnError: true
