Skip to content

Commit

Permalink
style: beautify db13438
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Apr 14, 2024
1 parent db13438 commit 88291b9
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 66 deletions.
27 changes: 10 additions & 17 deletions semantic_release/cli/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,20 +637,18 @@ def custom_git_environment() -> ContextManager[None]:
)
except HTTPError as err:
log.exception(err)
ctx.fail(
str.join("\n", [
str(err),
"Failed to create release!"
])
)
ctx.fail(str.join("\n", [str(err), "Failed to create release!"]))
except UnexpectedResponse as err:
log.exception(err)
ctx.fail(
str.join("\n", [
str(err),
"Unexpected response from remote VCS!",
"Before re-running, make sure to clean up any artifacts on the hvcs that may have already been created."
])
str.join(
"\n",
[
str(err),
"Unexpected response from remote VCS!",
"Before re-running, make sure to clean up any artifacts on the hvcs that may have already been created.",
],
)
)
except Exception as e:
log.exception(e)
Expand All @@ -662,12 +660,7 @@ def custom_git_environment() -> ContextManager[None]:
hvcs_client.upload_asset(release_id, asset)
except HTTPError as err:
log.exception(err)
ctx.fail(
str.join("\n", [
str(err),
"Failed to upload asset!"
])
)
ctx.fail(str.join("\n", [str(err), "Failed to upload asset!"]))
except Exception as e:
log.exception(e)
ctx.fail(str(e))
Expand Down
29 changes: 18 additions & 11 deletions semantic_release/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ class RemoteConfig(BaseModel):
@field_validator("url", "domain", "api_domain", "token", mode="before")
@classmethod
def resolve_env_vars(cls, val: Any) -> str | None:
ret_val = val if not isinstance(val, dict) else (
EnvConfigVar.model_validate(val).getvalue()
ret_val = (
val
if not isinstance(val, dict)
else (EnvConfigVar.model_validate(val).getvalue())
)
return ret_val or None

Expand Down Expand Up @@ -159,26 +161,31 @@ def check_url_scheme(self) -> Self:

return self


def check_insecure_flag(self, url_str: str, field_name: str) -> None:
if not url_str:
return

scheme = parse_url(url_str).scheme
if scheme == "http" and not self.insecure:
raise ValueError(
str.join("\n", [
"Insecure 'HTTP' URL detected and disabled by default.",
"Set the 'insecure' flag to 'True' to enable insecure connections."
])
str.join(
"\n",
[
"Insecure 'HTTP' URL detected and disabled by default.",
"Set the 'insecure' flag to 'True' to enable insecure connections.",
],
)
)

if scheme == "https" and self.insecure:
log.warning(
str.join("\n", [
f"'{field_name}' starts with 'https://' but the 'insecure' flag is set.",
"This flag is only necessary for 'http://' URLs."
])
str.join(
"\n",
[
f"'{field_name}' starts with 'https://' but the 'insecure' flag is set.",
"This flag is only necessary for 'http://' URLs.",
],
)
)


Expand Down
1 change: 1 addition & 0 deletions semantic_release/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MissingMergeBaseError(SemanticReleaseBaseError):
because of a shallow git clone.
"""


class UnexpectedResponse(Exception):
"""
Raised when an HTTP response cannot be parsed properly or the expected structure
Expand Down
12 changes: 0 additions & 12 deletions semantic_release/hvcs/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class HvcsBase:
def __init__(self, remote_url: str, *args: Any, **kwargs: Any) -> None:
self._remote_url = remote_url


@lru_cache(maxsize=1)
def _get_repository_owner_and_name(self) -> tuple[str, str]:
"""
Expand All @@ -54,19 +53,16 @@ def _get_repository_owner_and_name(self) -> tuple[str, str]:
parsed_git_url = parse_git_url(self._remote_url)
return parsed_git_url.namespace, parsed_git_url.repo_name


@property
def repo_name(self) -> str:
_, _name = self._get_repository_owner_and_name()
return _name


@property
def owner(self) -> str:
_owner, _ = self._get_repository_owner_and_name()
return _owner


def compare_url(self, from_rev: str, to_rev: str) -> str:
"""
Get the comparison link between two version tags.
Expand All @@ -82,7 +78,6 @@ def compare_url(self, from_rev: str, to_rev: str) -> str:
_not_supported(self, "compare_url")
return ""


def upload_dists(self, tag: str, dist_glob: str) -> int:
"""
Upload built distributions to a release on a remote VCS that
Expand All @@ -106,13 +101,11 @@ def get_release_id_by_tag(self, tag: str) -> int | None:
_not_supported(self, "get_release_id_by_tag")
return None


def edit_release_notes(self, release_id: int, release_notes: str) -> int:
"""Edit the changelog associated with a release, if supported"""
_not_supported(self, "edit_release_notes")
return -1


def create_or_update_release(
self, tag: str, release_notes: str, prerelease: bool = False
) -> int | str:
Expand All @@ -123,7 +116,6 @@ def create_or_update_release(
_not_supported(self, "create_or_update_release")
return -1


def asset_upload_url(self, release_id: str) -> str | None:
"""
Return the URL to use to upload an asset to the given release id, if releases
Expand All @@ -132,7 +124,6 @@ def asset_upload_url(self, release_id: str) -> str | None:
_not_supported(self, "asset_upload_url")
return None


def upload_asset(
self, release_id: int | str, file: str, label: str | None = None
) -> bool:
Expand All @@ -144,7 +135,6 @@ def upload_asset(
_not_supported(self, "upload_asset")
return True


def remote_url(self, use_token: bool) -> str:
"""
Return the remote URL for the repository, including the token for
Expand All @@ -153,7 +143,6 @@ def remote_url(self, use_token: bool) -> str:
_not_supported(self, "remote_url")
return ""


def commit_hash_url(self, commit_hash: str) -> str:
"""
Given a commit hash, return a web URL which links to this commit in the
Expand All @@ -162,7 +151,6 @@ def commit_hash_url(self, commit_hash: str) -> str:
_not_supported(self, "commit_hash_url")
return ""


def pull_request_url(self, pr_number: str) -> str:
"""
Given a number for a PR/Merge request/equivalent, return a web URL that links
Expand Down
14 changes: 6 additions & 8 deletions tests/command_line/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,12 @@ def test_changelog_post_to_release(
session.mount("http://", mock_adapter)
session.mount("https://", mock_adapter)

expected_request_url = (
"{api_url}/repos/{owner}/{repo_name}/releases".format(
# TODO: Fix as this is likely not correct given a custom domain and the
# use of GitHub which would be GitHub Enterprise Server which we don't yet support
api_url=f"https://api.{EXAMPLE_HVCS_DOMAIN}", # GitHub API URL
owner=EXAMPLE_REPO_OWNER,
repo_name=EXAMPLE_REPO_NAME,
)
expected_request_url = "{api_url}/repos/{owner}/{repo_name}/releases".format(
# TODO: Fix as this is likely not correct given a custom domain and the
# use of GitHub which would be GitHub Enterprise Server which we don't yet support
api_url=f"https://api.{EXAMPLE_HVCS_DOMAIN}", # GitHub API URL
owner=EXAMPLE_REPO_OWNER,
repo_name=EXAMPLE_REPO_NAME,
)

# Patch out env vars that affect changelog URLs but only get set in e.g.
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _netrc_file(machine: str) -> _TemporaryFileWrapper[str]:
context_manager.__exit__(
None if not exception else type(exception),
exception,
None if not exception else exception.__traceback__
None if not exception else exception.__traceback__,
)


Expand Down
13 changes: 7 additions & 6 deletions tests/unit/semantic_release/hvcs/test_bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

@pytest.fixture
def default_bitbucket_client():
remote_url = f"git@{Bitbucket.DEFAULT_DOMAIN}:{EXAMPLE_REPO_OWNER}/{EXAMPLE_REPO_NAME}.git"
remote_url = (
f"git@{Bitbucket.DEFAULT_DOMAIN}:{EXAMPLE_REPO_OWNER}/{EXAMPLE_REPO_NAME}.git"
)
return Bitbucket(remote_url=remote_url)


Expand Down Expand Up @@ -163,17 +165,14 @@ def test_bitbucket_client_init(
# Bad base domain schemes
(f"ftp://{EXAMPLE_HVCS_DOMAIN}", None, False),
(f"ftp://{EXAMPLE_HVCS_DOMAIN}", None, True),
# Unallowed insecure connections when base domain is insecure
(f"http://{EXAMPLE_HVCS_DOMAIN}", None, False),
# Bad API domain schemes
(None, f"ftp://api.{EXAMPLE_HVCS_DOMAIN}", False),
(None, f"ftp://api.{EXAMPLE_HVCS_DOMAIN}", True),
# Unallowed insecure connections when api domain is insecure
(None, f"http://{EXAMPLE_HVCS_DOMAIN}", False),
]
],
)
def test_bitbucket_client_init_with_invalid_scheme(
hvcs_domain: str | None,
Expand Down Expand Up @@ -229,7 +228,9 @@ def test_compare_url(default_bitbucket_client: Bitbucket):
to_rev=end_rev,
)
)
actual_url = default_bitbucket_client.compare_url(from_rev=start_rev, to_rev=end_rev)
actual_url = default_bitbucket_client.compare_url(
from_rev=start_rev, to_rev=end_rev
)
assert expected_url == actual_url


Expand Down
22 changes: 11 additions & 11 deletions tests/unit/semantic_release/hvcs/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ def mock_gitlab(status: str = "success"):

@pytest.fixture
def default_gl_client() -> Generator[Gitlab, None, None]:
remote_url = f"git@{Gitlab.DEFAULT_DOMAIN}:{EXAMPLE_REPO_OWNER}/{EXAMPLE_REPO_NAME}.git"
remote_url = (
f"git@{Gitlab.DEFAULT_DOMAIN}:{EXAMPLE_REPO_OWNER}/{EXAMPLE_REPO_NAME}.git"
)
with mock.patch.dict(os.environ, {}, clear=True):
yield Gitlab(remote_url=remote_url)

Expand Down Expand Up @@ -202,7 +204,7 @@ def default_gl_client() -> Generator[Gitlab, None, None]:
EXAMPLE_HVCS_DOMAIN,
f"http://{EXAMPLE_HVCS_DOMAIN}",
True,
)
),
],
)
@pytest.mark.parametrize(
Expand Down Expand Up @@ -241,7 +243,7 @@ def test_gitlab_client_init(
(f"ftp://{EXAMPLE_HVCS_DOMAIN}", False),
(f"ftp://{EXAMPLE_HVCS_DOMAIN}", True),
(f"http://{EXAMPLE_HVCS_DOMAIN}", False),
]
],
)
def test_gitlab_client_init_with_invalid_scheme(
hvcs_domain: str,
Expand Down Expand Up @@ -332,14 +334,12 @@ def test_remote_url(
def test_compare_url(default_gl_client: Gitlab):
start_rev = "revA"
end_rev = "revB"
expected_url = (
"{server}/{owner}/{repo}/-/compare/{from_rev}...{to_rev}".format(
server=default_gl_client.hvcs_domain.url,
owner=default_gl_client.owner,
repo=default_gl_client.repo_name,
from_rev=start_rev,
to_rev=end_rev,
)
expected_url = "{server}/{owner}/{repo}/-/compare/{from_rev}...{to_rev}".format(
server=default_gl_client.hvcs_domain.url,
owner=default_gl_client.owner,
repo=default_gl_client.repo_name,
from_rev=start_rev,
to_rev=end_rev,
)
actual_url = default_gl_client.compare_url(from_rev=start_rev, to_rev=end_rev)
assert expected_url == actual_url
Expand Down

0 comments on commit 88291b9

Please sign in to comment.