Skip to content

Commit

Permalink
refactor: improve file list passing
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll committed Feb 22, 2023
1 parent bb36f8e commit fcd6408
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 150 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

59 changes: 30 additions & 29 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,42 @@ import {
} from "./storage.js";
/**
* Find all files inside a dir, recursively.
* @function getLcovFiles
* @function getLocalLcovFileList
* @param {string} dir Dir path string.
* @return {string[{<package_name>: <path_to_lcov_file>}]} Array with lcove file names with package names as key.
*/

export type FileList = { name: string; path: string }[];
export type FileList = { packageName: string; path: string }[];
export type LvocList = { packageName: string; lcov: LcovData }[];
export function getLcovFiles(dir: string, filelist?: FileList, deepness = 0) {
export function getLocalLcovFileList(
dir: string,
filelist?: FileList,
deepness = 0,
) {
let fileArray = filelist || [];
readdirSync(dir).forEach((file) => {
const isDir = statSync(path.join(dir, file)).isDirectory();
if (isDir && file !== "node_modules" && deepness < 10) {
fileArray = getLcovFiles(
fileArray = getLocalLcovFileList(
path.join(dir, file),
fileArray,
deepness + 1,
);
} else if (!isDir && file.endsWith("lcov.info")) {
const name = dir.split("/")[1];
const existing = fileArray.find((f) => f.name === name);
const packageName = dir.split("/")[1];
const existing = fileArray.find(
(f) => f.packageName === packageName,
);
if (existing) {
console.warn(
`found more than one lcov file for ${name}: ${path.join(
`found more than one lcov file for ${packageName}: ${path.join(
dir,
file,
)}`,
);
}
fileArray.push({
name,
packageName,
path: path.join(dir, file),
});
}
Expand All @@ -58,25 +64,23 @@ function filePath(
base: string,
prNumber: number | undefined,
monorepoBasePath: string,
file?: { name: string },
file?: { packageName: string },
) {
return `${repo.owner}/${repo.repo}/${base}/${
prNumber ? `${prNumber}/` : ""
}${monorepoBasePath}/${file ? `${file.name}.lcov.info` : ""}`;
}${monorepoBasePath}/${file ? `${file.packageName}.lcov.info` : ""}`;
}

export async function retrieveLocalLcovFiles(
monorepoBasePath: string,
fileList: FileList,
): Promise<{ lcovArrayForMonorepo: LvocList }> {
const lcovArray = getLcovFiles(monorepoBasePath);

const lcovArrayForMonorepo: LvocList = [];
for (const file of lcovArray) {
for (const file of fileList) {
try {
const rLcove = await promises.readFile(file.path, "utf8");
const data = await parse(rLcove);
lcovArrayForMonorepo.push({
packageName: file.name,
packageName: file.packageName,
lcov: data,
});
} catch (error) {
Expand Down Expand Up @@ -152,18 +156,17 @@ export async function retrieveTemporaryLcovFiles(
}

export async function retrieveLcovBaseFiles(
fileList: { packageName: string }[],
s3Client: S3Client,
s3Bucket: string,
repo: Context["repo"],
monorepoBasePath: string,
base: string,
mainBase: string,
): Promise<{ lcovBaseArrayForMonorepo: LvocList }> {
const lcovArray = getLcovFiles(monorepoBasePath);

const lcovBaseArrayForMonorepo: LvocList = [];
await Promise.all(
lcovArray.map(async (file) => {
fileList.map(async (file) => {
try {
// eslint-disable-next-line no-console
console.info(
Expand All @@ -187,7 +190,7 @@ export async function retrieveLcovBaseFiles(
);
}
lcovBaseArrayForMonorepo.push({
packageName: file.name,
packageName: file.packageName,
lcov: await parse(data),
});
} catch (err) {
Expand Down Expand Up @@ -220,12 +223,12 @@ export async function retrieveLcovBaseFiles(
);
}
lcovBaseArrayForMonorepo.push({
packageName: file.name,
packageName: file.packageName,
lcov: await parse(data),
});
} catch (secondTryErr) {
console.warn(
`no base lcov file found for ${file.name}: ${secondTryErr}`,
`no base lcov file found for ${file.packageName}: ${secondTryErr}`,
);
}
}
Expand Down Expand Up @@ -269,18 +272,17 @@ export async function setTemporarLvocFilesAsBase(
}

export async function uploadTemporaryLvocFiles(
fileList: FileList,
s3Client: S3Client,
s3Bucket: string,
repo: Context["repo"],
prNumber: number,
monorepoBasePath: string,
base: string,
) {
const lcovFiles = await getLcovFiles(monorepoBasePath);

// upload them
await Promise.all(
lcovFiles.map(async (file) => {
fileList.map(async (file) => {
const rLcove = await promises.readFile(file.path, "utf8");
// console.log("file", file.name, file.path, rLcove.length);
await uploadFile(
Expand All @@ -291,8 +293,6 @@ export async function uploadTemporaryLvocFiles(
);
}),
);

return lcovFiles.length;
}

export async function generateReport(
Expand Down Expand Up @@ -345,6 +345,7 @@ export async function cleanUpNonchangedTemporaryLcovs(
monorepoBasePath: string,
base: string,
) {
let cleaned = 0;
// remove temporary coverage files that are equal to the base
await Promise.all(
lcovArrayForMonorepo.map(async (lcov) => {
Expand All @@ -360,14 +361,14 @@ export async function cleanUpNonchangedTemporaryLcovs(
"removing non needed temporary lcov file (identical to base) for ",
lcov.packageName,
);
cleaned += 1;
await deleteFile(
s3Client,
s3Bucket,
filePath(repo, base, prNumber, monorepoBasePath, {
name: lcov.packageName,
}),
filePath(repo, base, prNumber, monorepoBasePath, lcov),
);
}
}),
);
return cleaned;
}
6 changes: 6 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getOctokit } from "@actions/github";
import {
cleanUpNonchangedTemporaryLcovs,
generateReport,
getLocalLcovFileList,
retrieveLcovBaseFiles,
retrieveTemporaryLcovFiles,
setTemporarLvocFilesAsBase,
Expand Down Expand Up @@ -31,6 +32,7 @@ try {
region: "",
Bucket: "repository-code-coverage",
};

const s3Client = (s3Config.region && new S3Client(s3Config)) || undefined;

// eslint-disable-next-line no-console
Expand All @@ -53,7 +55,9 @@ try {
if (!s3Client) {
throw new Error("need s3 client for upload");
}
const lcovFileList = getLocalLcovFileList(monorepoBasePath);
await uploadTemporaryLvocFiles(
lcovFileList,
s3Client,
s3Config.Bucket,
{ owner: "hokify", repo: "hokify-server" },
Expand All @@ -71,6 +75,7 @@ try {
}

const prNumber = 6419;
const lcovFileList = getLocalLcovFileList(monorepoBasePath);
const [{ lcovArrayForMonorepo }, { lcovBaseArrayForMonorepo }] =
await Promise.all([
retrieveTemporaryLcovFiles(
Expand All @@ -82,6 +87,7 @@ try {
base,
),
retrieveLcovBaseFiles(
lcovFileList,
s3Client,
s3Config.Bucket,
context.repo,
Expand Down
Loading

0 comments on commit fcd6408

Please sign in to comment.