From a3c5c226148bbea405fcbf507cbfe983eac54834 Mon Sep 17 00:00:00 2001 From: Tre Date: Tue, 10 Dec 2024 17:15:13 +0000 Subject: [PATCH] [FTR] Merge Alerting Apis from `x-pack/test_serverless/shared` into `x-pack/test/api_integration/deployment_agnostic` (#193975) Follow up of [this pr](https://github.com/elastic/kibana/pull/192216), per [this discussion](https://github.com/elastic/kibana/pull/192216#discussion_r1760894369). Also, switch from `svlCommonApi` to `samlAuth` for internal headers. --------- Co-authored-by: Elastic Machine Co-authored-by: Dzmitry Lemechko (cherry picked from commit 10c337387485d77fd264c11888b2c28131ee515a) --- packages/kbn-code-owners/index.ts | 8 +++- .../kbn-code-owners/src/file_code_owner.ts | 44 +++++++++++++++++-- .../src/reporting/playwright.ts | 8 +++- .../lib/mocha/reporter/scout_ftr_reporter.ts | 8 +++- .../run_check_ftr_code_owners.ts | 12 ++--- .../src/mocha/junit_report_generation.js | 3 +- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/packages/kbn-code-owners/index.ts b/packages/kbn-code-owners/index.ts index b4e83b6194c54..1c371d27e2575 100644 --- a/packages/kbn-code-owners/index.ts +++ b/packages/kbn-code-owners/index.ts @@ -7,5 +7,9 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -export type { PathWithOwners } from './src/file_code_owner'; -export { getPathsWithOwnersReversed, getCodeOwnersForFile } from './src/file_code_owner'; +export type { PathWithOwners, CodeOwnership } from './src/file_code_owner'; +export { + getPathsWithOwnersReversed, + getCodeOwnersForFile, + runGetOwnersForFileCli, +} from './src/file_code_owner'; diff --git a/packages/kbn-code-owners/src/file_code_owner.ts b/packages/kbn-code-owners/src/file_code_owner.ts index 508fac9ce9741..8fea89273795e 100644 --- a/packages/kbn-code-owners/src/file_code_owner.ts +++ b/packages/kbn-code-owners/src/file_code_owner.ts @@ -20,6 +20,12 @@ export interface PathWithOwners { teams: string; ignorePattern: Ignore; } +export type CodeOwnership = Partial> | undefined; + +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. @@ -61,10 +67,40 @@ 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. + * + * This function depends on a --file param being passed on the cli, like this: + * $ node scripts/get_owners_for_file.js --file SOME-FILE + */ +export async function runGetOwnersForFileCli() { + run( + async ({ flags, log }) => { + const targetFile = flags.file as string; + 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(`Found matching entry in .github/CODEOWNERS: +${trimFrontSlash(result?.path ? result.path : '')} ${result.teams}`); + else log.error(`Ownership of file [${targetFile}] is UNKNOWN`); + }, + { + description: 'Report file ownership from GitHub CODEOWNERS file.', + flags: { + string: ['file'], + help: ` + --file Required, path to the file to report owners for. + `, + }, + } + ); } diff --git a/packages/kbn-scout-reporting/src/reporting/playwright.ts b/packages/kbn-scout-reporting/src/reporting/playwright.ts index fdea17cb844c0..f50b25bab83d3 100644 --- a/packages/kbn-scout-reporting/src/reporting/playwright.ts +++ b/packages/kbn-scout-reporting/src/reporting/playwright.ts @@ -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 []; diff --git a/packages/kbn-test/src/functional_test_runner/lib/mocha/reporter/scout_ftr_reporter.ts b/packages/kbn-test/src/functional_test_runner/lib/mocha/reporter/scout_ftr_reporter.ts index 4ffef48ec6443..23766e8784df0 100644 --- a/packages/kbn-test/src/functional_test_runner/lib/mocha/reporter/scout_ftr_reporter.ts +++ b/packages/kbn-test/src/functional_test_runner/lib/mocha/reporter/scout_ftr_reporter.ts @@ -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 []; diff --git a/packages/kbn-test/src/functional_test_runner/run_check_ftr_code_owners.ts b/packages/kbn-test/src/functional_test_runner/run_check_ftr_code_owners.ts index 6d99938ceea85..17bab9a44a44c 100644 --- a/packages/kbn-test/src/functional_test_runner/run_check_ftr_code_owners.ts +++ b/packages/kbn-test/src/functional_test_runner/run_check_ftr_code_owners.ts @@ -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); diff --git a/packages/kbn-test/src/mocha/junit_report_generation.js b/packages/kbn-test/src/mocha/junit_report_generation.js index 03bdc37f76720..2dfa0cdbfa3c0 100644 --- a/packages/kbn-test/src/mocha/junit_report_generation.js +++ b/packages/kbn-test/src/mocha/junit_report_generation.js @@ -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);