1df917f87SPatrick Williams#!/usr/bin/python3 2df917f87SPatrick Williams 3df917f87SPatrick Williamsimport argparse 4df917f87SPatrick Williamsimport json 5df917f87SPatrick Williamsimport os 6*e310dd91SPatrick Williams 7df917f87SPatrick Williamsfrom sh import ssh # type: ignore 8df917f87SPatrick Williams 9df917f87SPatrick Williams 10df917f87SPatrick Williamsclass subcmd: 11df917f87SPatrick Williams def __init__(self, parser: argparse._SubParsersAction) -> None: 12df917f87SPatrick Williams p = parser.add_parser( 13df917f87SPatrick Williams "dump-gerrit", help="Dump commit data from Gerrit" 14df917f87SPatrick Williams ) 15df917f87SPatrick Williams p.add_argument( 16df917f87SPatrick Williams "--server", 17df917f87SPatrick Williams "-s", 186ed67cb7SPatrick Williams help="Gerrit server SSH alias (default=openbmc.gerrit)", 196ed67cb7SPatrick Williams default="openbmc.gerrit", 20df917f87SPatrick Williams ) 21df917f87SPatrick Williams p.add_argument( 22df917f87SPatrick Williams "--after", 23df917f87SPatrick Williams "-a", 24df917f87SPatrick Williams help="Timestamp for Gerrit 'after:' directive (ex. YYYY-MM-DD)", 25df917f87SPatrick Williams required=True, 26df917f87SPatrick Williams ) 27df917f87SPatrick Williams p.set_defaults(cmd=self) 28df917f87SPatrick Williams 29df917f87SPatrick Williams def run(self, args: argparse.Namespace) -> int: 30df917f87SPatrick Williams data_path: str = args.dir 31df917f87SPatrick Williams 32df917f87SPatrick Williams if os.path.exists(data_path) and not os.path.isdir(data_path): 33df917f87SPatrick Williams print(f"Path {data_path} exists but is not a directory.") 34df917f87SPatrick Williams return 1 35df917f87SPatrick Williams 36df917f87SPatrick Williams if not os.path.exists(data_path): 37df917f87SPatrick Williams os.mkdir(data_path) 38df917f87SPatrick Williams 39df917f87SPatrick Williams query = list( 40df917f87SPatrick Williams ssh( 41df917f87SPatrick Williams args.server, 42df917f87SPatrick Williams "gerrit", 43df917f87SPatrick Williams "query", 44df917f87SPatrick Williams "--format=json", 45df917f87SPatrick Williams "--patch-sets", 46df917f87SPatrick Williams "--comments", 47df917f87SPatrick Williams "--files", 48df917f87SPatrick Williams "--no-limit", 49df917f87SPatrick Williams f"after:{args.after} AND delta:>=10", 50df917f87SPatrick Williams ) 51df917f87SPatrick Williams )[ 52df917f87SPatrick Williams 0:-1 53df917f87SPatrick Williams ] # The last result from Gerrit is a query stat result. 54df917f87SPatrick Williams 55df917f87SPatrick Williams for change in query: 56df917f87SPatrick Williams data = json.loads(change) 57df917f87SPatrick Williams formatted_data = json.dumps(data, indent=4) 58df917f87SPatrick Williams 59df917f87SPatrick Williams with open( 60df917f87SPatrick Williams os.path.join(data_path, str(data["number"]) + ".json"), 61df917f87SPatrick Williams "w", 62df917f87SPatrick Williams ) as file: 63df917f87SPatrick Williams file.write(formatted_data) 64df917f87SPatrick Williams return 0 65