name: Create Package Release

on:
  workflow_dispatch:
    inputs:
      versionType:
        type: choice
        description: 'Release Type'
        options:
          - minor
          - major
        required: true

jobs:
  create-release:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
      with:
        ref: main

    - name: Setup Git
      run: |
        git config --global user.name 'GitHub Actions'
        git config --global user.email 'actions@github.com'

    - name: Upgrade yarn with major version
      if: ${{ github.event.inputs.versionType == 'major' }}
      run: |
        yarn version --major --no-git-tag-version
        git add package.json

    - name: Upgrade yarn with minor version
      if: ${{ github.event.inputs.versionType == 'minor' }}
      run: |
        yarn version --minor --no-git-tag-version
        git add package.json

        
    - name: Create Release Branch
      run: |
        NEW_VERSION=$(npm pkg get version | tr -d '"') # Trim quotes wrapping command output
        TIMESTAMP=$(date +'%Y%m%d%H%M%S')
        BRANCH_NAME="release-changes-${NEW_VERSION}-${TIMESTAMP}"
        git checkout -b $BRANCH_NAME
        echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
        echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV

    - name: Commit Release Changes
      run: |
        git commit -m "Release ${{ env.NEW_VERSION }}"
        git push origin HEAD

    - name: Create Pull Request
      uses: repo-sync/pull-request@v2
      with:
        source_branch: ${{ env.BRANCH_NAME }}
        destination_branch: "main"
        github_token: ${{ secrets.GITHUB_TOKEN }}
        pr_title: "Release CLI: Version ${{ env.NEW_VERSION }}"
        pr_body: "Automated pull request to release the latest version of the CLI."
        pr_label: "automated-pr"
