diff --git a/mergify_cli/ci/cli.py b/mergify_cli/ci/cli.py index 80caa08d..a7d64c44 100644 --- a/mergify_cli/ci/cli.py +++ b/mergify_cli/ci/cli.py @@ -4,6 +4,7 @@ from mergify_cli.ci import detector from mergify_cli.ci.junit_processing import cli as junit_processing_cli from mergify_cli.ci.scopes import cli as scopes_cli +from mergify_cli.ci.scopes import exceptions as scopes_exc class JUnitFile(click.Path): @@ -214,7 +215,7 @@ def scopes( ) -> None: try: scopes = scopes_cli.detect(config_path=config_path) - except scopes_cli.ScopesError as e: + except scopes_exc.ScopesError as e: raise click.ClickException(str(e)) from e if write is not None: @@ -273,7 +274,7 @@ async def scopes_send( # noqa: PLR0913, PLR0917 if file is not None: try: dump = scopes_cli.DetectedScope.load_from_file(file) - except scopes_cli.ScopesError as e: + except scopes_exc.ScopesError as e: raise click.ClickException(str(e)) from e scopes.extend(dump.scopes) diff --git a/mergify_cli/ci/scopes/base_detector.py b/mergify_cli/ci/scopes/base_detector.py index d8068d8b..ac3316bf 100644 --- a/mergify_cli/ci/scopes/base_detector.py +++ b/mergify_cli/ci/scopes/base_detector.py @@ -4,10 +4,14 @@ import os import typing -import click import yaml from mergify_cli import utils +from mergify_cli.ci.scopes import exceptions + + +class BaseNotFoundError(exceptions.ScopesError): + pass class MergeQueuePullRequest(typing.TypedDict): @@ -92,10 +96,5 @@ def detect() -> Base: if base_ref: return Base(base_ref, is_merge_queue=False) - msg = ( - "Could not detect base SHA. Ensure checkout has sufficient history " - "(e.g., actions/checkout with fetch-depth: 0) or provide GITHUB_EVENT_PATH / GITHUB_BASE_REF." - ) - raise click.ClickException( - msg, - ) + msg = "Could not detect base SHA. Provide GITHUB_EVENT_PATH / GITHUB_BASE_REF." + raise BaseNotFoundError(msg) diff --git a/mergify_cli/ci/scopes/changed_files.py b/mergify_cli/ci/scopes/changed_files.py index a402994d..57960529 100644 --- a/mergify_cli/ci/scopes/changed_files.py +++ b/mergify_cli/ci/scopes/changed_files.py @@ -3,11 +3,13 @@ import subprocess import sys +from mergify_cli.ci.scopes import exceptions + COMMITS_BATCH_SIZE = 100 -class ChangedFilesError(Exception): +class ChangedFilesError(exceptions.ScopesError): pass diff --git a/mergify_cli/ci/scopes/cli.py b/mergify_cli/ci/scopes/cli.py index ea3c1c04..d5db3356 100644 --- a/mergify_cli/ci/scopes/cli.py +++ b/mergify_cli/ci/scopes/cli.py @@ -11,6 +11,7 @@ from mergify_cli import utils from mergify_cli.ci.scopes import base_detector from mergify_cli.ci.scopes import changed_files +from mergify_cli.ci.scopes import exceptions if typing.TYPE_CHECKING: @@ -21,15 +22,7 @@ SCOPE_NAME_RE = r"^[A-Za-z0-9_-]+$" -class ScopesError(Exception): - pass - - -class ConfigInvalidError(ScopesError): - pass - - -class ChangedFilesError(ScopesError): +class ConfigInvalidError(exceptions.ScopesError): pass @@ -111,7 +104,7 @@ def maybe_write_github_outputs( fh.write(f"{key}={val}\n") -class InvalidDetectedScopeError(ScopesError): +class InvalidDetectedScopeError(exceptions.ScopesError): pass @@ -135,10 +128,7 @@ def load_from_file(cls, filename: str) -> DetectedScope: def detect(config_path: str) -> DetectedScope: cfg = Config.from_yaml(config_path) base = base_detector.detect() - try: - changed = changed_files.git_changed_files(base.ref) - except changed_files.ChangedFilesError as e: - raise ChangedFilesError(str(e)) + changed = changed_files.git_changed_files(base.ref) scopes_hit, per_scope = match_scopes(cfg, changed) all_scopes = set(cfg.scopes.keys()) diff --git a/mergify_cli/ci/scopes/exceptions.py b/mergify_cli/ci/scopes/exceptions.py new file mode 100644 index 00000000..80a4f9f7 --- /dev/null +++ b/mergify_cli/ci/scopes/exceptions.py @@ -0,0 +1,2 @@ +class ScopesError(Exception): + pass diff --git a/mergify_cli/tests/ci/scopes/test_base_detector.py b/mergify_cli/tests/ci/scopes/test_base_detector.py index 2d893b54..4fa6a23a 100644 --- a/mergify_cli/tests/ci/scopes/test_base_detector.py +++ b/mergify_cli/tests/ci/scopes/test_base_detector.py @@ -1,7 +1,6 @@ import json import pathlib -import click import pytest from mergify_cli.ci.scopes import base_detector @@ -63,7 +62,10 @@ def test_detect_base_no_info(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.delenv("GITHUB_EVENT_PATH", raising=False) monkeypatch.delenv("GITHUB_BASE_REF", raising=False) - with pytest.raises(click.ClickException, match="Could not detect base SHA"): + with pytest.raises( + base_detector.BaseNotFoundError, + match="Could not detect base SHA", + ): base_detector.detect()