parameters:
- name: buildName
  type: string

- name: targetRid
  type: string

- name: architecture
  type: string

- name: dotnetDotnetRunId
  type: string

- name: includeArtifactsSize
  type: boolean
  default: false

jobs:
- job: ${{ parameters.buildName }}_${{ parameters.architecture }}
  timeoutInMinutes: 150
  pool:
    name: NetCore1ESPool-Svc-Internal
    demands: ImageOverride -equals 1es-ubuntu-2004
  variables:
  - template: ../variables/pipelines.yml
  steps:
  - script: |
      dotnet_dotnet_build='${{ replace(parameters.dotnetDotnetRunId, ' ', '') }}'

      if [[ -z "$dotnet_dotnet_build" ]]; then
        dotnet_dotnet_build=$(az pipelines runs list --branch '$(Build.SourceBranch)' --organization '$(AZDO_ORG)' --project '$(AZDO_PROJECT)' --pipeline-ids '$(DOTNET_DOTNET_CI_PIPELINE_ID)' --status completed --top 1 --query "[].id" --output tsv)
      fi

      if [[ -z "$dotnet_dotnet_build" ]]; then
        echo "Could not find a completed dotnet-dotnet build for branch '$(Build.SourceBranch)'"
        exit 1
      fi
      
      echo "Dotnet-dotnet build: https://dev.azure.com/dnceng/internal/_build/results?buildId=$dotnet_dotnet_build&view=results"

      installer_sha=$(az pipelines build tag list --organization '$(AZDO_ORG)' --project '$(AZDO_PROJECT)' --build-id $dotnet_dotnet_build --query "[?contains(@, 'installer')]" --output tsv | sed "s,installer ,,g")
      installer_build=$(az pipelines runs list --organization '$(AZDO_ORG)' --project '$(AZDO_PROJECT)' --pipeline-ids '$(INSTALLER_OFFICIAL_CI_PIPELINE_ID)' --query "[?sourceVersion == '$installer_sha'].id" --output tsv)
      if [[ -z "$installer_build" ]]; then
        echo "Could not find a build of installer for commit '$installer_sha'"
        exit 1
      fi

      echo "Installer build: https://dev.azure.com/dnceng/internal/_build/results?buildId=$installer_build&view=results"

      echo "##vso[build.addbuildtag]installer-$installer_sha"
      echo "##vso[task.setvariable variable=InstallerBuildId]$installer_build"
      echo "##vso[task.setvariable variable=DotnetDotnetBuildId]$dotnet_dotnet_build"
    displayName: Find associated builds
    name: Get_Build_Ids
    env:
      AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

  - template: ../steps/download-pipeline-artifact.yml
    parameters:
      patterns: '**/dotnet-sdk-+([0-9]).+([0-9]).+([0-9])?(-@(alpha|preview|rc|rtm)*)-linux*-${{ parameters.architecture }}.tar.gz'
      displayName: Download MSFT SDK

  - template: ../steps/download-vmr-artifact.yml
    parameters:
      buildName: ${{ parameters.buildName }}
      architecture: ${{ parameters.architecture }}
      patterns: '**/dotnet-sdk-+([0-9]).+([0-9]).+([0-9])?(-@(alpha|preview|rc|rtm)*)-${{ parameters.targetRid }}.tar.gz' 
      displayName: Download Source Build SDK

  - template: ../steps/download-vmr-artifact.yml
    parameters:
      buildName: ${{ parameters.buildName }}
      architecture: ${{ parameters.architecture }}
      patterns: '**/Private.SourceBuilt.Artifacts.+([0-9]).+([0-9]).+([0-9])?(-@(alpha|preview|rc|rtm)*).${{ parameters.targetRid }}.tar.gz' 
      displayName: Download Source Built Artifacts

  - script: |
      find $(Pipeline.Workspace)/Artifacts -type f -exec mv {} $(Pipeline.Workspace)/Artifacts \;
    displayName: Move Artifacts to root

  - script: |
      platform="linux"
      if [[ ${{ parameters.targetRid }} =~ "alpine" ]]; then
        platform="$platform-musl"
      fi
      msft_sdk_tarball_name=$(find "$(Pipeline.Workspace)/Artifacts" -name "dotnet-sdk-*-$platform-${{ parameters.architecture }}.tar.gz" -exec basename {} \;)

      if [[ -z "$msft_sdk_tarball_name" ]]; then
        echo "Microsoft SDK tarball does not exist in '$(Pipeline.Workspace)/Artifacts'. The associated build https://dev.azure.com/dnceng/internal/_build/results?buildId=$(InstallerBuildId) might have failed."
        exit 1
      fi

      sdk_tarball_name=$(find "$(Pipeline.Workspace)/Artifacts" -name "dotnet-sdk-*-${{ parameters.targetRid }}.tar.gz" -exec basename {} \;)

      if [[ -z "$sdk_tarball_name" ]]; then
        echo "Source-build SDK tarball does not exist in '$(Pipeline.Workspace)/Artifacts'. The associated build https://dev.azure.com/dnceng/internal/_build/results?buildId=$(DotnetDotnetBuildId) might have failed"
        exit 1
      fi

      artifacts_path=$(find "$(Pipeline.Workspace)/Artifacts" -name "Private.SourceBuilt.Artifacts.*.${{ parameters.targetRid }}.tar.gz" -exec basename {} \;)

      if [[ -z "$artifacts_path" ]]; then
        echo "Source-build artifacts path does not exist in '$(Pipeline.Workspace)/Artifacts'. The associated build https://dev.azure.com/dnceng/internal/_build/results?buildId=$(DotnetDotnetBuildId) might have failed"
        exit 1
      fi

      eng/common/build.sh -bl --projects $(Build.SourcesDirectory)/test/Microsoft.DotNet.SourceBuild.SmokeTests/Microsoft.DotNet.SourceBuild.SmokeTests.csproj --restore

      echo "##vso[task.setvariable variable=Platform]$platform"
      echo "##vso[task.setvariable variable=MsftSdkTarballPath]$(Pipeline.Workspace)/Artifacts/$msft_sdk_tarball_name"
      echo "##vso[task.setvariable variable=SdkTarballPath]$(Pipeline.Workspace)/Artifacts/$sdk_tarball_name"
      echo "##vso[task.setvariable variable=SourceBuiltArtifactsPath]$(Pipeline.Workspace)/Artifacts/$artifacts_path"
    displayName: Prepare Tests
    workingDirectory: $(Build.SourcesDirectory)

  - script: >
      .dotnet/dotnet test
      $(Build.SourcesDirectory)/test/Microsoft.DotNet.SourceBuild.SmokeTests/Microsoft.DotNet.SourceBuild.SmokeTests.csproj
      --filter "Category=SdkContent"
      --logger:'trx;LogFileName=$(Agent.JobName)_SDKDiffTests.trx'
      --logger:'console;verbosity=detailed'
      -c Release
      -bl:$(Build.SourcesDirectory)/artifacts/log/Debug/BuildTests_$(date +"%m%d%H%M%S").binlog 
      -flp:LogFile=$(Build.SourcesDirectory)/artifacts/logs/BuildTests_$(date +"%m%d%H%M%S").log
      -clp:v=m
      /p:MsftSdkTarballPath=$(MsftSdkTarballPath)
      /p:SdkTarballPath=$(SdkTarballPath)
      /p:SourceBuiltArtifactsPath=$(SourceBuiltArtifactsPath)
      /p:SmokeTestsWarnOnSdkContentDiffs=false
      /p:SmokeTestsIncludeArtifactsSizeTests=${{ parameters.includeArtifactsSize }}
      /p:TargetRid=${{ parameters.targetRid }}
      /p:PortableRid=$(Platform)-${{ parameters.architecture }}
    displayName: Run Tests
    workingDirectory: $(Build.SourcesDirectory)

  - script: |
      set -x
      targetFolder=$(Build.StagingDirectory)/BuildLogs/
      mkdir -p ${targetFolder}
      cd "$(Build.SourcesDirectory)"
      find artifacts/log/ -type f -name "BuildTests*.binlog" -exec cp {} --parents -t ${targetFolder} \;
      find artifacts/log/ -type f -name "BuildTests*.log" -exec cp {} --parents -t ${targetFolder} \;
      find artifacts/TestResults/ -type f -name "*.binlog" -exec cp {} --parents -t ${targetFolder} \;
      find artifacts/TestResults/ -type f -name "*.log" -exec cp {} --parents -t ${targetFolder} \;
      find artifacts/TestResults/ -type f -name "*.diff" -exec cp {} --parents -t ${targetFolder} \;
      find artifacts/TestResults/ -type f -name "Updated*.txt" -exec cp {} --parents -t ${targetFolder} \;
    displayName: Prepare BuildLogs staging directory
    continueOnError: true
    condition: succeededOrFailed()

  - publish: '$(Build.StagingDirectory)/BuildLogs'
    artifact: $(Agent.JobName)_BuildLogs_Attempt$(System.JobAttempt)
    displayName: Publish BuildLogs
    continueOnError: true
    condition: succeededOrFailed()

  - task: PublishTestResults@2
    displayName: Publish Test Results
    condition: succeededOrFailed()
    continueOnError: true
    inputs:
      testRunner: vSTest
      testResultsFiles: '*.trx'
      searchFolder: $(Build.SourcesDirectory)/test/Microsoft.DotNet.SourceBuild.SmokeTests/TestResults
      mergeTestResults: true
      publishRunAttachments: true
      testRunTitle: $(Agent.JobName)
