<commit_before>"""
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
``6.1.x`` or ``main``) and if it should be a major release.

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.

**Token**: currently the token from the GitHub Actions is used, pushed with
`pytest bot <<EMAIL>>` commit author.
"""
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)


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

    check_call(["git", "checkout", f"origin/{base_branch}"])

    try:
        version = find_next_version(base_branch, is_major, prerelease)
    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"],
        text=True,
        check=True,
        capture_output=True,
    )
    run(
        ["git", "config", "user.email", "<EMAIL>"],
        text=True,
        check=True,
        capture_output=True,
    )

    run(
        ["git", "checkout", "-b", release_branch, f"origin/{base_branch}"],
        text=True,
        check=True,
        capture_output=True,
    )

    print(f"Branch {Fore.CYAN}{release_branch}{Fore.RESET} created.")

    # important to use tox here because we have changed branches, so dependencies
    # might have changed as well
    cmdline = ["tox", "-e", "release", "--", version, "--skip-check-links"]
    print("Running", " ".join(cmdline))
    run(
        cmdline,
        text=True,
        check=True,
        capture_output=True,
    )

    oauth_url = f"https://{token}:x-oauth-basic@github.com/{SLUG}.git"
    run(
        ["git", "push", oauth_url, f"HEAD:{release_branch}", "--force"],
        text=True,
        check=True,
        capture_output=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}",<commit_msg>scripts: Don't capture output in prepare-release-pr.py (#8805)<commit_after>"""
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
``6.1.x`` or ``main``) and if it should be a major release.

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.

**Token**: currently the token from the GitHub Actions is used, pushed with
`pytest bot <<EMAIL>>` commit author.
"""
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)


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

    check_call(["git", "checkout", f"origin/{base_branch}"])

    try:
        version = find_next_version(base_branch, is_major, prerelease)
    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", "<EMAIL>"],
        check=True,
    )

    run(
        ["git", "checkout", "-b", release_branch, f"origin/{base_branch}"],
        check=True,
    )

    print(f"Branch {Fore.CYAN}{release_branch}{Fore.RESET} created.")

    # important to use tox here because we have changed branches, so dependencies
    # might have changed as well
    cmdline = ["tox", "-e", "release", "--", version, "--skip-check-links"]
    print("Running", " ".join(cmdline))
    run(
        cmdline,
        check=True,
    )

    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(
        f"Prepare release {version}",
{'fraction_non_alphanumeric': 0.12346921657440027, 'fraction_numerical': 0.00268411340379131, 'mean_word_length': 3.358187134502924, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2463658', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1786, 'n_words': 587}