Skip to content

Commit

Permalink
Change: Refactor the pontos-changelog CLI
Browse files Browse the repository at this point in the history
Only print the generated changelog to stdout and make writing into a
file optional. Behave similar as the prepare command where the current
release version is gathered from the last git tag if it isn't passed as
argument. This allows to generate the same changelog output as prepare
would create.
  • Loading branch information
bjoernricks committed Feb 24, 2023
1 parent 7325510 commit b59cdc5
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 117 deletions.
3 changes: 2 additions & 1 deletion pontos/changelog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from .conventional_commits import ChangelogBuilder, main
from .conventional_commits import ChangelogBuilder
from .errors import ChangelogBuilderError, ChangelogError
from .main import main

__all__ = [
"ChangelogError",
Expand Down
103 changes: 1 addition & 102 deletions pontos/changelog/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@


import re
import subprocess
import sys
from argparse import ArgumentParser
from datetime import date
from pathlib import Path
from typing import Dict, Iterable, List, Optional, Union
from typing import Dict, List, Optional, Union

import tomlkit

from pontos.changelog.errors import ChangelogBuilderError
from pontos.git import Git
from pontos.terminal.null import NullTerminal
from pontos.terminal.rich import RichTerminal

ADDRESS = "https://github.com/"

Expand Down Expand Up @@ -269,99 +264,3 @@ def _write_changelog_file(
changelog_dir.mkdir(parents=True, exist_ok=True)

changelog_file.write_text(changelog, encoding="utf-8")


def parse_args(args: Iterable[str] = None) -> ArgumentParser:
parser = ArgumentParser(
description="Conventional commits utility.",
prog="pontos-changelog",
)

parser.add_argument(
"--config",
"-C",
default="changelog.toml",
type=Path,
help="Conventional commits config file (toml), including conventions.",
)

parser.add_argument(
"--project",
required=True,
help="The github project",
)

parser.add_argument(
"--space",
default="greenbone",
help="User/Team name in github",
)

parser.add_argument(
"--current-version",
default="greenbone",
help="Current version before these changes",
)

parser.add_argument(
"--next-version",
help="The planned release version",
)

parser.add_argument(
"--output",
"-o",
default="unreleased.md",
help="The path to the output file (.md)",
)

parser.add_argument(
"--quiet",
"-q",
action="store_true",
help="Don't print messages to the terminal",
)

parser.add_argument(
"--log-file",
dest="log_file",
type=str,
help="Activate logging using the given file path",
)

return parser.parse_args(args=args)


def main(
args=None,
) -> None:
parsed_args = parse_args(args)

if parsed_args.quiet:
term = NullTerminal()
else:
term = RichTerminal()

term.bold_info("pontos-changelog")

with term.indent():
try:
changelog_builder = ChangelogBuilder(
config=parsed_args.config,
project=args.project,
space=args.space,
)
changelog_builder.create_changelog_file(
args.output,
last_version=args.current_version,
next_version=args.next_version,
)
except ChangelogBuilderError as e:
term.error(str(e))
sys.exit(1)
except subprocess.CalledProcessError as e:
term.error(f'Could not run command "{e.cmd}".')
term.out(f"Error was: {e.stderr}")
sys.exit(1)

sys.exit(0)
106 changes: 104 additions & 2 deletions pontos/changelog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,111 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# pylint: disable=invalid-name
import sys
from argparse import ArgumentParser
from pathlib import Path
from typing import Iterable, NoReturn

from pontos.changelog.conventional_commits import ChangelogBuilder
from pontos.errors import PontosError
from pontos.release.helper import get_last_release_version
from pontos.terminal.null import NullTerminal
from pontos.terminal.rich import RichTerminal


def parse_args(args: Iterable[str] = None) -> ArgumentParser:
parser = ArgumentParser(
description="Conventional commits utility.",
prog="pontos-changelog",
)

parser.add_argument(
"--config",
"-C",
default="changelog.toml",
type=Path,
help="Conventional commits config file (toml), including conventions.",
)

parser.add_argument(
"--project",
required=True,
help="The github project",
)

parser.add_argument(
"--space",
default="greenbone",
help="User/Team name in github",
)

parser.add_argument(
"--current-version",
help="Current version before these changes",
)

parser.add_argument(
"--next-version",
help="The planned release version",
)

parser.add_argument(
"--git-tag-prefix",
default="v",
help="Prefix for git tag versions. Default: %(default)s",
)

parser.add_argument(
"--output",
"-o",
type=Path,
help="Write changelog to this file.",
)

parser.add_argument(
"--quiet",
"-q",
action="store_true",
help="Don't print messages to the terminal",
)

return parser.parse_args(args=args)


def main(args: Iterable[str] = None) -> NoReturn:
parsed_args = parse_args(args)

term = NullTerminal if parsed_args.quiet else RichTerminal()

if parsed_args.current_version:
last_version = parsed_args.current_version
else:
last_version = get_last_release_version(parsed_args.git_tag_prefix)

try:
changelog_builder = ChangelogBuilder(
config=parsed_args.config,
project=parsed_args.project,
space=parsed_args.space,
)
if parsed_args.output:
changelog_builder.create_changelog_file(
parsed_args.output,
last_version=last_version,
next_version=parsed_args.next_version,
)
else:
changelog = changelog_builder.create_changelog(
last_version=last_version,
next_version=parsed_args.next_version,
)
print(changelog)
except PontosError as e:
term.error(str(e))
sys.exit(1)

sys.exit(0)

from .conventional_commits import main

if __name__ == "__main__":
main()
12 changes: 0 additions & 12 deletions tests/changelog/test_conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from pontos.changelog.conventional_commits import (
ChangelogBuilder,
ChangelogBuilderError,
parse_args,
)
from pontos.testing import temp_directory

Expand Down Expand Up @@ -542,14 +541,3 @@ def test_write_changelog_to_file(self, git_mock: MagicMock):
git_mock.return_value.log.assert_called_once_with(
"v0.0.1..HEAD", oneline=True
)


class ParseArgsTestCase(unittest.TestCase):
def test_parse_args(self):
parsed_args = parse_args(
["-q", "--log-file", "blah", "--project", "urghs"]
)

self.assertTrue(parsed_args.quiet)
self.assertEqual(parsed_args.log_file, "blah")
self.assertEqual(parsed_args.project, "urghs")
52 changes: 52 additions & 0 deletions tests/changelog/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (C) 2021-2023 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
from pathlib import Path

from pontos.changelog.main import parse_args


class ParseArgsTestCase(unittest.TestCase):
def test_parse_args(self):
parsed_args = parse_args(
[
"-q",
"--project",
"urghs",
"--space",
"bla",
"--config",
"foo.toml",
"--current-version",
"1.2.3",
"--next-version",
"2.3.4",
"--git-tag-prefix",
"a",
"--output",
"changelog.md",
]
)

self.assertTrue(parsed_args.quiet)
self.assertEqual(parsed_args.project, "urghs")
self.assertEqual(parsed_args.space, "bla")
self.assertEqual(parsed_args.config, Path("foo.toml"))
self.assertEqual(parsed_args.current_version, "1.2.3")
self.assertEqual(parsed_args.next_version, "2.3.4")
self.assertEqual(parsed_args.git_tag_prefix, "a")
self.assertEqual(parsed_args.output, Path("changelog.md"))

0 comments on commit b59cdc5

Please sign in to comment.