2020-12-16 18:50:02 +08:00
|
|
|
"""
|
|
|
|
This script is part of the pytest release process which is triggered manually in the Actions
|
|
|
|
tab of the repository.
|
|
|
|
|
|
|
|
The user will need to enter the base branch to start the release from (for example
|
2021-03-19 05:13:12 +08:00
|
|
|
``6.1.x`` or ``main``) and if it should be a major release.
|
2020-12-16 18:50:02 +08:00
|
|
|
|
|
|
|
The appropriate version will be obtained based on the given branch automatically.
|
|
|
|
|
|
|
|
After that, it will create a release using the `release` tox environment, and push a new PR.
|
|
|
|
|
2021-05-16 16:37:21 +08:00
|
|
|
**Token**: currently the token from the GitHub Actions is used, pushed with
|
|
|
|
`pytest bot <pytestbot@gmail.com>` commit author.
|
2020-12-16 18:50:02 +08:00
|
|
|
"""
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
from pathlib import Path
|
|
|
|
from subprocess import check_call
|
|
|
|
from subprocess import check_output
|
|
|
|
from subprocess import run
|
|
|
|
|
|
|
|
from colorama import Fore
|
|
|
|
from colorama import init
|
|
|
|
from github3.repos import Repository
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidFeatureRelease(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
SLUG = "pytest-dev/pytest"
|
|
|
|
|
|
|
|
PR_BODY = """\
|
|
|
|
Created automatically from manual trigger.
|
|
|
|
|
|
|
|
Once all builds pass and it has been **approved** by one or more maintainers, the build
|
|
|
|
can be released by pushing a tag `{version}` to this repository.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def login(token: str) -> Repository:
|
|
|
|
import github3
|
|
|
|
|
|
|
|
github = github3.login(token=token)
|
|
|
|
owner, repo = SLUG.split("/")
|
|
|
|
return github.repository(owner, repo)
|
|
|
|
|
|
|
|
|
2021-06-25 02:05:56 +08:00
|
|
|
def prepare_release_pr(
|
|
|
|
base_branch: str, is_major: bool, token: str, prerelease: str
|
|
|
|
) -> None:
|
2020-12-16 18:50:02 +08:00
|
|
|
print()
|
|
|
|
print(f"Processing release for branch {Fore.CYAN}{base_branch}")
|
|
|
|
|
|
|
|
check_call(["git", "checkout", f"origin/{base_branch}"])
|
|
|
|
|
2021-10-22 21:00:00 +08:00
|
|
|
changelog = Path("changelog")
|
|
|
|
|
|
|
|
features = list(changelog.glob("*.feature.rst"))
|
|
|
|
breaking = list(changelog.glob("*.breaking.rst"))
|
|
|
|
is_feature_release = bool(features or breaking)
|
|
|
|
|
2020-12-16 18:50:02 +08:00
|
|
|
try:
|
2021-10-22 21:00:00 +08:00
|
|
|
version = find_next_version(
|
|
|
|
base_branch, is_major, is_feature_release, prerelease
|
|
|
|
)
|
2020-12-16 18:50:02 +08:00
|
|
|
except InvalidFeatureRelease as e:
|
|
|
|
print(f"{Fore.RED}{e}")
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
print(f"Version: {Fore.CYAN}{version}")
|
|
|
|
|
|
|
|
release_branch = f"release-{version}"
|
|
|
|
|
|
|
|
run(
|
|
|
|
["git", "config", "user.name", "pytest bot"],
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
run(
|
|
|
|
["git", "config", "user.email", "pytestbot@gmail.com"],
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
run(
|
|
|
|
["git", "checkout", "-b", release_branch, f"origin/{base_branch}"],
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
print(f"Branch {Fore.CYAN}{release_branch}{Fore.RESET} created.")
|
|
|
|
|
2022-02-03 23:33:07 +08:00
|
|
|
if is_major:
|
|
|
|
template_name = "release.major.rst"
|
|
|
|
elif prerelease:
|
2021-10-22 21:00:00 +08:00
|
|
|
template_name = "release.pre.rst"
|
|
|
|
elif is_feature_release:
|
|
|
|
template_name = "release.minor.rst"
|
|
|
|
else:
|
|
|
|
template_name = "release.patch.rst"
|
|
|
|
|
2020-12-16 18:50:02 +08:00
|
|
|
# important to use tox here because we have changed branches, so dependencies
|
|
|
|
# might have changed as well
|
2021-10-22 21:00:00 +08:00
|
|
|
cmdline = [
|
|
|
|
"tox",
|
|
|
|
"-e",
|
|
|
|
"release",
|
|
|
|
"--",
|
|
|
|
version,
|
|
|
|
template_name,
|
2022-02-03 18:48:55 +08:00
|
|
|
release_branch, # doc_version
|
2021-10-22 21:00:00 +08:00
|
|
|
"--skip-check-links",
|
|
|
|
]
|
2020-12-16 18:50:02 +08:00
|
|
|
print("Running", " ".join(cmdline))
|
|
|
|
run(
|
2020-12-30 17:56:09 +08:00
|
|
|
cmdline,
|
|
|
|
check=True,
|
2020-12-16 18:50:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
oauth_url = f"https://{token}:x-oauth-basic@github.com/{SLUG}.git"
|
|
|
|
run(
|
|
|
|
["git", "push", oauth_url, f"HEAD:{release_branch}", "--force"],
|
|
|
|
check=True,
|
|
|
|
)
|
|
|
|
print(f"Branch {Fore.CYAN}{release_branch}{Fore.RESET} pushed.")
|
|
|
|
|
|
|
|
body = PR_BODY.format(version=version)
|
|
|
|
repo = login(token)
|
|
|
|
pr = repo.create_pull(
|
2020-12-30 17:56:09 +08:00
|
|
|
f"Prepare release {version}",
|
|
|
|
base=base_branch,
|
|
|
|
head=release_branch,
|
|
|
|
body=body,
|
2020-12-16 18:50:02 +08:00
|
|
|
)
|
|
|
|
print(f"Pull request {Fore.CYAN}{pr.url}{Fore.RESET} created.")
|
|
|
|
|
|
|
|
|
2021-10-22 21:00:00 +08:00
|
|
|
def find_next_version(
|
|
|
|
base_branch: str, is_major: bool, is_feature_release: bool, prerelease: str
|
|
|
|
) -> str:
|
2020-12-16 18:50:02 +08:00
|
|
|
output = check_output(["git", "tag"], encoding="UTF-8")
|
|
|
|
valid_versions = []
|
|
|
|
for v in output.splitlines():
|
|
|
|
m = re.match(r"\d.\d.\d+$", v.strip())
|
|
|
|
if m:
|
|
|
|
valid_versions.append(tuple(int(x) for x in v.split(".")))
|
|
|
|
|
|
|
|
valid_versions.sort()
|
|
|
|
last_version = valid_versions[-1]
|
|
|
|
|
|
|
|
if is_major:
|
2021-06-25 02:05:56 +08:00
|
|
|
return f"{last_version[0]+1}.0.0{prerelease}"
|
2020-12-16 18:50:02 +08:00
|
|
|
elif is_feature_release:
|
2021-06-25 02:05:56 +08:00
|
|
|
return f"{last_version[0]}.{last_version[1] + 1}.0{prerelease}"
|
2020-12-16 18:50:02 +08:00
|
|
|
else:
|
2021-06-25 02:05:56 +08:00
|
|
|
return f"{last_version[0]}.{last_version[1]}.{last_version[2] + 1}{prerelease}"
|
2020-12-16 18:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
init(autoreset=True)
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("base_branch")
|
|
|
|
parser.add_argument("token")
|
|
|
|
parser.add_argument("--major", action="store_true", default=False)
|
2021-06-25 02:05:56 +08:00
|
|
|
parser.add_argument("--prerelease", default="")
|
2020-12-16 18:50:02 +08:00
|
|
|
options = parser.parse_args()
|
|
|
|
prepare_release_pr(
|
2021-06-25 02:05:56 +08:00
|
|
|
base_branch=options.base_branch,
|
|
|
|
is_major=options.major,
|
|
|
|
token=options.token,
|
|
|
|
prerelease=options.prerelease,
|
2020-12-16 18:50:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|