From 1f9e597c7470346b0a0c53a0b07f589d059da4de Mon Sep 17 00:00:00 2001 From: micoferdinand98 Date: Wed, 5 Oct 2022 14:41:44 +0800 Subject: [PATCH] ci: custom check for license being Apache-2.0 Fixes 1642 Signed-off-by: micoferdinand98 --- .../tools/periodicExecuter/package.json | 6 ++ examples/test-run-transaction/package.json | 6 ++ .../test/typescript/unit-test/package.json | 6 ++ tools/custom-checks/check-apache-version.ts | 77 +++++++++++++++++++ tools/custom-checks/run-custom-checks.ts | 12 ++- 5 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tools/custom-checks/check-apache-version.ts diff --git a/examples/cactus-example-electricity-trade/tools/periodicExecuter/package.json b/examples/cactus-example-electricity-trade/tools/periodicExecuter/package.json index 12f02997ee1..1f3c8e31045 100644 --- a/examples/cactus-example-electricity-trade/tools/periodicExecuter/package.json +++ b/examples/cactus-example-electricity-trade/tools/periodicExecuter/package.json @@ -7,6 +7,12 @@ "build-ts": "tsc", "tslint": "tslint -c tslint.json -p tsconfig.json" }, + "license": "Apache-2.0", + "author": { + "name": "Hyperledger Cacti Contributors", + "email": "cacti@lists.hyperledger.org", + "url": "https://www.hyperledger.org/use/cacti" + }, "dependencies": { "@types/node": "14.18.12", "ts-node": "9.1.1" diff --git a/examples/test-run-transaction/package.json b/examples/test-run-transaction/package.json index 20cb1a62cc8..3bbf42a80ce 100644 --- a/examples/test-run-transaction/package.json +++ b/examples/test-run-transaction/package.json @@ -12,6 +12,12 @@ "replace-blp-config-path": "ts-node replaceBLPConfigPath.ts", "init-test-run-transaction": "ln -s ../examples/test-run-transaction/node_modules ../../dist/node_modules" }, + "license": "Apache-2.0", + "author": { + "name": "Hyperledger Cacti Contributors", + "email": "cacti@lists.hyperledger.org", + "url": "https://www.hyperledger.org/use/cacti" + }, "dependencies": { "@types/node": "14.18.12", "body-parser": "1.19.2", diff --git a/packages/cactus-plugin-ledger-connector-fabric-socketio/src/test/typescript/unit-test/package.json b/packages/cactus-plugin-ledger-connector-fabric-socketio/src/test/typescript/unit-test/package.json index 711f8833db6..8d7b880ef53 100644 --- a/packages/cactus-plugin-ledger-connector-fabric-socketio/src/test/typescript/unit-test/package.json +++ b/packages/cactus-plugin-ledger-connector-fabric-socketio/src/test/typescript/unit-test/package.json @@ -8,6 +8,12 @@ "tslint": "tslint -c tslint.json -p tsconfig.json", "copy-static-assets": "ts-node copyStaticAssets.ts" }, + "license": "Apache-2.0", + "author": { + "name": "Hyperledger Cacti Contributors", + "email": "cacti@lists.hyperledger.org", + "url": "https://www.hyperledger.org/use/cacti" + }, "dependencies": { "@types/node": "14.18.12", "config": "1.31.0", diff --git a/tools/custom-checks/check-apache-version.ts b/tools/custom-checks/check-apache-version.ts new file mode 100644 index 00000000000..46eabcd61fe --- /dev/null +++ b/tools/custom-checks/check-apache-version.ts @@ -0,0 +1,77 @@ +import fs from "fs-extra"; +import path from "path"; +import { fileURLToPath } from "url"; +import { globby, Options as GlobbyOptions } from "globby"; +import { RuntimeError } from "run-time-error"; +import { isStdLibRecord } from "./is-std-lib-record"; +export interface ICheckSiblingDepVersionConsistencyRequest { + readonly argv: string[]; + readonly env: NodeJS.ProcessEnv; + /** * The version that will be used as the correct one when checking the sibling + * * package dependency version declarations. * If you omit this (optional) parameter then the root package.json file + * * will be parsed to obtain its value at runtime. */ readonly version?: string; +} +/** * Verifies that each sibling dependency is up to date with the latest version * that was released at any given time. + * * Note: This only checks dependency versions for the packages that are hosted + * * within this monorepo (Hyperledger Cactus) not dependencies in general. * + * * For example if the cmd-api-server package depends on the common package and + * * currently the project is on release v1.2.3 then the dependency declaration of + * * the package.json file of the cmd-api-server package should also use this + * * v1.2.3 version not something outdated such as v0.3.1 because that may cause + * * (according to our experience) strange build issues that confuse people a lot + * * for no good reason. * + * * @returns An array with the first item being a boolean indicating * 1) success (`true`) or 2) failure (`false`) */ +export async function checkPkgLicense( + req: ICheckSiblingDepVersionConsistencyRequest, +): Promise<[boolean, string[]]> { + const TAG = "[tools/checkapache-version.ts]"; + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const SCRIPT_DIR = __dirname; + const PROJECT_DIR = path.join(SCRIPT_DIR, "../../"); + const correctLicense = "Apache-2.0"; + console.log(`${TAG} SCRIPT_DIR=${SCRIPT_DIR}`); + console.log(`${TAG} PROJECT_DIR=${PROJECT_DIR}`); + if (!req) { + throw new RuntimeError(`req parameter cannot be falsy.`); + } + if (!req.argv) { + throw new RuntimeError(`req.argv cannot be falsy.`); + } + if (!req.env) { + throw new RuntimeError(`req.env cannot be falsy.`); + } + const globbyOpts: GlobbyOptions = { + cwd: PROJECT_DIR, + ignore: ["**/node_modules"], + }; + const DEFAULT_GLOB = "**/package.json"; + const pkgJsonPaths = await globby(DEFAULT_GLOB, globbyOpts); + const oasPaths = await globby(DEFAULT_GLOB, globbyOpts); + console.log(`openapi.json paths (${oasPaths.length})`); + console.log(`${TAG} Correct License: ${correctLicense}`); + const errors: string[] = []; + const checks = pkgJsonPaths.map(async (pathRel) => { + const filePathAbs = path.join(PROJECT_DIR, pathRel); + const pkgJson: unknown = await fs.readJSON(filePathAbs); + if (typeof pkgJson !== "object") { + errors.push(`ERROR: ${pathRel} package.json cannot be empty.`); + return; + } + if (!pkgJson) { + errors.push(`ERROR: ${pathRel} package.json cannot be empty.`); + return; + } + if (!isStdLibRecord(pkgJson)) { + return; + } + const { license } = pkgJson; + if (license != correctLicense) { + console.log( + `${TAG} Error: ${pathRel} Expected ${correctLicense}. got ${license}. `, + ); + } + }); + await Promise.all(checks); + return [errors.length === 0, errors]; +} diff --git a/tools/custom-checks/run-custom-checks.ts b/tools/custom-checks/run-custom-checks.ts index de5148bc99b..cc3b9ca18e1 100644 --- a/tools/custom-checks/run-custom-checks.ts +++ b/tools/custom-checks/run-custom-checks.ts @@ -2,6 +2,7 @@ import esMain from "es-main"; import { checkOpenApiJsonSpecs } from "./check-open-api-json-specs"; import { checkPackageJsonSort } from "./check-package-json-sort"; import { checkSiblingDepVersionConsistency } from "./check-sibling-dep-version-consistency"; +import { checkPkgLicense } from "./check-apache-version"; export async function runCustomChecks( argv: string[], @@ -18,11 +19,11 @@ export async function runCustomChecks( const [success, errors] = await checkOpenApiJsonSpecs({ argv, env }); overallErrors = overallErrors.concat(errors); overallSuccess = overallSuccess && success; - } + } { const req = { argv, env }; - const [success, errors] = await checkSiblingDepVersionConsistency(req); + const [success, errors] = await checkPkgLicense(req); overallErrors = overallErrors.concat(errors); overallSuccess = overallSuccess && success; } @@ -34,6 +35,13 @@ export async function runCustomChecks( overallSuccess = overallSuccess && success; } + { + const req = { argv, env }; + const [success, errors] = await checkSiblingDepVersionConsistency(req); + overallErrors = overallErrors.concat(errors); + overallSuccess = overallSuccess && success; + } + if (!overallSuccess) { overallErrors.forEach((it) => console.error(it)); } else {