name: golangci-lint
on:
  push:
    branches:
      - master
  pull_request:

permissions:
  contents: read

jobs:
  golangci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v4
        with:
          go-version-file: go.mod
          # setup-go v4 uses cache automatically, which conflicts with golangci-lint's cache.
          # See https://github.com/golangci/golangci-lint-action/pull/704
          cache: false
      # A workspace file is needed for golangci-lint to check the sub-modules.
      # https://github.com/golangci/golangci-lint-action/issues/544
      - run: make go-workspace
      # To check sub-modules, you need to supply their paths as positional arguments.
      # This step finds the paths and adds them to a variable which is used
      # later in the args value.
      # https://github.com/golangci/golangci-lint/issues/828
      - name: find-go-modules
        id: find-go-modules
        run: |
          find . -type f -name 'go.mod' -printf '%h/...\n' \
          | jq -r -R -s 'split("\n")[:-1] | sort | join(" ") | "GO_MODULES=\(.)"' \
          >> "$GITHUB_OUTPUT"
      - uses: golangci/golangci-lint-action@v3
        with:
          version: v1.55.2
          args: --timeout=30m --config=.golangci.ci.yaml ${{ steps.find-go-modules.outputs.GO_MODULES }}
