|
| 1 | +import json |
| 2 | +import sys |
| 3 | +from http import HTTPStatus |
| 4 | +from typing import Annotated |
| 5 | + |
| 6 | +import click |
| 7 | + |
| 8 | +import smart_tests.args4p.typer as typer |
| 9 | +from smart_tests.args4p.converters import intType |
| 10 | + |
| 11 | +from ... import args4p |
| 12 | +from ...app import Application |
| 13 | +from ...utils.smart_tests_client import SmartTestsClient |
| 14 | +from ...utils.typer_types import DateTimeWithTimezone, parse_datetime_with_timezone, validate_iso_week |
| 15 | + |
| 16 | + |
| 17 | +@args4p.command(help="View never failing test data with weekly scores") |
| 18 | +def never_failing_tests( |
| 19 | + app: Application, |
| 20 | + year_week: Annotated[str | None, typer.Option( |
| 21 | + "--year-week", |
| 22 | + help="Specific ISO week for never failing tests (e.g., '2026-W15')", |
| 23 | + type=validate_iso_week, |
| 24 | + metavar="YYYY-Www" |
| 25 | + )] = None, |
| 26 | + weeks: Annotated[int | None, typer.Option( |
| 27 | + "--weeks", |
| 28 | + help="Number of weeks to retrieve (default: 1, max: 12)", |
| 29 | + type=intType(min=1, max=12), |
| 30 | + metavar="N" |
| 31 | + )] = None, |
| 32 | + from_date: Annotated[DateTimeWithTimezone | None, typer.Option( |
| 33 | + "--from", |
| 34 | + help="Start date/time (ISO 8601 format, e.g., '2026-04-08' or '2026-04-08T00:00:00Z')", |
| 35 | + type=parse_datetime_with_timezone, |
| 36 | + metavar="DATE" |
| 37 | + )] = None, |
| 38 | + to_date: Annotated[DateTimeWithTimezone | None, typer.Option( |
| 39 | + "--to", |
| 40 | + help="End date/time (ISO 8601 format, e.g., '2026-04-14' or '2026-04-14T23:59:59Z')", |
| 41 | + type=parse_datetime_with_timezone, |
| 42 | + metavar="DATE" |
| 43 | + )] = None, |
| 44 | + test_path: Annotated[str | None, typer.Option( |
| 45 | + "--test-path", |
| 46 | + help="Test path filter (full path string, e.g., 'class=MyTest')", |
| 47 | + metavar="NAME" |
| 48 | + )] = None, |
| 49 | + limit: Annotated[int | None, typer.Option( |
| 50 | + "--limit", |
| 51 | + help="Max results to return per week (default: 50, max: 500)", |
| 52 | + type=intType(min=1, max=500), |
| 53 | + metavar="N" |
| 54 | + )] = None, |
| 55 | +): |
| 56 | + """View never failing tests with weekly trends""" |
| 57 | + client = SmartTestsClient(app=app) |
| 58 | + |
| 59 | + params = {} |
| 60 | + if year_week: |
| 61 | + params["year-week"] = year_week |
| 62 | + if weeks: |
| 63 | + params["weeks"] = str(weeks) |
| 64 | + if from_date: |
| 65 | + params["from"] = from_date.datetime().strftime("%Y-%m-%d") |
| 66 | + if to_date: |
| 67 | + params["to"] = to_date.datetime().strftime("%Y-%m-%d") |
| 68 | + if test_path: |
| 69 | + params["test-path"] = test_path |
| 70 | + if limit: |
| 71 | + params["limit"] = str(limit) |
| 72 | + |
| 73 | + try: |
| 74 | + res = client.request("get", "view/never-failing-tests", params=params) |
| 75 | + |
| 76 | + if res.status_code == HTTPStatus.NOT_FOUND: |
| 77 | + click.secho( |
| 78 | + "No never failing test data found. Check your filters and try again.", |
| 79 | + fg='yellow', err=True |
| 80 | + ) |
| 81 | + sys.exit(1) |
| 82 | + |
| 83 | + res.raise_for_status() |
| 84 | + response_json = res.json() |
| 85 | + |
| 86 | + click.echo(json.dumps(response_json, indent=2)) |
| 87 | + |
| 88 | + except Exception as e: |
| 89 | + client.print_exception_and_recover( |
| 90 | + e, |
| 91 | + "Warning: failed to retrieve never failing tests from server" |
| 92 | + ) |
| 93 | + sys.exit(1) |
0 commit comments