name: release-3-create-github-release

permissions:
  contents: write
  id-token: write
  attestations: write

on:
  workflow_dispatch:
    inputs:
      ref:
        description: "The ref (branch or SHA) to process"
        required: false
        type: string
  workflow_call:
    inputs:
      skip_latest:
        description: "skip_latest: Tag latest will be pushed unless skip_latest=true"
        required: true
        type: boolean
        default: false
defaults:
  run:
    shell: bash -xe {0}

jobs:
  create-github-release:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8
        with:
          # Use the ref if provided, otherwise obtain the parent workflow branch, defaults to main.
          ref: ${{ github.event.inputs.ref || github.event.workflow_run.head_branch }}

      - id: git-info
        run: |
          VERSION="$(grep -m1 '^version = "' Cargo.toml | cut -d'"' -f2)"
          echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
          echo "tag=v$VERSION" >> $GITHUB_OUTPUT
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - name: Create GitHub Release
        run: |
          echo "Releasing $TAG $SHA"
          body=$(./scripts/extract-release-info.sh "$VERSION" | jq -R -s .)
          curl -v --fail -X POST \
            -H "Content-Type: application/json" \
            -H "Authorization: Bearer $GITHUB_TOKEN" \
            -d '{
              "tag_name": "'"$TAG"'",
              "target_commitish": "'"$SHA"'",
              "name": "obelisk-'"$TAG"'",
              "body": '"$body"',
              "draft": false,
              "prerelease": false
            }' \
            https://api.github.com/repos/obeli-sk/obelisk/releases
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SHA: ${{ steps.git-info.outputs.sha }}
          TAG: ${{ steps.git-info.outputs.tag }}
          VERSION: ${{ steps.git-info.outputs.version }}

  call_child:
    needs: create-github-release
    uses: ./.github/workflows/release-3.1-upload-artifacts.yml
    secrets: inherit
    with:
      skip_latest: "${{inputs.skip_latest}}"
