Skip to content
Closed
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
9 changes: 6 additions & 3 deletions .github/workflows/prepare-release-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false
# persist-credentials is needed in order for us to push the release branch.
persist-credentials: true

- name: Set up Python
uses: actions/setup-python@v5
Expand All @@ -47,13 +48,15 @@ jobs:
env:
BRANCH: ${{ github.event.inputs.branch }}
PRERELEASE: ${{ github.event.inputs.prerelease }}
GH_TOKEN: ${{ github.token }}
run: |
tox -e prepare-release-pr -- "$BRANCH" ${{ github.token }} --prerelease="$PRERELEASE"
tox -e prepare-release-pr -- "$BRANCH" --prerelease="$PRERELEASE"

- name: Prepare release PR (major release)
if: github.event.inputs.major == 'yes'
env:
BRANCH: ${{ github.event.inputs.branch }}
PRERELEASE: ${{ github.event.inputs.prerelease }}
GH_TOKEN: ${{ github.token }}
run: |
tox -e prepare-release-pr -- "$BRANCH" ${{ github.token }} --major --prerelease="$PRERELEASE"
tox -e prepare-release-pr -- "$BRANCH" --major --prerelease="$PRERELEASE"
41 changes: 15 additions & 26 deletions scripts/prepare-release-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

After that, it will create a release using the `release` tox environment, and push a new PR.

**Token**: currently the token from the GitHub Actions is used, pushed with
`pytest bot <pytestbot@gmail.com>` commit author.
Note: the script uses the `gh` command-line tool, so `GH_TOKEN` must be set in the environment.
"""

from __future__ import annotations
Expand All @@ -25,7 +24,6 @@

from colorama import Fore
from colorama import init
from github3.repos import Repository


class InvalidFeatureRelease(Exception):
Expand Down Expand Up @@ -54,17 +52,7 @@ class InvalidFeatureRelease(Exception):
"""


def login(token: str) -> Repository:
import github3

github = github3.login(token=token)
owner, repo = SLUG.split("/")
return github.repository(owner, repo)


def prepare_release_pr(
base_branch: str, is_major: bool, token: str, prerelease: str
) -> None:
def prepare_release_pr(base_branch: str, is_major: bool, prerelease: str) -> None:
print()
print(f"Processing release for branch {Fore.CYAN}{base_branch}")

Expand Down Expand Up @@ -131,22 +119,25 @@ def prepare_release_pr(
check=True,
)

oauth_url = f"https://{token}:x-oauth-basic@github.com/{SLUG}.git"
run(
["git", "push", oauth_url, f"HEAD:{release_branch}", "--force"],
["git", "push", 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(
f"Prepare release {version}",
base=base_branch,
head=release_branch,
body=body,
run(
[
"gh",
"pr",
"new",
f"--base={base_branch}",
f"--head={release_branch}",
f"--title=Release {version}",
f"--body={body}",
],
check=True,
)
print(f"Pull request {Fore.CYAN}{pr.url}{Fore.RESET} created.")


def find_next_version(
Expand All @@ -163,7 +154,7 @@ def find_next_version(
last_version = valid_versions[-1]

if is_major:
return f"{last_version[0]+1}.0.0{prerelease}"
return f"{last_version[0] + 1}.0.0{prerelease}"
elif is_feature_release:
return f"{last_version[0]}.{last_version[1] + 1}.0{prerelease}"
else:
Expand All @@ -174,14 +165,12 @@ 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)
parser.add_argument("--prerelease", default="")
options = parser.parse_args()
prepare_release_pr(
base_branch=options.base_branch,
is_major=options.major,
token=options.token,
prerelease=options.prerelease,
)

Expand Down
4 changes: 0 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,7 @@ usedevelop = True
passenv = *
deps =
colorama
github3.py
pre-commit>=2.9.3
wheel
# https://github.com/twisted/towncrier/issues/340
towncrier<21.3.0
commands = python scripts/release.py {posargs}

[testenv:prepare-release-pr]
Expand Down
Loading