|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import Logger from './logger'; |
| 7 | + |
| 8 | +const CODEOWNERS_ID = 'CodeOwners'; |
| 9 | + |
| 10 | +export interface CodeownersEntry { |
| 11 | + readonly pattern: string; |
| 12 | + readonly owners: readonly string[]; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Parses CODEOWNERS file content into a list of entries. |
| 17 | + * Later entries take precedence over earlier ones (per GitHub spec). |
| 18 | + */ |
| 19 | +export function parseCodeownersFile(content: string): CodeownersEntry[] { |
| 20 | + const entries: CodeownersEntry[] = []; |
| 21 | + for (const rawLine of content.split(/\r?\n/)) { |
| 22 | + const line = rawLine.trim(); |
| 23 | + if (!line || line.startsWith('#')) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + const parts = line.split(/\s+/); |
| 27 | + if (parts.length < 2) { |
| 28 | + continue; |
| 29 | + } |
| 30 | + const [pattern, ...owners] = parts; |
| 31 | + entries.push({ pattern, owners }); |
| 32 | + } |
| 33 | + return entries; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Given a parsed CODEOWNERS file and a file path, returns the set of owners |
| 38 | + * for that path. Returns an empty array if no rule matches. |
| 39 | + * |
| 40 | + * Matching follows GitHub semantics: the last matching pattern wins. |
| 41 | + */ |
| 42 | +export function getOwnersForPath(entries: readonly CodeownersEntry[], filePath: string): readonly string[] { |
| 43 | + let matched: readonly string[] = []; |
| 44 | + for (const entry of entries) { |
| 45 | + if (matchesCodeownersPattern(entry.pattern, filePath)) { |
| 46 | + matched = entry.owners; |
| 47 | + } |
| 48 | + } |
| 49 | + return matched; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Checks whether the given user login or any of the given team slugs |
| 54 | + * (in `@org/team` format) appear among the owners list. |
| 55 | + */ |
| 56 | +export function isOwnedByUser( |
| 57 | + owners: readonly string[], |
| 58 | + userLogin: string, |
| 59 | + teamSlugs: readonly string[], |
| 60 | +): boolean { |
| 61 | + const normalizedLogin = `@${userLogin.toLowerCase()}`; |
| 62 | + const normalizedTeams = new Set(teamSlugs.map(t => t.toLowerCase())); |
| 63 | + |
| 64 | + return owners.some(owner => { |
| 65 | + const normalized = owner.toLowerCase(); |
| 66 | + return normalized === normalizedLogin || normalizedTeams.has(normalized); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +function matchesCodeownersPattern(pattern: string, filePath: string): boolean { |
| 71 | + try { |
| 72 | + const regex = codeownersPatternToRegex(pattern); |
| 73 | + return regex.test(filePath); |
| 74 | + } catch (e) { |
| 75 | + Logger.error(`Error matching CODEOWNERS pattern "${pattern}": ${e}`, CODEOWNERS_ID); |
| 76 | + return false; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Converts a CODEOWNERS pattern to a RegExp. |
| 82 | + * |
| 83 | + * GitHub CODEOWNERS rules: |
| 84 | + * - A leading `/` anchors to the repo root; otherwise the pattern matches anywhere. |
| 85 | + * - A trailing `/` means "directory and everything inside". |
| 86 | + * - `*` matches within a single path segment; `**` matches across segments. |
| 87 | + * - Bare filenames (no `/`) match anywhere in the tree. |
| 88 | + * - `?` matches a single non-slash character. |
| 89 | + */ |
| 90 | +function codeownersPatternToRegex(pattern: string): RegExp { |
| 91 | + let p = pattern; |
| 92 | + const anchored = p.startsWith('/'); |
| 93 | + if (anchored) { |
| 94 | + p = p.slice(1); |
| 95 | + } |
| 96 | + |
| 97 | + if (p.endsWith('/')) { |
| 98 | + p = p + '**'; |
| 99 | + } |
| 100 | + |
| 101 | + const hasSlash = p.includes('/'); |
| 102 | + |
| 103 | + let regexStr = ''; |
| 104 | + let i = 0; |
| 105 | + while (i < p.length) { |
| 106 | + if (p[i] === '*') { |
| 107 | + if (p[i + 1] === '*') { |
| 108 | + if (p[i + 2] === '/') { |
| 109 | + // `**/` matches zero or more directories |
| 110 | + regexStr += '(?:.+/)?'; |
| 111 | + i += 3; |
| 112 | + } else { |
| 113 | + // `**` at end or before non-slash: match everything |
| 114 | + regexStr += '.*'; |
| 115 | + i += 2; |
| 116 | + } |
| 117 | + } else { |
| 118 | + // `*` matches anything except `/` |
| 119 | + regexStr += '[^/]*'; |
| 120 | + i++; |
| 121 | + } |
| 122 | + } else if (p[i] === '?') { |
| 123 | + regexStr += '[^/]'; |
| 124 | + i++; |
| 125 | + } else if (p[i] === '.') { |
| 126 | + regexStr += '\\.'; |
| 127 | + i++; |
| 128 | + } else if (p[i] === '[') { |
| 129 | + const closeBracket = p.indexOf(']', i + 1); |
| 130 | + if (closeBracket !== -1) { |
| 131 | + regexStr += p.slice(i, closeBracket + 1); |
| 132 | + i = closeBracket + 1; |
| 133 | + } else { |
| 134 | + regexStr += '\\['; |
| 135 | + i++; |
| 136 | + } |
| 137 | + } else { |
| 138 | + regexStr += p[i]; |
| 139 | + i++; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // If the pattern has no slash (bare filename) and is not anchored, |
| 144 | + // it can match anywhere in the tree. |
| 145 | + const prefix = (!anchored && !hasSlash) ? '(?:^|.+/)' : '^'; |
| 146 | + |
| 147 | + // GitHub treats patterns without glob characters as matching both the |
| 148 | + // exact path and everything inside it (implicit directory match). |
| 149 | + const hasGlob = /[*?\[]/.test(p); |
| 150 | + const suffix = hasGlob ? '$' : '(?:/.*)?$'; |
| 151 | + |
| 152 | + return new RegExp(prefix + regexStr + suffix); |
| 153 | +} |
| 154 | + |
| 155 | +/** Standard CODEOWNERS file paths in order of precedence (first found wins). */ |
| 156 | +export const CODEOWNERS_PATHS = ['.github/CODEOWNERS', 'CODEOWNERS', 'docs/CODEOWNERS'] as const; |
0 commit comments