I have a CODEOWNERS file that is complicated, and I want to be able to test how the wildcards and order of precedence works. But I don't know how to be able to test that offline, prior to deploying the change and making actual pull requests in github.
So suppose I have a CODEOWNERS file, and then I say that I touch file1, file2, file3 - is there a way to evaluate or parse the codeowners file offline, and based on that figure out which owners will need to approve a PR touching these files?
(Adding this only because of the "What have you tried" section of the wizard):
I asked same question of copilot, which suggested either using this project:
go install github/mszostok/codeowners@latest
codeowners check file1 file2 file3
or using a python script that it spit out. I have no idea if this script will work, though.
import fnmatch
def parse_codeowners(file_path):
rules = []
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#'): # Ignore comments and empty lines
parts = line.split()
pattern = parts[0]
owners = parts[1:]
rules.append((pattern, owners))
return rules
def find_owners(rules, file_path):
owners = []
for pattern, rule_owners in rules:
if fnmatch.fnmatch(file_path, pattern):
owners = rule_owners # The most specific rule takes precedence
return owners
# Load CODEOWNERS file
codeowners_file = 'CODEOWNERS' # Adjust path as needed
rules = parse_codeowners(codeowners_file)
# Test files
test_files = ['file1', 'file2', 'file3']
for test_file in test_files:
owners = find_owners(rules, test_file)
print(f"File: {test_file}, Owners: {', '.join(owners) if owners else 'None'}")
Edit: I'm pretty sure the script doesn't work after a cursory test of it, but at least it should illustrate what I'm trying to do.