Skip to content

Commit

Permalink
Add shortcircuit for files that do not exist in repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
wayneseymour committed Sep 19, 2024
1 parent ac199ba commit ea3b979
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/kbn-code-owners/src/file_code_owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface PathWithOwners {
teams: string;
ignorePattern: Ignore;
}
const existOrThrow = (targetFile: string) => {
if (existsSync(targetFile) === false)
throw createFailError(`Unable to determine code owners: file ${targetFile} Not Found`);
};

/**
* Get the .github/CODEOWNERS entries, prepared for path matching.
Expand All @@ -30,9 +34,7 @@ export interface PathWithOwners {
*/
export function getPathsWithOwnersReversed(): PathWithOwners[] {
const codeownersPath = joinPath(REPO_ROOT, '.github', 'CODEOWNERS');
if (existsSync(codeownersPath) === false) {
throw createFailError(`Unable to determine code owners: file ${codeownersPath} not found`);
}
existOrThrow(codeownersPath);
const codeownersContent = readFileSync(codeownersPath, { encoding: 'utf8', flag: 'r' });
const codeownersLines = codeownersContent.split(/\r?\n/);
const codeowners = codeownersLines
Expand Down Expand Up @@ -80,6 +82,7 @@ export async function runGetOwnersForFileCli() {
async ({ flags, log }) => {
const targetFile = flags.file as string;
if (!targetFile) throw createFlagError(`Missing --flag argument`);
existOrThrow(targetFile); // This call is duplicated in getPathsWithOwnersReversed(), so this is a short circuit
const result = getCodeOwnersForFile(targetFile);
if (result) log.success(result);
else log.error(`Ownership of file [${targetFile}] is UNKNOWN`);
Expand Down

0 comments on commit ea3b979

Please sign in to comment.