|
| 1 | +"""CLI command for running QA checks on connectors.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from pathlib import Path |
| 5 | +from typing import List, Optional |
| 6 | + |
| 7 | +import rich_click as click |
| 8 | + |
| 9 | +from airbyte_cdk.cli.airbyte_cdk._util import resolve_connector_name_and_directory |
| 10 | +from airbyte_cdk.qa.checks import ENABLED_CHECKS |
| 11 | +from airbyte_cdk.qa.connector import Connector |
| 12 | +from airbyte_cdk.qa.models import CheckResult, CheckStatus, Report |
| 13 | + |
| 14 | + |
| 15 | +@click.command(name="pre-release-check") |
| 16 | +@click.option( |
| 17 | + "-c", |
| 18 | + "--check", |
| 19 | + "selected_checks", |
| 20 | + multiple=True, |
| 21 | + type=click.Choice([type(check).__name__ for check in ENABLED_CHECKS]), |
| 22 | + help="The name of the check to run. If not provided, all checks will be run.", |
| 23 | +) |
| 24 | +@click.option( |
| 25 | + "--connector-name", |
| 26 | + type=str, |
| 27 | + help="Name of the connector to check. Ignored if --connector-directory is provided.", |
| 28 | +) |
| 29 | +@click.option( |
| 30 | + "--connector-directory", |
| 31 | + type=click.Path(exists=True, file_okay=False, path_type=Path), |
| 32 | + help="Path to the connector directory.", |
| 33 | +) |
| 34 | +@click.option( |
| 35 | + "-r", |
| 36 | + "--report-path", |
| 37 | + "report_path", |
| 38 | + type=click.Path(file_okay=True, path_type=Path, writable=True, dir_okay=False), |
| 39 | + help="The path to the report file to write the results to as JSON.", |
| 40 | +) |
| 41 | +def pre_release_check( |
| 42 | + selected_checks: List[str], |
| 43 | + connector_name: Optional[str] = None, |
| 44 | + connector_directory: Optional[Path] = None, |
| 45 | + report_path: Optional[Path] = None, |
| 46 | +) -> None: |
| 47 | + """Run pre-release checks on a connector. |
| 48 | +
|
| 49 | + This command runs quality assurance checks on a connector to ensure it meets |
| 50 | + Airbyte's standards for release. The checks include: |
| 51 | +
|
| 52 | + - Documentation checks |
| 53 | + - Metadata checks |
| 54 | + - Packaging checks |
| 55 | + - Security checks |
| 56 | + - Asset checks |
| 57 | + - Testing checks |
| 58 | +
|
| 59 | + If no connector name or directory is provided, we will look within the current working |
| 60 | + directory. If the current working directory is not a connector directory (e.g. starting |
| 61 | + with 'source-') and no connector name or path is provided, the process will fail. |
| 62 | + """ |
| 63 | + connector_name, connector_directory = resolve_connector_name_and_directory( |
| 64 | + connector_name=connector_name, |
| 65 | + connector_directory=connector_directory, |
| 66 | + ) |
| 67 | + |
| 68 | + connector = Connector(connector_name, connector_directory) |
| 69 | + |
| 70 | + checks_to_run = [check for check in ENABLED_CHECKS if type(check).__name__ in selected_checks] if selected_checks else ENABLED_CHECKS |
| 71 | + |
| 72 | + check_results = [] |
| 73 | + for check in checks_to_run: |
| 74 | + result = check.run(connector) |
| 75 | + check_results.append(result) |
| 76 | + status_color = { |
| 77 | + CheckStatus.PASSED: "green", |
| 78 | + CheckStatus.FAILED: "red", |
| 79 | + CheckStatus.SKIPPED: "yellow", |
| 80 | + }[result.status] |
| 81 | + click.echo(f"[{status_color}]{result.status.value}[/{status_color}] {check.name}: {result.message}") |
| 82 | + |
| 83 | + if report_path: |
| 84 | + Report(check_results=check_results).write(report_path) |
| 85 | + click.echo(f"Report written to {report_path}") |
| 86 | + |
| 87 | + failed_checks = [check_result for check_result in check_results if check_result.status is CheckStatus.FAILED] |
| 88 | + if failed_checks: |
| 89 | + raise click.ClickException(f"{len(failed_checks)} checks failed") |
0 commit comments