name: Import analysis

on:
  pull_request:

jobs:
  import_analysis:
    name: "Bootstrap import analysis"
    runs-on: ubuntu-latest
    permissions:
       pull-requests: write
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          path: dd-trace-py

      - name: Install Python 3.12
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: '3.12'

      - name: Install rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@9d7e65c320fdb52dcd45ffaa68deb6c02c8754d9 # v1.12.0
        
      - name: Install apt dependencies
        run: sudo apt-get update && sudo apt-get install -y --no-install-recommends build-essential

      - name: Set up PR environment
        run: |
          python -m venv .venv-pr
          source .venv-pr/bin/activate
          python -m pip install ./dd-trace-py
          python -c "import ddtrace.auto"
          deactivate

      - name: Collect import data from PR
        env:
          DD_REMOTE_CONFIGURATION_ENABLED: 0
        run: |
          source .venv-pr/bin/activate
          for n in {0..1000}; do
            python -X importtime -c "import ddtrace.auto" 2> import-pr-${n}.txt
          done
          deactivate

      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          ref: ${{ github.base_ref }}
          persist-credentials: false
          path: dd-trace-py
  
      - name: Set up base environment
        run: |
          python -m venv .venv-base
          source .venv-base/bin/activate
          python -m pip install ./dd-trace-py
          python -c "import ddtrace.auto"
          deactivate
  
      - name: Collect import data from base
        env:
          DD_REMOTE_CONFIGURATION_ENABLED: 0
        run: |
          source .venv-base/bin/activate
          for n in {0..1000}; do
            python -X importtime -c "import ddtrace.auto" 2> import-base-${n}.txt
          done
          deactivate

      - name: Analyze import data
        run: |
          source .venv-pr/bin/activate
          python -m pip install -r dd-trace-py/scripts/import-analysis/requirements.txt
          python dd-trace-py/scripts/import-analysis/import_analysis.py > import_analysis.txt
          deactivate

      - name: Report analysis result on PR
        uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
        with:
          file-path: import_analysis.txt
          comment-tag: import_analysis
          mode: upsert
        if: always()

  import_cycles:
    name: "Circular import analysis"
    runs-on: ubuntu-latest
    permissions:
       pull-requests: write
    steps:
      - name: "Install Python 3.12"
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: '3.12'

      - name: "Checkout the PR"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          path: dd-trace-py
            
      - name: "Install dependencies"
        run: python -m pip install -r dd-trace-py/scripts/import-analysis/requirements-cycles.txt

      # TODO: Remove this step
      - name: Cache the cycle script
        run: cp dd-trace-py/scripts/import-analysis/cycles.py .

      - name: "Analyze imports on PR"
        run: python dd-trace-py/scripts/import-analysis/cycles.py analyze cycles-pr.json

      - name: "Checkout the base branch"
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          ref: ${{ github.base_ref }}
          persist-credentials: false
          path: dd-trace-py

      # TODO: Remove this step
      - name: Ensure cycle script is available
        run: |
          mkdir -p dd-trace-py/scripts/import-analysis || true
          mv cycles.py dd-trace-py/scripts/import-analysis/cycles.py

      - name: "Analyze imports on base branch"
        run: python dd-trace-py/scripts/import-analysis/cycles.py analyze cycles-base.json

      - name: "Upload cycle artifacts"
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: cycles
          path: cycles-*.json

      - name: "Compare import cycles"
        run: python dd-trace-py/scripts/import-analysis/cycles.py compare cycles-base.json cycles-pr.json > cycles_report.txt

      - name: "Report analysis result on PR"
        uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
        with:
          file-path: cycles_report.txt
          comment-tag: import_cycles
          mode: upsert
        if: failure()

      