From 801b2551e357cea9dd855201a2d99fd3a7caad41 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 16 Jan 2023 00:17:32 +0100 Subject: [PATCH] repo-tools: add release tag validation option Signed-off-by: Patrik Oldsberg --- package.json | 2 +- .../src/commands/api-reports/api-extractor.ts | 26 +++++++++++++++++++ .../src/commands/api-reports/api-reports.ts | 2 ++ packages/repo-tools/src/commands/index.ts | 4 +++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 5383ba3d51..c06d353501 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "build:backend": "yarn workspace backend build", "build:all": "backstage-cli repo build --all", "build:api-reports": "yarn build:api-reports:only --tsc", - "build:api-reports:only": "backstage-repo-tools api-reports --allow-warnings 'packages/core-components,plugins/+(catalog|catalog-import|git-release-manager|jenkins|kubernetes)' -o ae-wrong-input-file-type", + "build:api-reports:only": "backstage-repo-tools api-reports --allow-warnings 'packages/core-components,plugins/+(catalog|catalog-import|git-release-manager|jenkins|kubernetes)' -o ae-wrong-input-file-type --validate-release-tags", "build:api-docs": "LANG=en_EN yarn build:api-reports --docs", "tsc": "tsc", "tsc:full": "backstage-cli repo clean && tsc --skipLibCheck false --incremental false", diff --git a/packages/repo-tools/src/commands/api-reports/api-extractor.ts b/packages/repo-tools/src/commands/api-reports/api-extractor.ts index c9081255b8..91df053f22 100644 --- a/packages/repo-tools/src/commands/api-reports/api-extractor.ts +++ b/packages/repo-tools/src/commands/api-reports/api-extractor.ts @@ -334,6 +334,7 @@ interface ApiExtractionOptions { tsconfigFilePath: string; allowWarnings?: boolean | string[]; omitMessages?: string[]; + validateReleaseTags?: boolean; } export async function runApiExtraction({ @@ -343,6 +344,7 @@ export async function runApiExtraction({ tsconfigFilePath, allowWarnings = false, omitMessages = [], + validateReleaseTags = false, }: ApiExtractionOptions) { await fs.remove(outputDir); @@ -494,6 +496,30 @@ export async function runApiExtraction({ compilerState, }); + // This release tag validation makes sure that the release tag of known entry points match expectations. + // The root index entrypoint is only allowed @public exports, while /alpha and /beta only allow @alpha and @beta. + if (validateReleaseTags) { + if (['index', 'alpha', 'beta'].includes(name)) { + const report = await fs.readFile( + extractorConfig.reportFilePath, + 'utf8', + ); + const lines = report.split(/\r?\n/); + const expectedTag = name === 'index' ? 'public' : name; + for (let i = 0; i < lines.length; i += 1) { + const line = lines[i]; + const match = line.match(/^\/\/ @(alpha|beta|public)/); + if (match && match[1] !== expectedTag) { + throw new Error( + `Unexpected release tag ${match[1]} in ${ + extractorConfig.reportFilePath + } at line ${i + 1}`, + ); + } + } + } + } + if (!extractorResult.succeeded) { if (shouldLogInstructions) { logApiReportInstructions(); diff --git a/packages/repo-tools/src/commands/api-reports/api-reports.ts b/packages/repo-tools/src/commands/api-reports/api-reports.ts index 66436644d2..a917975750 100644 --- a/packages/repo-tools/src/commands/api-reports/api-reports.ts +++ b/packages/repo-tools/src/commands/api-reports/api-reports.ts @@ -33,6 +33,7 @@ type Options = { allowWarnings?: string; allowAllWarnings?: boolean; omitMessages?: string; + validateReleaseTags?: boolean; } & OptionValues; export const buildApiReports = async (paths: string[] = [], opts: Options) => { @@ -90,6 +91,7 @@ export const buildApiReports = async (paths: string[] = [], opts: Options) => { tsconfigFilePath, allowWarnings: allowAllWarnings || allowWarnings, omitMessages: Array.isArray(omitMessages) ? omitMessages : [], + validateReleaseTags: opts.validateReleaseTags, }); } if (cliPackageDirs.length > 0) { diff --git a/packages/repo-tools/src/commands/index.ts b/packages/repo-tools/src/commands/index.ts index f7a16b6d4e..f283e9c743 100644 --- a/packages/repo-tools/src/commands/index.ts +++ b/packages/repo-tools/src/commands/index.ts @@ -37,6 +37,10 @@ export function registerCommands(program: Command) { '-o, --omit-messages ', 'select some message code to be omited on the API Extractor (comma separated values i.e ae-cyclic-inherit-doc,ae-missing-getter )', ) + .option( + '--validate-release-tags', + 'Turn on release tag validation for the public, beta, and alpha APIs', + ) .description('Generate an API report for selected packages') .action( lazy(() =>