name: CI

on:
  push:
    branches:
      - main
    tags:
      - v*
  pull_request:

permissions:
  contents: read

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go: [1.22.x, 1.21.x, 1.20.x]
      fail-fast: false
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go }}
      - name: Test
        run: make test
      - name: Test with GOARCH=386
        run: env GOARCH=386 go test -v ./...
      - name: Test Coverage
        run: |
          go test -cover ./... | grep -F 100.0% || {
            go test -cover ./...
            echo Coverage decreased!
            exit 1
          } >&2
      - name: Lint
        run: make lint

  release:
    name: Release
    needs: test
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup release body
        run: sed -n '/\[${{ github.ref_name }}\]/,/^$/{//!p}' CHANGELOG.md >release-body.txt
      - name: Create release
        uses: ncipollo/release-action@v1
        with:
          name: Release ${{ github.ref_name }}
          bodyFile: release-body.txt
