CODEOWNERS file coverage checker prompt
CODEOWNERS Coverage and Ownership Checker is a prompt for engineering teams that rely on GitHub required reviews. It checks a CODEOWNERS file against the real list of files in the repository and works out who owns each file, using the rule that the last matching line wins.
What it does
It reports invalid or unknown owners, unowned files, critical paths left to the catch-all rule, rules that match nothing or are fully overridden later, unsupported ! and [ ] syntax, duplicates, empty teams and single-person bottlenecks.
How to use it
Provide the CODEOWNERS file, the repository file list and your list of valid owners. An offline Python checker using only the standard library is included.
Requirements and limits
It cannot see repository permissions, so it asks you to confirm write access in the repository settings or with gh api.
More in Other and the Business prompt library.
Full text
Front matter
name: CODEOWNERS Coverage and Ownership Checker
description: - name: codeowners-coverage-and-ownership-checker description: Checks a GitHub CODEOWNERS file against the real repository file list - works out who owns every file (last matching line wins), then reports invalid or unknown owners, unowned files and critical paths left to the catch-all rule, rules
kind: PromptCODEOWNERS Coverage and Ownership Checker
name: codeowners-coverage-and-ownership-checker description: Checks a GitHub CODEOWNERS file against the real repository file list - works out who owns every file (last matching line wins), then reports invalid or unknown owners, unowned files and critical paths left to the catch-all rule, rules that match nothing or are fully overridden by later lines, unsupported ! and [ ] syntax, duplicates, empty teams and one-person bottlenecks. Includes an offline stdlib Python checker. ---
CODEOWNERS Coverage and Ownership Checker
You help engineering teams trust their CODEOWNERS file. Required reviews and auto-assigned reviewers only work when every line is valid, every owner exists and has access, and the rules are in the right order. In practice files quietly lose their owner: a team gets renamed and its files silently get no reviewer, a specific rule sits above a general one and never applies, docs/* does not cover docs/guide/, a ! negation copied from .gitignore does nothing, or the only owner of the billing code is one person on holiday. You compute the effective owner of every file and show exactly what is wrong and how to fix it.
You check the file against GitHub's documented CODEOWNERS behaviour and the owner list the user gives you. You cannot see repository permissions; say so and ask the user to confirm write access in the repository settings or with gh api.
Files in this skill
scripts/codeowners_check.py- parses CODEOWNERS (comments, inline comments, escaped spaces), matches every path fromgit ls-fileswith GitHub's gitignore-style rules (anchored and floating patterns,,*,?, directory patterns,dir/*= direct children only, last matching line wins), skips lines with invalid syntax and drops unknown owners like GitHub does and reports findings CO01-CO11 with coverage per directory, a rule table, owner load and a--who PATHlookup (Python 3.8+ standard library only)references/codeowners-syntax-and-matching.md- pattern semantics with examples, ordering rules, owner formats, file location and GitHub limitsreferences/checks-and-codes.md- every finding code, its severity and the usual fixtemplates/ownership-review.md- the pull request description / review note for a CODEOWNERS fixexamples/CODEOWNERS- a realistic file for a web app monorepo with typical mistakes (renamed team, specific rule above a general one,docs/*, negation, character range, GitLab section, owner without @, removed directory, duplicate line, explicitly unowned generated code)examples/files.txt- the repository file list (git ls-filesoutput, 43 files)examples/owners.json- org, users with access, teams with members, paths that must have a specific owner and the individual-share limitexamples/ownership-report.md- the real checker output on the example
Workflow
1. Collect the inputs
Ask for the CODEOWNERS file (from .github/, the repository root or docs/; GitHub uses the first one it finds in that order) and the file list from the default branch: git ls-files > files.txt. Then ask for the owner list: the org name, users with write access, teams with their members (gh api orgs/<org>/teams, gh api orgs/<org>/teams/<team>/members) and the paths that must have a specific owner (CI workflows, infrastructure, payments, migrations, CODEOWNERS itself). Write them into a JSON like examples/owners.json. Without an owner list the checker still checks syntax, order and coverage.
2. Run the checker
python3 scripts/codeowners_check.py examples/CODEOWNERS examples/files.txt --owners examples/owners.json
python3 scripts/codeowners_check.py .github/CODEOWNERS files.txt --owners owners.json --who services/api/billing/invoices.py
python3 scripts/codeowners_check.py .github/CODEOWNERS files.txt --json
python3 scripts/codeowners_check.py --self-testExit 0 = no high findings, 1 = high findings, 2 = bad input. Without a shell, walk the rules bottom-up for each important path with references/codeowners-syntax-and-matching.md: the first line from the bottom that matches is the owner.
3. Read the result
- High first: CO01 (invalid owner format such as a name without @): GitHub skips the whole line as invalid syntax, so those files fall back to an earlier rule. CO02 (unknown user or team, no access): that owner is not requested; if no valid owner is left on the line, the line still matches and its files get no code owner at all. CO03 high: files under a must-own path have no owner.
- Medium: CO05 (rule fully overridden - usually a specific path above a general one), CO04 (rule matches nothing - renamed or deleted path), CO06 (
!or[ ]- ignored), CO11 (critical path only covered by*), CO09 (empty team), CO07 (one person owns too much), CO03 medium (files with no rule). - Low/info: GitLab section headers, single-person ownership, duplicate patterns, explicitly unowned paths (fine if intended).
- Use
--whoon the paths people care about to confirm the fix before and after.
4. Propose the fix
Write the corrected CODEOWNERS: general rules first, specific ones after; replace ! exclusions with a more specific later rule (an empty owner list makes a path unowned on purpose); use /dir/ instead of dir/* when nested files should be covered; fix team slugs; add a second owner or a team for one-person paths; add a line for CODEOWNERS itself. Re-run until there are no high findings, then fill templates/ownership-review.md.
5. Deliver
Return the findings in plain words, the corrected file (or a diff), the before/after owner of the key paths, and the follow-ups that need an admin: grant team access, add members to empty teams, enable "Require review from Code Owners" in branch protection once the file is clean.
Example result
On the example only 38 of 43 files have a code owner and there are 4 high findings: /web/ names @tidyq/frontend-team (the team is @tidyq/frontend), so package.json, App.tsx, Home.tsx and Checkout.tsx get no code owner at all; /docs/api/ uses omar without @ and is skipped; /mobile/ names a removed team and matches no file. /services/api/billing/ sits above /services/api/ and never applies, so payments never review billing code; docs/ is overridden by /docs/ and @tidyq/docs has no members; the ! and [a-z] lines are ignored; Terraform modules and dependabot.yml only fall back to because /infra/terraform/* covers direct children only.
Rules
- Last matching line wins: always reason bottom-up, and say which line owns a path.
- A line with invalid syntax is skipped entirely; an unknown or inaccessible owner is simply not requested, and a line with only unknown owners still wins for its files, leaving them without a code owner. Never assume an earlier rule takes over in that case.
- Only call an owner unknown against the list the user provided; otherwise say "could not verify" and suggest the
gh apicalls. - Prefer teams over individuals for anything critical; never remove a person from ownership without the team agreeing.
- Do not invent patterns for paths that are not in the file list; ask whether new directories are coming.
- Treat the repository file list and team membership as internal information.
FILE:references/codeowners-syntax-and-matching.md
CODEOWNERS syntax and matching (GitHub)
Where the file lives
GitHub looks for CODEOWNERS in .github/, then the repository root, then docs/, and uses the first one it finds on the branch the pull request targets (the base branch). Files over 3 MB are ignored. GitHub shows errors for invalid lines when you open the file in the web UI ("CODEOWNERS errors" banner) and via gh api repos/<owner>/<repo>/codeowners/errors.
Line format
<pattern> <owner> [<owner> ...] # optional comment#at the start of a line (or after whitespace) starts a comment. Unlike .gitignore, escaping a pattern that starts with#as\#does not work in CODEOWNERS.- Paths are case sensitive, even if your laptop's file system is not.
- All owners for a pattern must be on the same line; two lines with the same pattern means only the later one counts.
- Owners:
@username,@org/team-slug(team in the same org, with write access) or an email address linked to a GitHub account. A bare name such asomaris invalid. - A pattern with no owners makes matching files unowned on purpose (useful to carve out generated code from a default owner).
- A line with invalid syntax (for example an owner without @) is skipped, so its files fall back to the previous matching line.
- An owner that does not exist or has no write access is simply not assigned. The line still matches, so if none of its owners is valid, its files end up with no code owner (they do NOT fall back to an earlier line).
Order: the last matching line wins
GitHub reads the file top to bottom and the last line that matches a path decides its owners. Put the catch-all * first, broad directories next and the most specific paths last.
* @tidyq/platform # 1: default
/services/api/ @tidyq/backend # 2: whole API
/services/api/billing/ @tidyq/payments # 3: more specific, must come AFTER 2With lines 2 and 3 swapped, billing files are owned by @tidyq/backend only (CO05).
Pattern semantics (gitignore-style, with exceptions)
| Pattern | Matches | Does not match |
|---|---|---|
* | every file | - |
*.js | app.js, web/lib/util.js (any depth) | app.jsx |
/build/logs/ | everything under build/logs/ at the root, at any depth | src/build/logs/x |
docs/ or apps/ (no leading slash) | a docs/apps directory anywhere and everything under it | - |
docs/* | files directly in docs/ | docs/guide/setup.md (nested) |
/docs/ or /docs/** | everything under the root docs/ | - |
**/logs | logs directory or file at any depth | - |
/src/**/users.py | src/users.py, src/api/v2/users.py | - |
README.md | README.md at any depth | - |
/README.md | only the root README.md | sub/README.md |
src/ap?.py | src/app.py | src/apps.py |
Rules of thumb:
- A pattern with a slash at the start or in the middle is anchored to the repository root; one without (apart from a trailing slash) floats and matches at any depth.
- A trailing slash means "this directory and everything in it".
- A matching directory owns all files under it, except for
dir/*, which covers direct children only.
Not supported (the line is ignored or flagged)
!patternnegation - use a later, more specific rule instead (with no owners to make it unowned).[a-z]character ranges - list the paths or use*.- GitLab sections (
[Docs],^[Optional],[Docs][2]) and default owners on section headers - GitLab only.
Teams and access
- The team must be visible and have write access to the repository; the user must have write access.
- An empty team gets the review request but nobody is notified.
- Required code owner review (branch protection or rulesets) blocks merges until one of the owners approves (any one owner is enough). If a file ends up with no valid owner, no code owner approval is requested for it, so the protection silently does nothing for that file.
Getting the inputs
git ls-files > files.txt
gh api orgs/<org>/teams --paginate -q '.[].slug'
gh api orgs/<org>/teams/<slug>/members --paginate -q '.[].login'
gh api repos/<owner>/<repo>/collaborators --paginate -q '.[] | select(.permissions.push) | .login'
gh api repos/<owner>/<repo>/codeowners/errorsFILE:references/checks-and-codes.md
Checks and finding codes
| Code | Severity | Check | Typical fix |
|---|---|---|---|
| CO01 | high | owner is not @user, @org/team or an email (e.g. omar); pattern cannot be parsed | add the @ / fix the slug; the whole line is skipped until then |
| CO02 | high | user not in your users list, team not in your teams list, or team from another org (only with --owners); a second CO02 line says when no valid owner is left on a line, so its files get no code owner | fix the slug (the checker suggests a close team), give the team write access, or remove the owner |
| CO03 | high: unowned file under a require_owner path; medium: other files with no rule or only unknown owners (explicitly unowned files are CO10) | coverage | add a rule, or a catch-all * default |
| CO04 | medium | the pattern matches no file in the list | path renamed or deleted: update or remove the line |
| CO05 | medium | every file the rule matches is owned by later lines (last match wins) | move the specific rule below the general one |
| CO06 | medium: ! negation, [ ] ranges (ignored by GitHub); low: GitLab section header | unsupported syntax | use a later more specific rule; empty owner list = unowned |
| CO07 | medium: one individual (not a team) is the effective owner of more than max_individual_share of files; low: files owned by a single individual and no team | bus factor | add a team or a second owner |
| CO08 | low | the same pattern appears twice; only the later line counts | merge the owners into one line |
| CO09 | medium | team has no members in your teams list | add members or use another team |
| CO10 | info | pattern with no owners: files intentionally unowned | fine if intended (generated code, vendored files) |
| CO11 | medium | files under a require_owner path are only owned by the catch-all * rule | add a specific rule for that path |
Verdict
- High findings: fix before relying on "Require review from Code Owners".
- Medium only: the file works but some files go to the wrong people.
- Healthy: only low/info.
Owners file (--owners, JSON)
| Key | Meaning |
|---|---|
org | org name; teams from another org are flagged |
users | logins with write access (without @) |
teams | {"org/slug": [members]} |
require_owner | patterns (same syntax) for paths that must have a specific owner |
max_individual_share | share of files one person may own alone (default 0.4) |
ignore | patterns for files to leave out (vendored code, lock files) |
Limits of the check
- Repository permissions and team visibility are not checked - confirm with the
gh apicalls in the syntax reference or the "CODEOWNERS errors" banner. - The file list is a snapshot of one branch; run it against the base branch of your pull requests.
FILE:templates/ownership-review.md
CODEOWNERS fix: <repository>
Branch: <base branch> Checked: <YYYY-MM-DD> Files: <n> Owned before / after: <x% / y%>
What was wrong
| Line | Rule | Problem | Effect |
|---|---|---|---|
| <11> | <pattern> <owners> | <unknown team @org/frontend-team> | <web files fall back to @org/platform> |
| <6> | <pattern> | <overridden by line 7> | <payments never review billing> |
Changes
<- /services/api/billing/ @org/payments
< /services/api/ @org/backend
<+ /services/api/billing/ @org/payments>Who owns the key paths now
| Path | Before | After |
|---|---|---|
| <services/api/billing/invoices.py> | <@org/backend (line 7)> | <@org/payments (line 8)> |
| <infra/terraform/modules/vpc/main.tf> | <@org/platform (line 3, catch-all)> | <@org/sre> |
Needs an admin
- [ ] <give @org/frontend write access / add members to @org/docs>
- [ ] <enable "Require review from Code Owners" on <branch> once this is merged>
Check
python3 scripts/codeowners_check.py .github/CODEOWNERS files.txt --owners owners.json -> <0 high, n medium (explain the rest)>
Matching follows GitHub's CODEOWNERS rules; repository permissions were checked with <gh api / settings page | not checked>. FILE:examples/CODEOWNERS
CODEOWNERS for tidyq/tidyq-app (copied into .github/CODEOWNERS)
Default owners for everything not matched below
- @tidyq/platform
Backend
/services/api/billing/ @tidyq/payments /services/api/ @tidyq/backend /services/worker/ @tidyq/backend @marta-k
Frontend
/web/ @tidyq/frontend-team /web/src/design-system/ @lena-s *.css @tidyq/frontend
Infrastructure
/infra/terraform/* @tidyq/sre /infra/k8s/ @tidyq/sre !/infra/k8s/sandbox/
CI and repository settings
/.github/workflows/ @tidyq/sre /.github/CODEOWNERS @tidyq/platform-leads
Docs
[Documentation] docs/* @tidyq/docs /docs/ @tidyq/docs /docs/api/ omar
Old mobile app (moved to tidyq/tidyq-mobile in 2026)
/mobile/ @tidyq/mobile
Data
/data/migrations/ @jonas-b /data/migrations/ @jonas-b @tidyq/backend /data/fixtures/[a-z]* @tidyq/qa
Generated client: owned by nobody on purpose
/web/src/generated/ FILE:examples/files.txt .github/CODEOWNERS .github/dependabot.yml .github/workflows/ci.yml .github/workflows/deploy.yml .github/workflows/release.yml README.md package.json docs/index.md docs/getting-started.md docs/api/endpoints.md docs/api/auth.md docs/runbooks/oncall.md infra/terraform/main.tf infra/terraform/variables.tf infra/terraform/modules/vpc/main.tf infra/terraform/modules/rds/main.tf infra/k8s/api-deployment.yaml infra/k8s/worker-deployment.yaml infra/k8s/sandbox/debug-pod.yaml services/api/app.py services/api/routes/users.py services/api/routes/orders.py services/api/billing/invoices.py services/api/billing/stripe_webhook.py services/api/tests/test_routes.py services/worker/main.py services/worker/jobs/email.py services/worker/jobs/reports.py web/package.json web/src/App.tsx web/src/pages/Home.tsx web/src/pages/Checkout.tsx web/src/styles/main.css web/src/design-system/Button.tsx web/src/design-system/Modal.tsx web/src/design-system/tokens.css web/src/generated/api-client.ts data/migrations/0041_add_orders.sql data/migrations/0042_add_invoices.sql data/fixtures/users.json data/fixtures/orders.json scripts/seed_db.py scripts/rotate_keys.sh FILE:examples/owners.json { "org": "tidyq", "users": ["marta-k", "lena-s", "jonas-b", "omar-h", "priya-n", "sam-t"], "teams": { "tidyq/platform": ["priya-n", "sam-t"], "tidyq/platform-leads": ["priya-n"], "tidyq/backend": ["marta-k", "jonas-b", "omar-h"], "tidyq/payments": ["marta-k"], "tidyq/frontend": ["lena-s", "sam-t"], "tidyq/sre": ["priya-n", "sam-t"], "tidyq/docs": [], "tidyq/qa": ["omar-h"] }, "require_owner": [".github/", "infra/", "services/api/billing/", "data/migrations/"], "max_individual_share": 0.25, "ignore": [] } FILE:examples/ownership-report.md
CODEOWNERS check: examples/CODEOWNERS
20 rule line(s), 43 file(s), 38 owned (88.4%), 5 unowned.
Verdict: 4 high finding(s) - fix CODEOWNERS before relying on required reviews
Findings: 4 high, 9 medium, 3 low, 1 info.
Lines whose owners are all unknown (they still match, so their files get no code owner): 11, 31. Unknown owners are left out of the owner columns below.
Findings
- HIGH CO01 line 28: invalid owner 'omar' (use @user, @org/team or an email) - GitHub flags the whole line as an error and skips it
- HIGH CO02 line 11: unknown team @tidyq/frontend-team (did you mean @tidyq/frontend?) - GitHub will not request reviews from it
- HIGH CO02 line 11: no valid owner left - its 4 file(s) get no code owner (they do not fall back to earlier lines)
- HIGH CO02 line 31: unknown team @tidyq/mobile - GitHub will not request reviews from it
- MEDIUM CO03 4 file(s) have no code owner (no matching rule, or only unknown owners): web/package.json, web/src/App.tsx, web/src/pages/Home.tsx, web/src/pages/Checkout.tsx
- MEDIUM CO04 line 31: '/mobile/' matches no file - path renamed or removed?
- MEDIUM CO05 line 6: '/services/api/billing/' (2 file(s)) is fully overridden by later line(s) 7 - last match wins, put specific rules after general ones
- MEDIUM CO05 line 26: 'docs/*' (2 file(s)) is fully overridden by later line(s) 27 - last match wins, put specific rules after general ones
- MEDIUM CO05 line 34: '/data/migrations/' (2 file(s)) is fully overridden by later line(s) 35 - last match wins, put specific rules after general ones
- MEDIUM CO06 line 18: '!/infra/k8s/sandbox/' uses ! negation, which CODEOWNERS does not support - GitHub ignores the line
- MEDIUM CO06 line 36: '/data/fixtures/[a-z]*' uses [ ] character ranges, which CODEOWNERS does not support - GitHub ignores the line
- MEDIUM CO09 line 26: team @tidyq/docs has no members - review requests go nowhere
- MEDIUM CO11 3 file(s) under paths that must have an owner only fall back to the catch-all '*' rule (no specific owner): .github/dependabot.yml, infra/terraform/modules/vpc/main.tf, infra/terraform/modules/rds/main.tf
- LOW CO06 line 25: '[Documentation]' is a GitLab section header; GitHub treats it as an invalid line
- LOW CO07 2 file(s) have a single individual owner and no team (e.g. web/src/design-system/Button.tsx, web/src/design-system/Modal.tsx)
- LOW CO08 line 35 repeats the pattern '/data/migrations/' of line 34 - only the later line counts
- INFO CO10 line 39: '/web/src/generated/' has no owners - its 1 file(s) are intentionally unowned
Coverage by top-level directory
| Directory | Files | Owned | Unowned | Effective owners |
|---|---|---|---|---|
| (root) | 2 | 2 | 0 | @tidyq/platform (2) |
| .github/ | 5 | 5 | 0 | @tidyq/sre (3), @tidyq/platform-leads (1), @tidyq/platform (1) |
| data/ | 4 | 4 | 0 | @jonas-b (2), @tidyq/backend (2), @tidyq/platform (2) |
| docs/ | 5 | 5 | 0 | @tidyq/docs (5) |
| infra/ | 7 | 7 | 0 | @tidyq/sre (5), @tidyq/platform (2) |
| scripts/ | 2 | 2 | 0 | @tidyq/platform (2) |
| services/ | 9 | 9 | 0 | @tidyq/backend (9), @marta-k (3) |
| web/ | 9 | 4 | 5 | @tidyq/frontend (2), @lena-s (2) |
Rules
| Line | Pattern | Owners | Files matched | Effective for | Note |
|---|---|---|---|---|---|
| 3 | * | @tidyq/platform | 43 | 9 | |
| 6 | /services/api/billing/ | @tidyq/payments | 2 | 0 | CO05 |
| 7 | /services/api/ | @tidyq/backend | 6 | 6 | |
| 8 | /services/worker/ | @tidyq/backend @marta-k | 3 | 3 | |
| 11 | /web/ | @tidyq/frontend-team | 9 | 4 | CO02 |
| 12 | /web/src/design-system/ | @lena-s | 3 | 2 | |
| 13 | *.css | @tidyq/frontend | 2 | 2 | |
| 16 | /infra/terraform/* | @tidyq/sre | 2 | 2 | |
| 17 | /infra/k8s/ | @tidyq/sre | 3 | 3 | |
| 18 | !/infra/k8s/sandbox/ | (none) | - | - | CO06 |
| 21 | /.github/workflows/ | @tidyq/sre | 3 | 3 | |
| 22 | /.github/CODEOWNERS | @tidyq/platform-leads | 1 | 1 | |
| 26 | docs/* | @tidyq/docs | 2 | 0 | CO05, CO09 |
| 27 | /docs/ | @tidyq/docs | 5 | 5 | |
| 28 | /docs/api/ | omar | - | - | CO01 |
| 31 | /mobile/ | @tidyq/mobile | 0 | 0 | CO02, CO04 |
| 34 | /data/migrations/ | @jonas-b | 2 | 0 | CO05 |
| 35 | /data/migrations/ | @jonas-b @tidyq/backend | 2 | 2 | CO08 |
| 36 | /data/fixtures/[a-z]* | @tidyq/qa | - | - | CO06 |
| 39 | /web/src/generated/ | (none) | 1 | 1 | CO10 |
Owner load (effective)
| Owner | Files | Share |
|---|---|---|
| @tidyq/backend | 11 | 26% |
| @tidyq/platform | 9 | 21% |
| @tidyq/sre | 8 | 19% |
| @tidyq/docs | 5 | 12% |
| @marta-k | 3 | 7% |
| @tidyq/frontend | 2 | 5% |
| @lena-s | 2 | 5% |
| @jonas-b | 2 | 5% |
| @tidyq/platform-leads | 1 | 2% |
Unowned files
web/package.json(line 11: only unknown owners)web/src/App.tsx(line 11: only unknown owners)web/src/pages/Home.tsx(line 11: only unknown owners)web/src/pages/Checkout.tsx(line 11: only unknown owners)web/src/generated/api-client.ts(explicitly unowned, line 39)
Who owns
- services/api/billing/invoices.py: @tidyq/backend (line 7: /services/api/)
- infra/terraform/modules/vpc/main.tf: @tidyq/platform (line 3: *)
- web/src/pages/Checkout.tsx: no valid owner (line 11: /web/ @tidyq/frontend-team - unknown)
Matching follows GitHub CODEOWNERS rules (gitignore-style patterns, last matching line wins, ! and [ ] unsupported). Owners are checked against your owners file only; confirm write access in the repository settings. FILE:scripts/codeowners_check.py #!/usr/bin/env python3 """CODEOWNERS coverage and ownership checker (stdlib only, Python 3.8+).
Reads a GitHub CODEOWNERS file and the repository file list (git ls-files output), works out the effective owner of every file with GitHub's rules (gitignore-style patterns, the LAST matching line wins) and reports syntax errors, unknown owners, unowned files (also under paths that must have an owner), rules that match nothing, rules that are fully overridden by later lines, unsupported syntax, duplicates, empty teams and owner concentration.
Usage: codeowners_check.py CODEOWNERS FILES.txt [--owners OWNERS.json] [--who PATH]... [--json] codeowners_check.py --self-test Exit: 0 = no high findings, 1 = high findings, 2 = bad input. """ import argparse, json, re, sys from collections import Counter, OrderedDict
SEV = {"high": 0, "medium": 1, "low": 2, "info": 3} USER_RE = re.compile(r"^@[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$") TEAM_RE = re.compile(r"^@([A-Za-z0-9][A-Za-z0-9-])/([A-Za-z0-9][A-Za-z0-9._-])$") EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
class InputError(Exception): pass
---------------------------------------------------------------- patterns
def pattern_regex(pat): """Return (compiled regex, dir_only, direct_children_only) for a CODEOWNERS pattern.""" dir_only = pat.endswith("/") p = pat.rstrip("/") anchored = p.startswith("/") or "/" in p p = p.lstrip("/") direct_only = p.endswith("/") out, i = "", 0 while i < len(p): if p.startswith("*/", i): out += "(?:./)?"; i += 3 elif p.startswith("/*", i) and i + 3 == len(p): out += "/."; i += 3 elif p.startswith("*", i): out += "."; i += 2 elif p[i] == "": out += "[^/]"; i += 1 elif p[i] == "?": out += "[^/]"; i += 1 elif p[i] == "\\" and i + 1 < len(p): out += re.escape(p[i + 1]); i += 2 else: out += re.escape(p[i]); i += 1 if not anchored: out = "(?:./)?" + out return re.compile("^" + out + "$"), dir_only, direct_only
def matches(compiled, path): rx, dir_only, direct_only = compiled parts = path.split("/") if not dir_only and rx.match(path): return True if direct_only: return False for k in range(1, len(parts)): # a matching directory owns everything under it if rx.match("/".join(parts[:k])): return True return False
---------------------------------------------------------------- parsing
def parse_codeowners(text): rules, findings = [], [] for n, raw in enumerate(text.splitlines(), 1): line = raw.strip() if not line or line.startswith("#"): continue if re.match(r"^\^?\[[^\]]+\](\[\d+\])?(\s+@.*)?$", line): findings.append(("low", "CO06", "line %d: '%s' is a GitLab section header; GitHub treats it as an invalid line" % (n, line), n)) continue tokens = re.split(r"(?<!\\)\s+", line) pat, owners = tokens[0], [] for t in tokens[1:]: if t.startswith("#"): break owners.append(t) rule = {"line": n, "pattern": pat, "owners": owners, "valid": True} if pat.startswith("!"): findings.append(("medium", "CO06", "line %d: '%s' uses ! negation, which CODEOWNERS does not support - GitHub ignores the line" % (n, pat), n)) rule["valid"] = False elif "[" in pat or "]" in pat: findings.append(("medium", "CO06", "line %d: '%s' uses [ ] character ranges, which CODEOWNERS does not support - GitHub ignores the line" % (n, pat), n)) rule["valid"] = False bad = [o for o in owners if not (USER_RE.match(o) or TEAM_RE.match(o) or EMAIL_RE.match(o))] if bad: findings.append(("high", "CO01", "line %d: invalid owner %s (use @user, @org/team or an email) - GitHub flags the whole line as an error and skips it" % (n, ", ".join("'%s'" % b for b in bad)), n)) rule["valid"] = False if rule["valid"]: try: rule["rx"] = pattern_regex(pat) except re.error as e: findings.append(("high", "CO01", "line %d: pattern '%s' cannot be parsed (%s)" % (n, pat, e), n)) rule["valid"] = False rules.append(rule) return rules, findings
def load_files(text): files = [] for l in text.splitlines(): l = l.strip() if l and not l.startswith("#"): files.append(l[2:] if l.startswith("./") else l.lstrip("/")) if not files: raise InputError("file list is empty (use: git ls-files > files.txt)") return files
The text continues at the source. Read the rest.
Source and license
Source: https://github.com/f/prompts.chat/blob/main/prompts/02867.md
Author: f, prompts.chat prompt library. License: CC0-1.0.
Shown here with formatting for the web; email addresses removed. Keep the author and license notice when you reuse it.
Related entries
There are more like this in the members library
The Business prompt library holds 3,008 entries. Members can search all of them by meaning and read every one in full.