| | |
| | |
| | |
| |
|
| | import argparse |
| | import json |
| | import os |
| | import sys |
| | from pathlib import Path |
| | from typing import Dict, List |
| |
|
| | class MergeQueueManager: |
| | def __init__(self): |
| | self.queue_dir = Path("/data/adaptai/.github/queue") |
| | self.queue_dir.mkdir(exist_ok=True) |
| | |
| | self.queues = { |
| | 'a': self.queue_dir / "class_a.json", |
| | 'b': self.queue_dir / "class_b.json", |
| | 'c': self.queue_dir / "class_c.json" |
| | } |
| | |
| | |
| | for queue_file in self.queues.values(): |
| | if not queue_file.exists(): |
| | queue_file.write_text(json.dumps([])) |
| | |
| | def add_to_queue(self, pr_number: int, pr_class: str) -> None: |
| | """Add PR to the appropriate queue""" |
| | queue_file = self.queues[pr_class] |
| | queue = json.loads(queue_file.read_text()) |
| | |
| | |
| | if pr_number not in queue: |
| | queue.append(pr_number) |
| | queue_file.write_text(json.dumps(queue)) |
| | print(f"Added PR #{pr_number} to {pr_class.upper()} queue") |
| | else: |
| | print(f"PR #{pr_number} already in {pr_class.upper()} queue") |
| | |
| | def remove_from_queue(self, pr_number: int) -> None: |
| | """Remove PR from all queues""" |
| | for pr_class, queue_file in self.queues.items(): |
| | queue = json.loads(queue_file.read_text()) |
| | if pr_number in queue: |
| | queue.remove(pr_number) |
| | queue_file.write_text(json.dumps(queue)) |
| | print(f"Removed PR #{pr_number} from {pr_class.upper()} queue") |
| | |
| | def get_next_batch(self, pr_class: str, batch_size: int) -> List[int]: |
| | """Get next batch of PRs to process""" |
| | queue_file = self.queues[pr_class] |
| | queue = json.loads(queue_file.read_text()) |
| | |
| | if not queue: |
| | return [] |
| | |
| | |
| | batch = queue[:batch_size] |
| | return batch |
| | |
| | def process_batch(self, pr_class: str, batch: List[int]) -> None: |
| | """Process a batch of PRs and remove from queue""" |
| | if not batch: |
| | return |
| | |
| | queue_file = self.queues[pr_class] |
| | queue = json.loads(queue_file.read_text()) |
| | |
| | |
| | new_queue = [pr for pr in queue if pr not in batch] |
| | queue_file.write_text(json.dumps(new_queue)) |
| | |
| | print(f"Processed batch from {pr_class.upper()} queue: {batch}") |
| | print(f"Remaining in queue: {new_queue}") |
| | |
| | def get_queue_status(self) -> Dict[str, List[int]]: |
| | """Get status of all queues""" |
| | status = {} |
| | for pr_class, queue_file in self.queues.items(): |
| | queue = json.loads(queue_file.read_text()) |
| | status[pr_class] = queue |
| | return status |
| |
|
| | def main(): |
| | parser = argparse.ArgumentParser(description="Merge Queue Manager") |
| | parser.add_argument("--pr", type=int, help="PR number") |
| | parser.add_argument("--class", dest="pr_class", choices=['a', 'b', 'c'], help="PR risk class") |
| | parser.add_argument("--action", choices=['add', 'remove', 'status', 'process'], help="Action to perform") |
| | parser.add_argument("--batch-size", type=int, default=1, help="Batch size for processing") |
| | |
| | args = parser.parse_args() |
| | manager = MergeQueueManager() |
| | |
| | if args.action == "add" and args.pr and args.pr_class: |
| | manager.add_to_queue(args.pr, args.pr_class) |
| | elif args.action == "remove" and args.pr: |
| | manager.remove_from_queue(args.pr) |
| | elif args.action == "status": |
| | status = manager.get_queue_status() |
| | print(json.dumps(status, indent=2)) |
| | elif args.action == "process" and args.pr_class: |
| | batch = manager.get_next_batch(args.pr_class, args.batch_size) |
| | if batch: |
| | print(f"Processing batch: {batch}") |
| | |
| | |
| | manager.process_batch(args.pr_class, batch) |
| | else: |
| | print(f"No PRs in {args.pr_class.upper()} queue") |
| | else: |
| | parser.print_help() |
| |
|
| | if __name__ == "__main__": |
| | main() |