parameters:
  ArtifactPath: $(Build.ArtifactStagingDirectory)
  ArtifactName: 'packages'
  RepoRoot: $(Build.SourcesDirectory)
  Condition: true

steps:
  - pwsh: |
      $apiChangeDetectRequestUrl = "https://apiview.dev/api/PullRequests/CreateAPIRevisionIfAPIHasChanges"
      echo "##vso[task.setvariable variable=ApiChangeDetectRequestUrl]$apiChangeDetectRequestUrl"
    displayName: "Set API change detect request URL"
    condition: and(${{ parameters.Condition}}, eq(variables['ApiChangeDetectRequestUrl'], ''))

  - task: Powershell@2
    inputs:
      filePath: ${{ parameters.RepoRoot }}/eng/common/scripts/Detect-Api-Changes.ps1
      arguments: >
        -ArtifactPath ${{parameters.ArtifactPath}}
        -CommitSha '$(System.PullRequest.SourceCommitId)'
        -BuildId $(Build.BuildId)
        -PullRequestNumber $(System.PullRequest.PullRequestNumber)
        -RepoFullName $(Build.Repository.Name)
        -APIViewUri $(ApiChangeDetectRequestUrl)
        -ArtifactName ${{ parameters.ArtifactName }}
        -DevopsProject $(System.TeamProject)
      pwsh: true
    displayName: Create APIView if API has changes
    condition: and(${{ parameters.Condition }}, succeededOrFailed(), eq(variables['Build.Reason'],'PullRequest'))
