| |
| |
| |
|
|
| import argparse |
| import json |
| import os |
| import re |
| import sys |
| from pathlib import Path |
| from typing import Dict, List, Set |
|
|
| class CODEOWNERSVerifier: |
| def __init__(self): |
| self.codeowners_path = Path("/data/adaptai/CODEOWNERS") |
| self.owners_map = self._parse_codeowners() |
| |
| def _parse_codeowners(self) -> Dict[str, List[str]]: |
| """Parse CODEOWNERS file into path -> owners mapping""" |
| owners_map = {} |
| |
| with open(self.codeowners_path, 'r') as f: |
| for line in f: |
| line = line.strip() |
| |
| if not line or line.startswith('#'): |
| continue |
| |
| |
| parts = line.split() |
| if len(parts) < 2: |
| continue |
| |
| pattern = parts[0] |
| owners = parts[1:] |
| owners_map[pattern] = owners |
| |
| return owners_map |
| |
| def get_required_approvers(self, changed_files: List[str]) -> Set[str]: |
| """Get required approvers based on changed files""" |
| required_approvers = set() |
| |
| for file_path in changed_files: |
| file_approvers = self._get_file_approvers(file_path) |
| required_approvers.update(file_approvers) |
| |
| return required_approvers |
| |
| def _get_file_approvers(self, file_path: str) -> List[str]: |
| """Get approvers for a specific file""" |
| approvers = [] |
| |
| |
| for pattern, owners in self.owners_map.items(): |
| if self._matches_pattern(pattern, file_path): |
| approvers.extend(owners) |
| |
| return approvers |
| |
| def _matches_pattern(self, pattern: str, file_path: str) -> bool: |
| """Check if file matches CODEOWNERS pattern""" |
| |
| regex_pattern = pattern.replace('.', '\.').replace('*', '.*').replace('?', '.') |
| |
| |
| if not regex_pattern.startswith('^'): |
| regex_pattern = '^' + regex_pattern |
| |
| |
| return re.match(regex_pattern, file_path) is not None |
| |
| def verify_approvals(self, changed_files: List[str], actual_approvers: List[str]) -> bool: |
| """Verify that actual approvers cover required approvers""" |
| required = self.get_required_approvers(changed_files) |
| actual_set = set(actual_approvers) |
| |
| |
| missing = required - actual_set |
| |
| if missing: |
| print(f"Missing approvals from: {', '.join(missing)}") |
| print(f"Required: {', '.join(required)}") |
| print(f"Actual: {', '.join(actual_set)}") |
| return False |
| |
| print(f"All required approvals present: {', '.join(required)}") |
| return True |
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="CODEOWNERS Verification") |
| parser.add_argument("--changed-files", nargs='+', help="List of changed files") |
| parser.add_argument("--approvers", nargs='+', help="List of actual approvers") |
| parser.add_argument("--pr-number", type=int, help="PR number for GitHub API") |
| |
| args = parser.parse_args() |
| |
| verifier = CODEOWNERSVerifier() |
| |
| if args.changed_files and args.approvers: |
| success = verifier.verify_approvals(args.changed_files, args.approvers) |
| sys.exit(0 if success else 1) |
| elif args.pr_number: |
| |
| print("GitHub API integration not implemented yet") |
| sys.exit(1) |
| else: |
| parser.print_help() |
| sys.exit(1) |
|
|
| if __name__ == "__main__": |
| main() |