name: ci

on:
  push:
    branches: ["**"]
  pull_request:
  workflow_dispatch:

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: "1.22.x"
          cache: true

      - name: Format check (gofmt)
        run: |
          test -z "$(gofmt -l .)"

      - name: Vet
        run: go vet ./...

      - name: Test (with coverage)
        run: go test ./... -coverprofile=coverage.out

      - name: Coverage floor
        run: |
          total="$(go tool cover -func=coverage.out | tail -n 1 | awk '{print $3}' | sed 's/%//')"
          echo "total coverage: ${total}%"
          awk -v cov="${total}" 'BEGIN { exit !(cov+0 >= 70.0) }' || { echo "coverage floor not met (min 70.0%)"; exit 1; }

      - name: golangci-lint
        uses: golangci/golangci-lint-action@v6
        with:
          version: v1.61.0
          args: ./...
