name: Linting

on:
  push:
    branches:
      - main
      - beta
      - release
      - "hotfix*"
  pull_request:
    branches:
      - main
      - beta
      - release
      - "hotfix*"
  merge_group:
    types: [checks_requested]
  workflow_dispatch:

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

jobs:
  linting-path-filter:
    name: Linting path filter
    timeout-minutes: 5
    runs-on: ubuntu-latest
    permissions:
      pull-requests: read
    outputs:
      all: ${{ steps.filter.outputs.all }}
      ts-files: ${{ steps.filter.outputs.ts-files }}
      js-files: ${{ steps.filter.outputs.js-files }}
      github-scripts: ${{ steps.filter.outputs.github-scripts }}
      local-scripts: ${{ steps.filter.outputs.local-scripts }}
    steps:
      - name: Checkout GitHub repository
        uses: actions/checkout@v6
        with:
          sparse-checkout: |
            .github/lint-filters.yml

      - uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
        id: filter
        with:
          filters: .github/lint-filters.yml

  run-linters:
    name: Run all linters
    timeout-minutes: 10
    runs-on: ubuntu-latest
    needs: linting-path-filter

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v6
        with:
          submodules: "recursive"

      - uses: ./.github/actions/setup-deps
        if: |
          needs.linting-path-filter.outputs.all == 'true' ||
          needs.linting-path-filter.outputs.ts-files == 'true' ||
          needs.linting-path-filter.outputs.js-files == 'true' ||
          needs.linting-path-filter.outputs.github-scripts == 'true' ||
          needs.linting-path-filter.outputs.local-scripts == 'true'

      # Lint files with Biome-Lint - https://biomejs.dev/linter/
      - name: Lint with Biome
        run: pnpm biome:ci
        if: >-
          ${{ !cancelled() &&
            (
              needs.linting-path-filter.outputs.all == 'true' ||
              needs.linting-path-filter.outputs.ts-files == 'true' ||
              needs.linting-path-filter.outputs.js-files == 'true' ||
              needs.linting-path-filter.outputs.github-scripts == 'true' ||
              needs.linting-path-filter.outputs.local-scripts == 'true'
            ) }}

      # Validate dependencies with dependency-cruiser - https://github.com/sverweij/dependency-cruiser
      - name: Run Dependency Cruiser
        run: pnpm depcruise
        if: >-
          ${{ !cancelled() &&
            (
              needs.linting-path-filter.outputs.all == 'true' ||
              needs.linting-path-filter.outputs.ts-files == 'true' ||
              needs.linting-path-filter.outputs.js-files == 'true' ||
              needs.linting-path-filter.outputs.github-scripts == 'true' ||
              needs.linting-path-filter.outputs.local-scripts == 'true'
            ) }}

      # Validate types with tsc - https://www.typescriptlang.org/docs/handbook/compiler-options.html#using-the-cli
      - name: Run Typecheck (Main Repo + Scripts)
        run: pnpm typecheck:main
        id: typecheck-main
        if: ${{ !cancelled() && (needs.linting-path-filter.outputs.all == 'true' || needs.linting-path-filter.outputs.ts-files == 'true' || needs.linting-path-filter.outputs.local-scripts == 'true') }}

      - name: Run Typecheck (Github Scripts)
        run: pnpm typecheck:github
        id: typecheck-github
        if: ${{ !cancelled() && (needs.linting-path-filter.outputs.all == 'true' || needs.linting-path-filter.outputs.github-scripts == 'true') }}

      - name: Run Typecheck (JS files)
        run: pnpm typecheck:js
        id: typecheck-js
        if: ${{ !cancelled() && (needs.linting-path-filter.outputs.all == 'true' || needs.linting-path-filter.outputs.js-files == 'true') }}

      - name: Check for REUSE compliance
        id: reuse-lint
        uses: fsfe/reuse-action@v5
        if: ${{ !cancelled() }}
