name: validate
on:
  push:
    tags:
      - v*
    branches:
      - main
      - release-*
  pull_request:
  schedule:
    # Runs at 00:00 UTC every Monday
    - cron: '0 0 * * 1'
env:
  GO_VERSION: 1.24
permissions:
  contents: read

jobs:
  lint:
    timeout-minutes: 30
    permissions:
      contents: read
      pull-requests: read
      checks: write # to allow the action to annotate code in the PR.
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 2
      - uses: actions/setup-go@v6
        with:
          go-version: "${{ env.GO_VERSION }}"
      - uses: golangci/golangci-lint-action@v8
        with:
          version: v2.4
      # Extra linters, only checking new code from a pull request.
      - name: lint-extra
        if: github.event_name == 'pull_request'
        run: |
          golangci-lint run --config .golangci-extra.yml --new-from-rev=HEAD~1

  go-fix:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 2
      - uses: actions/setup-go@v6
        with:
          go-version: "${{ env.GO_VERSION }}"
      - name: run go fix
        run: |
          go fix ./...
          git diff --exit-code

  codespell:
    runs-on: ubuntu-24.04
    steps:
    - uses: actions/checkout@v5
    - name: install deps
      # Use a known version of codespell.
      run: pip install --break-system-packages codespell==v2.4.1
    - name: run codespell
      run: codespell

  space-at-eol:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v5
      - run: if git -P grep -I -n '\s$'; then echo "^^^ extra whitespace at EOL, please fix"; exit 1; fi

  deps:
    runs-on: ubuntu-24.04
    steps:
    - uses: actions/checkout@v5
    - uses: actions/setup-go@v6
      with:
        go-version: "${{ env.GO_VERSION }}"
    - run: go mod tidy --diff

  govulncheck:
    runs-on: ubuntu-24.04
    steps:
    - uses: golang/govulncheck-action@v1

  all-done:
    needs:
      - codespell
      - deps
      - go-fix
      - govulncheck
      - lint
      - space-at-eol
    runs-on: ubuntu-24.04
    steps:
    - run: echo "All jobs completed"
