| 1 |
"""Script for preparing the repo for a new release.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import argparse |
| 6 |
import datetime |
| 7 |
import re |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
PATCH = ("patch", "bugfix") |
| 13 |
MINOR = ("minor", "feature") |
| 14 |
MAJOR = ("major", "breaking") |
| 15 |
|
| 16 |
REPO_URL = "https://github.com/rstcheck/rstcheck" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
def bump_version(current_version: str, release_type: str = "patch") -> str: |
| 21 |
"""Bump the current version for the next release. |
| 22 |
|
| 23 |
:param release_type: type of release; |
| 24 |
allowed values are: patch | minor/feature | major/breaking; |
| 25 |
defaults to "patch" |
| 26 |
:raises ValueError: when an invalid release_type is given. |
| 27 |
:raises ValueError: when the version string from pyproject.toml is not parsable. |
| 28 |
:return: new version string |
| 29 |
""" |
| 30 |
if release_type not in PATCH + MINOR + MAJOR: |
| 31 |
msg = f"Invalid version increase type: {release_type}" |
| 32 |
raise ValueError(msg) |
| 33 |
|
| 34 |
version_parts = re.match(r"v?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)", current_version) |
| 35 |
if not version_parts: |
| 36 |
msg = f"Unparsable version: {current_version}" |
| 37 |
raise ValueError(msg) |
| 38 |
|
| 39 |
if release_type in MAJOR: |
| 40 |
version = f"{int(version_parts.group('major')) + 1}.0.0" |
| 41 |
elif release_type in MINOR: |
| 42 |
version = f"{version_parts.group('major')}.{int(version_parts.group('minor')) + 1}.0" |
| 43 |
elif release_type in PATCH: |
| 44 |
version = ( |
| 45 |
f"{version_parts.group('major')}" |
| 46 |
f".{version_parts.group('minor')}" |
| 47 |
f".{int(version_parts.group('patch')) + 1}" |
| 48 |
) |
| 49 |
else: |
| 50 |
print("Given `RELEASE TYPE` is invalid.") |
| 51 |
sys.exit(1) |
| 52 |
|
| 53 |
return f"v{version}" |
| 54 |
|
| 55 |
|
| 56 |
def update_changelog(new_version: str, last_version: str, *, first_release: bool) -> None: |
| 57 |
"""Update CHANGELOG.md to be release ready. |
| 58 |
|
| 59 |
:param new_version: new version string |
| 60 |
:param last_version: current version string |
| 61 |
:first_release: if this is the first release |
| 62 |
""" |
| 63 |
with Path("CHANGELOG.md").open(encoding="utf8") as changelog_file: |
| 64 |
changelog_lines = changelog_file.read().split("\n") |
| 65 |
|
| 66 |
release_line = 0 |
| 67 |
|
| 68 |
for idx, line in enumerate(changelog_lines): |
| 69 |
if line.startswith("## Unreleased"): |
| 70 |
release_line = idx |
| 71 |
|
| 72 |
if release_line: |
| 73 |
today = datetime.datetime.now(tz=datetime.UTC).date().isoformat() |
| 74 |
compare = f"{'' if first_release else ''}{last_version}...{new_version}" |
| 75 |
changelog_lines[release_line] = ( |
| 76 |
"## Unreleased\n" |
| 77 |
"\n" |
| 78 |
f"[diff {new_version}...main]" |
| 79 |
f"({REPO_URL}/compare/{new_version}...main)\n" |
| 80 |
"\n" |
| 81 |
f"## [{new_version} ({today})]({REPO_URL}/releases/{new_version})\n" |
| 82 |
"\n" |
| 83 |
f"[diff {compare}]({REPO_URL}/compare/{compare})" |
| 84 |
) |
| 85 |
|
| 86 |
if len(changelog_lines) - 1 >= release_line + 2: |
| 87 |
changelog_lines.pop(release_line + 1) |
| 88 |
changelog_lines.pop(release_line + 1) |
| 89 |
|
| 90 |
with Path("CHANGELOG.md").open("w", encoding="utf8") as changelog_file: |
| 91 |
changelog_file.write("\n".join(changelog_lines)) |
| 92 |
|
| 93 |
|
| 94 |
def commit_and_tag(version: str) -> None: |
| 95 |
"""Git commit and tag the new release.""" |
| 96 |
subprocess.run( |
| 97 |
[ |
| 98 |
"git", |
| 99 |
"commit", |
| 100 |
"--no-verify", |
| 101 |
f'--message="release {version} [skip ci]"', |
| 102 |
"--include", |
| 103 |
"pyproject.toml", |
| 104 |
"CHANGELOG.md", |
| 105 |
], |
| 106 |
check=True, |
| 107 |
) |
| 108 |
subprocess.run( |
| 109 |
["git", "tag", "-am", f"'{version}'", version], |
| 110 |
check=True, |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
def _parser() -> argparse.Namespace: |
| 115 |
"""Create parser and return parsed args.""" |
| 116 |
parser = argparse.ArgumentParser() |
| 117 |
parser.add_argument( |
| 118 |
"increase_type", |
| 119 |
metavar="RELEASE TYPE", |
| 120 |
default="patch", |
| 121 |
nargs="?", |
| 122 |
help=( |
| 123 |
"Release type: patch/bugfix | minor/feature | major/breaking; " |
| 124 |
"gets ignored on `--first-release`; " |
| 125 |
"defaults to patch" |
| 126 |
), |
| 127 |
) |
| 128 |
parser.add_argument( |
| 129 |
"--first-release", |
| 130 |
action="store_true", |
| 131 |
help="Flag for first release to prevent version bumping.", |
| 132 |
) |
| 133 |
return parser.parse_args() |
| 134 |
|
| 135 |
|
| 136 |
def _main() -> int: |
| 137 |
"""Prepare release main routine.""" |
| 138 |
args = _parser() |
| 139 |
|
| 140 |
if args.first_release: |
| 141 |
release_version = "v1.0.0" |
| 142 |
|
| 143 |
current_version = subprocess.run( |
| 144 |
["git", "rev-list", "--max-parents=0", "HEAD"], |
| 145 |
check=True, |
| 146 |
capture_output=True, |
| 147 |
).stdout.decode()[0:7] |
| 148 |
else: |
| 149 |
git_tags = ( |
| 150 |
subprocess.run( |
| 151 |
["git", "tag", "--list"], |
| 152 |
check=True, |
| 153 |
capture_output=True, |
| 154 |
cwd=Path(__file__).parent, |
| 155 |
) |
| 156 |
.stdout.decode() |
| 157 |
.split("\n") |
| 158 |
) |
| 159 |
git_tags = [t for t in git_tags if t.startswith("v")] |
| 160 |
git_tags.sort() |
| 161 |
current_version = git_tags[-1] |
| 162 |
release_version = bump_version(current_version, args.increase_type) |
| 163 |
update_changelog( |
| 164 |
release_version, |
| 165 |
current_version, |
| 166 |
first_release=args.first_release, |
| 167 |
) |
| 168 |
commit_and_tag(release_version) |
| 169 |
return 0 |
| 170 |
|
| 171 |
|
| 172 |
if __name__ == "__main__": |
| 173 |
sys.exit(_main()) |