name: "Pre-release"

on:
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+-alpha.[0-9]+"  # e.g. v0.37.0-alpha.1, v0.38.0-alpha.10
      - "v[0-9]+.[0-9]+.[0-9]+-beta.[0-9]+"   # e.g. v0.37.0-beta.1, v0.38.0-beta.10
      - "v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+"      # e.g. v0.37.0-rc1, v0.38.0-rc10

jobs:
  prerelease:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-go@v5
        with:
          go-version: '1.22'

      # Similar check to ./release-version.yml, but enforces this when pushing
      # tags. The ./release-version.yml check can be bypassed and is mainly
      # present for informational purposes.
      - name: Check release version
        run: |
          # We strip the refs/tags/v prefix of the tag name.
          TAG_VERSION=${GITHUB_REF#refs/tags/v}
          # Get the version of the code, which has no "v" prefix.
          CODE_VERSION=`go run ./cmd/cometbft/ version`
          if [ "$TAG_VERSION" != "$CODE_VERSION" ]; then
            echo ""
            echo "Tag version ${TAG_VERSION} does not match code version ${CODE_VERSION}"
            echo ""
            echo "Please either fix the release tag or the version of the software in version/version.go."
            exit 1
          fi

      - name: Generate release notes
        run: |
          VERSION="${GITHUB_REF#refs/tags/}"
          CHANGELOG_URL="https://github.com/cometbft/cometbft/blob/${VERSION}/CHANGELOG.md"
          echo "See the [CHANGELOG](${CHANGELOG_URL}) for changes available in this pre-release, but not yet officially released." > ../release_notes.md

      - name: Release
        uses: goreleaser/goreleaser-action@v6
        with:
          version: latest
          args: release --clean --release-notes ../release_notes.md
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  prerelease-success:
    needs: prerelease
    if: ${{ success() }}
    runs-on: ubuntu-latest
    steps:
      - name: Notify Slack upon pre-release
        uses: slackapi/slack-github-action@v2.0.0
        env:
          RELEASE_URL: "${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ github.ref_name }}"
        with:
          webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
          webhook-type: incoming-webhook
          payload: |
            blocks:
              - type: "section"
                text:
                  type: "mrkdwn"
                  text: ":sparkles: New CometBFT pre-release: <${{ env.RELEASE_URL }}|${{ github.ref_name }}>"
