Files
renovate/tools/sync-module-labels.ts
Jamie Tanna e31dfd74e8 refactor(tools/sync-module-labels): move shared code into a shared helper (#42249)
As otherwise this leads to us importing the tool itself, which has a
top-level function that runs the tool.

As noted on a comment on #42012, this means that the tests are now
executing `gh` which isn't intended.

Co-authored-by: Claude Sonnet 4.6 <jamie.tanna+claude-code@mend.io>
2026-03-30 17:31:35 +00:00

79 lines
2.1 KiB
TypeScript

import { Command } from 'commander';
import { init, logger } from '../lib/logger/index.ts';
import { exec } from './utils/exec.ts';
import type { CliOptions, GitHubLabel } from './utils/sync-module-labels.ts';
import {
formatCreateLabelCommands,
formatMissingLabels,
getExpectedModuleLabels,
getMissingModuleLabels,
} from './utils/sync-module-labels.ts';
const defaultRepo = 'renovatebot/renovate';
async function getRepoLabels(repo: string): Promise<GitHubLabel[]> {
const result = await exec('gh', [
'label',
'list',
'-R',
repo,
'--limit',
'1000',
'--json',
'name,color,description',
]);
const labels = JSON.parse(result.stdout) as GitHubLabel[];
return labels.sort((left, right) => left.name.localeCompare(right.name));
}
await init();
process.on('unhandledRejection', (err) => {
// Will print "unhandledRejection err is not defined"
logger.error({ err }, 'unhandledRejection');
process.exit(-1);
});
const program = new Command('node tools/sync-module-labels.ts')
.description('Check that datasource/manager/platform GitHub labels exist.')
.option('--repo <owner/name>', `Repository to query`, defaultRepo)
.option(
'--show-commands',
'Print gh label create commands for any missing labels',
)
.action(async (options: CliOptions) => {
const expectedLabels = getExpectedModuleLabels();
const existingLabels = await getRepoLabels(options.repo);
const missingLabels = getMissingModuleLabels(
expectedLabels,
existingLabels,
);
if (missingLabels.length === 0) {
logger.info(
`All datasource/manager/platform labels exist in ${options.repo}.`,
);
return;
}
logger.error(
`Missing ${missingLabels.length} datasource/manager/platform labels in ${options.repo}:\n${formatMissingLabels(
missingLabels,
)}`,
);
if (options.showCommands) {
logger.info(
`Run the following commands to create the missing labels:\n${formatCreateLabelCommands(
options.repo,
missingLabels,
)}`,
);
}
process.exitCode = 1;
});
void program.parseAsync();