|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +from launchable.app import Application |
| 7 | +from launchable.commands.helper import find_or_create_session |
| 8 | +from launchable.commands.test_path_writer import TestPathWriter |
| 9 | +from launchable.utils.click import ignorable_error |
| 10 | +from launchable.utils.env_keys import REPORT_ERROR_KEY |
| 11 | +from launchable.utils.launchable_client import LaunchableClient |
| 12 | +from launchable.utils.tracking import Tracking, TrackingClient |
| 13 | + |
| 14 | +from ...utils.commands import Command |
| 15 | + |
| 16 | + |
| 17 | +@click.group(help="Early flake detection") |
| 18 | +@click.option( |
| 19 | + '--session', |
| 20 | + 'session', |
| 21 | + help='In the format builds/<build-name>/test_sessions/<test-session-id>', |
| 22 | + type=str, |
| 23 | + required=True |
| 24 | +) |
| 25 | +@click.option( |
| 26 | + '--confidence', |
| 27 | + help='Confidence level for flake detection', |
| 28 | + type=click.Choice(['low', 'medium', 'high'], case_sensitive=False), |
| 29 | + required=True, |
| 30 | +) |
| 31 | +@click.pass_context |
| 32 | +def flake_detection(ctx, confidence, session): |
| 33 | + tracking_client = TrackingClient(Command.FLAKE_DETECTION, app=ctx.obj) |
| 34 | + client = LaunchableClient(app=ctx.obj, tracking_client=tracking_client, test_runner=ctx.invoked_subcommand) |
| 35 | + session_id = None |
| 36 | + try: |
| 37 | + session_id = find_or_create_session( |
| 38 | + context=ctx, |
| 39 | + session=session, |
| 40 | + build_name=None, |
| 41 | + tracking_client=tracking_client |
| 42 | + ) |
| 43 | + except click.UsageError as e: |
| 44 | + click.echo(click.style(str(e), fg="red"), err=True) |
| 45 | + sys.exit(1) |
| 46 | + except Exception as e: |
| 47 | + tracking_client.send_error_event( |
| 48 | + event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR, |
| 49 | + stack_trace=str(e), |
| 50 | + ) |
| 51 | + if os.getenv(REPORT_ERROR_KEY): |
| 52 | + raise e |
| 53 | + else: |
| 54 | + click.echo(ignorable_error(e), err=True) |
| 55 | + if session_id is None: |
| 56 | + return |
| 57 | + |
| 58 | + class FlakeDetection(TestPathWriter): |
| 59 | + def __init__(self, app: Application): |
| 60 | + super(FlakeDetection, self).__init__(app=app) |
| 61 | + |
| 62 | + def run(self): |
| 63 | + test_paths = [] |
| 64 | + try: |
| 65 | + res = client.request( |
| 66 | + "get", |
| 67 | + "retry/flake-detection", |
| 68 | + params={ |
| 69 | + "confidence": confidence.upper(), |
| 70 | + "session-id": os.path.basename(session_id), |
| 71 | + "test-runner": ctx.invoked_subcommand}) |
| 72 | + res.raise_for_status() |
| 73 | + test_paths = res.json().get("testPaths", []) |
| 74 | + except Exception as e: |
| 75 | + tracking_client.send_error_event( |
| 76 | + event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR, |
| 77 | + stack_trace=str(e), |
| 78 | + ) |
| 79 | + if os.getenv(REPORT_ERROR_KEY): |
| 80 | + raise e |
| 81 | + else: |
| 82 | + click.echo(ignorable_error(e), err=True) |
| 83 | + if test_paths: |
| 84 | + self.print(test_paths) |
| 85 | + |
| 86 | + ctx.obj = FlakeDetection(app=ctx.obj) |
0 commit comments