|
| 1 | +import chalk from 'chalk'; |
| 2 | +import {readdirSync, readFileSync, statSync} from 'fs'; |
| 3 | +import {IMinimatch, Minimatch} from 'minimatch'; |
| 4 | +import {join} from 'path'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Script that lints the CODEOWNERS file and makes sure that all files have an owner. |
| 8 | + */ |
| 9 | + |
| 10 | +/** Path for the Github owners file. */ |
| 11 | +const ownersFilePath = '.github/CODEOWNERS'; |
| 12 | + |
| 13 | +/** Path for the .gitignore file. */ |
| 14 | +const gitIgnorePath = '.gitignore'; |
| 15 | + |
| 16 | +let errors = 0; |
| 17 | +const ownedPaths = readFileSync(ownersFilePath, 'utf8').split('\n') |
| 18 | + // Trim lines. |
| 19 | + .map(line => line.trim()) |
| 20 | + // Remove empty lines and comments. |
| 21 | + .filter(line => line && !line.startsWith('#')) |
| 22 | + // Split off just the path glob. |
| 23 | + .map(line => line.split(/\s+/)[0]) |
| 24 | + // Turn paths into Minimatch objects. |
| 25 | + .map(path => new Minimatch(path, {dot: true, matchBase: true})); |
| 26 | + |
| 27 | +const ignoredPaths = readFileSync(gitIgnorePath, 'utf8').split('\n') |
| 28 | + // Trim lines. |
| 29 | + .map(line => line.trim()) |
| 30 | + // Remove empty lines and comments. |
| 31 | + .filter(line => line && !line.startsWith('#')) |
| 32 | + // Turn paths into Minimatch objects. |
| 33 | + .map(path => new Minimatch(path, {dot: true, matchBase: true})); |
| 34 | + |
| 35 | +for (let paths = getChildPaths('.'); paths.length;) { |
| 36 | + paths = Array.prototype.concat(...paths |
| 37 | + // Remove ignored paths |
| 38 | + .filter(path => !ignoredPaths.reduce( |
| 39 | + (isIgnored, ignoredPath) => isIgnored || ignoredPath.match('/' + path), false)) |
| 40 | + // Remove paths that match an owned path. |
| 41 | + .filter(path => !ownedPaths.reduce( |
| 42 | + (isOwned, ownedPath) => isOwned || isOwnedBy(path, ownedPath), false)) |
| 43 | + // Report an error for any files that didn't match any owned paths. |
| 44 | + .filter(path => { |
| 45 | + if (statSync(path).isFile()) { |
| 46 | + console.log(chalk.red(`No code owner found for "${path}".`)); |
| 47 | + errors++; |
| 48 | + return false; |
| 49 | + } |
| 50 | + return true; |
| 51 | + }) |
| 52 | + // Get the next level of children for any directories. |
| 53 | + .map(path => getChildPaths(path))); |
| 54 | +} |
| 55 | + |
| 56 | +if (errors) { |
| 57 | + throw Error(`Found ${errors} files with no owner. Code owners for the files ` + |
| 58 | + `should be added in the CODEOWNERS file.`); |
| 59 | +} |
| 60 | + |
| 61 | +/** Check if the given path is owned by the given owned path matcher. */ |
| 62 | +function isOwnedBy(path: string, ownedPath: IMinimatch) { |
| 63 | + // If the owned path ends with `**` its safe to eliminate whole directories. |
| 64 | + if (ownedPath.pattern.endsWith('**') || statSync(path).isFile()) { |
| 65 | + return ownedPath.match('/' + path); |
| 66 | + } |
| 67 | + return false; |
| 68 | +} |
| 69 | + |
| 70 | +/** Get the immediate child paths of the given path. */ |
| 71 | +function getChildPaths(path: string) { |
| 72 | + return readdirSync(path).map(child => join(path, child)); |
| 73 | +} |
0 commit comments