{%- set upload_artifact_s3_action = "seemethere/upload-artifact-s3@v5" -%}
{%- set download_artifact_s3_action = "seemethere/download-artifact-s3@v4" -%}
{%- set upload_artifact_action = "actions/upload-artifact@v4.4.0" -%}
{%- set download_artifact_action = "actions/download-artifact@v4.1.7" -%}

{%- set timeout_minutes = 240 -%}
{%- set timeout_minutes_windows_binary = 300 -%}

{%- macro concurrency(build_environment) -%}
concurrency:
  group: !{{ build_environment }}-${{ github.event.pull_request.number || github.ref_name }}-${{ github.ref_type == 'branch' && github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
  cancel-in-progress: true
{%- endmacro -%}

{%- macro display_ec2_information() -%}
      - name: Display EC2 information
        shell: bash
        run: |
          set -euo pipefail
          function get_ec2_metadata() {
            # Pulled from instance metadata endpoint for EC2
            # see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
            category=$1
            curl -H "X-aws-ec2-metadata-token: $(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 30")" -fsSL "http://169.254.169.254/latest/meta-data/${category}"
          }
          echo "ami-id: $(get_ec2_metadata ami-id)"
          echo "instance-id: $(get_ec2_metadata instance-id)"
          echo "instance-type: $(get_ec2_metadata instance-type)"
          echo "system info $(uname -a)"
{%- endmacro -%}

{%- macro setup_ec2_windows() -%}
      !{{ display_ec2_information() }}
      - name: "[FB EMPLOYEES] Enable SSH (Click me for login details)"
        uses: pytorch/test-infra/.github/actions/setup-ssh@release/2.8
        continue-on-error: true
        with:
          github-secret: ${{ secrets.GITHUB_TOKEN }}
      - name: Enable git long paths and symlinks on Windows and disable fsmonitor daemon
        shell: bash
        run: |
          git config --global core.longpaths true
          git config --global core.symlinks true

          # https://git-scm.com/docs/git-fsmonitor--daemon.  The daemon could lock
          # the directory on Windows and prevent GHA from checking out as reported
          # in https://github.com/actions/checkout/issues/1018
          git config --global core.fsmonitor false
      # Needed for binary builds, see: https://github.com/pytorch/pytorch/issues/73339#issuecomment-1058981560
      - name: Enable long paths on Windows
        shell: powershell
        run: |
          Set-ItemProperty -Path "HKLM:\\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1
      # Since it's just a defensive command, the workflow should continue even the command fails. This step can be
      # removed once Windows Defender is removed from the AMI
      - name: Disables Windows Defender scheduled and real-time scanning for files in directories used by PyTorch
        continue-on-error: true
        shell: powershell
        run: |
          Add-MpPreference -ExclusionPath $(Get-Location).tostring(),$Env:TEMP -ErrorAction Ignore
          # Let's both exclude the path and disable Windows Defender completely just to be sure
          # that it doesn't interfere
          Set-MpPreference -DisableRealtimeMonitoring $True -ErrorAction Ignore
{%- endmacro -%}

{%- macro apply_filter() -%}
      - name: Check if the job is disabled
        id: filter
        # Binary workflows checkout to pytorch subdirectory instead
        uses: ./pytorch/.github/actions/filter-test-configs
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          # NB: Use a mock test matrix with a default value here. After filtering, if the
          # returned matrix is empty, it means that the job is disabled
          test-matrix: |
            { include: [
              { config: "default" },
            ]}
{%- endmacro -%}

{%- macro checkout(submodules="recursive", deep_clone=True, directory="", repository="pytorch/pytorch", branch="", checkout_pr_head=True) -%}
      - name: Checkout !{{ 'PyTorch' if repository == "pytorch/pytorch" else repository }}
        uses: actions/checkout@v4
        with:
      {%- if branch %}
          ref: !{{ branch }}
      {%- elif checkout_pr_head %}
          ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
      {%- endif %}
      {%- if deep_clone %}
          # deep clone, to allow use of git merge-base
          fetch-depth: 0
      {%- endif %}
          submodules: !{{ submodules }}
      {%- if repository != "pytorch/pytorch" %}
          repository: !{{ repository }}
      {%- endif %}
      {%- if directory %}
          path: !{{ directory }}
      {%- endif %}
          show-progress: false
      - name: Clean !{{ 'PyTorch' if repository == "pytorch/pytorch" else repository }} checkout
        run: |
          # Remove any artifacts from the previous checkouts
          git clean -fxd
      {%- if directory%}
        working-directory: !{{ directory }}
      {%- endif %}
{%- endmacro -%}

{%- macro wait_and_kill_ssh_windows(pytorch_directory="") -%}
      - name: Wait until all sessions have drained
        shell: powershell
{%- if pytorch_directory %}
        working-directory: !{{ pytorch_directory }}
{%- endif %}
        if: always()
        timeout-minutes: 120
        run: |
          .github\scripts\wait_for_ssh_to_drain.ps1
      - name: Kill active ssh sessions if still around (Useful if workflow was cancelled)
        shell: powershell
{%- if pytorch_directory %}
        working-directory: !{{ pytorch_directory }}
{%- endif %}
        if: always()
        run: |
          .github\scripts\kill_active_ssh_sessions.ps1
{%- endmacro -%}
