Skip to content

Commit

Permalink
Updated script
Browse files Browse the repository at this point in the history
Signed-off-by: jagpreetsinghsasan <[email protected]>
  • Loading branch information
jagpreetsinghsasan committed Sep 8, 2023
1 parent d1c2036 commit 5573f4f
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions tools/optimize-ci.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { execSync } from "child_process";
import { existsSync, readFileSync } from "fs";
import { NpmExtractor } from '@h4ad/dependency-extractor';
import { NpmExtractor } from "@h4ad/dependency-extractor";

// this function generates a list of packages, extensions and examples which need to be
// tested by the CI due to a given git diff
async function packagesExtensionsAndExamplesAffectedByDiff(gitDiffFilePaths) {
let uniquePackagesExtensionsAndExamplesAffectedByDiff = new Set();
const dependencyGraph = await generateDependencyGraph();
//for each package
const uniquePackagesAffectedByDiff = extractUniquePackagesFromDiff(
gitDiffFilePaths,
);
const uniquePackagesAffectedByDiff =
extractUniquePackagesFromDiff(gitDiffFilePaths);
const allPackagesAndDependenciesAffectedByDiff = new Set();
uniquePackagesAffectedByDiff.forEach((uniquePackage) => {
dependencyGraph.forEach((cactusPackageDependencySet, cactusPackage) => {
Expand All @@ -22,9 +21,8 @@ async function packagesExtensionsAndExamplesAffectedByDiff(gitDiffFilePaths) {
});

//for each extension
const uniqueExtensionsAffectedByDiff = extractUniqueExtensionsFromDiff(
gitDiffFilePaths,
);
const uniqueExtensionsAffectedByDiff =
extractUniqueExtensionsFromDiff(gitDiffFilePaths);
const allExtensionsAndDependenciesAffectedByDiff = new Set();
uniqueExtensionsAffectedByDiff.forEach((uniqueExtension) => {
dependencyGraph.forEach((cactusExtensionDependencySet, cactusExtension) => {
Expand All @@ -36,9 +34,8 @@ async function packagesExtensionsAndExamplesAffectedByDiff(gitDiffFilePaths) {
});

//for each example
const uniqueExamplesAffectedByDiff = extractUniqueExamplesFromDiff(
gitDiffFilePaths,
);
const uniqueExamplesAffectedByDiff =
extractUniqueExamplesFromDiff(gitDiffFilePaths);
const allExamplesAndDependenciesAffectedByDiff = new Set();
uniqueExamplesAffectedByDiff.forEach((uniqueExample) => {
dependencyGraph.forEach((cactusExampleDependencySet, cactusExample) => {
Expand Down Expand Up @@ -233,27 +230,27 @@ function checkIfNoDiff(filePaths) {
}

// return true when diff exists and contains only markdown file changes
function checkIfOnlyDocsDiff(filePaths) {
let onlyDocsDiff = true;
if (checkIfNoDiff(filePaths)) onlyDocsDiff = false;
filePaths.forEach((filePath) => {
const fileExtension = filePath.substring(
filePath.lastIndexOf(".") + 1,
filePath.length,
);
if (
!(filePath.includes("docs/") || filePath.includes(".github/workflows/"))
) {
if (
fileExtension != "md" &&
filePath != "package.json" &&
filePath != "yarn.lock" &&
filePath != "tools/optimize-ci.js"
)
onlyDocsDiff = false;
}
});
return onlyDocsDiff;
function checkIfCiNeedsToRunForDiff(filePaths) {
let ciNeedsToRunForDiff = false;
if (!checkIfNoDiff(filePaths)) {
ciNeedsToRunForDiff = false;
filePaths.forEach((filePath) => {
const fileExtension = filePath.substring(
filePath.lastIndexOf(".") + 1,
filePath.length,
);
if (!(filePath.includes("docs/") || filePath.includes(".github/"))) {
if (
fileExtension != "md" &&
filePath != "package.json" &&
filePath != "yarn.lock" &&
filePath != "tools/optimize-ci.js"
)
ciNeedsToRunForDiff = true;
}
});
}
return ciNeedsToRunForDiff;
}

// return string[] containing all the filepaths in git diff with main branch
Expand Down Expand Up @@ -284,7 +281,10 @@ const gitDiffFilePaths = getGitDiff();
// checking if its a documentation PR
// TODO: Update this task to work on docs/ folder of packages
// instead of checking for .md files
optimizedCliOutput.set("onlyDocsDiff", checkIfOnlyDocsDiff(gitDiffFilePaths));
optimizedCliOutput.set(
"ciNeedsToRunForDiff",
checkIfCiNeedsToRunForDiff(gitDiffFilePaths),
);

// printing a list of all packages which need to be tested by CI as they are affected
optimizedCliOutput.set(
Expand All @@ -294,7 +294,7 @@ optimizedCliOutput.set(

// This outputs something like
// {
// "onlyDocsDiff": false,
// "ciNeedsToRunForDiff": true,
// "affectedPluginsExamplesAndExtensions": [
// "cactus-cmd-api-server",
// "cactus-plugin-htlc-coordinator-besu",
Expand All @@ -307,4 +307,4 @@ optimizedCliOutput.set(
// "cactus-example-supply-chain-backend"
// ]
// }
console.log(JSON.stringify(Object.fromEntries(optimizedCliOutput)));
console.log(JSON.stringify(Object.fromEntries(optimizedCliOutput)));

0 comments on commit 5573f4f

Please sign in to comment.