|
| 1 | +#!python3 |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import argparse |
| 5 | +import subprocess |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +from packaging import version |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from collections.abc import Sequence |
| 12 | + |
| 13 | + |
| 14 | +class Args(argparse.Namespace): |
| 15 | + version: str |
| 16 | + dry_run: bool |
| 17 | + |
| 18 | + |
| 19 | +def parse_args(argv: Sequence[str] | None = None) -> Args: |
| 20 | + parser = argparse.ArgumentParser( |
| 21 | + prog="towncrier-automation", |
| 22 | + description=( |
| 23 | + "This script runs towncrier for a given version, " |
| 24 | + "creates a branch off of the current one, " |
| 25 | + "and then creates a PR into the original branch with the changes. " |
| 26 | + "The PR will be backported to main if the current branch is not main." |
| 27 | + ), |
| 28 | + ) |
| 29 | + parser.add_argument( |
| 30 | + "version", |
| 31 | + type=str, |
| 32 | + help=( |
| 33 | + "The new version for the release must have at least three parts, like `major.minor.patch` and no `major.minor`. " |
| 34 | + "It can have a suffix like `major.minor.patch.dev0` or `major.minor.0rc1`." |
| 35 | + ), |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + "--dry-run", |
| 39 | + help="Whether or not to dry-run the actual creation of the pull request", |
| 40 | + action="store_true", |
| 41 | + ) |
| 42 | + args = parser.parse_args(argv, Args()) |
| 43 | + if len(version.Version(args.version).release) != 3: |
| 44 | + raise ValueError( |
| 45 | + f"Version argument {args.version} must contain major, minor, and patch version." |
| 46 | + ) |
| 47 | + version.parse(args.version) # validate |
| 48 | + return args |
| 49 | + |
| 50 | + |
| 51 | +def main(argv: Sequence[str] | None = None) -> None: |
| 52 | + args = parse_args(argv) |
| 53 | + |
| 54 | + # Run towncrier |
| 55 | + subprocess.run( |
| 56 | + ["towncrier", "build", f"--version={args.version}", "--yes"], check=True |
| 57 | + ) |
| 58 | + |
| 59 | + # Check if we are on the main branch to know if we need to backport |
| 60 | + base_branch = subprocess.run( |
| 61 | + ["git", "rev-parse", "--abbrev-ref", "HEAD"], |
| 62 | + capture_output=True, |
| 63 | + text=True, |
| 64 | + check=True, |
| 65 | + ).stdout.strip() |
| 66 | + pr_description = "" if base_branch == "main" else "on-merge: backport to main" |
| 67 | + branch_name = f"release_notes_{args.version}" |
| 68 | + |
| 69 | + # Create a new branch + commit |
| 70 | + subprocess.run(["git", "switch", "-c", branch_name], check=True) |
| 71 | + subprocess.run(["git", "add", "docs/release-notes"], check=True) |
| 72 | + pr_title = f"(chore): generate {args.version} release notes" |
| 73 | + subprocess.run(["git", "commit", "-m", pr_title], check=True) |
| 74 | + |
| 75 | + # Create a PR |
| 76 | + subprocess.run( |
| 77 | + [ |
| 78 | + "gh", |
| 79 | + "pr", |
| 80 | + "create", |
| 81 | + f"--base={base_branch}", |
| 82 | + f"--head={branch_name}", |
| 83 | + f"--title={pr_title}", |
| 84 | + f"--body={pr_description}", |
| 85 | + *(["--dry-run"] if args.dry_run else []), |
| 86 | + ], |
| 87 | + check=True, |
| 88 | + ) |
| 89 | + |
| 90 | + # Enable auto-merge |
| 91 | + if not args.dry_run: |
| 92 | + subprocess.run( |
| 93 | + ["gh", "pr", "merge", branch_name, "--auto", "--squash"], check=True |
| 94 | + ) |
| 95 | + else: |
| 96 | + print("Dry run, not merging") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments