Ejemplo n.º 1
0
def get_changelog_entry(branch, repo, version, *, auth=None, resolve_backports=False):
    """Get a changelog for the changes since the last tag on the given branch.

    Parameters
    ----------
    branch : str
        The target branch
    respo : str
        The GitHub organization/repo
    version : str
        The new version
    auth : str, optional
        The GitHub authorization token
    resolve_backports: bool, optional
        Whether to resolve backports to the original PR

    Returns
    -------
    str
        A formatted changelog entry with markers
    """
    since = run(f"git tag --merged {branch}")
    if not since:  # pragma: no cover
        raise ValueError(f"No tags found on branch {branch}")

    since = since.splitlines()[-1]
    print(f"Getting changes to {repo} since {since}...")

    md = generate_activity_md(repo, since=since, kind="pr", auth=auth)

    if not md:
        print("No PRs found")
        return f"## {version}\nNo merged PRs"

    md = md.splitlines()

    start = -1
    full_changelog = ""
    for (ind, line) in enumerate(md):
        if "[full changelog]" in line:
            full_changelog = line.replace("full changelog", "Full Changelog")
        elif line.strip().startswith("## Merged PRs"):
            start = ind + 1

    prs = md[start:]

    if resolve_backports:
        for (ind, line) in enumerate(prs):
            if re.search(r"\[@meeseeksmachine\]", line) is not None:
                match = re.search(r"Backport PR #(\d+)", line)
                if match:
                    prs[ind] = format_pr_entry(match.groups()[0])

    prs = "\n".join(prs).strip()

    # Move the contributor list to a heading level 3
    prs = prs.replace("## Contributors", "### Contributors")

    # Replace "*" unordered list marker with "-" since this is what
    # Prettier uses
    prs = re.sub(r"^\* ", "- ", prs)
    prs = re.sub(r"\n\* ", "\n- ", prs)

    output = f"""
## {version}

{full_changelog}

{prs}
""".strip()

    return output
def get_version_entry(branch,
                      repo,
                      version,
                      *,
                      auth=None,
                      resolve_backports=False):
    """Get a changelog for the changes since the last tag on the given branch.

    Parameters
    ----------
    branch : str
        The target branch
    respo : str
        The GitHub owner/repo
    version : str
        The new version
    auth : str, optional
        The GitHub authorization token
    resolve_backports: bool, optional
        Whether to resolve backports to the original PR

    Returns
    -------
    str
        A formatted changelog entry with markers
    """
    tags = util.run(
        f"git --no-pager tag --sort=-creatordate --merged {branch}")
    if not tags:  # pragma: no cover
        raise ValueError(f"No tags found on branch {branch}")

    since = tags.splitlines()[0]
    branch = branch.split("/")[-1]
    util.log(f"Getting changes to {repo} since {since} on branch {branch}...")

    md = generate_activity_md(repo,
                              since=since,
                              kind="pr",
                              heading_level=2,
                              auth=auth,
                              branch=branch)

    if not md:
        util.log("No PRs found")
        return f"## {version}\n\nNo merged PRs"

    entry = md.replace("[full changelog]", "[Full Changelog]")

    entry = entry.splitlines()[2:]

    for (ind, line) in enumerate(entry):
        if re.search(r"\[@meeseeksmachine\]", line) is not None:
            match = re.search(r"Backport PR #(\d+)", line)
            if match:
                entry[ind] = format_pr_entry(repo, match.groups()[0])

    # Remove github actions PRs
    gh_actions = "[@github-actions](https://github.com/github-actions)"
    entry = [e for e in entry if gh_actions not in e]

    # Remove automated changelog PRs
    entry = [e for e in entry if PR_PREFIX not in e]

    entry = "\n".join(entry).strip()

    # Replace "*" unordered list marker with "-" since this is what
    # Prettier uses
    # TODO: remove after github_activity 0.1.4+ is available
    entry = re.sub(r"^\* ", "- ", entry)
    entry = re.sub(r"\n\* ", "\n- ", entry)

    output = f"""
## {version}

{entry}
""".strip()

    return output
def get_version_entry(branch, repo):
    """Get a changelog for the changes since the last tag on the given branch.

    Parameters
    ----------
    branch : str
        The target branch
    repo : str
        The GitHub owner/repo

    Returns
    -------
    str
        A formatted changelog entry with markers
    """
    auth = os.environ['GITHUB_ACCESS_TOKEN']

    run('git config --global user.email "*****@*****.**"')
    run('git config --global user.name "foo"')

    run(f"git clone https://github.com/{repo} test")
    prev_dir = os.getcwd()
    os.chdir("test")
    default_branch = run("git branch --show-current")

    branch = branch or default_branch

    run(f'git remote set-url origin https://github.com/{repo}')
    run(f'git fetch origin {branch} --tags')

    since = run(
        f"git --no-pager tag --sort=-creatordate --merged origin/{branch}")
    if not since:  # pragma: no cover
        raise ValueError(f"No tags found on branch {branch}")

    os.chdir(prev_dir)
    shutil.rmtree(Path(prev_dir) / "test")

    since = os.environ.get('INPUT_SINCE') or since.splitlines()[0]
    until = os.environ.get('INPUT_UNTIL') or None

    branch = branch.split("/")[-1]
    print(f"Getting changes to {repo} since {since} on branch {branch}...")

    md = generate_activity_md(repo,
                              since=since,
                              until=until,
                              kind="pr",
                              heading_level=2,
                              branch=branch)

    if not md:
        print("No PRs found")
        return f"## New Version\n\nNo merged PRs"

    entry = md.replace("full changelog", "Full Changelog")

    entry = entry.splitlines()

    for (ind, line) in enumerate(entry):
        if re.search(r"\[@meeseeksmachine\]", line) is not None:
            match = re.search(r"Backport PR #(\d+)", line)
            if match:
                entry[ind] = format_pr_entry(target, match.groups()[0])

    entry = "\n".join(entry).strip()

    output = f"""
{entry}
""".strip()

    return output