|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import os |
| 6 | + |
| 7 | +from dotenv import load_dotenv |
| 8 | + |
| 9 | +from .clickhouse_client_helper import CHCliFactory |
| 10 | +from .github_client_helper import GHClientFactory |
| 11 | +from .testers.autorevert import autorevert_checker |
| 12 | +from .testers.do_restart import do_restart_workflow |
| 13 | +from .testers.restart_checker import workflow_restart_checker |
| 14 | + |
| 15 | + |
| 16 | +def setup_logging(log_level: str) -> None: |
| 17 | + """Set up logging configuration.""" |
| 18 | + numeric_level = getattr(logging, log_level.upper(), None) |
| 19 | + if not isinstance(numeric_level, int): |
| 20 | + raise ValueError(f"Invalid log level: {log_level}") |
| 21 | + logging.basicConfig(level=numeric_level) |
| 22 | + |
| 23 | + |
| 24 | +def get_opts() -> argparse.Namespace: |
| 25 | + parser = argparse.ArgumentParser() |
| 26 | + |
| 27 | + # General options and configurations |
| 28 | + parser.add_argument( |
| 29 | + "--log-level", |
| 30 | + default=os.environ.get("LOG_LEVEL", "INFO"), |
| 31 | + choices=["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
| 32 | + help="Set the logging level for the application.", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--clickhouse-host", default=os.environ.get("CLICKHOUSE_HOST", "") |
| 36 | + ) |
| 37 | + parser.add_argument( |
| 38 | + "--clickhouse-port", |
| 39 | + type=int, |
| 40 | + default=int(os.environ.get("CLICKHOUSE_PORT", "8443")), |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + "--clickhouse-username", default=os.environ.get("CLICKHOUSE_USERNAME", "") |
| 44 | + ) |
| 45 | + parser.add_argument( |
| 46 | + "--clickhouse-password", default=os.environ.get("CLICKHOUSE_PASSWORD", "") |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + "--clickhouse-database", |
| 50 | + default=os.environ.get("CLICKHOUSE_DATABASE", "default"), |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + "--github-access-token", default=os.environ.get("GITHUB_TOKEN", "") |
| 54 | + ) |
| 55 | + parser.add_argument("--github-app-id", default=os.environ.get("GITHUB_APP_ID", "")) |
| 56 | + parser.add_argument( |
| 57 | + "--github-app-secret", default=os.environ.get("GITHUB_APP_SECRET", "") |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "--github-installation-id", |
| 61 | + type=int, |
| 62 | + default=int(os.environ.get("GITHUB_INSTALLATION_ID", "0")), |
| 63 | + ) |
| 64 | + |
| 65 | + # no subcommand runs the lambda flow |
| 66 | + subparsers = parser.add_subparsers(dest="subcommand") |
| 67 | + |
| 68 | + # autorevert subcommand |
| 69 | + workflow_parser = subparsers.add_parser( |
| 70 | + "autorevert", help="Analyze workflows looking for autorevert patterns" |
| 71 | + ) |
| 72 | + workflow_parser.add_argument( |
| 73 | + "workflows", |
| 74 | + nargs="+", |
| 75 | + help="Workflow name(s) to analyze - single name or comma/space separated" |
| 76 | + + ' list (e.g., "pull" or "pull,trunk,inductor")', |
| 77 | + ) |
| 78 | + workflow_parser.add_argument( |
| 79 | + "--hours", type=int, default=48, help="Lookback window in hours (default: 48)" |
| 80 | + ) |
| 81 | + workflow_parser.add_argument( |
| 82 | + "--verbose", |
| 83 | + "-v", |
| 84 | + action="store_true", |
| 85 | + help="Show detailed output including commit summaries", |
| 86 | + ) |
| 87 | + |
| 88 | + # workflow-restart-checke subcommand |
| 89 | + workflow_restart_parser = subparsers.add_parser( |
| 90 | + "workflow-restart-checke", help="Check for restarted workflows" |
| 91 | + ) |
| 92 | + workflow_restart_parser.add_argument( |
| 93 | + "workflow", |
| 94 | + help="Workflow file name (e.g., trunk.yml)", |
| 95 | + ) |
| 96 | + workflow_restart_parser.add_argument( |
| 97 | + "--commit", |
| 98 | + help="Check specific commit SHA", |
| 99 | + ) |
| 100 | + workflow_restart_parser.add_argument( |
| 101 | + "--days", |
| 102 | + type=int, |
| 103 | + default=7, |
| 104 | + help="If no `--commit` specified, look back days for bulk query (default: 7)", |
| 105 | + ) |
| 106 | + |
| 107 | + # do-restart subcommand |
| 108 | + do_restart_parser = subparsers.add_parser( |
| 109 | + "do-restart", help="Restart a workflow for a specific commit" |
| 110 | + ) |
| 111 | + do_restart_parser.add_argument( |
| 112 | + "workflow", |
| 113 | + help="Workflow file name to restart (e.g., trunk.yml)", |
| 114 | + ) |
| 115 | + do_restart_parser.add_argument( |
| 116 | + "commit", |
| 117 | + help="Commit SHA to restart the workflow for", |
| 118 | + ) |
| 119 | + |
| 120 | + return parser.parse_args() |
| 121 | + |
| 122 | + |
| 123 | +def main(*args, **kwargs) -> None: |
| 124 | + load_dotenv() |
| 125 | + opts = get_opts() |
| 126 | + setup_logging(opts.log_level) |
| 127 | + CHCliFactory.setup_client( |
| 128 | + opts.clickhouse_host, |
| 129 | + opts.clickhouse_port, |
| 130 | + opts.clickhouse_username, |
| 131 | + opts.clickhouse_password, |
| 132 | + opts.clickhouse_database, |
| 133 | + ) |
| 134 | + GHClientFactory.setup_client( |
| 135 | + opts.github_app_id, |
| 136 | + opts.github_app_secret, |
| 137 | + opts.github_installation_id, |
| 138 | + opts.github_access_token, |
| 139 | + ) |
| 140 | + |
| 141 | + if not CHCliFactory().connection_test(): |
| 142 | + raise RuntimeError( |
| 143 | + "ClickHouse connection test failed. Please check your configuration." |
| 144 | + ) |
| 145 | + |
| 146 | + if opts.subcommand == "lambda": |
| 147 | + print("TODO: run lambda flow") |
| 148 | + elif opts.subcommand == "workflows": |
| 149 | + autorevert_checker(opts.workflows, hours=opts.hours, verbose=opts.verbose) |
| 150 | + elif opts.subcommand == "workflow-restart-checker": |
| 151 | + workflow_restart_checker(opts.workflow, commit=opts.commit, days=opts.days) |
| 152 | + elif opts.subcommand == "do-restart": |
| 153 | + do_restart_workflow(opts.workflow, commit=opts.commit) |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main() |
0 commit comments