Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 49 additions & 37 deletions dist/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/github/GithubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ export class GithubClient {
* Lists the workflow run artifacts for a completed workflow
* @param branch the branch name
*/
async findLatestWorkflowRunForBranch({
async findLatestWorkflowRunsForBranch({
branch,
}: {
branch: string;
}): Promise<WorkflowRun | undefined> {
}): Promise<WorkflowRun[] | undefined> {
const response = await this.client.rest.actions.listWorkflowRunsForRepo({
...this.repo,
branch,
Expand All @@ -240,13 +240,13 @@ export class GithubClient {
page: 1,
});

debugLog("findLatestWorkflowRunForBranch response:", response);
debugLog("findLatestWorkflowRunsForBranch response:", response);

if (response.status >= 400) {
throw new Error("Unable to findLatestWorkflowRunForBranch");
throw new Error("Unable to findLatestWorkflowRunsForBranch");
}

return response.data.workflow_runs[0];
return response.data.workflow_runs;
}

/**
Expand Down
80 changes: 47 additions & 33 deletions src/github/SyftGithubAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,28 +349,34 @@ async function comparePullRequestTargetArtifact(): Promise<void> {
const client = getClient(repo, core.getInput("github-token"));

const pr = (payload as PullRequestEvent).pull_request;
const branchWorkflow = await client.findLatestWorkflowRunForBranch({
const branchWorkflows = await client.findLatestWorkflowRunsForBranch({
branch: pr.base.ref,
});

debugLog("Got branchWorkflow:", branchWorkflow);
if (!branchWorkflows) {
return;
}

if (branchWorkflow) {
const baseBranchArtifacts = await client.listWorkflowRunArtifacts({
runId: branchWorkflow.id,
});
for (const branchWorkflow of branchWorkflows) {
debugLog("Got branchWorkflow:", branchWorkflow);

debugLog("Got baseBranchArtifacts:", baseBranchArtifacts);
if (branchWorkflow) {
const baseBranchArtifacts = await client.listWorkflowRunArtifacts({
runId: branchWorkflow.id,
});

for (const artifact of baseBranchArtifacts) {
if (artifact.name === getArtifactName()) {
const baseArtifact = await client.downloadWorkflowRunArtifact({
artifactId: artifact.id,
});
debugLog("Got baseBranchArtifacts:", baseBranchArtifacts);

core.info(
`Downloaded SBOM from ref '${pr.base.ref}' to ${baseArtifact}`,
);
for (const artifact of baseBranchArtifacts) {
if (artifact.name === getArtifactName()) {
const baseArtifact = await client.downloadWorkflowRunArtifact({
artifactId: artifact.id,
});

core.info(
`Downloaded SBOM from ref '${pr.base.ref}' to ${baseArtifact}`,
);
}
}
}
}
Expand Down Expand Up @@ -519,7 +525,7 @@ export async function attachReleaseAssets(): Promise<void> {
const matcher = new RegExp(sbomArtifactPattern);

const artifacts = await client.listCurrentWorkflowArtifacts();
let matched = artifacts.filter((a) => {
const matched = artifacts.filter((a) => {
const matches = matcher.test(a.name);
if (matches) {
core.debug(`Found artifact: ${a.name}`);
Expand All @@ -534,28 +540,36 @@ export async function attachReleaseAssets(): Promise<void> {
core.info(
"No artifacts found in this workflow. Searching for release artifacts from prior workflow...",
);
const latestRun = await client.findLatestWorkflowRunForBranch({
const latestRuns = await client.findLatestWorkflowRunsForBranch({
branch: release.target_commitish,
});

debugLog("Got latest run for prior workflow", latestRun);
if (!latestRuns) {
return;
}

if (latestRun) {
const runArtifacts = await client.listWorkflowRunArtifacts({
runId: latestRun.id,
});
for (const latestRun of latestRuns) {
debugLog("Got latest run for prior workflow", latestRun);

matched = runArtifacts.filter((a) => {
const matches = matcher.test(a.name);
if (matches) {
core.debug(`Found run artifact: ${a.name}`);
} else {
core.debug(
`Run artifact: ${a.name} not matching ${sbomArtifactPattern}`,
);
}
return matches;
});
if (latestRun) {
const runArtifacts = await client.listWorkflowRunArtifacts({
runId: latestRun.id,
});

matched.push(
...runArtifacts.filter((a) => {
const matches = matcher.test(a.name);
if (matches) {
core.debug(`Found run artifact: ${a.name}`);
} else {
core.debug(
`Run artifact: ${a.name} not matching ${sbomArtifactPattern}`,
);
}
return matches;
}),
);
}
}
}

Expand Down
24 changes: 12 additions & 12 deletions tests/GithubClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test, { describe, it, beforeEach, mock } from "node:test";
import assert from "node:assert";
import { getMocks } from "./mocks";
import type { Release, WorkflowRun } from "@octokit/webhooks-types";

const { data, mocks, setData, restoreInitialData } = getMocks(test);

Expand All @@ -11,8 +12,6 @@ for (const [name, factory] of Object.entries(mocks)) {

mock.method(Date, "now", () => 1482363367071);

import type { Release } from "@octokit/webhooks-types";

const githubClient = await import("../src/github/GithubClient");
const { debugLog } = githubClient;

Expand Down Expand Up @@ -71,23 +70,24 @@ describe("Github Client", { timeout: 30000 }, () => {
});

it("calls workflow run for branch methods", async () => {
const expected: Partial<WorkflowRun>[] = [
{
id: 3,
head_branch: "main",
conclusion: "success",
},
];
setData({
workflowRuns: [
{
id: 3,
head_branch: "main",
conclusion: "success",
},
],
workflowRuns: expected,
});
const client = githubClient.getClient(
{ owner: "test-owner", repo: "test-repo" },
"token"
);
const run: any = await client.findLatestWorkflowRunForBranch({
const runs: any = await client.findLatestWorkflowRunsForBranch({
branch: "main",
});
assert.equal(run.id, 3);
assert.deepEqual(runs, expected);
});

it("calls findRelease methods", async () => {
Expand Down Expand Up @@ -201,7 +201,7 @@ describe("Github Client", { timeout: 30000 }, () => {
}

try {
await client.findLatestWorkflowRunForBranch({
await client.findLatestWorkflowRunsForBranch({
branch: "main",
});
assert.fail("exception should be thrown");
Expand Down
Loading