Skip to content

Commit

Permalink
[FTR] Merge Alerting Apis from x-pack/test_serverless/shared into `…
Browse files Browse the repository at this point in the history
…x-pack/test/api_integration/deployment_agnostic` (elastic#193975)

Follow up of [this pr](elastic#192216),
per [this
discussion](elastic#192216 (comment)).

Also, switch from `svlCommonApi` to `samlAuth` for internal headers.

---------

Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Dzmitry Lemechko <[email protected]>
(cherry picked from commit 10c3373)
  • Loading branch information
wayneseymour committed Dec 19, 2024
1 parent 5f539ab commit a3c5c22
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 16 deletions.
8 changes: 6 additions & 2 deletions packages/kbn-code-owners/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
44 changes: 40 additions & 4 deletions packages/kbn-code-owners/src/file_code_owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ 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`);
};

/**
* Get the .github/CODEOWNERS entries, prepared for path matching.
Expand Down Expand Up @@ -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.
`,
},
}
);
}
8 changes: 6 additions & 2 deletions packages/kbn-scout-reporting/src/reporting/playwright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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 [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-test/src/mocha/junit_report_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a3c5c22

Please sign in to comment.