[Ownership] Print owner match also, not just owner (#202704)

## Summary

Resolves: https://github.com/elastic/kibana/issues/202666

## For reviewers
To see the change, run this:
`node scripts/get_owners_for_file.js --file
test/functional/apps/console/_autocomplete.ts`
### Results:
#### Before
```
 succ elastic/kibana-management
```
#### After
```
 succ Found matching entry in .github/CODEOWNERS:
      test/functional/apps/console/*.ts elastic/kibana-management
```

---------

Co-authored-by: Robert Oskamp <traeluki@gmail.com>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: David Olaru <dolaru@elastic.co>
This commit is contained in:
Tre 2024-12-10 17:15:13 +00:00 committed by GitHub
parent 2a76fe3ee4
commit e706b6689d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 31 additions and 17 deletions

View file

@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/
export type { PathWithOwners } from './src/file_code_owner';
export type { PathWithOwners, CodeOwnership } from './src/file_code_owner';
export {
getPathsWithOwnersReversed,
getCodeOwnersForFile,

View file

@ -21,6 +21,8 @@ export interface PathWithOwners {
teams: string;
ignorePattern: Ignore;
}
export type CodeOwnership = Partial<Pick<PathWithOwners, 'path' | 'teams'>> | undefined;
const existOrThrow = (targetFile: string) => {
if (existsSync(targetFile) === false)
throw createFailError(`Unable to determine code owners: file ${targetFile} Not Found`);
@ -65,14 +67,13 @@ export function getPathsWithOwnersReversed(): PathWithOwners[] {
export function getCodeOwnersForFile(
filePath: string,
reversedCodeowners?: PathWithOwners[]
): string | undefined {
): CodeOwnership {
const pathsWithOwners = reversedCodeowners ?? getPathsWithOwnersReversed();
const match = pathsWithOwners.find((p) => p.ignorePattern.test(filePath).ignored);
return match?.teams;
if (match?.path && match.teams) return { path: match.path, teams: match.teams };
return;
}
const trimFrontSlash = (x: string): string => x.replace(/^\//, '');
/**
* Run the getCodeOwnersForFile() method above.
* Report back to the cli with either success and the owner(s), or a failure.
@ -87,7 +88,9 @@ export async function runGetOwnersForFileCli() {
if (!targetFile) throw createFlagError(`Missing --file argument`);
existOrThrow(targetFile); // This call is duplicated in getPathsWithOwnersReversed(), so this is a short circuit
const result = getCodeOwnersForFile(targetFile);
if (result) log.success(result);
if (result)
log.success(`Found matching entry in .github/CODEOWNERS:
${trimFrontSlash(result?.path ? result.path : '')} ${result.teams}`);
else log.error(`Ownership of file [${targetFile}] is UNKNOWN`);
},
{

View file

@ -23,7 +23,11 @@ import { ToolingLog } from '@kbn/tooling-log';
import { SCOUT_REPORT_OUTPUT_ROOT } from '@kbn/scout-info';
import stripANSI from 'strip-ansi';
import { REPO_ROOT } from '@kbn/repo-info';
import { PathWithOwners, getPathsWithOwnersReversed, getCodeOwnersForFile } from '@kbn/code-owners';
import {
type PathWithOwners,
getPathsWithOwnersReversed,
getCodeOwnersForFile,
} from '@kbn/code-owners';
import { generateTestRunId, getTestIDForTitle, ScoutReport, ScoutReportEventAction } from '.';
import { environmentMetadata } from '../datasources';
@ -60,7 +64,7 @@ export class ScoutPlaywrightReporter implements Reporter {
}
private getFileOwners(filePath: string): string[] {
const concatenatedOwners = getCodeOwnersForFile(filePath, this.pathsWithOwners);
const concatenatedOwners = getCodeOwnersForFile(filePath, this.pathsWithOwners)?.teams;
if (concatenatedOwners === undefined) {
return [];

View file

@ -18,7 +18,11 @@ import {
ScoutReportEventAction,
datasources,
} from '@kbn/scout-reporting';
import { getCodeOwnersForFile, getPathsWithOwnersReversed, PathWithOwners } from '@kbn/code-owners';
import {
getCodeOwnersForFile,
getPathsWithOwnersReversed,
type PathWithOwners,
} from '@kbn/code-owners';
import { Runner, Test } from '../../../fake_mocha_types';
/**
@ -64,7 +68,7 @@ export class ScoutFTRReporter {
}
private getFileOwners(filePath: string): string[] {
const concatenatedOwners = getCodeOwnersForFile(filePath, this.pathsWithOwners);
const concatenatedOwners = getCodeOwnersForFile(filePath, this.pathsWithOwners)?.teams;
if (concatenatedOwners === undefined) {
return [];

View file

@ -10,7 +10,11 @@
import { run } from '@kbn/dev-cli-runner';
import { createFailError } from '@kbn/dev-cli-errors';
import { getRepoFiles } from '@kbn/get-repo-files';
import { getCodeOwnersForFile, getPathsWithOwnersReversed } from '@kbn/code-owners';
import {
getCodeOwnersForFile,
getPathsWithOwnersReversed,
type CodeOwnership,
} from '@kbn/code-owners';
const TEST_DIRECTORIES = ['test', 'x-pack/test', 'x-pack/test_serverless'];
@ -36,10 +40,8 @@ export async function runCheckFtrCodeOwnersCli() {
const testFiles = await getRepoFiles(TEST_DIRECTORIES);
for (const { repoRel } of testFiles) {
const owners = getCodeOwnersForFile(repoRel, reversedCodeowners);
if (owners === undefined || owners === '') {
missingOwners.add(repoRel);
}
const owners: CodeOwnership = getCodeOwnersForFile(repoRel, reversedCodeowners);
if (owners === undefined || owners.teams === '') missingOwners.add(repoRel);
}
const timeSpent = fmtMs(performance.now() - start);

View file

@ -142,8 +142,9 @@ export function setupJUnitReportGeneration(runner, options = {}) {
// adding code owners only for the failed test case
if (failed) {
const testCaseRelativePath = getPath(node);
const owners = getCodeOwnersForFile(testCaseRelativePath, reversedCodeowners);
attrs.owners = owners || ''; // empty string when no codeowners are defined
attrs.owners = owners?.teams || ''; // empty string when no codeowners are defined
}
return testsuitesEl.ele('testcase', attrs);