diff --git a/src/runboat/models.py b/src/runboat/models.py index 51e8c97..1921284 100644 --- a/src/runboat/models.py +++ b/src/runboat/models.py @@ -9,7 +9,7 @@ from pydantic import BaseModel from . import github, k8s from .github import CommitInfo, GitHubStatusState -from .settings import get_build_settings, settings +from .settings import settings from .utils import slugify _logger = logging.getLogger(__name__) @@ -181,7 +181,9 @@ class Build(BaseModel): name = f"b{uuid.uuid4()}" slug = cls.make_slug(commit_info) _logger.info(f"Deploying {slug} ({name}).") - build_settings = get_build_settings(commit_info.repo, commit_info.target_branch) + build_settings = settings.get_build_settings( + commit_info.repo, commit_info.target_branch + ) if len(build_settings) > 1: raise NotImplementedError( "Having more than one build per commit is not supported yet." diff --git a/src/runboat/settings.py b/src/runboat/settings.py index 8cfcc1b..0837ffb 100644 --- a/src/runboat/settings.py +++ b/src/runboat/settings.py @@ -52,18 +52,27 @@ class Settings(BaseSettings): # HTML fragment for second footer. additional_footer_html: str = "" + def get_build_settings(self, repo: str, target_branch: str) -> list[BuildSettings]: + for repo_settings in self.repos: + if not re.match(repo_settings.repo, repo, re.IGNORECASE): + continue + if not re.match(repo_settings.branch, target_branch): + continue + return repo_settings.builds + raise RepoOrBranchNotSupported( + f"Branch {target_branch} of {repo} not supported." + ) + + def is_repo_and_branch_supported(self, repo: str, target_branch: str) -> bool: + try: + self.get_build_settings(repo, target_branch) + except RepoOrBranchNotSupported: + return False + else: + return True + class Config: env_prefix = "RUNBOAT_" settings = Settings() - - -def get_build_settings(repo: str, target_branch: str) -> list[BuildSettings]: - for repo_settings in settings.repos: - if not re.match(repo_settings.repo, repo, re.IGNORECASE): - continue - if not re.match(repo_settings.branch, target_branch): - continue - return repo_settings.builds - raise RepoOrBranchNotSupported(f"Branch {target_branch} of {repo} not supported.") diff --git a/tests/test_settings.py b/tests/test_settings.py index 6fe83e1..dc3aef2 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,14 +1,14 @@ import pytest from runboat.exceptions import RepoOrBranchNotSupported -from runboat.settings import BuildSettings, get_build_settings +from runboat.settings import BuildSettings, settings def test_get_build_settings() -> None: - assert get_build_settings("OCA/mis-builder", "15.0") == [ + assert settings.get_build_settings("OCA/mis-builder", "15.0") == [ BuildSettings(image="ghcr.io/oca/oca-ci/py3.8-odoo15.0:latest") ] with pytest.raises(RepoOrBranchNotSupported): - get_build_settings("acsone/mis-builder", "15.0") + settings.get_build_settings("acsone/mis-builder", "15.0") with pytest.raises(RepoOrBranchNotSupported): - assert not get_build_settings("OCA/mis-builder", "15.0-stuff") + assert not settings.get_build_settings("OCA/mis-builder", "15.0-stuff")