diff --git a/Makefile b/Makefile index 4c565100..4df25d14 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ init: format: poetry run ruff format gitopscli tests + poetry run ruff gitopscli tests --fix format-check: poetry run ruff format gitopscli tests --check diff --git a/gitopscli/appconfig_api/app_tenant_config.py b/gitopscli/appconfig_api/app_tenant_config.py index 7efc191f..f88cab25 100644 --- a/gitopscli/appconfig_api/app_tenant_config.py +++ b/gitopscli/appconfig_api/app_tenant_config.py @@ -76,7 +76,7 @@ def __set_dirty(self) -> None: def __generate_config_from_tenant_repo( tenant_repo: GitRepo, -) -> Any: # TODO: supposed to be ruamel object than Any # noqa: FIX002 +) -> Any: tenant_app_dirs = __get_all_tenant_applications_dirs(tenant_repo) tenant_config_template = f""" config: diff --git a/gitopscli/commands/deploy.py b/gitopscli/commands/deploy.py index 12b666c2..d72324f9 100644 --- a/gitopscli/commands/deploy.py +++ b/gitopscli/commands/deploy.py @@ -80,7 +80,7 @@ def __update_values(self, git_repo: GitRepo) -> dict[str, Any]: single_commit = args.single_commit or args.commit_message full_file_path = git_repo.get_full_file_path(args.file) updated_values = {} - for key, value in args.values.items(): # noqa: PD011 + for key, value in args.values.items(): try: updated_value = update_yaml_file(full_file_path, key, value) except (FileNotFoundError, IsADirectoryError) as ex: diff --git a/gitopscli/commands/sync_apps.py b/gitopscli/commands/sync_apps.py index 7b290c2a..0bd30c39 100644 --- a/gitopscli/commands/sync_apps.py +++ b/gitopscli/commands/sync_apps.py @@ -47,7 +47,6 @@ def _sync_apps_command(args: SyncAppsCommand.Args) -> None: ) -# TODO: BETTER NAMES FOR STUFF HERE # noqa: FIX002 def __sync_apps( tenant_git_repo: GitRepo, root_git_repo: GitRepo, diff --git a/gitopscli/git_api/bitbucket_git_repo_api_adapter.py b/gitopscli/git_api/bitbucket_git_repo_api_adapter.py index 4cd6c5b2..ceb0edc6 100644 --- a/gitopscli/git_api/bitbucket_git_repo_api_adapter.py +++ b/gitopscli/git_api/bitbucket_git_repo_api_adapter.py @@ -35,14 +35,7 @@ def get_clone_url(self) -> str: raise GitOpsException(f"Error connecting to '{self.__git_provider_url}''") from ex if "errors" in repo: for error in repo["errors"]: - exception = error["exceptionName"] - if exception == "com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException": - raise GitOpsException("Bad credentials") - if exception == "com.atlassian.bitbucket.project.NoSuchProjectException": - raise GitOpsException(f"Organisation '{self.__organisation}' does not exist") - if exception == "com.atlassian.bitbucket.repository.NoSuchRepositoryException": - raise GitOpsException(f"Repository '{self.__organisation}/{self.__repository_name}' does not exist") - raise GitOpsException(error["message"]) + raise self.__map_clone_error(error) if "links" not in repo: raise GitOpsException(f"Repository '{self.__organisation}/{self.__repository_name}' does not exist") for clone_link in repo["links"]["clone"]: @@ -52,6 +45,16 @@ def get_clone_url(self) -> str: raise GitOpsException("Couldn't determine repository URL.") return str(repo_url) + def __map_clone_error(self, error: dict[str, str]) -> GitOpsException: + exception = error["exceptionName"] + if exception == "com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException": + return GitOpsException("Bad credentials") + if exception == "com.atlassian.bitbucket.project.NoSuchProjectException": + return GitOpsException(f"Organisation '{self.__organisation}' does not exist") + if exception == "com.atlassian.bitbucket.repository.NoSuchRepositoryException": + return GitOpsException(f"Repository '{self.__organisation}/{self.__repository_name}' does not exist") + return GitOpsException(error["message"]) + def create_pull_request_to_default_branch( self, from_branch: str, @@ -85,8 +88,8 @@ def create_pull_request( def merge_pull_request( self, pr_id: int, - merge_method: Literal["squash", "rebase", "merge"] = "merge", # noqa: ARG002 - merge_parameters: dict[str, Any] | None = None, # noqa: ARG002 + _merge_method: Literal["squash", "rebase", "merge"] = "merge", + _merge_parameters: dict[str, Any] | None = None, ) -> None: pull_request = self.__bitbucket.get_pullrequest(self.__organisation, self.__repository_name, pr_id) self.__bitbucket.merge_pull_request( diff --git a/gitopscli/git_api/git_repo_api_logging_proxy.py b/gitopscli/git_api/git_repo_api_logging_proxy.py index a455d9f0..dd2691d2 100644 --- a/gitopscli/git_api/git_repo_api_logging_proxy.py +++ b/gitopscli/git_api/git_repo_api_logging_proxy.py @@ -40,7 +40,7 @@ def merge_pull_request( self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge", - merge_parameters: dict[str, Any] | None = None, # noqa: ARG002 + _merge_parameters: dict[str, Any] | None = None, ) -> None: logging.info("Merging pull request %s", pr_id) self.__api.merge_pull_request(pr_id, merge_method=merge_method) diff --git a/gitopscli/git_api/github_git_repo_api_adapter.py b/gitopscli/git_api/github_git_repo_api_adapter.py index 0b22bd73..553ebb25 100644 --- a/gitopscli/git_api/github_git_repo_api_adapter.py +++ b/gitopscli/git_api/github_git_repo_api_adapter.py @@ -61,7 +61,7 @@ def merge_pull_request( self, pr_id: int, merge_method: Literal["squash", "rebase", "merge"] = "merge", - merge_parameters: dict[str, Any] | None = None, # noqa: ARG002 + _merge_parameters: dict[str, Any] | None = None, ) -> None: pull_request = self.__get_pull_request(pr_id) pull_request.merge(merge_method=merge_method) @@ -70,7 +70,7 @@ def add_pull_request_comment( self, pr_id: int, text: str, - parent_id: int | None = None, # noqa: ARG002 + _parent_id: int | None = None, ) -> None: pull_request = self.__get_pull_request(pr_id) pull_request.create_issue_comment(text) diff --git a/gitopscli/git_api/gitlab_git_repo_api_adapter.py b/gitopscli/git_api/gitlab_git_repo_api_adapter.py index afa525b8..a1b7e09f 100644 --- a/gitopscli/git_api/gitlab_git_repo_api_adapter.py +++ b/gitopscli/git_api/gitlab_git_repo_api_adapter.py @@ -1,5 +1,6 @@ import logging import time +from http import HTTPStatus from typing import Any, Literal import gitlab @@ -29,7 +30,7 @@ def __init__( except gitlab.exceptions.GitlabAuthenticationError as ex: raise GitOpsException("Bad Personal Access Token") from ex except gitlab.exceptions.GitlabGetError as ex: - if ex.response_code == 404: # noqa: PLR2004 + if ex.response_code == HTTPStatus.NOT_FOUND: raise GitOpsException(f"Repository '{organisation}/{repository_name}' does not exist") from ex raise GitOpsException(f"Error getting repository: '{ex.error_message}'") from ex @@ -82,7 +83,6 @@ def merge_pull_request( merge_request.rebase(merge_parameters) return merge_request.merge(merge_parameters) - return # noqa: TRY300 except gitlab.exceptions.GitlabMRClosedError as ex: # "Branch cannot be merged" error can occur if the server # is still processing the merge request internally @@ -95,12 +95,14 @@ def merge_pull_request( if max_retries == 0: raise GitOpsException("Error merging pull request: 'Branch cannot be merged'") from ex time.sleep(2.5) + else: + return def add_pull_request_comment( self, pr_id: int, text: str, - parent_id: int | None = None, # noqa:ARG002 + _parent_id: int | None = None, ) -> None: merge_request = self.__project.mergerequests.get(pr_id) merge_request.notes.create({"body": text}) diff --git a/gitopscli/io_api/yaml_util.py b/gitopscli/io_api/yaml_util.py index 51536105..aa62cea8 100644 --- a/gitopscli/io_api/yaml_util.py +++ b/gitopscli/io_api/yaml_util.py @@ -73,9 +73,10 @@ def merge_yaml_element(file_path: str, element_path: str, desired_value: Any) -> work_path = work_path[key] for key, value in desired_value.items(): + tmp_value = value if key in work_path and work_path[key] is not None: - value = {**work_path[key], **value} # noqa: PLW2901 - work_path[key] = value + tmp_value = {**work_path[key], **tmp_value} + work_path[key] = tmp_value # delete missing key: current = work_path.copy().items() diff --git a/pyproject.toml b/pyproject.toml index aee27584..06a35bc5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,16 +46,27 @@ target-version = "py311" [tool.ruff.lint] select = ["ALL"] -ignore = ["ANN101", "ANN401", -"C901", "PLR0913", # ignore complexity -"D", -"COM812", "ISC001", -"EM101", "EM102", -"S101", "TD", "TRY003"] - +ignore = [ + "ANN101", # https://docs.astral.sh/ruff/rules/missing-type-self/ + "ANN401", # https://docs.astral.sh/ruff/rules/any-type/ + "C901", # https://docs.astral.sh/ruff/rules/complex-structure/ + "PLR0913", # https://docs.astral.sh/ruff/rules/too-many-arguments/ + "D", # https://docs.astral.sh/ruff/rules/#pydocstyle-d + "COM812", # https://docs.astral.sh/ruff/rules/missing-trailing-comma/ (clashes with formatter) + "EM101", # https://docs.astral.sh/ruff/rules/raw-string-in-exception/ + "EM102", # https://docs.astral.sh/ruff/rules/f-string-in-exception/ + "S101", # https://docs.astral.sh/ruff/rules/assert/ + "TRY003", # https://docs.astral.sh/ruff/rules/raise-vanilla-args/ + "PD", # https://docs.astral.sh/ruff/rules/#pandas-vet-pd (false positives) +] [tool.ruff.per-file-ignores] "**/__init__.py" = ["F401"] -"tests/**/*.py" = ["S106", "S108", "ANN", "PT009"] +"tests/**/*.py" = [ + "S106", # https://docs.astral.sh/ruff/rules/hardcoded-password-func-arg/ + "S108", # https://docs.astral.sh/ruff/rules/hardcoded-temp-file/ + "ANN", # https://docs.astral.sh/ruff/rules/#flake8-annotations-ann + "PT009" # https://docs.astral.sh/ruff/rules/pytest-unittest-assertion/ +] # the following exclusions have been introduced to prevent huge changes # feel free to remove them and fix the code "gitopscli/appconfig_api/app_tenant_config.py" = ["PTH110", "PTH112", "PTH118"] diff --git a/tests/commands/mock_mixin.py b/tests/commands/mock_mixin.py index 323f4b94..4d95e37d 100644 --- a/tests/commands/mock_mixin.py +++ b/tests/commands/mock_mixin.py @@ -1,8 +1,7 @@ -from abc import ABC from unittest.mock import MagicMock, patch, seal -class MockMixin(ABC): # noqa: B024 +class MockMixin: def init_mock_manager(self, command_class: type) -> None: self.command_class = command_class self.mock_manager = MagicMock() diff --git a/tests/git_api/test_git_repo.py b/tests/git_api/test_git_repo.py index 77087df6..750398eb 100644 --- a/tests/git_api/test_git_repo.py +++ b/tests/git_api/test_git_repo.py @@ -264,8 +264,7 @@ def test_commit_with_custom_author(self, logging_mock): self.assertIn("README.md", commits[0].stats.files) logging_mock.info.assert_called_once_with("Creating commit with message: %s", "new commit") - @patch("gitopscli.git_api.git_repo.logging") - def test_commit_with_custom_author_name_but_no_email_returns_validation_error(self, logging_mock): # noqa: ARG002 + def test_commit_with_custom_author_name_but_no_email_returns_validation_error(self): with GitRepo(self.__mock_repo_api) as testee: with pytest.raises(GitOpsException) as ex: testee.commit( @@ -279,8 +278,7 @@ def test_commit_with_custom_author_name_but_no_email_returns_validation_error(se "Please provide the name and email address of the Git author or provide neither!" ) - @patch("gitopscli.git_api.git_repo.logging") - def test_commit_with_custom_author_email_but_no_name_returns_validation_error(self, logging_mock): # noqa: ARG002 + def test_commit_with_custom_author_email_but_no_name_returns_validation_error(self): with GitRepo(self.__mock_repo_api) as testee: with pytest.raises(GitOpsException) as ex: testee.commit(