name: lint

on:
  workflow_call:

jobs:
  filter:
    name: filter
    runs-on: ubuntu-latest
    outputs:
      golang: ${{ steps.filter.outputs.golang }}
      golang_files: ${{ steps.filter.outputs.golang_files }}
    steps:
      - uses: actions/checkout@v4
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          list-files: shell
          filters: |
            golang:
              - added|modified: '**/*.go'
              - added|modified: '/go.sum'
              - added|modified: '/go.mod'

  codeql:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        # We must fetch at least the immediate parents so that if this is
        # a pull request then we can checkout the head.
        fetch-depth: 2

    - run: git checkout HEAD^2
      if: ${{ github.event_name == 'pull_request' }}

  spelling:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Check spelling
        uses: codespell-project/actions-codespell@v2

  go:
    runs-on: ubuntu-latest
    needs:
      - filter
    if: needs.filter.outputs.golang == 'true'
    steps:
      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.22.5

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Check Code Formatting
        run: |
          test -z "$(gofmt -d ${{ needs.filter.outputs.golang_files }})" || exit 1
