name: License Check

on:
  pull_request:
    branches: [master]

concurrency:
  group: license-${{ github.ref }}
  cancel-in-progress: true

jobs:
  changes:
    name: Detect Changes
    runs-on: ubuntu-latest
    outputs:
      should_run: ${{ steps.filter.outputs.deps }}
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4

      - uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
        id: filter
        with:
          filters: |
            deps:
              - 'Cargo.toml'
              - 'Cargo.lock'
              - 'deny.toml'

  license:
    name: Dependency Licenses
    needs: changes
    if: needs.changes.outputs.should_run == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4

      - uses: EmbarkStudios/cargo-deny-action@91bf2b620e09e18d6eb78b92e7861937469acedb # v2.0.17
        with:
          command: check licenses

  license-check:
    name: License Check Status
    if: always()
    needs: [changes, license]
    runs-on: ubuntu-latest
    steps:
      - name: Check results
        run: |
          if [[ "${{ needs.changes.outputs.should_run }}" != "true" ]]; then
            echo "Skipped: no dependency files changed"
            exit 0
          fi
          if [[ "${{ needs.license.result }}" != "success" ]]; then
            echo "License check failed"
            exit 1
          fi
          echo "License check passed"
