Ejemplo n.º 1
0
def test_changelog_ignore_footer_urls(ignore_footer_urls, expected):
    changelog = ChangeLog.from_git_commits([FIX_COMMIT_WITH_URL])
    content = changelog.format(
        ChangeLogTypeEnum.changelog_file,
        FormatTypeEnum.markdown,
        is_pre_release=False,
        ignore_footer_urls=ignore_footer_urls,
    )
    assert (
        content
        == f"""## Fixes:

- {expected}Update logic behind math operations"""
    )
Ejemplo n.º 2
0
def test_changelog_strip_spaces(fix_commit):
    changelog = ChangeLog.from_git_commits(
        [
            FEATURE_COMMIT,
            fix_commit,
            CI_BREAKING_COMMIT,
            DOCS_SCOPE_COMMIT,
            REFACTOR_COMMIT,
        ]
    )
    content = changelog.format(
        ChangeLogTypeEnum.git_commit,
        FormatTypeEnum.markdown,
        is_pre_release=False,
    )
    assert content == CHANGELOG_GIT_MD
Ejemplo n.º 3
0
def test_changelog_format_git(format_type, is_pre_release, expected):
    changelog = ChangeLog.from_git_commits(
        [
            FEATURE_COMMIT,
            FIX_COMMIT,
            CI_BREAKING_COMMIT,
            DOCS_SCOPE_COMMIT,
            REFACTOR_COMMIT,
        ]
    )
    content = changelog.format(
        ChangeLogTypeEnum.git_commit,
        format_type,
        is_pre_release=is_pre_release,
    )
    assert content == expected
Ejemplo n.º 4
0
def test_changelog_with_fix_commit():
    changelog = ChangeLog.from_git_commits([FIX_COMMIT])
    assert changelog.has_breaking_change is False
    assert changelog.has_minor_change is False
    assert changelog.has_micro_change is True
Ejemplo n.º 5
0
def test_changelog_invalid_commit_non_strict_mode():
    changelog = ChangeLog.from_git_commits([INVALID_COMMIT], strict=False)
    assert changelog.has_breaking_change is False
    assert changelog.has_minor_change is False
    assert changelog.has_micro_change is True
Ejemplo n.º 6
0
def test_changelog_invalid_commit():
    with pytest.raises(ValueError):
        ChangeLog.from_git_commits([INVALID_COMMIT])
Ejemplo n.º 7
0
def test_changelog_empty(changelog_type, format_type, expected):
    changelog = ChangeLog.from_git_commits([])
    content = changelog.format(changelog_type, format_type)
    assert content == expected
