[8.x] [ftr] Speed up FTR code owner check (#205093) (#205415)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ftr] Speed up FTR code owner check
(#205093)](https://github.com/elastic/kibana/pull/205093)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"David
Olaru","email":"dolaru@elastic.co"},"sourceCommit":{"committedDate":"2025-01-02T17:10:20Z","message":"[ftr]
Speed up FTR code owner check (#205093)\n\n## Summary\r\n\r\nSwitch to
one matcher w/ all code owner patterns rather than separate\r\nmatchers
for each code owner pattern. Reduces the run time
of\r\n`scripts/check_ftr_code_owners.js` by ~10x.\r\n\r\n###
Before\r\n```console\r\n▶ node scripts/check_ftr_code_owners.js\r\n info
Reading CODEOWNERS file\r\n info Checking ownership for 8653 test files
(this will take a while)\r\n info Ownership check complete (took 18.89
s)\r\n succ All test files have a code owner. 🥳\r\n```\r\n\r\n####
After\r\n```console\r\n▶ node scripts/check_ftr_code_owners.js\r\n info
Checked 8653 test files in 1.59s\r\n succ All test files have a code
owner
🥳\r\n```","sha":"a0eebb82c9ef8385cecd4a5f6c0d1ebd22561789","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","v8.18.0"],"title":"[ftr]
Speed up FTR code owner
check","number":205093,"url":"https://github.com/elastic/kibana/pull/205093","mergeCommit":{"message":"[ftr]
Speed up FTR code owner check (#205093)\n\n## Summary\r\n\r\nSwitch to
one matcher w/ all code owner patterns rather than separate\r\nmatchers
for each code owner pattern. Reduces the run time
of\r\n`scripts/check_ftr_code_owners.js` by ~10x.\r\n\r\n###
Before\r\n```console\r\n▶ node scripts/check_ftr_code_owners.js\r\n info
Reading CODEOWNERS file\r\n info Checking ownership for 8653 test files
(this will take a while)\r\n info Ownership check complete (took 18.89
s)\r\n succ All test files have a code owner. 🥳\r\n```\r\n\r\n####
After\r\n```console\r\n▶ node scripts/check_ftr_code_owners.js\r\n info
Checked 8653 test files in 1.59s\r\n succ All test files have a code
owner
🥳\r\n```","sha":"a0eebb82c9ef8385cecd4a5f6c0d1ebd22561789"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/205093","number":205093,"mergeCommit":{"message":"[ftr]
Speed up FTR code owner check (#205093)\n\n## Summary\r\n\r\nSwitch to
one matcher w/ all code owner patterns rather than separate\r\nmatchers
for each code owner pattern. Reduces the run time
of\r\n`scripts/check_ftr_code_owners.js` by ~10x.\r\n\r\n###
Before\r\n```console\r\n▶ node scripts/check_ftr_code_owners.js\r\n info
Reading CODEOWNERS file\r\n info Checking ownership for 8653 test files
(this will take a while)\r\n info Ownership check complete (took 18.89
s)\r\n succ All test files have a code owner. 🥳\r\n```\r\n\r\n####
After\r\n```console\r\n▶ node scripts/check_ftr_code_owners.js\r\n info
Checked 8653 test files in 1.59s\r\n succ All test files have a code
owner
🥳\r\n```","sha":"a0eebb82c9ef8385cecd4a5f6c0d1ebd22561789"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: David Olaru <dolaru@elastic.co>
This commit is contained in:
Kibana Machine 2025-01-03 05:53:22 +11:00 committed by GitHub
parent 83065da636
commit 59a6cf6d4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 63 additions and 73 deletions

View file

@ -69,8 +69,6 @@ export { getUrl } from './src/jest/get_url';
export { runCheckJestConfigsCli } from './src/jest/run_check_jest_configs_cli';
export { runCheckFtrCodeOwnersCli } from './src/functional_test_runner/run_check_ftr_code_owners';
export { runJest } from './src/jest/run';
export * from './src/kbn_archiver_cli';

View file

@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { run } from '@kbn/dev-cli-runner';
import { createFailError } from '@kbn/dev-cli-errors';
import { getRepoFiles } from '@kbn/get-repo-files';
import { getCodeOwnersEntries } from '@kbn/code-owners';
import ignore from 'ignore';
const TEST_DIRECTORIES = ['test', 'x-pack/test', 'x-pack/test_serverless'];
export async function checkFTRCodeOwnersCLI() {
await run(
async ({ log }) => {
const matcher = ignore().add(
getCodeOwnersEntries()
.filter((entry) => entry.teams.length > 0)
.map((entry) => entry.pattern)
);
const hasOwner = (path: string): boolean => matcher.test(path).ignored;
const testFiles = await getRepoFiles(TEST_DIRECTORIES);
const filesWithoutOwner = testFiles
.filter((repoPath) => !hasOwner(repoPath.repoRel))
.map((repoPath) => repoPath.repoRel);
log.info(`Checked ${testFiles.length} test files in ${process.uptime().toFixed(2)}s`);
if (filesWithoutOwner.length === 0) {
log.success(`All test files have a code owner 🥳`);
return;
}
log.write('Test files without a code owner:');
log.write(filesWithoutOwner.map((i) => ` - ${i}`).join('\n'));
throw createFailError(`Found ${filesWithoutOwner.length} test files without code owner`);
},
{
description: 'Check that all test files are covered by GitHub CODEOWNERS',
}
);
}

View file

@ -16,8 +16,8 @@ import { ToolingLog } from '@kbn/tooling-log';
import { getTimeReporter } from '@kbn/ci-stats-reporter';
import exitHook from 'exit-hook';
import { readConfigFile, EsVersion } from './lib';
import { FunctionalTestRunner } from './functional_test_runner';
import { readConfigFile, EsVersion } from '../lib';
import { FunctionalTestRunner } from '../functional_test_runner';
export function runFtrCli() {
const runStartTime = Date.now();

View file

@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
export { runFtrCli } from './ftr';
export { checkFTRCodeOwnersCLI } from './code_owners';

View file

@ -18,6 +18,6 @@ export {
runCheckFtrConfigsCli,
DedicatedTaskRunner,
} from './lib';
export { runFtrCli } from './cli';
export * from './cli';
export * from './lib/docker_servers';
export * from './public_types';

View file

@ -1,67 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { run } from '@kbn/dev-cli-runner';
import { createFailError } from '@kbn/dev-cli-errors';
import { getRepoFiles } from '@kbn/get-repo-files';
import { getOwningTeamsForPath, getCodeOwnersEntries } from '@kbn/code-owners';
const TEST_DIRECTORIES = ['test', 'x-pack/test', 'x-pack/test_serverless'];
const fmtMs = (ms: number) => {
if (ms < 1000) {
return `${Math.round(ms)} ms`;
}
return `${(Math.round(ms) / 1000).toFixed(2)} s`;
};
const fmtList = (list: Iterable<string>) => [...list].map((i) => ` - ${i}`).join('\n');
export async function runCheckFtrCodeOwnersCli() {
run(
async ({ log }) => {
const start = performance.now();
const missingOwners = new Set<string>();
// cache codeowners for quicker lookup
log.info('Reading CODEOWNERS file');
const codeOwnersEntries = getCodeOwnersEntries();
const testFiles = await getRepoFiles(TEST_DIRECTORIES);
log.info(`Checking ownership for ${testFiles.length} test files (this will take a while)`);
for (const { repoRel } of testFiles) {
const owners = getOwningTeamsForPath(repoRel, codeOwnersEntries);
if (owners.length === 0) {
missingOwners.add(repoRel);
}
}
const timeSpent = fmtMs(performance.now() - start);
log.info(`Ownership check complete (took ${timeSpent})`);
if (missingOwners.size) {
log.error(
`The following test files do not have a GitHub code owner:\n${fmtList(missingOwners)}`
);
throw createFailError(
`Found ${missingOwners.size} test files without code owner (checked ${testFiles.length} test files in ${timeSpent})`
);
}
log.success(`All test files have a code owner. 🥳`);
},
{
description: 'Check that all test files are covered by GitHub CODEOWNERS',
}
);
}

View file

@ -8,4 +8,4 @@
*/
require('../src/setup_node_env');
require('@kbn/test').runCheckFtrCodeOwnersCli();
void require('@kbn/test').checkFTRCodeOwnersCLI();