# PyTorch PR PyTorch Tests Builds Pipeline on Azure DevOps
#
# This pipeline:
#   1) ensures that CircleCI builds for a given PR
#      have finished, and that its artifacts are
#      ready for download
#   2) runs custom PyTorch unit-tests on PyTorch
#      wheels generated during PR builds.

resources:
  webhooks:
    - webhook: GitHubPyTorchPRTrigger
      connection: GitHubPyTorchPRTriggerConnection
      filters:
        - path: repositoryName
          value: pytorch_tests

stages:
- stage: 'EnsureArtifactsReady'
  displayName: 'Ensure PyTorch PR Artifacts are ready'
  jobs:
  - template: job_templates/wheel-wait-template.yml
  variables:
    _TARGET_BRANCH_TO_CHECK: ${{parameters.GitHubPyTorchPRTrigger.TARGET_BRANCH_TO_CHECK_AZ_DEVOPS_PR}}
    _TARGET_PR_NUMBER: ${{parameters.GitHubPyTorchPRTrigger.PR_NUMBER}}
    _TARGET_COMMIT: ${{parameters.GitHubPyTorchPRTrigger.TARGET_COMMIT}}

- stage: 'PRCustomTests'
  displayName: 'Run custom unit tests on PyTorch wheels'
  dependsOn: EnsureArtifactsReady
  condition: succeeded()
  jobs:
  - template: job_templates/pytorch-template-unix.yml
    parameters:
      name: ubuntu_1804_GPU_docker
      pool: $(BUILD_POOL_PR)
      customMatrixes:
        PR_Custom_Tests:
          _PYTHON_VERSION: $(PYTHON_VERSION_PR)
          _CUDA_BUILD_VERSION: $(CUDA_BUILD_VERSION_PR)
          _TARGET_CIRCLECI_BUILD: $(TARGET_CIRCLECI_BUILD_PR)
          _TARGET_BRANCH_TO_CHECK: ${{parameters.GitHubPyTorchPRTrigger.TARGET_BRANCH_TO_CHECK_AZ_DEVOPS_PR}}
          _TARGET_PR_NUMBER: ${{parameters.GitHubPyTorchPRTrigger.PR_NUMBER}}
          _TARGET_COMMIT: ${{parameters.GitHubPyTorchPRTrigger.TARGET_COMMIT}}
          _DOCKER_IMAGE: $(DOCKER_IMAGE_PR)
          _RUN_TESTS: $(RUN_TESTS_PR)

- stage: 'NotifyWebapp'
  displayName: 'Notify Webapp that pipeline is finished'
  dependsOn: PRCustomTests
  condition: succeededOrFailed()
  jobs:
  - template: job_templates/notify-webapp-template.yml
    parameters:
      name: ubuntu_1804_CPU
      pool: $(BUILD_POOL_LIN_1)
      customMatrixes:
        PR_Notify_WebApp:
          _TARGET_CIRCLECI_BUILD: $(TARGET_CIRCLECI_BUILD_PR)
          _TARGET_BRANCH_TO_CHECK: ${{parameters.GitHubPyTorchPRTrigger.TARGET_BRANCH_TO_CHECK_AZ_DEVOPS_PR}}
          _TARGET_PR_NUMBER: ${{parameters.GitHubPyTorchPRTrigger.PR_NUMBER}}
          _TARGET_COMMIT: ${{parameters.GitHubPyTorchPRTrigger.TARGET_COMMIT}}
