Skip to content
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

Use the correct SHA for dependency snapshots from pull requests #401

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions dist/attachReleaseAssets/index.js

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

29 changes: 27 additions & 2 deletions dist/downloadSyft/index.js

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

29 changes: 27 additions & 2 deletions dist/runSyftAction/index.js

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

26 changes: 25 additions & 1 deletion src/github/SyftGithubAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,29 @@ export function getSbomFormat(): SyftOptions["format"] {
return (core.getInput("format") as SyftOptions["format"]) || "spdx-json";
}

/**
* Returns the SHA of the current commit, which will either be the head
* of the pull request branch or the value of github.context.sha, depending
* on the event type.
*/
export function getSha(): string {
const pull_request_events = [
"pull_request",
"pull_request_comment",
"pull_request_review",
"pull_request_review_comment",
// Note that pull_request_target is omitted here.
// That event runs in the context of the base commit of the PR,
// so the snapshot should not be associated with the head commit.
];
if (pull_request_events.includes(github.context.eventName)) {
const pr = (github.context.payload as PullRequestEvent).pull_request;
return pr.head.sha;
} else {
return github.context.sha;
}
}

/**
* Uploads a SBOM as a workflow artifact
* @param contents SBOM file contents
Expand Down Expand Up @@ -382,7 +405,8 @@ export async function uploadDependencySnapshot(): Promise<void> {
);
return;
}
const { workflow, job, runId, repo, sha, ref } = github.context;
const { workflow, job, runId, repo, ref } = github.context;
const sha = getSha();
const client = getClient(repo, core.getInput("github-token"));

const snapshot = JSON.parse(
Expand Down