Skip to content

Commit

Permalink
feat: store to r2 artefacts
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdlangton committed Sep 30, 2024
1 parent 823c11e commit 4f9bc48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions functions/github/repos/[org]/[repo]/sarif.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export async function onRequestGet(context) {
return Response.json({ sarif: files, errors })
}

const saveArtefact = async (r2adapter, jsonData, artefactUuid, artefactType) => {
const saveArtefact = async (r2adapter, strContent, artefactUuid, artefactType) => {
const objectPath = `${artefactType}/${artefactUuid}.json`
const putOptions = { httpMetadata: { contentType: 'application/json', contentEncoding: 'utf8' } }
const reportInfo = await r2adapter.put(objectPath, JSON.stringify(jsonData), putOptions)
const reportInfo = await r2adapter.put(objectPath, strContent, putOptions)
console.log(objectPath, reportInfo)
return objectPath
}
Expand Down
18 changes: 12 additions & 6 deletions functions/github/repos/[org]/[repo]/spdx.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ export async function onRequestGet(context) {
console.log('content', content)
continue
}
const { spdxId, spdxStr, findingIds } = await process(prisma, verificationResult.session, repoName, content)
const { spdxStr, findingIds } = await process(prisma, verificationResult.session, repoName, content)
findings = [...findings, ...findingIds]
const objectPrefix = `github/${app.installationId}/repos/${repoName}/sbom/`
console.log(`${repoName}/sbom/${spdxId}.json`, await env.r2artefact.put(`${objectPrefix}${spdxId}.json`, spdxStr, putOptions))
const spdxObjectPath = await saveArtefact(env.r2artefact, spdxStr, crypto.randomUUID(), `spdx`)
files.push(content)
}
const memberKeys = await prisma.MemberKey.findMany({
Expand All @@ -91,16 +90,23 @@ export async function onRequestGet(context) {
console.log('content', content)
continue
}
const { spdxId, spdxStr, findingIds } = await process(prisma, verificationResult.session, repoName, content)
const { spdxStr, findingIds } = await process(prisma, verificationResult.session, repoName, content)
findings = [...findings, ...findingIds]
const objectPrefix = `github/pat_${memberKey.id}/repos/${repoName}/sbom/`
console.log(`${repoName}/sbom/${spdxId}.json`, await env.r2artefact.put(`${objectPrefix}${spdxId}.json`, spdxStr, putOptions))
const spdxObjectPath = await saveArtefact(env.r2artefact, spdxStr, crypto.randomUUID(), `spdx`)
files.push({ spdx: content, errors })
}

return Response.json({ ok: true, files, findings })
}

const saveArtefact = async (r2adapter, strContent, artefactUuid, artefactType) => {
const objectPath = `${artefactType}/${artefactUuid}.json`
const putOptions = { httpMetadata: { contentType: 'application/json', contentEncoding: 'utf8' } }
const reportInfo = await r2adapter.put(objectPath, strContent, putOptions)
console.log(objectPath, reportInfo)
return objectPath
}

const process = async (prisma, session, repoName, content) => {
const spdx = content.sbom
const packages = JSON.stringify(spdx.packages)
Expand Down

0 comments on commit 4f9bc48

Please sign in to comment.