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

ci: custom check for license being Apache-2.0 #2131

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@hyperledger/cactus-examples-check-connection-ethereum-validator",
"version": "2.0.0-alpha.1",
"license": "Apache-2.0",
"private": true,
"scripts": {
"start": "node ../../dist/packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/www.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "periodicExecuter",
"version": "0.0.1",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "npm run build-ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "transferNumericAsset",
"version": "0.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "npm run build-ts && npm run copy-static-assets",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "periodicExecuter",
"version": "0.0.1",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "npm run build-ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "transferNumericAsset",
"version": "0.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "npm run build-ts && npm run copy-static-assets",
Expand Down
1 change: 1 addition & 0 deletions examples/test-run-transaction/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "test-run-transaction",
"license": "Apache-2.0",
"private": true,
"scripts": {
"start": "node ../../dist/packages/cactus-cmd-socketio-server/src/main/typescript/routing-interface/www.js",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@hyperledger/cactus",
"license": "Apache-2.0",
"private": true,
"description": "Root project for Cactus which contains all core components and plugins developed by the project.",
"workspaces": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "testcli",
"license": "Apache-2.0",
"version": "0.0.0",
"private": true,
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "testcli",
"license": "Apache-2.0",
"version": "0.0.0",
"private": true,
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@hyperledger/cactus-dummy-package",
"version": "2.0.0-alpha.1",
"license": "Apache-2.0",
"description": "Dummy package for testing.",
"main": "index.js"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "validatorDriver",
"version": "0.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "npm run build-ts && npm run copy-static-assets",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "validatorDriver",
"version": "0.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"build": "npm run build-ts && npm run copy-static-assets",
Expand Down
84 changes: 84 additions & 0 deletions tools/custom-checks/check-pkg-licenses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 ICheckPkgLicensesRequest {
readonly argv: string[];
readonly env: NodeJS.ProcessEnv;
readonly expectedLicenseSpdx?: string;
}

export const DEFAULT_EXPECTED_LICENSE_SPDX = "Apache-2.0" as const;

/**
* @returns An array with the first item being a boolean indicating
* 1) success (`true`) or 2) failure (`false`) and the second item being an
* array of strings which contain all the errors found (if any).
*/
export async function checkPkgLicenses(
req: ICheckPkgLicensesRequest,
): Promise<[boolean, string[]]> {
const TAG = "[tools/check-pkg-licenses.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}`);
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 expectedLicenseSpdx =
req.expectedLicenseSpdx ?? DEFAULT_EXPECTED_LICENSE_SPDX;

console.log(`${TAG} req.expectedLicenseSpdx=${req.expectedLicenseSpdx}`);
console.log(
`${TAG} DEFAULT_EXPECTED_LICENSE_SPDX=${DEFAULT_EXPECTED_LICENSE_SPDX}`,
);
console.log(`${TAG} Concluded expectedLicenseSpdx=${expectedLicenseSpdx}`);

const globbyOpts: GlobbyOptions = {
cwd: PROJECT_DIR,
ignore: ["**/node_modules"],
};
const DEFAULT_GLOB = "**/package.json";

console.log(`${TAG} Glob pattern=%s - options=%o`, DEFAULT_GLOB, globbyOpts);
const pkgJsonPaths = await globby(DEFAULT_GLOB, globbyOpts);
console.log(`${TAG} Glob found %o package.json files.`, pkgJsonPaths.length);

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 !== expectedLicenseSpdx) {
const eMsg = `${TAG} Error: ${pathRel} has incorrect license SPDX. Expected ${expectedLicenseSpdx}, got ${license} instead.`;
console.log(eMsg);
errors.push(eMsg);
}
});
await Promise.all(checks);
return [errors.length === 0, errors];
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function checkSiblingDepVersionConsistency(

const pkgJsonPaths = await globby(DEFAULT_GLOB, globbyOpts);
console.log(`${TAG} package.json paths: (${pkgJsonPaths.length}): `);

const lernaJsonPathAbs = path.join(PROJECT_DIR, "./lerna.json");
console.log(`${TAG} Reading root lerna.json at ${lernaJsonPathAbs}`);
const lernaJson = await fs.readJSON(lernaJsonPathAbs);
Expand Down
1 change: 1 addition & 0 deletions tools/custom-checks/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"license": "Apache-2.0",
"type": "module"
}
6 changes: 6 additions & 0 deletions tools/custom-checks/run-custom-checks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import esMain from "es-main";
import { checkOpenApiJsonSpecs } from "./check-open-api-json-specs";
import { checkPackageJsonSort } from "./check-package-json-sort";
import { checkPkgLicenses } from "./check-pkg-licenses";
import { checkSiblingDepVersionConsistency } from "./check-sibling-dep-version-consistency";
import { checkPkgNpmScope } from "./check-pkg-npm-scope";

Expand Down Expand Up @@ -50,6 +51,11 @@ export async function runCustomChecks(
overallSuccess = overallSuccess && success;
}

{
const [success, errors] = await checkPkgLicenses({ argv, env });
overallErrors = overallErrors.concat(errors);
overallSuccess = overallSuccess && success;
}
if (!overallSuccess) {
overallErrors.forEach((it) => console.error(it));
} else {
Expand Down
1 change: 1 addition & 0 deletions tools/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"license": "Apache-2.0",
"type": "module"
}
2 changes: 1 addition & 1 deletion weaver/common/policy-dsl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"url": "git@github.ibm.com:dlt-interoperability/policy-dsl.git"
},
"author": "",
"license": "ISC",
"license": "Apache-2.0",
"devDependencies": {
"ava": "4.3.3",
"jest": "29.6.2"
Expand Down
1 change: 1 addition & 0 deletions weaver/docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "weaver-dlt-interoperability",
"version": "0.0.0",
"license": "Apache-2.0",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
Expand Down
Loading