Skip to content
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

chore: Refactor GH action for Public Api Check #5203

Merged
merged 8 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions .github/actions/check-public-api/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 29 additions & 30 deletions build-packages/check-public-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
/* eslint-disable jsdoc/require-jsdoc */

import path, {
join,
resolve,
parse,
basename,
dirname,
posix,
sep
} from 'path';
import { join, resolve, parse, basename, dirname, posix, sep } from 'path';
import { promises, existsSync } from 'fs';
import { glob } from 'glob';
import { info, warning, error, getInput, setFailed } from '@actions/core';
Expand All @@ -20,15 +12,11 @@ import {
readIncludeExcludeWithDefaults,
transpileDirectory
} from '@sap-cloud-sdk/generator-common/internal';
import type { CompilerOptions } from 'typescript';
import { getPackages } from '@manypkg/get-packages';
import type { CompilerOptions } from 'typescript';

const { readFile, lstat, readdir } = promises;

const localConfigPath = join(
process.cwd(),
'build-packages/check-public-api/local-config.json'
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused and leftover from a previous PR. It was missed in the review.

const pathToTsConfigRoot = join(process.cwd(), 'tsconfig.json');
const pathRootNodeModules = join(process.cwd(), 'node_modules');
export const regexExportedIndex = /export(?:type)?\{([\w,]+)\}from'\./g;
Expand All @@ -48,14 +36,24 @@ function paths(pathToPackage: string): {
pathCompiled: string;
} {
return {
pathToSource: join(pathToPackage, 'src'),
pathToPackageJson: join(pathToPackage, 'package.json'),
pathToTsConfig: join(pathToPackage, 'tsconfig.json'),
pathToNodeModules: join(pathToPackage, 'node_modules'),
pathToSource: getPathWithPosixSeparator(join(pathToPackage, 'src')),
pathToPackageJson: getPathWithPosixSeparator(
join(pathToPackage, 'package.json')
),
pathToTsConfig: getPathWithPosixSeparator(
join(pathToPackage, 'tsconfig.json')
),
pathToNodeModules: getPathWithPosixSeparator(
join(pathToPackage, 'node_modules')
),
pathCompiled: 'dist'
};
}

function getPathWithPosixSeparator(filePath: string): string {
return filePath.split(sep).join(posix.sep);
}

function mockFileSystem(pathToPackage: string) {
const { pathToSource, pathToTsConfig, pathToNodeModules, pathToPackageJson } =
paths(pathToPackage);
Expand Down Expand Up @@ -105,15 +103,13 @@ function compareApisAndLog(
allExportedTypes: ExportedObject[]
): boolean {
let setsAreEqual = true;
const ignoredPaths = getListFromInput('ignored_paths');
const ignoredPathPattern = getInput('ignored_path_pattern');

allExportedTypes.forEach(exportedType => {
const normalizedPath = exportedType.path.split(sep).join(posix.sep);
const normalizedPath = getPathWithPosixSeparator(exportedType.path);

const isPathMatched = ignoredPaths.length
? ignoredPaths.some(ignoredPath =>
normalizedPath.includes(ignoredPath.split(sep).join(posix.sep))
)
const isPathMatched = ignoredPathPattern
? new RegExp(ignoredPathPattern).test(normalizedPath)
: false;
if (
!allExportedIndex.find(nameInIndex => exportedType.name === nameInIndex)
Expand Down Expand Up @@ -169,7 +165,10 @@ export async function checkApiOfPackage(pathToPackage: string): Promise<void> {
usePrettier: false
}
},
{ exclude: includeExclude?.exclude!, include: ['**/*.ts'] }
{
exclude: includeExclude ? includeExclude.exclude : [],
include: ['**/*.ts']
}
);

const forceInternalExports = getInput('force_internal_exports') === 'true';
Expand Down Expand Up @@ -309,16 +308,16 @@ export async function parseIndexFile(
...parseExportedObjectsInFile(fileContent).map(obj => obj.name)
];
const starFiles = captureGroupsFromGlobalRegex(
/export \* from '([\w\/.-]+)'/g,
/export \* from '([\w/.-]+)'/g,
fileContent
);
const starFileExports = await Promise.all(
starFiles.map(async relativeFilePath => {
const filePath = relativeFilePath.endsWith('.js')
const absolutePath = relativeFilePath.endsWith('.js')
? resolve(cwd, `${relativeFilePath.slice(0, -3)}.ts`)
: resolve(cwd, `${relativeFilePath}.ts`);

return parseIndexFile(filePath, forceInternalExports);
return parseIndexFile(absolutePath, forceInternalExports);
})
);
return [...localExports, ...starFileExports.flat()];
Expand Down Expand Up @@ -410,8 +409,8 @@ async function runCheckApi() {
for (const pkg of packagesToCheck) {
try {
await checkApiOfPackage(pkg.dir);
} catch (error) {
setFailed(`API check failed for ${pkg.relativeDir}: ${error}`);
} catch (e) {
setFailed(`API check failed for ${pkg.relativeDir}: ${e}`);
process.exit(1);
}
}
Expand Down
Loading