Skip to content

Commit

Permalink
Remove: Replace --space and --project with --repository argument
Browse files Browse the repository at this point in the history
Drop the `--space` and `--project` in favor of the single `--repository`
argument.
  • Loading branch information
bjoernricks committed Feb 6, 2024
1 parent f03cb9a commit 85ad900
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 407 deletions.
13 changes: 3 additions & 10 deletions pontos/changelog/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,10 @@ def parse_args(args: Optional[Sequence[str]] = None) -> Namespace:
).complete = shtab.FILE # type: ignore[attr-defined]

parser.add_argument(
"--project",
"--repository",
required=True,
help="The github project. Used for building the links to the "
"repository.",
)

parser.add_argument(
"--space",
default="greenbone",
help="User/Team name in github. Used for building the links to the "
"repository",
help="The github repository (owner/name). Used for building the links "
"to the repository.",
)

parser.add_argument(
Expand Down
20 changes: 8 additions & 12 deletions pontos/changelog/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ConventionalCommits:
from pontos.changelog import ConventionalCommits
collector = ConventionalCommits(space="my-org", project="my-project)
collector = ConventionalCommits()
commits = collector.get_commits(
from_ref="v1.2.3",
to_ref="v2.0.0",
Expand Down Expand Up @@ -195,7 +195,7 @@ class ChangelogBuilder:
from pontos.changelog import ChangelogBuilder
builder = ChangelogBuilder(space="my-org", project="my-project)
builder = ChangelogBuilder(repository="my-org/my-project)
changelog = builder.create_changelog(
last_version="1.2.3",
next_version="2.0.0",
Expand All @@ -205,24 +205,21 @@ class ChangelogBuilder:
def __init__(
self,
*,
space: str,
project: str,
repository: str,
git_tag_prefix: Optional[str] = "v",
config: Optional[Path] = None,
) -> None:
"""
Create a new ChangelogBuilder instance.
Args:
space: GitHub space to use (organization or user name).
project: GitHub project to create a changelog for
repository: GitHub repository (owner/name) to create the changelog
for. For example: "octocat/Hello-World"
git_tag_prefix: Git tag prefix to use when checking for git tags.
Default is "v".
config: TOML config for conventional commit parsing settings
"""
self._project = project
self._space = space

self._repository = repository
self._git_tag_prefix = git_tag_prefix
self._conventional_commits = ConventionalCommits(config)

Expand Down Expand Up @@ -312,15 +309,14 @@ def _build_changelog(
for log_entry in commit_dict[commit_type["group"]]:
commit_id, commit_message = log_entry
commit_link = (
f"{ADDRESS}{self._space}/{self._project}/"
f"commit/{commit_id}"
f"{ADDRESS}{self._repository}/" f"commit/{commit_id}"
)
msg = f"{commit_message} [{commit_id}]({commit_link})"
changelog.append(f"* {msg}")

# comparison line (footer)
pre = "\n[Unreleased]: "
compare_link = f"{ADDRESS}{self._space}/{self._project}/compare/"
compare_link = f"{ADDRESS}{self._repository}/compare/"
if next_version and last_version:
pre = f"\n[{next_version}]: "
diff = (
Expand Down
7 changes: 4 additions & 3 deletions pontos/changelog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def main(args: Optional[Sequence[str]] = None) -> NoReturn:
try:
changelog_builder = ChangelogBuilder(
config=parsed_args.config,
project=parsed_args.project,
space=parsed_args.space,
repository=parsed_args.repository,
)
if parsed_args.output:
changelog_builder.create_changelog_file(
Expand All @@ -46,9 +45,11 @@ def main(args: Optional[Sequence[str]] = None) -> NoReturn:
next_version=parsed_args.next_version,
)
term.out(changelog)
except KeyboardInterrupt:
sys.exit(1)
except PontosError as e:
term.error(str(e))
sys.exit(1)
sys.exit(2)

sys.exit(0)

Expand Down
124 changes: 63 additions & 61 deletions pontos/release/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
from argparse import (
ArgumentParser,
ArgumentTypeError,
BooleanOptionalAction,
Namespace,
)
Expand Down Expand Up @@ -39,34 +40,22 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)


def parse_args(
args: Optional[Sequence[str]] = None,
) -> Tuple[Optional[str], Optional[str], Namespace]:
def repository_type(value: str) -> str:
"""
Return user, token, parsed arguments
Validates the repository format of owner/name
"""
parser = ArgumentParser(
description="Release handling utility.",
prog="pontos-release",
)
shtab.add_argument_to(parser)

parser.add_argument(
"--quiet",
"-q",
action="store_true",
help="Don't print messages to the terminal",
)
splitted = value.split("/")
if len(splitted) != 2:
raise ArgumentTypeError(
f"Invalid repository format {value}. Format must be owner/name."
)
return value

subparsers = parser.add_subparsers(
title="subcommands",
description="Valid subcommands",
help="Additional help",
dest="command",
required=True,
)

create_parser = subparsers.add_parser(
def add_create_parser(
subparsers: argparse._SubParsersAction,
) -> None:
create_parser: ArgumentParser = subparsers.add_parser(
"create",
aliases=["release"],
help="Create a new release",
Expand Down Expand Up @@ -137,26 +126,12 @@ def parse_args(
default=os.environ.get("GPG_SIGNING_KEY"),
)

repo_group = create_parser.add_argument_group(
"Repository",
description="Where to publish the new release. Either a full repository"
" name or a space/project combination.",
)
repo_group.add_argument(
create_parser.add_argument(
"--repository",
help="GitHub repository name (owner/name). For example "
"octocat/Hello-World",
)
repo_group.add_argument(
"--space",
default="greenbone",
help="Owner (User/Team/Organization) name at GitHub",
help="GitHub repository name (owner/name) where to publish the new "
"release. For example octocat/Hello-World",
type=repository_type,
)
repo_group.add_argument(
"--project",
help="The GitHub project",
)

create_parser.add_argument(
"--local",
action="store_true",
Expand All @@ -183,7 +158,11 @@ def parse_args(
action="store_true",
)

sign_parser = subparsers.add_parser(

def add_sign_parser(
subparsers: argparse._SubParsersAction,
) -> None:
sign_parser: ArgumentParser = subparsers.add_parser(
"sign",
help="Create signatures for an existing release",
description="Create signatures for an existing release",
Expand Down Expand Up @@ -219,24 +198,11 @@ def parse_args(
nargs="?",
help="Prefix for git tag versions. Default: %(default)s",
)
repo_group = sign_parser.add_argument_group(
"Repository",
description="Where to publish the new release. Either a full repository"
" name or a space/project combination.",
)
repo_group.add_argument(
sign_parser.add_argument(
"--repository",
help="GitHub repository name (owner/name). For example "
"octocat/Hello-World",
)
repo_group.add_argument(
"--space",
default="greenbone",
help="Owner (User/Team/Organization) name at GitHub",
)
repo_group.add_argument(
"--project",
help="The GitHub project",
help="GitHub repository name (owner/name) where to download the "
"release files from. For example octocat/Hello-World",
type=repository_type,
)
sign_parser.add_argument(
"--passphrase",
Expand All @@ -249,7 +215,11 @@ def parse_args(
"--dry-run", action="store_true", help="Do not upload signed files."
)

show_parser = subparsers.add_parser(

def add_show_parser(
subparsers: argparse._SubParsersAction,
) -> None:
show_parser: ArgumentParser = subparsers.add_parser(
"show",
help="Show release information about the current release version and "
"determine the next release version",
Expand Down Expand Up @@ -301,6 +271,38 @@ def parse_args(
choices=enum_choice(OutputFormat),
)


def parse_args(
args: Optional[Sequence[str]] = None,
) -> Tuple[Optional[str], Optional[str], Namespace]:
"""
Return user, token, parsed arguments
"""
parser = ArgumentParser(
description="Release handling utility.",
prog="pontos-release",
)
shtab.add_argument_to(parser)

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

subparsers = parser.add_subparsers(
title="subcommands",
description="Valid subcommands",
help="Additional help",
dest="command",
required=True,
)

add_create_parser(subparsers)
add_sign_parser(subparsers)
add_show_parser(subparsers)

parsed_args = parser.parse_args(args)

scheme: type[VersioningScheme] = getattr(
Expand Down
47 changes: 12 additions & 35 deletions pontos/release/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from .helper import (
ReleaseType,
find_signing_key,
get_git_repository_name,
get_next_release_version,
repository_split,
)
Expand Down Expand Up @@ -89,8 +88,7 @@ def _create_changelog(
cc_config: Optional[Path],
) -> str:
changelog_builder = ChangelogBuilder(
space=self.space, # type: ignore[arg-type]
project=self.project,
repository=self.repository,
config=cc_config,
git_tag_prefix=self.git_tag_prefix,
)
Expand All @@ -114,12 +112,12 @@ async def _create_release(
github = GitHubAsyncRESTApi(token=token)

git_version = f"{self.git_tag_prefix}{release_version}"
repo = f"{self.space}/{self.project}"
_, project = repository_split(self.repository)

await github.releases.create(
repo,
self.repository,
git_version,
name=f"{self.project} {release_version}",
name=f"{project} {release_version}",
body=release_text,
prerelease=release_version.is_pre_release or github_pre_release,
)
Expand All @@ -128,9 +126,7 @@ async def async_run( # type: ignore[override]
self,
*,
token: str,
repository: Optional[str],
space: Optional[str],
project_name: Optional[str],
repository: str,
versioning_scheme: VersioningScheme,
release_type: ReleaseType,
release_version: Optional[Version],
Expand All @@ -149,12 +145,7 @@ async def async_run( # type: ignore[override]
Args:
token: A token for creating a release on GitHub
repository: GitHub repository (owner/name). Overrides space and
project.
space: GitHub username or organization. Required for generating
links in the changelog.
project: Name of the project to release. If not set it will be
gathered via the git remote url.
repository: GitHub repository (owner/name)
versioning_scheme: The versioning scheme to use for version parsing
and calculation
release_type: Type of the release to prepare. Defines the release
Expand Down Expand Up @@ -187,25 +178,13 @@ async def async_run( # type: ignore[override]
else find_signing_key(self.terminal)
)
self.git_tag_prefix = git_tag_prefix or ""
self.repository = repository

if repository:
if space:
self.print_warning(
f"Repository {repository} overrides space setting {space}"
)

try:
space, project_name = repository_split(repository)
except ValueError as e:
self.print_error(str(e))
return CreateReleaseReturnValue.INVALID_REPOSITORY

self.space = space
self.project = (
project_name
if project_name is not None
else get_git_repository_name()
)
try:
_, self.project_name = repository_split(repository)
except ValueError as e:
self.print_error(str(e))
return CreateReleaseReturnValue.INVALID_REPOSITORY

self.terminal.info(f"Using versioning scheme {versioning_scheme.name}")

Expand Down Expand Up @@ -409,8 +388,6 @@ def create_release(
).run(
token=token,
repository=args.repository,
space=args.space,
project_name=args.project,
versioning_scheme=args.versioning_scheme,
release_type=args.release_type,
release_version=args.release_version,
Expand Down
Loading

0 comments on commit 85ad900

Please sign in to comment.