-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: github correlator name when run in matrix build #482
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ const exeSuffix = process.platform == "win32" ? ".exe" : ""; | |
* Tries to get a unique artifact name or otherwise as appropriate as possible | ||
*/ | ||
export function getArtifactName(): string { | ||
const fileName = core.getInput("artifact-name"); | ||
const fileName = getArtifactNameInput(); | ||
|
||
// if there is an explicit filename just return it, this could cause issues | ||
// where earlier sboms are overwritten by later ones | ||
|
@@ -93,6 +93,13 @@ export function getArtifactName(): string { | |
return `${repo}-${job}${stepName}.${extension}`; | ||
} | ||
|
||
/** | ||
* Returns the artifact-name input value | ||
*/ | ||
function getArtifactNameInput() { | ||
return core.getInput("artifact-name"); | ||
} | ||
|
||
/** | ||
* Gets a reference to the syft command and executes the syft action | ||
* @param input syft input parameters | ||
|
@@ -443,10 +450,19 @@ export async function uploadDependencySnapshot(): Promise<void> { | |
fs.readFileSync(githubDependencySnapshotFile).toString("utf8") | ||
) as DependencySnapshot; | ||
|
||
let correlator = `${workflow}_${job}`; | ||
// if running in a matrix build, it is not possible to determine a unique value, | ||
// so a user must explicitly specify the artifact-name input, there isn't any | ||
// other indicator of being run within a matrix build, so we must use that | ||
// here in order to properly correlate dependency snapshots | ||
const artifactInput = getArtifactNameInput(); | ||
if (artifactInput) { | ||
correlator += `_${artifactInput}`; | ||
} | ||
|
||
// Need to add the job and repo details | ||
snapshot.job = { | ||
correlator: | ||
core.getInput("dependency-snapshot-correlator") || `${workflow}_${job}`, | ||
correlator: core.getInput("dependency-snapshot-correlator") || correlator, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to hide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
id: `${runId}`, | ||
}; | ||
snapshot.sha = sha; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,4 +75,47 @@ describe("GitHub Snapshot", () => { | |
|
||
expect(submission).toMatchSnapshot(); | ||
}); | ||
|
||
it("runs with artifact-name input", async () => { | ||
setData({ | ||
inputs: { | ||
path: "tests/fixtures/npm-project", | ||
"dependency-snapshot": "true", | ||
"upload-artifact": "false", | ||
"artifact-name": "my-matrix-build-1", | ||
}, | ||
context: { | ||
...context.push({ | ||
ref: "main", | ||
}), | ||
sha: "f293f09uaw90gwa09f9wea", | ||
workflow: "my-workflow", | ||
job: "default-import-job", | ||
action: "__anchore_sbom-action", | ||
}, | ||
}); | ||
|
||
await action.runSyftAction(); | ||
await action.uploadDependencySnapshot(); | ||
|
||
// validate the request was made | ||
expect(requestArgs).toBeDefined(); | ||
expect(requestArgs).toHaveLength(2); | ||
expect(requestArgs[0]).toBe("POST /repos/test-org/test-repo/dependency-graph/snapshots"); | ||
|
||
// check the resulting snapshot file | ||
const data = requestArgs[1].data; | ||
const submission = JSON.parse(data); | ||
|
||
expect(submission.scanned).toBeDefined(); | ||
|
||
// redact changing data | ||
submission.scanned = ""; | ||
submission.detector.version = ""; | ||
|
||
expect(submission.job).toBeDefined() | ||
expect(submission.job.correlator).toContain("my-matrix-build-1") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason not to assert more specifically? It looks like this will always be |
||
|
||
expect(submission).toMatchSnapshot(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are 3 paths through here:
dependency-snapshot-correlator
input is definedartifact-name
input is definedartifact-name
nordependency-snapshot-correlator
is defined.Is it worth having a table test to hit all 3 of these? AFAICT, only scenario 2 is currently exercised by tests.
It might be worth extracting a helper method to make the test set up simpler.