Ejemplo n.º 8
0
def main(argv: Argv = None) -> int:
    # Parse arguments
    args = parse_args(argv or sys.argv[1:])

    # Initialize project config
    project_config = ProjectConfig.from_path(args.path)

    # Read latest git tag and parse current version
    git = Git(path=project_config.path)

    current_tag = git.retrieve_last_tag_or_none()
    echo_value(
        "Current tag: ",
        current_tag or EMPTY,
        is_ci=args.is_ci,
        ci_name="current_tag",
    )

    current_version: Optional[Version] = None
    if current_tag is not None:
        current_version = Version.from_tag(current_tag, config=project_config)

    echo_value(
        "Current version: ",
        (current_version.format(
            config=project_config) if current_version else EMPTY),
        is_ci=args.is_ci,
        ci_name="current_version",
    )

    # Read commits from last tag
    if current_tag is not None and current_version is not None:
        try:
            git_commits = git.list_commits(current_tag)
            if not git_commits and current_version.pre_release is None:
                raise ValueError("No commits found after latest tag")
        except ValueError:
            print(
                f"ERROR: No commits found after: {current_tag!r}. Exit...",
                file=sys.stderr,
            )
            return 1

        # Create changelog using commits from last tag
        changelog = ChangeLog.from_git_commits(
            git_commits, strict=project_config.strict_mode)

        # Supply update config and guess next version
        update_config = create_update_config(changelog, args.is_pre_release)

        # Guess next version
        next_version = current_version.update(update_config)
    # Create initial changelog
    else:
        next_version = Version.guess_initial_version(
            config=project_config, is_pre_release=args.is_pre_release)
        changelog = ChangeLog.from_git_commits(
            (INITIAL_PRE_RELEASE_COMMIT if next_version.pre_release is not None
             else INITIAL_RELEASE_COMMIT, ), )

    git_changelog = changelog.format(
        ChangeLogTypeEnum.git_commit,
        project_config.changelog_format_type_git,
        ignore_footer_urls=project_config.changelog_ignore_footer_urls,
    )
    echo_value(
        "\nChangeLog\n\n",
        git_changelog,
        is_ci=args.is_ci,
        ci_name="changelog",
    )

    next_version_str = next_version.format(config=project_config)
    echo_value(
        "\nNext version: ",
        next_version_str,
        is_ci=args.is_ci,
        ci_name="next_version",
    )

    # Applying changes to version files
    if not args.is_ci and not args.is_dry_run:
        update_message = (
            "Are you sure to update version files and changelog? [y/N] ")
        if input(update_message).lower() != "y":
            print("OK! OK! Exit...")
            return 0

    update_version_files(
        project_config,
        current_version,
        next_version,
        is_dry_run=args.is_dry_run,
    )

    # Run post-bump hook
    run_post_bump_hook(project_config, is_dry_run=args.is_dry_run)

    # Update changelog
    update_changelog_file(project_config,
                          next_version,
                          changelog,
                          is_dry_run=args.is_dry_run)

    # Supply necessary CI output
    if args.is_ci:
        github_actions_output(
            "next_tag",
            project_config.tag_format.format(version=next_version_str),
        )
        github_actions_output(
            "next_tag_message",
            "\n\n".join((
                project_config.tag_subject_format.format(
                    version=next_version_str),
                git_changelog,
            )),
        )
        github_actions_output(
            "pr_branch",
            project_config.pr_branch_format.format(version=next_version_str),
        )
        github_actions_output(
            "pr_title",
            project_config.pr_title_format.format(version=next_version_str),
        )

    print("All OK!")
    return 0
Ejemplo n.º 9
0
def update_changelog_file(
    config: ProjectConfig,
    next_version: Version,
    changelog: ChangeLog,
    *,
    is_dry_run: bool = False,
) -> None:
    """Update changelog file with new version.

    In most cases it just prepend new changelog on top of the file, but if next
    version is pre-release, do little trickery instead to include
    In Development header as well.
    """
    changelog_path = find_changelog_path(config)
    next_version_str = next_version.format(config=config)

    echo_message(
        f"Adding {next_version_str} release notes to {changelog_path.name} "
        "file",
        is_dry_run=is_dry_run,
    )
    if is_dry_run:
        return

    dev_header = in_development_header(
        next_version.version.format(), config.changelog_format_type_file
    )
    changelog_content = changelog.format(
        ChangeLogTypeEnum.changelog_file,
        config.changelog_format_type_file,
        is_pre_release=next_version.pre_release is not None,
    )

    if next_version.pre_release is None:
        next_version_changelog = "\n\n".join(
            (
                version_header(
                    next_version_str,
                    config.changelog_format_type_file,
                    is_pre_release=False,
                    include_date=config.changelog_file_include_date,
                ),
                changelog_content,
            )
        )
    else:
        next_version_changelog = "\n\n".join(
            (
                dev_header,
                version_header(
                    next_version_str,
                    config.changelog_format_type_file,
                    is_pre_release=True,
                    include_date=config.changelog_file_include_date,
                ),
                changelog_content,
            )
        )

    next_version_changelog = f"{next_version_changelog}\n\n"

    if not changelog_path.exists():
        changelog_path.write_text(f"{next_version_changelog.strip()}\n")
    else:
        if not update_file(
            changelog_path, f"{dev_header}\n\n", next_version_changelog
        ):
            changelog_path.write_text(
                "".join((next_version_changelog, changelog_path.read_text()))
            )