-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10f066e
commit 2b04b71
Showing
13 changed files
with
437 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.