Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release repository argument #975

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions pontos/release/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
find_signing_key,
get_git_repository_name,
get_next_release_version,
repository_split,
)


Expand Down Expand Up @@ -62,6 +63,7 @@ class CreateReleaseReturnValue(IntEnum):
CREATE_RELEASE_ERROR = auto()
UPDATE_VERSION_ERROR = auto()
UPDATE_VERSION_AFTER_RELEASE_ERROR = auto()
INVALID_REPOSITORY = auto()


class CreateReleaseCommand(AsyncCommand):
Expand All @@ -87,7 +89,7 @@ def _create_changelog(
cc_config: Optional[Path],
) -> str:
changelog_builder = ChangelogBuilder(
space=self.space,
space=self.space, # type: ignore[arg-type]
project=self.project,
config=cc_config,
git_tag_prefix=self.git_tag_prefix,
Expand Down Expand Up @@ -126,7 +128,8 @@ async def async_run( # type: ignore[override]
self,
*,
token: str,
space: str,
repository: Optional[str],
space: Optional[str],
project_name: Optional[str],
versioning_scheme: VersioningScheme,
release_type: ReleaseType,
Expand All @@ -146,6 +149,8 @@ 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
Expand Down Expand Up @@ -182,12 +187,25 @@ async def async_run( # type: ignore[override]
else find_signing_key(self.terminal)
)
self.git_tag_prefix = git_tag_prefix or ""

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()
)
self.space = space

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

Expand Down Expand Up @@ -390,6 +408,7 @@ def create_release(
terminal=terminal, error_terminal=error_terminal
).run(
token=token,
repository=args.repository,
space=args.space,
project_name=args.project,
versioning_scheme=args.versioning_scheme,
Expand Down
12 changes: 12 additions & 0 deletions pontos/release/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ def get_next_release_version(
return calculator.next_release_candidate_version(last_release_version)

raise VersionError(f"Unsupported release type {release_type.value}.")


def repository_split(repository: str) -> tuple[str, str]:
"""
Split a GitHub repository (owner/name) into a space, project tuple
"""
splitted_repo = repository.split("/")
if len(splitted_repo) != 2:
raise ValueError(
f"Invalid repository {repository}. Format must be " "owner/name."
)
return splitted_repo[0], splitted_repo[1]
43 changes: 32 additions & 11 deletions pontos/release/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,27 @@ def parse_args(args) -> Tuple[Optional[str], Optional[str], Namespace]:
help="The key to sign the commits and tag for a release",
default=os.environ.get("GPG_SIGNING_KEY"),
)
create_parser.add_argument(
"--project",
help="The github project",

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.",
)
create_parser.add_argument(
repo_group.add_argument(
"--repository",
help="GitHub repository name (owner/name). For example "
"octocat/Hello-World",
)
repo_group.add_argument(
"--space",
default="greenbone",
help="User/Team name in github",
help="Owner (User/Team/Organization) name at GitHub",
)
repo_group.add_argument(
"--project",
help="The GitHub project",
)

create_parser.add_argument(
"--local",
action="store_true",
Expand Down Expand Up @@ -218,16 +230,25 @@ def parse_args(args) -> Tuple[Optional[str], Optional[str], Namespace]:
nargs="?",
help="Prefix for git tag versions. Default: %(default)s",
)
sign_parser.add_argument(
"--project",
help="The github project",
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.",
)
sign_parser.add_argument(
repo_group.add_argument(
"--repository",
help="GitHub repository name (owner/name). For example "
"octocat/Hello-World",
)
repo_group.add_argument(
"--space",
default="greenbone",
help="user/team name in github",
help="Owner (User/Team/Organization) name at GitHub",
)
repo_group.add_argument(
"--project",
help="The GitHub project",
)

sign_parser.add_argument(
"--passphrase",
help=(
Expand Down
41 changes: 29 additions & 12 deletions pontos/release/sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import subprocess
from argparse import Namespace
from asyncio.subprocess import Process
from enum import IntEnum
from enum import IntEnum, auto
from os import PathLike
from pathlib import Path
from typing import AsyncContextManager, Optional, SupportsInt, Union
Expand All @@ -27,7 +27,7 @@
from pontos.version.helper import get_last_release_version
from pontos.version.schemes import VersioningScheme

from .helper import get_git_repository_name
from .helper import get_git_repository_name, repository_split


class SignReturnValue(IntEnum):
Expand All @@ -36,12 +36,13 @@ class SignReturnValue(IntEnum):
"""

SUCCESS = 0
TOKEN_MISSING = 1
NO_PROJECT = 2
NO_RELEASE_VERSION = 3
NO_RELEASE = 4
UPLOAD_ASSET_ERROR = 5
SIGNATURE_GENERATION_FAILED = 6
TOKEN_MISSING = auto()
NO_PROJECT = auto()
NO_RELEASE_VERSION = auto()
NO_RELEASE = auto()
UPLOAD_ASSET_ERROR = auto()
SIGNATURE_GENERATION_FAILED = auto()
INVALID_REPOSITORY = auto()


class SignatureError(PontosError):
Expand Down Expand Up @@ -175,12 +176,13 @@ async def async_run( # type: ignore[override]
self,
*,
token: str,
space: str,
repository: Optional[str],
space: Optional[str],
project: Optional[str],
versioning_scheme: VersioningScheme,
signing_key: str,
passphrase: str,
dry_run: Optional[bool] = False,
project: Optional[str],
git_tag_prefix: Optional[str],
release_version: Optional[Version],
release_series: Optional[str] = None,
Expand All @@ -190,13 +192,15 @@ async def async_run( # type: ignore[override]

Args:
token: A token for creating a release on GitHub
dry_run: True to not upload the signature files
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.
versioning_scheme: The versioning scheme to use for version parsing
and calculation
dry_run: True to not upload the signature files
git_tag_prefix: An optional prefix to use for handling a git tag
from the release version.
release_version: Optional release version to use. If not set the
Expand All @@ -217,6 +221,18 @@ async def async_run( # type: ignore[override]

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

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

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

try:
project = (
project if project is not None else get_git_repository_name()
Expand Down Expand Up @@ -373,12 +389,13 @@ def sign(
*,
terminal: Terminal,
error_terminal: Terminal,
token: str,
token: Optional[str],
**_kwargs,
) -> SupportsInt:
return SignCommand(terminal=terminal, error_terminal=error_terminal).run(
token=token,
dry_run=args.dry_run,
repository=args.repository,
project=args.project,
space=args.space,
versioning_scheme=args.versioning_scheme,
Expand Down
Loading
Loading