8000 fix: github correlator name when run in matrix build (#482) · anchore/sbom-action@a5bbe18 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5bbe18

Browse files
fix: github correlator name when run in matrix build (#482)
* fix: github correlator name when run in matrix build Signed-off-by: Keith Zantow <kzantow@gmail.com> * chore: add explicit check for correlator containing artifact-name Signed-off-by: Keith Zantow <kzantow@gmail.com> * test: explicitly test different correlator paths Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * chore: commit build output Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> --------- Signed-off-by: Keith Zantow <kzantow@gmail.com> Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> Co-authored-by: Will Murphy <willmurphyscode@users.noreply.github.com>
1 parent 55dc4ee commit a5bbe18

File tree

6 files changed

+343
-9
lines changed

6 files changed

+343
-9
lines changed

dist/attachReleaseAssets/index.js

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/downloadSyft/index.js

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runSyftAction/index.js

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/github/SyftGithubAction.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const exeSuffix = process.platform == "win32" ? ".exe" : "";
3636
* Tries to get a unique artifact name or otherwise as appropriate as possible
3737
*/
3838
export function getArtifactName(): string {
39-
const fileName = core.getInput("artifact-name");
39+
const fileName = getArtifactNameInput();
4040

4141
// if there is an explicit filename just return it, this could cause issues
4242
// where earlier sboms are overwritten by later ones
@@ -93,6 +93,13 @@ export function getArtifactName(): string {
9393
return `${repo}-${job}${stepName}.${extension}`;
9494
}
9595

96+
/**
97+
* Returns the artifact-name input value
98+
*/
99+
function getArtifactNameInput() {
100+
return core.getInput("artifact-name");
101+
}
102+
96103
/**
97104
* Gets a reference to the syft command and executes the syft action
98105
* @param input syft input parameters
@@ -443,10 +450,19 @@ export async function uploadDependencySnapshot(): Promise<void> {
443450
fs.readFileSync(githubDependencySnapshotFile).toString("utf8")
444451
) as DependencySnapshot;
445452

453+
let correlator = `${workflow}_${job}`;
454+
// if running in a matrix build, it is not possible to determine a unique value,
455+
// so a user must explicitly specify the artifact-name input, there isn't any
456+
// other indicator of being run within a matrix build, so we must use that
457+
// here in order to properly correlate dependency snapshots
458+
const artifactInput = getArtifactNameInput();
459+
if (artifactInput) {
460+
correlator += `_${artifactInput}`;
461+
}
462+
446463
// Need to add the job and repo details
447464
snapshot.job = {
448-
correlator:
449-
core.getInput("dependency-snapshot-correlator") || `${workflow}_${job}`,
465+
correlator: core.getInput("dependency-snapshot-correlator") || correlator,
450466
id: `${runId}`,
451467
};
452468
snapshot.sha = sha;

tests/integration/GitHubSnapshot.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe("GitHub Snapshot", () => {
6767
const data = requestArgs[1].data;
6868
const submission = JSON.parse(data);
6969

70+
expect(submission.job.correlator).toEqual("my-workflow_default-import-job")
7071
expect(submission.scanned).toBeDefined();
7172

7273
// redact changing data
@@ -75,4 +76,90 @@ describe("GitHub Snapshot", () => {
7576

7677
expect(submission).toMatchSnapshot();
7778
});
79+
80+
it("runs with artifact-name input", async () => {
81+
setData({
82+
inputs: {
83+
path: "tests/fixtures/npm-project",
84+
"dependency-snapshot": "true",
85+
"upload-artifact": "false",
86+
"artifact-name": "my-matrix-build-1",
87+
},
88+
context: {
89+
...context.push({
90+
ref: "main",
91+
}),
92+
sha: "f293f09uaw90gwa09f9wea",
93+
workflow: "my-workflow",
94+
job: "default-import-job",
95+
action: "__anchore_sbom-action",
96+
},
97+
});
98+
99+
await action.runSyftAction();
100+
await action.uploadDependencySnapshot();
101+
102+
// validate the request was made
103+
expect(requestArgs).toBeDefined();
104+
expect(requestArgs).toHaveLength(2);
105+
expect(requestArgs[0]).toBe("POST /repos/test-org/test-repo/dependency-graph/snapshots");
106+
107+
// check the resulting snapshot file
108+
const data = requestArgs[1].data;
109+
const submission = JSON.parse(data);
110+
111+
expect(submission.scanned).toBeDefined();
112+
113+
// redact changing data
114+
submission.scanned = "";
115+
submission.detector.version = "";
116+
117+
expect(submission.job).toBeDefined()
118+
expect(submission.job.correlator).toEqual("my-workflow_default-import-job_my-matrix-build-1")
119+
120+
expect(submission).toMatchSnapshot();
121+
});
122+
123 628B +
it("runs with dependency-snapshot-correlator defined", async () => {
124+
setData({
125+
inputs: {
126+
path: "tests/fixtures/npm-project",
127+
"dependency-snapshot": "true",
128+
"upload-artifact": "false",
129+
"dependency-snapshot-correlator": "some-correlator",
130+
},
131+
context: {
132+
...context.push({
133+
ref: "main",
134+
}),
135+
sha: "f293f09uaw90gwa09f9wea",
136+
workflow: "my-workflow",
137+
job: "default-import-job",
138+
action: "__anchore_sbom-action",
139+
},
140+
});
141+
142+
await action.runSyftAction();
143+
await action.uploadDependencySnapshot();
144+
145+
// validate the request was made
146+
expect(requestArgs).toBeDefined();
147+
expect(requestArgs).toHaveLength(2);
148+
expect(requestArgs[0]).toBe("POST /repos/test-org/test-repo/dependency-graph/snapshots");
149+
150+
// check the resulting snapshot file
151+
const data = requestArgs[1].data;
152+
const submission = JSON.parse(data);
153+
154+
expect(submission.scanned).toBeDefined();
155+
156+
// redact changing data
157+
submission.scanned = "";
158+
submission.detector.version = "";
159+
160+
expect(submission.job).toBeDefined()
161+
expect(submission.job.correlator).toEqual("some-correlator")
162+
163+
expect(submission).toMatchSnapshot();
164+
});
78165
});

0 commit comments

Comments
 (0)
0