Skip to content

Commit e9b2381

Browse files
committed
Chore(cicd_bot): Tidy up failure output
1 parent 107fe25 commit e9b2381

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed

sqlmesh/core/console.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,17 @@ def show_row_diff(
289289

290290
class BaseConsole(abc.ABC):
291291
@abc.abstractmethod
292-
def log_error(self, message: str) -> None:
292+
def log_error(self, message: str, *args: t.Any, **kwargs: t.Any) -> None:
293293
"""Display error info to the user."""
294294

295295
@abc.abstractmethod
296-
def log_warning(self, short_message: str, long_message: t.Optional[str] = None) -> None:
296+
def log_warning(
297+
self,
298+
short_message: str,
299+
long_message: t.Optional[str] = None,
300+
*args: t.Any,
301+
**kwargs: t.Any,
302+
) -> None:
297303
"""Display warning info to the user.
298304
299305
Args:
@@ -3082,15 +3088,23 @@ def consume_captured_errors(self) -> str:
30823088
finally:
30833089
self._errors = []
30843090

3085-
def log_warning(self, short_message: str, long_message: t.Optional[str] = None) -> None:
3091+
def log_warning(
3092+
self,
3093+
short_message: str,
3094+
long_message: t.Optional[str] = None,
3095+
*args: t.Any,
3096+
**kwargs: t.Any,
3097+
) -> None:
30863098
if short_message not in self._warnings:
30873099
self._warnings.append(short_message)
3088-
super().log_warning(short_message, long_message)
3100+
if kwargs.pop("print", True):
3101+
super().log_warning(short_message, long_message)
30893102

3090-
def log_error(self, message: str) -> None:
3103+
def log_error(self, message: str, *args: t.Any, **kwargs: t.Any) -> None:
30913104
if message not in self._errors:
30923105
self._errors.append(message)
3093-
super().log_error(message)
3106+
if kwargs.pop("print", True):
3107+
super().log_error(message)
30943108

30953109
def log_skipped_models(self, snapshot_names: t.Set[str]) -> None:
30963110
if snapshot_names:
@@ -3127,6 +3141,11 @@ def __init__(self, **kwargs: t.Any) -> None:
31273141
kwargs.pop("alert_block_collapsible_threshold", 200)
31283142
)
31293143

3144+
# capture_only = True: capture but dont print to console
3145+
# capture_only = False: capture and also print to console
3146+
self.warning_capture_only = kwargs.pop("warning_capture_only", False)
3147+
self.error_capture_only = kwargs.pop("error_capture_only", False)
3148+
31303149
super().__init__(
31313150
**{**kwargs, "console": RichConsole(no_color=True, width=kwargs.pop("width", None))}
31323151
)
@@ -3401,6 +3420,12 @@ def stop_promotion_progress(self, success: bool = True) -> None:
34013420
super().stop_promotion_progress(success)
34023421
self._print("\n")
34033422

3423+
def log_warning(self, short_message: str, long_message: t.Optional[str] = None) -> None:
3424+
super().log_warning(short_message, long_message, print=not self.warning_capture_only)
3425+
3426+
def log_error(self, message: str) -> None:
3427+
super().log_error(message, print=not self.error_capture_only)
3428+
34043429
def log_success(self, message: str) -> None:
34053430
self._print(message)
34063431

@@ -3427,19 +3452,24 @@ def log_test_results(self, result: ModelTextTestResult, target_dialect: str) ->
34273452

34283453
def log_skipped_models(self, snapshot_names: t.Set[str]) -> None:
34293454
if snapshot_names:
3430-
msg = " " + "\n ".join(snapshot_names)
3431-
self._print(f"**Skipped models**\n\n{msg}")
3455+
self._print(f"**Skipped models**")
3456+
for snapshot_name in snapshot_names:
3457+
self._print(f"* `{snapshot_name}`")
3458+
self._print("")
34323459

34333460
def log_failed_models(self, errors: t.List[NodeExecutionFailedError]) -> None:
34343461
if errors:
3435-
self._print("\n```\nFailed models\n")
3462+
self._print("**Failed models**")
34363463

34373464
error_messages = _format_node_errors(errors)
34383465

34393466
for node_name, msg in error_messages.items():
3440-
self._print(f" **{node_name}**\n\n{msg}")
3467+
self._print(f"* `{node_name}`\n")
3468+
self._print(" ```")
3469+
self._print(msg)
3470+
self._print(" ```")
34413471

3442-
self._print("```\n")
3472+
self._print("")
34433473

34443474
def show_linter_violations(
34453475
self, violations: t.List[RuleViolation], model: Model, is_error: bool = False

sqlmesh/integrations/github/cicd/command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def github(ctx: click.Context, token: str) -> None:
3030
"""Github Action CI/CD Bot. See https://sqlmesh.readthedocs.io/en/stable/integrations/github/ for details"""
3131
# set a larger width because if none is specified, it auto-detects 80 characters when running in GitHub Actions
3232
# which can result in surprise newlines when outputting dates to backfill
33-
set_console(MarkdownConsole(width=1000))
33+
set_console(MarkdownConsole(width=1000, warning_capture_only=True, error_capture_only=True))
3434
ctx.obj["github"] = GithubController(
3535
paths=ctx.obj["paths"],
3636
token=token,

sqlmesh/integrations/github/cicd/controller.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,11 @@ def _get_pr_environment_summary_action_required(
603603

604604
def _get_pr_environment_summary_failure(self, exception: t.Optional[Exception] = None) -> str:
605605
console_output = self._console.consume_captured_output()
606+
failure_msg = ""
606607

607608
if isinstance(exception, PlanError):
608-
failure_msg = f"Plan application failed.\n"
609609
if exception.args and (msg := exception.args[0]) and isinstance(msg, str):
610-
failure_msg += f"\n{msg}\n"
610+
failure_msg += f"*{msg}*\n"
611611
if console_output:
612612
failure_msg += f"\n{console_output}"
613613
elif isinstance(exception, (SQLMeshError, SqlglotError, ValueError)):
@@ -713,6 +713,7 @@ def update_pr_environment(self) -> None:
713713
Creates a PR environment from the logic present in the PR. If the PR contains changes that are
714714
uncategorized, then an error will be raised.
715715
"""
716+
self._console.consume_captured_output() # clear output buffer
716717
self._context.apply(self.pr_plan) # will raise if PR environment creation fails
717718

718719
# update PR info comment

tests/integrations/github/cicd/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _make_function(
117117

118118
orig_console = get_console()
119119
try:
120-
set_console(MarkdownConsole())
120+
set_console(MarkdownConsole(warning_capture_only=True, error_capture_only=True))
121121

122122
return GithubController(
123123
paths=paths,

tests/integrations/github/cicd/test_integration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,8 @@ def test_error_msg_when_applying_plan_with_bug(
15821582
assert GithubCheckConclusion(pr_checks_runs[2]["conclusion"]).is_failure
15831583
assert pr_checks_runs[2]["output"]["title"] == "PR Virtual Data Environment: hello_world_2"
15841584
summary = pr_checks_runs[2]["output"]["summary"].replace("\n", "")
1585-
assert 'Failed models **"memory"."sushi"."waiter_revenue_by_day"**' in summary
1585+
assert '**Skipped models*** `"memory"."sushi"."top_waiters"`' in summary
1586+
assert '**Failed models*** `"memory"."sushi"."waiter_revenue_by_day"`' in summary
15861587
assert 'Binder Error: Referenced column "non_existing_col" not found in FROM clause!' in summary
15871588

15881589
assert "SQLMesh - Prod Plan Preview" in controller._check_run_mapping

0 commit comments

Comments
 (0)