Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions mergify_cli/ci/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
15 changes: 7 additions & 8 deletions mergify_cli/ci/scopes/base_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
4 changes: 3 additions & 1 deletion mergify_cli/ci/scopes/changed_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
18 changes: 4 additions & 14 deletions mergify_cli/ci/scopes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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


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


class InvalidDetectedScopeError(ScopesError):
class InvalidDetectedScopeError(exceptions.ScopesError):
pass


Expand All @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions mergify_cli/ci/scopes/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ScopesError(Exception):
pass
6 changes: 4 additions & 2 deletions mergify_cli/tests/ci/scopes/test_base_detector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import pathlib

import click
import pytest

from mergify_cli.ci.scopes import base_detector
Expand Down Expand Up @@ -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()


Expand Down