name: Create Release

on:
  push:
    branches:
      - main

jobs:
  create-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: read
    if: contains(github.event.head_commit.message, 'Cut release v')
    steps:
      - uses: actions/github-script@v7
        name: Read Cut release PR info and create release
        with:
          script: |
            const { owner, repo } = context.repo
            const pulls = await github.rest.repos.listPullRequestsAssociatedWithCommit({
              owner,
              repo,
              commit_sha: context.sha,
            })
            const { title, body } = pulls.data[0]
            const version = title.split('Cut release ')[1]

            const result = await github.rest.repos.createRelease({
              owner,
              repo,
              body,
              tag_name: version,
              name: version,
              draft: true,
            })
            console.log(result)
