Skip to content

Commit 9216510

Browse files
authored
chore: remove click.Exception from modules (#807)
For separation of concern only the root module should use it.
1 parent 9336584 commit 9216510

File tree

6 files changed

+23
-27
lines changed

6 files changed

+23
-27
lines changed

mergify_cli/ci/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from mergify_cli.ci import detector
55
from mergify_cli.ci.junit_processing import cli as junit_processing_cli
66
from mergify_cli.ci.scopes import cli as scopes_cli
7+
from mergify_cli.ci.scopes import exceptions as scopes_exc
78

89

910
class JUnitFile(click.Path):
@@ -214,7 +215,7 @@ def scopes(
214215
) -> None:
215216
try:
216217
scopes = scopes_cli.detect(config_path=config_path)
217-
except scopes_cli.ScopesError as e:
218+
except scopes_exc.ScopesError as e:
218219
raise click.ClickException(str(e)) from e
219220

220221
if write is not None:
@@ -273,7 +274,7 @@ async def scopes_send( # noqa: PLR0913, PLR0917
273274
if file is not None:
274275
try:
275276
dump = scopes_cli.DetectedScope.load_from_file(file)
276-
except scopes_cli.ScopesError as e:
277+
except scopes_exc.ScopesError as e:
277278
raise click.ClickException(str(e)) from e
278279
scopes.extend(dump.scopes)
279280

mergify_cli/ci/scopes/base_detector.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import os
55
import typing
66

7-
import click
87
import yaml
98

109
from mergify_cli import utils
10+
from mergify_cli.ci.scopes import exceptions
11+
12+
13+
class BaseNotFoundError(exceptions.ScopesError):
14+
pass
1115

1216

1317
class MergeQueuePullRequest(typing.TypedDict):
@@ -92,10 +96,5 @@ def detect() -> Base:
9296
if base_ref:
9397
return Base(base_ref, is_merge_queue=False)
9498

95-
msg = (
96-
"Could not detect base SHA. Ensure checkout has sufficient history "
97-
"(e.g., actions/checkout with fetch-depth: 0) or provide GITHUB_EVENT_PATH / GITHUB_BASE_REF."
98-
)
99-
raise click.ClickException(
100-
msg,
101-
)
99+
msg = "Could not detect base SHA. Provide GITHUB_EVENT_PATH / GITHUB_BASE_REF."
100+
raise BaseNotFoundError(msg)

mergify_cli/ci/scopes/changed_files.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import subprocess
44
import sys
55

6+
from mergify_cli.ci.scopes import exceptions
7+
68

79
COMMITS_BATCH_SIZE = 100
810

911

10-
class ChangedFilesError(Exception):
12+
class ChangedFilesError(exceptions.ScopesError):
1113
pass
1214

1315

mergify_cli/ci/scopes/cli.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from mergify_cli import utils
1212
from mergify_cli.ci.scopes import base_detector
1313
from mergify_cli.ci.scopes import changed_files
14+
from mergify_cli.ci.scopes import exceptions
1415

1516

1617
if typing.TYPE_CHECKING:
@@ -21,15 +22,7 @@
2122
SCOPE_NAME_RE = r"^[A-Za-z0-9_-]+$"
2223

2324

24-
class ScopesError(Exception):
25-
pass
26-
27-
28-
class ConfigInvalidError(ScopesError):
29-
pass
30-
31-
32-
class ChangedFilesError(ScopesError):
25+
class ConfigInvalidError(exceptions.ScopesError):
3326
pass
3427

3528

@@ -111,7 +104,7 @@ def maybe_write_github_outputs(
111104
fh.write(f"{key}={val}\n")
112105

113106

114-
class InvalidDetectedScopeError(ScopesError):
107+
class InvalidDetectedScopeError(exceptions.ScopesError):
115108
pass
116109

117110

@@ -135,10 +128,7 @@ def load_from_file(cls, filename: str) -> DetectedScope:
135128
def detect(config_path: str) -> DetectedScope:
136129
cfg = Config.from_yaml(config_path)
137130
base = base_detector.detect()
138-
try:
139-
changed = changed_files.git_changed_files(base.ref)
140-
except changed_files.ChangedFilesError as e:
141-
raise ChangedFilesError(str(e))
131+
changed = changed_files.git_changed_files(base.ref)
142132
scopes_hit, per_scope = match_scopes(cfg, changed)
143133

144134
all_scopes = set(cfg.scopes.keys())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ScopesError(Exception):
2+
pass

mergify_cli/tests/ci/scopes/test_base_detector.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import pathlib
33

4-
import click
54
import pytest
65

76
from mergify_cli.ci.scopes import base_detector
@@ -63,7 +62,10 @@ def test_detect_base_no_info(monkeypatch: pytest.MonkeyPatch) -> None:
6362
monkeypatch.delenv("GITHUB_EVENT_PATH", raising=False)
6463
monkeypatch.delenv("GITHUB_BASE_REF", raising=False)
6564

66-
with pytest.raises(click.ClickException, match="Could not detect base SHA"):
65+
with pytest.raises(
66+
base_detector.BaseNotFoundError,
67+
match="Could not detect base SHA",
68+
):
6769
base_detector.detect()
6870

6971

0 commit comments

Comments
 (0)