-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Add shared validation runner package #258768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tylersmalley
merged 4 commits into
elastic:main
from
tylersmalley:split-dev-validation-runner
Mar 23, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * 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 { existsSync } from 'fs'; | ||
| import Path from 'path'; | ||
|
|
||
| import { REPO_ROOT } from '@kbn/repo-info'; | ||
|
|
||
| import { getMoonExecutablePath, normalizeRepoRelativePath } from './query_projects'; | ||
|
|
||
| type ChangedFilesScope = 'local' | 'staged' | 'branch'; | ||
|
|
||
| export interface GetMoonChangedFilesOptions { | ||
| scope: ChangedFilesScope; | ||
| base?: string; | ||
| head?: string; | ||
| } | ||
|
|
||
| interface MoonChangedFilesResponse { | ||
| files: string[]; | ||
| } | ||
|
|
||
| /** Builds CLI args for `moon query changed-files` based on scope. */ | ||
| export const buildChangedFilesArgs = ({ scope, base, head }: GetMoonChangedFilesOptions) => { | ||
| const args = ['query', 'changed-files']; | ||
|
|
||
| switch (scope) { | ||
| case 'local': | ||
| args.push('--local'); | ||
| break; | ||
| case 'staged': | ||
| args.push('--local', '--status', 'staged'); | ||
| break; | ||
| case 'branch': | ||
| if (base) args.push('--base', base); | ||
| if (head) args.push('--head', head); | ||
| break; | ||
| } | ||
|
|
||
| return args; | ||
| }; | ||
|
|
||
| /** | ||
| * Queries Moon for changed files in the given scope. | ||
| * | ||
| * Returns repo-relative paths of files that exist on disk (deleted files are excluded). | ||
| */ | ||
| export const getMoonChangedFiles = async ({ | ||
| scope, | ||
| base, | ||
| head, | ||
| }: GetMoonChangedFilesOptions): Promise<string[]> => { | ||
| const execa = (await import('execa')).default; | ||
| const moonExec = await getMoonExecutablePath(); | ||
| const args = buildChangedFilesArgs({ scope, base, head }); | ||
|
|
||
| const { stdout } = await execa(moonExec, args, { | ||
| cwd: REPO_ROOT, | ||
| stdin: 'ignore', | ||
| env: { | ||
| ...process.env, | ||
| CI_STATS_DISABLED: 'true', | ||
| }, | ||
| }); | ||
|
|
||
| const { files } = JSON.parse(stdout) as MoonChangedFilesResponse; | ||
|
|
||
| return files | ||
| .map(normalizeRepoRelativePath) | ||
| .filter((file) => existsSync(Path.resolve(REPO_ROOT, file))) | ||
| .sort((a, b) => a.localeCompare(b)); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /* | ||
| * 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 Path from 'path'; | ||
| import { existsSync } from 'fs'; | ||
|
|
||
| import { | ||
| getRemoteDefaultBranchRefs, | ||
| resolveNearestMergeBase, | ||
| type ValidationDownstreamMode, | ||
| } from '@kbn/dev-utils'; | ||
| import { REPO_ROOT } from '@kbn/repo-info'; | ||
|
|
||
| export type MoonDownstreamMode = ValidationDownstreamMode; | ||
|
|
||
| /** Minimal Moon project metadata needed for affected-source resolution. */ | ||
| export interface MoonProject { | ||
| id: string; | ||
| sourceRoot: string; | ||
| } | ||
|
|
||
| /** Derived summary of affected projects for root-project escalation handling. */ | ||
| export interface MoonAffectedProjectSummary { | ||
| sourceRoots: string[]; | ||
| isRootProjectAffected: boolean; | ||
| } | ||
|
|
||
| /** Resolved base revision metadata for Moon affected queries. */ | ||
| export interface MoonAffectedBase { | ||
| base: string; | ||
| baseRef: string; | ||
| } | ||
|
|
||
| interface MoonQueryProjectsResponse { | ||
| projects: Array<{ | ||
| id: string; | ||
| source: string; | ||
| config?: { | ||
| project?: { | ||
| metadata?: { | ||
| sourceRoot?: string; | ||
| }; | ||
| }; | ||
| }; | ||
| }>; | ||
| } | ||
|
|
||
| /** Options for resolving the affected base revision from git state. */ | ||
| export interface ResolveMoonAffectedBaseOptions { | ||
| headRef?: string; | ||
| } | ||
|
|
||
| export const ROOT_MOON_PROJECT_ID = 'kibana'; | ||
|
|
||
| let moonExecutablePath: string | undefined; | ||
|
|
||
| /** Normalizes repository-relative paths to POSIX separators for stable matching. */ | ||
| export const normalizeRepoRelativePath = (pathValue: string) => | ||
| Path.normalize(pathValue).split(Path.sep).join('/'); | ||
|
|
||
| /** Resolves the path to the Moon executable. */ | ||
| export const getMoonExecutablePath = async () => { | ||
| if (moonExecutablePath) { | ||
| return moonExecutablePath; | ||
| } | ||
|
|
||
| const moonBinPath = Path.resolve(REPO_ROOT, 'node_modules/.bin/moon'); | ||
| if (existsSync(moonBinPath)) { | ||
| moonExecutablePath = moonBinPath; | ||
| return moonExecutablePath; | ||
| } | ||
|
|
||
| const execa = (await import('execa')).default; | ||
| const { stdout } = await execa('yarn', ['--silent', 'which', 'moon'], { | ||
| cwd: REPO_ROOT, | ||
| stdin: 'ignore', | ||
| }); | ||
|
|
||
| moonExecutablePath = stdout.trim(); | ||
| return moonExecutablePath; | ||
| }; | ||
|
|
||
| /** Resolves the base revision used for Moon affected comparisons. */ | ||
| export const resolveMoonAffectedBase = async ({ | ||
| headRef = 'HEAD', | ||
| }: ResolveMoonAffectedBaseOptions = {}): Promise<MoonAffectedBase> => { | ||
| const envBase = process.env.GITHUB_PR_MERGE_BASE?.trim(); | ||
| if (envBase) { | ||
| return { | ||
| base: envBase, | ||
| baseRef: 'GITHUB_PR_MERGE_BASE', | ||
| }; | ||
| } | ||
|
|
||
| const baseRefs = await getRemoteDefaultBranchRefs(); | ||
| if (baseRefs.length === 0) { | ||
| throw new Error( | ||
| 'Unable to resolve a remote default branch for affected type check. Set GITHUB_PR_MERGE_BASE to override.' | ||
| ); | ||
| } | ||
|
|
||
| const bestCandidate = await resolveNearestMergeBase({ | ||
| baseRefs, | ||
| headRef, | ||
| }); | ||
| if (!bestCandidate) { | ||
| throw new Error( | ||
| `Unable to resolve merge-base for affected type check from remote default branches: ${baseRefs.join( | ||
| ', ' | ||
| )}.` | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| base: bestCandidate.mergeBase, | ||
| baseRef: bestCandidate.baseRef, | ||
| }; | ||
| }; | ||
|
|
||
| const parseMoonProjectsResponse = (stdout: string): MoonProject[] => { | ||
| const response = JSON.parse(stdout) as MoonQueryProjectsResponse; | ||
| return response.projects.map((project) => { | ||
| const sourceRoot = project.config?.project?.metadata?.sourceRoot ?? project.source; | ||
| return { | ||
| id: project.id, | ||
| sourceRoot: normalizeRepoRelativePath(sourceRoot), | ||
| }; | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Queries Moon for affected projects by piping pre-resolved changed files JSON | ||
| * into `moon query projects --affected`. | ||
| * | ||
| * Use this when changed files have already been resolved to avoid duplicate Moon queries. | ||
| */ | ||
| export const getAffectedMoonProjectsFromChangedFiles = async ({ | ||
| changedFilesJson, | ||
| downstream = 'none', | ||
| }: { | ||
| changedFilesJson: string; | ||
| downstream?: MoonDownstreamMode; | ||
| }): Promise<MoonProject[]> => { | ||
| const execa = (await import('execa')).default; | ||
| const moonExec = await getMoonExecutablePath(); | ||
|
|
||
| const projectArgs = ['query', 'projects', '--affected']; | ||
| if (downstream !== 'none') { | ||
| projectArgs.push('--downstream', downstream); | ||
| } | ||
|
|
||
| const { stdout } = await execa(moonExec, projectArgs, { | ||
| cwd: REPO_ROOT, | ||
| input: changedFilesJson, | ||
| env: { | ||
| ...process.env, | ||
| CI_STATS_DISABLED: 'true', | ||
| }, | ||
| }); | ||
|
|
||
| return parseMoonProjectsResponse(stdout); | ||
| }; | ||
|
|
||
| /** Summarizes affected Moon projects into non-root source roots and root-project flag. */ | ||
| export const summarizeAffectedMoonProjects = ( | ||
| projects: MoonProject[] | ||
| ): MoonAffectedProjectSummary => { | ||
| const nonRootProjects = projects.filter((project) => project.id !== ROOT_MOON_PROJECT_ID); | ||
|
|
||
| return { | ||
| sourceRoots: nonRootProjects.map((project) => project.sourceRoot), | ||
| isRootProjectAffected: projects.some((project) => project.id === ROOT_MOON_PROJECT_ID), | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/platform/packages/shared/kbn-dev-validation-runner/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # @kbn/dev-validation-runner | ||
|
|
||
| Shared orchestration helpers for validation-style developer CLIs. |
28 changes: 28 additions & 0 deletions
28
src/platform/packages/shared/kbn-dev-validation-runner/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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 { | ||
| describeValidationScope, | ||
| describeValidationNoTargetsScope, | ||
| describeValidationScoping, | ||
| resolveValidationBaseContext, | ||
| } from './src/run_validation_command'; | ||
| export type { ValidationBaseContext } from './src/run_validation_command'; | ||
| export { | ||
| resolveValidationAffectedProjects, | ||
| type ValidationAffectedProjectsContext, | ||
| } from './src/resolve_validation_run_context'; | ||
| export { | ||
| buildValidationCliArgs, | ||
| formatReproductionCommand, | ||
| hasValidationRunFlags, | ||
| readValidationRunFlags, | ||
| VALIDATION_RUN_HELP, | ||
| VALIDATION_RUN_STRING_FLAGS, | ||
| } from './src/validation_run_cli'; |
14 changes: 14 additions & 0 deletions
14
src/platform/packages/shared/kbn-dev-validation-runner/jest.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* | ||
| * 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". | ||
| */ | ||
|
|
||
| module.exports = { | ||
| preset: '@kbn/test', | ||
| rootDir: '../../../../..', | ||
| roots: ['<rootDir>/src/platform/packages/shared/kbn-dev-validation-runner'], | ||
| }; |
10 changes: 10 additions & 0 deletions
10
src/platform/packages/shared/kbn-dev-validation-runner/kibana.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "type": "shared-common", | ||
| "id": "@kbn/dev-validation-runner", | ||
| "owner": [ | ||
| "@elastic/kibana-operations" | ||
| ], | ||
| "group": "platform", | ||
| "visibility": "shared", | ||
| "devOnly": true | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this a temporary change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's just preventing it from reporting in the child moon process.