Coverage: Add some DX improvements to by codeowner script (#112673)

* Coverage: Add some DX improvements to by codeowner script

* Potential fix for code scanning alert no. 3796: Shell command built from environment values

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* Potential fix for code scanning alert no. 3797: Shell command built from environment values

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* fix package.json and yarn lock

* reorder imports

* fix issue for frontend-platform: exclude files in any /test/ dir

* wip

* add ora spinner for codeowners manifest step

* cleanup

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
Paul Marbach
2025-10-22 15:09:39 -04:00
committed by GitHub
co-authored by Copilot Autofix powered by AI
parent 23726c7c90
commit 123e46c33b
6 changed files with 298 additions and 40 deletions
+43 -22
View File
@@ -1,30 +1,44 @@
#!/usr/bin/env node
const { AutoComplete } = require('enquirer');
const cp = require('node:child_process');
const { readFile } = require('node:fs/promises');
const { hideBin } = require('yargs/helpers');
const yargs = require('yargs/yargs');
const { CODEOWNERS_JSON_PATH: CODEOWNERS_MANIFEST_CODEOWNERS_PATH } = require('./codeowners-manifest/constants.js');
const { getCodeowners } = require('./codeowners-manifest/utils.js');
const JEST_CONFIG_PATH = 'jest.config.codeowner.js';
async function promptCodeownerName() {
const teams = await getCodeowners();
const prompt = new AutoComplete({
name: 'flavor',
message: 'Select your team to run tests by codeowner.',
limit: 10,
choices: teams.filter((team) => team.startsWith('@grafana/')),
});
return await prompt.run();
}
/**
* Run test coverage for a specific codeowner
* @param {string} codeownerName - The codeowner name to run coverage for
* @param {string} codeownersPath - Path to the teams.json file
* @param {string} jestConfigPath - Path to the Jest config file
* @param {boolean} noOpen - Whether to skip opening the coverage report in the browser
*/
async function runTestCoverageByCodeowner(codeownerName, codeownersPath, jestConfigPath) {
const codeownersJson = await readFile(codeownersPath, 'utf8');
const codeowners = JSON.parse(codeownersJson);
if (!codeowners.includes(codeownerName)) {
throw new Error(`Codeowner ${codeownerName} was not found in ${codeownersPath}, check spelling`);
async function runTestCoverageByCodeowner(codeownerName, noOpen = process.env.CI === 'true') {
const teams = await getCodeowners();
if (!teams.includes(codeownerName)) {
throw new Error(`Codeowner "${codeownerName}" was not found.`);
}
process.env.TEAM_NAME = codeownerName;
process.env.CODEOWNER_NAME = codeownerName;
process.env.SHOULD_OPEN_COVERAGE_REPORT = String(!noOpen);
return new Promise((resolve, reject) => {
const child = cp.spawn('jest', [`--config=${jestConfigPath}`], { stdio: 'inherit', shell: true });
const child = cp.spawn('jest', [`--config=${JEST_CONFIG_PATH}`], {
stdio: 'inherit',
shell: true,
});
child.on('error', (error) => {
reject(new Error(`Failed to start Jest: ${error.message}`));
@@ -43,22 +57,29 @@ async function runTestCoverageByCodeowner(codeownerName, codeownersPath, jestCon
if (require.main === module) {
(async () => {
try {
const codeownerName = process.argv[2];
const argv = yargs(hideBin(process.argv)).parse();
const teams = await getCodeowners();
let codeownerName = argv._[0];
if (codeownerName != null && !teams.includes(codeownerName)) {
const msg = `Codeowner "${codeownerName}" was not found.`;
codeownerName = null;
if (process.env.CI === 'true') {
throw new Error(msg);
} else {
console.warn(`⚠️ ${msg}`);
}
}
if (!codeownerName) {
console.error('Codeowner argument is required ...');
console.error('Usage: yarn test:coverage:by-codeowner @grafana/team-name');
process.exit(1);
codeownerName = await promptCodeownerName();
}
const noOpen = argv['open'] === false;
console.log(`🧪 Running test coverage for codeowner: ${codeownerName}`);
await runTestCoverageByCodeowner(codeownerName, CODEOWNERS_MANIFEST_CODEOWNERS_PATH, JEST_CONFIG_PATH);
await runTestCoverageByCodeowner(codeownerName, noOpen);
} catch (e) {
if (e.code === 'ENOENT') {
console.error(`Could not read ${CODEOWNERS_MANIFEST_CODEOWNERS_PATH} ...`);
} else {
console.error(e.message);
}
console.error(e.message);
process.exit(1);
}
})();