Skip to content
Merged
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
81 changes: 47 additions & 34 deletions src/usethis/_integrations/sonarqube/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import re
from pathlib import Path

from pydantic import TypeAdapter

Expand All @@ -20,49 +19,23 @@ class _NonstandardPythonVersionError(Exception):
"""Raised when a non-standard Python version is detected."""


def get_sonar_project_properties() -> str: # noqa: PLR0912
def get_sonar_project_properties() -> str:
"""Get contents for (or from) the sonar-project.properties file."""
path = usethis_config.cpd() / "sonar-project.properties"
if path.exists() and path.is_file():
return path.read_text(encoding="utf-8")

# Get Python version
try:
python_version = _get_short_version(Path(".python-version").read_text().strip())
python_version = _get_short_version(
(usethis_config.cpd() / ".python-version").read_text().strip()
)
except (FileNotFoundError, _NonstandardPythonVersionError):
python_version = get_python_version()

# Get Project key
try:
project_key = PyprojectTOMLManager()[
["tool", "usethis", "sonarqube", "project-key"]
]
except KeyError:
msg = "Could not find SonarQube project key at 'tool.usethis.sonarqube.project-key' in 'pyproject.toml'."
raise MissingProjectKeyError(msg) from None
except FileNotFoundError:
msg = "Could not find 'pyproject.toml' for SonarQube project key at 'tool.usethis.sonarqube.project-key'."
raise MissingProjectKeyError(msg) from None
_validate_project_key(project_key)

# Get verbosity setting
try:
verbose = PyprojectTOMLManager()[["tool", "usethis", "sonarqube", "verbose"]]
except (FileNotFoundError, KeyError):
verbose = False
verbose = TypeAdapter(bool).validate_python(verbose)

# Get exclusions
try:
exclusions = PyprojectTOMLManager()[
["tool", "usethis", "sonarqube", "exclusions"]
]
except (FileNotFoundError, KeyError):
exclusions = []
# TypeAdapter(list).validate_python() ensures we have a list and returns a new list
exclusions = TypeAdapter(list).validate_python(exclusions)
for exclusion in exclusions:
TypeAdapter(str).validate_python(exclusion)
project_key = _get_sonarqube_project_key()
verbose = _is_sonarqube_verbose()
exclusions = _get_sonarqube_exclusions()

# Get coverage report output path
try:
Expand Down Expand Up @@ -107,6 +80,46 @@ def _get_short_version(version: str) -> str:
return match.group(1)


def _get_sonarqube_project_key() -> str:
try:
project_key = PyprojectTOMLManager()[
["tool", "usethis", "sonarqube", "project-key"]
]
except KeyError:
msg = "Could not find SonarQube project key at 'tool.usethis.sonarqube.project-key' in 'pyproject.toml'."
raise MissingProjectKeyError(msg) from None
except FileNotFoundError:
msg = "Could not find 'pyproject.toml' for SonarQube project key at 'tool.usethis.sonarqube.project-key'."
raise MissingProjectKeyError(msg) from None
_validate_project_key(project_key)
return project_key


def _is_sonarqube_verbose() -> bool:
try:
verbose = PyprojectTOMLManager()[["tool", "usethis", "sonarqube", "verbose"]]
except (FileNotFoundError, KeyError):
verbose = False
verbose = TypeAdapter(bool).validate_python(verbose)

return verbose


def _get_sonarqube_exclusions() -> list[str]:
try:
exclusions = PyprojectTOMLManager()[
["tool", "usethis", "sonarqube", "exclusions"]
]
except (FileNotFoundError, KeyError):
exclusions = []
# TypeAdapter(list).validate_python() ensures we have a list and returns a new list
exclusions = TypeAdapter(list).validate_python(exclusions)
for exclusion in exclusions:
TypeAdapter(str).validate_python(exclusion)

return exclusions


def _validate_project_key(project_key: str) -> None:
"""Validate the SonarQube project key.

Expand Down