Skip to content

Commit

Permalink
ci(github): add check to validate exported types being correct
Browse files Browse the repository at this point in the history
Primary Changes
---------------
1. Added get-all-tgz-path.ts to get all tgz files path
2. Added run-attw-on-tgz.ts to run attw on each tgz filepath
3. Added workflow to run the are-the-types-wrong script

Fixes: hyperledger-cacti#3140

Signed-off-by: ruzell22 <ruzell.vince.aquino@accenture.com>
  • Loading branch information
ruzell22 committed Sep 30, 2024
1 parent 499f703 commit 3d16008
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"approveformyorg",
"askar",
"Askar",
"attw",
"Authz",
"authzn",
"AWSSM",
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/are-the-types-wrong.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: AreTheTypesWrong scan

env:
NODEJS_VERSION: v18.18.2

on:
push:
# Publish `main` as Docker `latest` image.
branches:
- main

# Publish `v1.2.3` tags as releases.
tags:
- v*
pull_request:
branches:
- main

jobs:
scanning:
name: AreTheTypesWrong scan
runs-on: ubuntu-22.04
steps:
- name: Install Indy SDK
run: >
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CE7709D068DB5E88 \
&& sudo add-apt-repository "deb https://repo.sovrin.org/sdk/deb bionic stable" \
&& sudo apt-get update \
&& sudo apt-get install -y \
libindy \
libnullpay \
libvcx \
indy-cli \
&& sudo rm -f /etc/apt/sources.list.d/sovrin.list*
- name: Set up NodeJS ${{ env.NODEJS_VERSION }}
uses: actions/setup-node@v4.0.3
with:
node-version: ${{ env.NODEJS_VERSION }}
- uses: actions/checkout@v4.1.7
- name: Installing AreTheTypesWrong Library
run: npm i -g @arethetypeswrong/cli
- run: npm run configure
- name: Running AreTheTypesWrong scan
run: yarn run are-the-types-wrong
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"format:prettier": "prettier --write --config .prettierrc.js \"./**/{openapi.json,*.ts,*.js}\"",
"spellcheck": "cspell lint --no-progress \"*/*/src/**/*.{js,ts}\"",
"tsc": "NODE_OPTIONS=\"--max_old_space_size=3072\" tsc --build --verbose",
"are-the-types-wrong": "TS_NODE_PROJECT=./tools/tsconfig.json node --trace-deprecation --experimental-modules --abort-on-uncaught-exception --loader ts-node/esm --experimental-specifier-resolution=node ./tools/custom-checks/run-attw-on-tgz.ts",
"codegen": "run-s 'codegen:warmup-*' codegen:lerna codegen:cleanup",
"codegen:cleanup": "rm --force --verbose ./openapitools.json",
"codegen:lerna": "lerna run codegen",
Expand Down
53 changes: 53 additions & 0 deletions tools/custom-checks/get-all-tgz-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from "path";
import { fileURLToPath } from "url";
import { globby, Options as GlobbyOptions } from "globby";
import lernaCfg from "../../lerna.json" assert { type: "json" };

/**
* Interface for the response of the getAllTgzPath function.
* @property {string} gitRootDir - The root directory of the git project.
* @property {Array<string>} relativePaths - An array of relative paths to the
* package directories.
* @property {Array<string>} absolutePaths - An array of absolute paths to the
* package directories.
*/

export interface IGetAllTgzPathResponse {
readonly relativePaths: Readonly<Array<string>>;
}

/**
* Asynchronous function to get all tgz filepaths in a Lerna monorepo.
* @returns {Promise<IGetAllTgzPathResponse>} A promise that resolves to an
* object containing the root directory of the git project, and arrays of
* relative paths to the all tgz files.
*/

export async function getAllTgzPath(): Promise<IGetAllTgzPathResponse> {
const TAG = "[tools/get-all-tgz-path.ts]";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const SCRIPT_DIR = __dirname;
const PROJECT_DIR = path.join(SCRIPT_DIR, "../../");

console.log(`${TAG} SCRIPT_DIR=${SCRIPT_DIR}`);
console.log(`${TAG} PROJECT_DIR=${PROJECT_DIR}`);

const globbyOpts: GlobbyOptions = {
cwd: PROJECT_DIR,
onlyFiles: true,
expandDirectories: false,
ignore: ["**/node_modules"],
};

const tgzFilesPattern = lernaCfg.packages.map(
(pkg: string) => `${pkg}/**/hyperledger-*.tgz`,
);

const tgzFilesRelative = await globby(tgzFilesPattern, globbyOpts);
console.log("%s found %s tgz files.", TAG, tgzFilesRelative.length);

return {
relativePaths: tgzFilesRelative,
};
}
44 changes: 44 additions & 0 deletions tools/custom-checks/run-attw-on-tgz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import esMain from "es-main";
import { getAllTgzPath } from "./get-all-tgz-path";
import { exec } from "child_process";
import { exit } from "process";

let attwFailedPackages: string[] = [];

export async function runAttwOnTgz(): Promise<void> {
const TAG = "[tools/custom-checks/run-attw-on-tgz.ts]";
console.log(`${TAG} Fetching .tgz file paths.`);

{
const { relativePaths: tgzFilesRelative } = await getAllTgzPath();
console.log(`${TAG} Found ${tgzFilesRelative.length} .tgz files.`);
for (const filePath of tgzFilesRelative) {
await execCommand("attw", filePath);
}
}

function execCommand(binaryName: string, filePath: string): Promise<void> {
return new Promise((resolve) => {
const command = binaryName + " ./" + filePath;
exec(command, (error, stdout) => {
if (error) {
console.log(stdout);
attwFailedPackages.push(filePath);
resolve();
} else {
resolve();
}
});
});
}
}

if (esMain(import.meta)) {
await runAttwOnTgz();
if (attwFailedPackages.length > 0) {
console.log("Types are wrong for these packages:");
console.log(attwFailedPackages);
exit(1);
}
exit(0);
}

0 comments on commit 3d16008

Please sign in to comment.