Download .github/scripts/verify_codeowners.py from LevelUp2x/adaptai: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/datasets/LevelUp2x/adaptai/resolve/2147bc06c2ccf123b96f98eace2e6bf6486e7f0e/.github/scripts/verify_codeowners.py
- Command line
-
hf download hf://datasets/LevelUp2x/adaptai@2147bc06c2ccf123b96f98eace2e6bf6486e7f0e/.github/scripts/verify_codeowners.py
-
curl -L -o verify_codeowners.py https://huggingface.co/datasets/LevelUp2x/adaptai/resolve/2147bc06c2ccf123b96f98eace2e6bf6486e7f0e/.github/scripts/verify_codeowners.py
3.97 kB
| #!/usr/bin/env python3 | |
| # CODEOWNERS Verification Script | |
| # Ensures PRs have required approvals from domain owners | |
| 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() | |
| # Skip comments and empty lines | |
| if not line or line.startswith('#'): | |
| continue | |
| # Split into pattern and owners | |
| 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 = [] | |
| # Check each pattern in CODEOWNERS | |
| 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""" | |
| # Convert pattern to regex | |
| regex_pattern = pattern.replace('.', '\.').replace('*', '.*').replace('?', '.') | |
| # Anchor to start of string | |
| if not regex_pattern.startswith('^'): | |
| regex_pattern = '^' + regex_pattern | |
| # Check match | |
| 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) | |
| # Check if all required approvers are in 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: | |
| # TODO: Integrate with GitHub API to get changed files and approvers | |
| print("GitHub API integration not implemented yet") | |
| sys.exit(1) | |
| else: | |
| parser.print_help() | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |