Skip to content

Commit

Permalink
Merge pull request baloise#216 from baloise/fix-lint-errors-2
Browse files Browse the repository at this point in the history
Fix some lint errors
  • Loading branch information
christiansiegel authored Nov 10, 2023
2 parents 55adc20 + 8af3f90 commit 9ec0832
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 35 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gitopscli/appconfig_api/app_tenant_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion gitopscli/commands/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion gitopscli/commands/sync_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 13 additions & 10 deletions gitopscli/git_api/bitbucket_git_repo_api_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion gitopscli/git_api/git_repo_api_logging_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions gitopscli/git_api/github_git_repo_api_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions gitopscli/git_api/gitlab_git_repo_api_adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import time
from http import HTTPStatus
from typing import Any, Literal

import gitlab
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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})
Expand Down
5 changes: 3 additions & 2 deletions gitopscli/io_api/yaml_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 19 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 1 addition & 2 deletions tests/commands/mock_mixin.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
6 changes: 2 additions & 4 deletions tests/git_api/test_git_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 9ec0832

Please sign in to comment.