name: Go
on:
  push:
    branches:
      - main
  pull_request: { }

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-go@v6
        with:
          go-version: stable

      - name: Build
        run: go build -gcflags="-m" ./...

      - name: Test
        run: go test -v -race ./...

  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-go@v6
        with:
          go-version: stable

      - uses: golangci/golangci-lint-action@v9
        with:
          version: latest
          only-new-issues: true
          args: -E gosec --timeout=2m --verbose

  modtidy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-go@v6
        with:
          go-version: stable

      - name: Run go mod tidy
        run: |
          go mod tidy
          gitdiff="$(git diff -U0)"
          echo "$gitdiff"
          test -z "$gitdiff"

  vendor-diff:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v6
        with:
          go-version: stable

      - name: Checkout base commit
        uses: actions/checkout@v6
        with:
          path: a
          ref: ${{ github.base_ref }}

      - name: Download dependencies of base commit
        run: go mod vendor
        working-directory: a

      - name: Checkout PR
        uses: actions/checkout@v6
        with:
          path: b

      - name: Download dependencies of PR
        run: go mod vendor
        working-directory: b

      - name: Diff of dependencies
        run: diff -ur --color=always a/vendor b/vendor || true
