name: diff-shades

on:
  push:
    branches: [main]
    paths: ["src/**", "pyproject.toml", ".github/workflows/*"]

  pull_request:
    paths: ["src/**", "pyproject.toml", ".github/workflows/*"]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
  cancel-in-progress: true

env:
  HATCH_BUILD_HOOKS_ENABLE: "1"
  # Clang is less picky with the C code it's given than gcc (and may generate faster
  # binaries too).
  CC: clang-18

permissions: {}

jobs:
  configure:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-config.outputs.matrix }}

    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          persist-credentials: false

      - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.13"

      - name: Install diff-shades and support dependencies
        run: |
          python -m pip install 'click>=8.1.7' packaging urllib3
          python -m pip install https://github.com/ichard26/diff-shades/archive/stable.zip

      - name: Calculate run configuration & metadata
        id: set-config
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: python scripts/diff_shades_gha_helper.py config ${{ github.event_name }}

  analysis-base:
    name: analysis / base / ${{ matrix.mode }}
    needs: configure
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJson(needs.configure.outputs.matrix) }}

    steps:
      - name: Checkout this repository (full clone)
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          # The baseline revision could be rather old so a full clone is ideal.
          fetch-depth: 0
          persist-credentials: false

      - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.13"

      - name: Install diff-shades
        run: |
          python -m pip install https://github.com/ichard26/diff-shades/archive/stable.zip
          git config user.name "diff-shades-gha"
          git config user.email "diff-shades-gha@example.com"

      - name: Attempt to use cached baseline analysis
        id: baseline-cache
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: ${{ matrix.baseline-analysis }}
          key: ${{ matrix.baseline-cache-key }}

      - name: Build and install baseline revision
        if: steps.baseline-cache.outputs.cache-hit != 'true'
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: >
          ${{ matrix.baseline-setup-cmd }}
          && python -m pip install .

      - name: Analyze baseline revision
        if: steps.baseline-cache.outputs.cache-hit != 'true'
        run: >
          diff-shades analyze ${{ matrix.baseline-analysis }}
          -v --work-dir projects-cache/ ${{ matrix.force-flag }}

      - name: Upload baseline analysis
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: ${{ matrix.baseline-analysis }}
          path: ${{ matrix.baseline-analysis }}

  analysis-target:
    name: analysis / target / ${{ matrix.mode }}
    needs: configure
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJson(needs.configure.outputs.matrix) }}

    steps:
      - name: Checkout this repository (full clone)
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          # The baseline revision could be rather old so a full clone is ideal.
          fetch-depth: 0
          persist-credentials: false

      - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.13"

      - name: Install diff-shades
        run: |
          python -m pip install https://github.com/ichard26/diff-shades/archive/stable.zip
          git config user.name "diff-shades-gha"
          git config user.email "diff-shades-gha@example.com"

      - name: Build and install target revision
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: >
          ${{ matrix.target-setup-cmd }}
          && python -m pip install .

      # Pull it from previous runs - we're NOT trying to get it from this run
      # (but it wouldn't cause problems if we theoretically did)
      - name: Attempt to find baseline analysis
        id: baseline-cache
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: ${{ matrix.baseline-analysis }}
          key: ${{ matrix.baseline-cache-key }}

      - name: Analyze target revision (with repeated projects)
        if: steps.baseline-cache.outputs.cache-hit == 'true'
        run: >
          diff-shades analyze ${{ matrix.target-analysis }}
          -v --work-dir projects-cache/ ${{ matrix.force-flag }}
          --repeat-projects-from ${{ matrix.baseline-analysis }}

      - name: Analyze target revision (without repeated projects)
        if: steps.baseline-cache.outputs.cache-hit != 'true'
        run: >
          diff-shades analyze ${{ matrix.target-analysis }}
          -v --work-dir projects-cache/ ${{ matrix.force-flag }}

      - name: Upload target analysis
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: ${{ matrix.target-analysis }}
          path: ${{ matrix.target-analysis }}

      - name: Check for failed files for target revision
        run: >
          diff-shades show-failed --check --show-log ${{ matrix.target-analysis }}

  compare:
    name: compare / ${{ matrix.mode }}
    needs: ["configure", "analysis-base", "analysis-target"]
    if: ${{ !cancelled() }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJson(needs.configure.outputs.matrix) }}

    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          persist-credentials: false

      - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
          merge-multiple: true

      - uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.13"

      - name: Install diff-shades and support dependencies
        run: |
          python -m pip install 'click>=8.1.7' packaging urllib3
          python -m pip install https://github.com/ichard26/diff-shades/archive/stable.zip

      - name: Generate HTML diff report
        run: >
          diff-shades --dump-html diff.html compare --diff
          ${{ matrix.baseline-analysis }} ${{ matrix.target-analysis }}

      - name: Upload diff report
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: ${{ matrix.mode }}-diff.html
          path: diff.html

      - name: Generate summary file (PR only)
        if: github.event_name == 'pull_request' && matrix.mode == 'preview-new-changes'
        run: >
          python scripts/diff_shades_gha_helper.py comment-body
          ${{ matrix.baseline-analysis }} ${{ matrix.target-analysis }}
          ${{ matrix.baseline-sha }} ${{ matrix.target-sha }}
          ${{ github.event.pull_request.number }}

      - name: Upload summary file (PR only)
        if: github.event_name == 'pull_request' && matrix.mode == 'preview-new-changes'
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: .pr-comment.json
          path: .pr-comment.json
          include-hidden-files: true

      - name: Verify zero changes (PR only)
        if: matrix.mode == 'assert-no-changes'
        run: >
          diff-shades compare --check ${{ matrix.baseline-analysis }} ${{ matrix.target-analysis }}
          || (echo "Please verify you didn't change the stable code style unintentionally!" && exit 1)
