Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/create-release-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Create Release PR

on:
workflow_dispatch:
inputs:
bump:
description: 'Type of version bump'
required: true
type: choice
options:
- patch
- minor
- major
default: patch

jobs:
create-release-pr:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout develop branch
uses: actions/checkout@v4
with:
ref: develop
fetch-depth: 0
token: ${{ secrets.GH_TOKEN }}

- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Configure Git
run: |
git config user.name "Deadbot0"
git config user.email "deadbot1101@gmail.com"

- name: Compute next version
id: version
run: |
NEXT_VERSION=$(python3 ./scripts/increment_version.py ${{ inputs.bump }} --print-only)
echo "version=$NEXT_VERSION" >> "$GITHUB_OUTPUT"

- name: Create release branch
run: |
git checkout -b release/v${{ steps.version.outputs.version }}
git push origin release/v${{ steps.version.outputs.version }}

- name: Create Pull Request
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
gh pr create \
--base master \
--head release/v${{ steps.version.outputs.version }} \
--title "Release v${{ steps.version.outputs.version }}" \
--body "Automated PR to release version ${{ steps.version.outputs.version }} into master. Merging this will trigger the version bump, build, and release pipeline."
107 changes: 59 additions & 48 deletions scripts/increment_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import argparse
import tomllib
from typing import TypedDict, Optional, Literal
from pathlib import Path
from typing import Literal, TypedDict

Check failure on line 4 in scripts/increment_version.py

View workflow job for this annotation

GitHub Actions / lint

ruff (I001)

scripts/increment_version.py:1:1: I001 Import block is un-sorted or un-formatted help: Organize imports

VERSION_DIR = Path('pyproject.toml')
VERSION_FILE = Path('src/_version.py')
Expand All @@ -11,55 +11,20 @@
major: int
minor: int
patch: int
beta: Optional[int]
beta: int | None


IncrementType = Literal['major', 'minor', 'patch', 'beta']


def increment_version(increment_type: IncrementType):
"""Increments Deadbot version number

Args:
increment_type: The type of increment being used - major, minor, patch, or beta
"""
version = read_version()

match increment_type:
case 'major':
version['major'] += 1
version['minor'] = 0
version['patch'] = 0
version['beta'] = 0

case 'minor':
version['minor'] += 1
version['patch'] = 0
version['beta'] = 0

case 'patch':
version['patch'] += 1
version['beta'] = 0

case 'beta':
version['beta'] += 1

write_version(version)
return


def read_version() -> VersionInfo:
with open(VERSION_DIR, 'rb') as f:
pyproject_data = tomllib.load(f)

version = pyproject_data['tool']['poetry']['version']
version_components = version.split('-')
def parse_version_string(version_str: str) -> VersionInfo:
version_components = version_str.split('-')
[major, minor, patch] = version_components[0].split('.')

# no beta is more simply represented as "0" as beta starts at "1"
beta = 0
if len(version_components) == 2:
beta = version_components[1].split('.')[1]
beta = int(version_components[1].split('.')[1])

return {
'major': int(major),
Expand All @@ -69,11 +34,45 @@
}


def write_version(version: VersionInfo):
version_string = f'{version["major"]}.' f'{version["minor"]}.' f'{version["patch"]}'
def read_version() -> VersionInfo:
with open(VERSION_DIR, 'rb') as f:
pyproject_data = tomllib.load(f)
version = pyproject_data['tool']['poetry']['version']
return parse_version_string(version)


def get_next_version(current: VersionInfo, increment_type: IncrementType) -> VersionInfo:
"""Pure function that calculates the next version without side-effects."""
next_version = current.copy()

match increment_type:
case 'major':
next_version['major'] += 1
next_version['minor'] = 0
next_version['patch'] = 0
next_version['beta'] = 0
case 'minor':
next_version['minor'] += 1
next_version['patch'] = 0
next_version['beta'] = 0
case 'patch':
next_version['patch'] += 1
next_version['beta'] = 0
case 'beta':
next_version['beta'] += 1

return next_version


def format_version(version: VersionInfo) -> str:
version_string = f'{version["major"]}.{version["minor"]}.{version["patch"]}'
if version['beta']:
version_string += f'-beta.{version["beta"]}'
return version_string


def write_version(version: VersionInfo):
version_string = format_version(version)

# Update pyproject.toml
lines = []
Expand All @@ -93,11 +92,23 @@
)


def increment_version(increment_type: IncrementType):
version = read_version()
next_version = get_next_version(version, increment_type)
write_version(next_version)


if __name__ == '__main__':
increment_type = sys.argv[1]
valid_types = ['major', 'minor', 'patch', 'beta']
parser = argparse.ArgumentParser(description='Increment Deadbot version')
parser.add_argument('increment_type', choices=['major', 'minor', 'patch', 'beta'])
parser.add_argument('--print-only', action='store_true', help='Print the next version number without writing to files')

args = parser.parse_args()

if increment_type not in valid_types:
raise Exception(f'Invalid increment type "{increment_type}" - must be one of {valid_types}')
current_version = read_version()
next_version = get_next_version(current_version, args.increment_type)

increment_version(increment_type)
if args.print_only:
print(format_version(next_version))
else:
write_version(next_version)
Loading