name: Go Release

on:
  repository_dispatch:
    types: [tag_created]
  push:
    tags:
      - "v*"

jobs:
  test:
    name: Run tests
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: ./go.mod

      - name: Run tests
        run: go test -v ./...

  build:
    # only run in main upstream repo
    if: ${{ github.repository_owner == 'danielmiessler' }}
    name: Build & Release with Goreleaser
    needs: [test]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: ./go.mod
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v6
        with:
          distribution: goreleaser
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Update Release Description
        run: go run ./cmd/generate_changelog --release ${{ github.event.client_payload.tag || github.ref_name }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
