Skip to content

Commit cedcba6

Browse files
authored
fix(sbom-tools): allow CodeQL report fetching for PR heads (#343)
Github runs CodeQL on PR merge commits, not PR head commits, so we need a way to identify the CodeQL runs for those merge refs. Do that by accepting a PR number as an environment variable.
1 parent 44624b7 commit cedcba6

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

packages/sbom-tools/src/commands/fetch-codeql-results.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ResolvedCommitInformation = {
1717
repo: string;
1818
forPackage?: string;
1919
commit: string;
20+
alternativeRef?: string;
2021
};
2122

2223
type UnresolvedRepoInformation = Omit<ResolvedCommitInformation, 'commit'> &
@@ -25,7 +26,7 @@ type UnresolvedRepoInformation = Omit<ResolvedCommitInformation, 'commit'> &
2526
// Get CodeQL SARIF reports for a single commit in a single repository
2627
async function getSingleCommitSarif(
2728
octokit: Octokit,
28-
{ owner, repo, commit }: ResolvedCommitInformation
29+
{ owner, repo, commit, alternativeRef }: ResolvedCommitInformation
2930
): Promise<unknown[]> {
3031
const reportIds = new Set<number>();
3132
for (let page = 0; ; page++) {
@@ -36,7 +37,7 @@ async function getSingleCommitSarif(
3637
});
3738
const previousPageAlreadyHadSomeData = reportIds.size > 0;
3839
for (const item of data) {
39-
if (item.commit_sha === commit) {
40+
if (item.commit_sha === commit || item.ref === alternativeRef) {
4041
reportIds.add(item.id);
4142
}
4243
}
@@ -183,11 +184,17 @@ async function getCurrentRepo(): Promise<ResolvedCommitInformation> {
183184
encoding: 'utf8',
184185
})
185186
).stdout.trim();
187+
188+
let alternativeRef;
189+
if (process.env.GITHUB_PR_NUMBER) {
190+
alternativeRef = `refs/pull/${process.env.GITHUB_PR_NUMBER}/merge`;
191+
}
192+
186193
const repo = repoForPackageJSON(
187194
JSON.parse(await fs.readFile('package.json', 'utf8')),
188195
'<root>'
189196
);
190-
return { ...repo, commit };
197+
return { ...repo, commit, alternativeRef };
191198
}
192199

193200
export async function fetchCodeQLResults(

0 commit comments

Comments
 (0)