name: Lint
on:
  push:
    branches: [main]
  pull_request:
    paths:
      - ".github/actions/**"
      - .github/workflows/lint.yml
      - "**/*.{yml,yaml,md,mdx,js,jsx,ts,tsx,json,toml,css}"
      - pnpm-lock.yaml
      - package.json
      - "Cargo.**"
      - "crates/**"
      - ".cargo/**"
      - rust-toolchain

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
  actions: write
  contents: read
  pull-requests: read

jobs:
  rust_lint:
    name: Rust lints
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Rust
        uses: ./.github/actions/setup-rust
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"

      - name: Run cargo fmt check
        run: |
          cargo fmt --check

      - name: Check Cargo.toml formatting (taplo)
        run: npx @taplo/cli@0.5.2 format --check

      - name: Check licenses
        uses: EmbarkStudios/cargo-deny-action@v2
        with:
          command: check licenses

  format_lint:
    name: Formatting
    runs-on: ubuntu-latest
    env:
      TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
      TURBO_TEAM: ${{ vars.TURBO_TEAM }}
      TURBO_CACHE: remote:rw
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: "Setup Node"
        uses: ./.github/actions/setup-node
        with:
          extra-flags: --no-optional

      - name: Install Global Turbo
        uses: ./.github/actions/install-global-turbo

      - name: Lint
        # Manually set TURBO_API to an empty string to override Hetzner env
        run: |
          TURBO_API= turbo run lint --env-mode=strict

  cleanup:
    name: Cleanup
    needs:
      - rust_lint
      - format_lint
    if: always()
    uses: ./.github/workflows/pr-clean-caches.yml
    secrets: inherit
