Skip to content

Commit

Permalink
fix: upload lcov files after test run and re-use them on merge (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
simllll authored Feb 20, 2023
1 parent 8eb5eec commit 12898c8
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
55 changes: 48 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { S3Client } from "@aws-sdk/client-s3";
import { LcovData, parse } from "./lcov.js";
import { OktokitClient, upsertComment } from "./github.js";
import { generateDiffForMonorepo } from "./comment.js";
import { downloadFile, uploadFile } from "./storage.js";
import {
downloadFile,
getFileList,
renameFile,
uploadFile,
} from "./storage.js";

/**
* Find all files inside a dir, recursively.
Expand Down Expand Up @@ -35,10 +40,13 @@ export function getLcovFiles(dir: string, filelist?: FileList) {
function filePath(
repo: Context["repo"],
base: string,
prNumber: number | undefined,
monorepoBasePath: string,
file: { name: string },
file?: { name: string },
) {
return `${repo.owner}/${repo.repo}/${base}/${monorepoBasePath}/${file.name}.lcov.info`;
return `${repo.owner}/${repo.repo}/${base}/${
prNumber ? `${prNumber}/` : ""
}${monorepoBasePath}/${file ? `${file.name}.lcov.info` : ""}`;
}

export async function retrieveLcovFiles(monorepoBasePath: string) {
Expand Down Expand Up @@ -88,7 +96,7 @@ export async function retrieveLcovBaseFiles(
const data = await downloadFile(
s3Client,
bucket,
filePath(repo, base, monorepoBasePath, file),
filePath(repo, base, undefined, monorepoBasePath, file),
);
lcovBaseArrayForMonorepo.push({
packageName: file.name,
Expand All @@ -104,7 +112,7 @@ export async function retrieveLcovBaseFiles(
const data = await downloadFile(
s3Client,
bucket,
filePath(repo, base, monorepoBasePath, file),
filePath(repo, base, undefined, monorepoBasePath, file),
);
lcovBaseArrayForMonorepo.push({
packageName: file.name,
Expand All @@ -124,10 +132,43 @@ export async function retrieveLcovBaseFiles(
};
}

export async function uploadLvocFiles(
export async function setTemporarLvocFilesAsBase(
s3Client: S3Client,
s3Bucket: string,
repo: Context["repo"],
prNumber: number,
monorepoBasePath: string,
base: string,
) {
// rename them to base
const files = await getFileList(
s3Client,
s3Bucket,
filePath(repo, base, prNumber, monorepoBasePath),
);
await Promise.all(
files.map(async (file) => {
if (!file.Key) return;
// console.log("file", file.name, file.path, rLcove.length);
await renameFile(
s3Client,
s3Bucket,
filePath(repo, base, prNumber, monorepoBasePath, {
name: file.Key,
}),
filePath(repo, base, undefined, monorepoBasePath, {
name: file.Key,
}),
);
}),
);
}

export async function uploadTemporaryLvocFiles(
s3Client: S3Client,
s3Bucket: string,
repo: Context["repo"],
prNumber: number,
monorepoBasePath: string,
base: string,
) {
Expand All @@ -141,7 +182,7 @@ export async function uploadLvocFiles(
await uploadFile(
s3Client,
s3Bucket,
filePath(repo, base, monorepoBasePath, file),
filePath(repo, base, prNumber, monorepoBasePath, file),
rLcove,
);
}),
Expand Down
5 changes: 3 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { generateDiffForMonorepo } from "./comment.js";
import {
retrieveLcovBaseFiles,
retrieveLcovFiles,
uploadLvocFiles,
uploadTemporaryLvocFiles,
} from "./app.js";

try {
Expand Down Expand Up @@ -36,10 +36,11 @@ try {
if (!s3Client) {
throw new Error("need s3 client for upload");
}
await uploadLvocFiles(
await uploadTemporaryLvocFiles(
s3Client,
s3Config.Bucket,
{ owner: "@hokify", repo: "hokify-server" },
1,
monorepoBasePath,
base,
);
Expand Down
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { getInput, setFailed } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { S3Client } from "@aws-sdk/client-s3";
import { generateReport, uploadLvocFiles } from "./app.js";
import {
generateReport,
setTemporarLvocFilesAsBase,
uploadTemporaryLvocFiles,
} from "./app.js";

const token = getInput("github-token");
const monorepoBasePath = getInput("monorepo-base-path");
Expand Down Expand Up @@ -29,10 +33,11 @@ try {
}

// upload new lcov base files to storage
await uploadLvocFiles(
await setTemporarLvocFilesAsBase(
s3Client,
s3ConfigParsed.Bucket,
context.repo,
context.payload.pull_request.number,
monorepoBasePath,
base,
);
Expand All @@ -53,6 +58,17 @@ try {
context.payload.pull_request.number,
base,
);

if (s3Client) {
await uploadTemporaryLvocFiles(
s3Client,
s3ConfigParsed.Bucket,
context.repo,
context.payload.pull_request.number,
monorepoBasePath,
base,
);
}
}
} catch (err) {
// eslint-disable-next-line no-console
Expand Down
40 changes: 40 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {
CopyObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
ListObjectsV2Command,
PutObjectCommand,
S3Client,
} from "@aws-sdk/client-s3";
Expand All @@ -19,6 +22,43 @@ export async function uploadFile(
);
}

export async function renameFile(
s3Client: S3Client,
bucket: string,
fileFrom: string,
fileTo: string,
) {
await s3Client.send(
new CopyObjectCommand({
Bucket: bucket,
CopySource: fileFrom,
Key: fileTo,
}),
);

await s3Client.send(
new DeleteObjectCommand({
Bucket: bucket,
Key: fileFrom,
}),
);
}

export async function getFileList(
s3Client: S3Client,
bucket: string,
path: string,
) {
const response = await s3Client.send(
new ListObjectsV2Command({
Bucket: bucket,
Prefix: path,
}),
);

return response.Contents || [];
}

export async function downloadFile(
s3Client: S3Client,
bucket: string,
Expand Down

0 comments on commit 12898c8

Please sign in to comment.