Skip to content

Commit

Permalink
chore: various refactoring (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Dec 17, 2024
1 parent 10f066e commit 2b04b71
Show file tree
Hide file tree
Showing 13 changed files with 437 additions and 303 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
push:
branches:
- master
- main

# This will cancel previous runs when a branch or PR is updated
concurrency:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
push:
branches:
- master
- main

# This will cancel previous runs when a branch or PR is updated
concurrency:
Expand All @@ -13,7 +13,7 @@ concurrency:

jobs:
test:
name: Integration Tests (Node)
name: Test
runs-on: ubuntu-latest
timeout-minutes: 30

Expand Down
348 changes: 213 additions & 135 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions src/artifact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Zip from "adm-zip";
import { dirname, resolve } from "path";

import * as artifact from "@actions/artifact";
import * as core from "@actions/core";
import { context, getOctokit } from "@actions/github";

const { owner, repo } = context.repo;

// cannot use artifactClient because downloads are limited to uploads in the same workflow run
// cf. https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts#downloading-or-deleting-artifacts
export async function findPreviousArtifact(
token: string,
baseReport: string,
repository: string,
baseBranch: string
): Promise<[string, string]> {
core.startGroup(
`Searching artifact "${baseReport}" on repository "${repository}", on branch "${baseBranch}"`
);

const octokit = getOctokit(token);

let count = 100;
let artifactId: number | null = null;
let refCommitHash: string | undefined = undefined;
// Artifacts are returned in most recent first order.
for await (const res of octokit.paginate.iterator(octokit.rest.actions.listArtifactsForRepo, {
owner,
repo,
})) {
if (count == 0) {
break;
}
const artifact = res.data.find((artifact) => !artifact.expired && artifact.name === baseReport);
count = count - 1;
if (!artifact) {
await new Promise((resolve) => setTimeout(resolve, 900)); // avoid reaching the API rate limit

continue;
}

artifactId = artifact.id;
refCommitHash = artifact.workflow_run?.head_sha;
core.info(
`Found artifact named "${baseReport}" with ID "${artifactId}" from commit "${refCommitHash}"`
);
break;
}
core.endGroup();

if (artifactId) {
core.startGroup(
`Downloading artifact "${baseReport}" of repository "${repository}" with ID "${artifactId}"`
);
const res = await octokit.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: artifactId,
archive_format: "zip",
});

let referenceContent = "";
const zip = new Zip(Buffer.from(res.data as ArrayBuffer));
for (const entry of zip.getEntries()) {
core.info(`Loading gas reports from "${entry.entryName}"`);
referenceContent = zip.readAsText(entry);
}
core.endGroup();
return [refCommitHash as string, referenceContent];
} else {
core.error(`No workflow run found with an artifact named "${baseReport}"`);
throw Error(`No workflow run found with an artifact named "${baseReport}"`);
}
}

export async function uploadArtifact(headBranch: string, report: string) {
const headBranchEscaped = headBranch.replace(/[/\\]/g, "-");
const outReport = `${headBranchEscaped}.${report}`;

const localReportPath = resolve(report);

const artifactClient = artifact.create();

core.startGroup(`Upload new report from "${localReportPath}" as artifact named "${outReport}"`);
const uploadResponse = await artifactClient.uploadArtifact(
outReport,
[localReportPath],
dirname(localReportPath),
{
continueOnError: false,
}
);

if (uploadResponse.failedItems.length > 0) throw Error("Failed to upload gas report.");

core.info(`Artifact ${uploadResponse.artifactName} has been successfully uploaded!`);
core.endGroup();
}
Loading

0 comments on commit 2b04b71

Please sign in to comment.