fix function exporting and test results

Signed-off-by: Meredith Lancaster <[email protected]>
This commit is contained in:
Meredith Lancaster
2025-12-08 13:49:54 -08:00
parent 9ca26d4946
commit c034e76488
2 changed files with 19 additions and 16 deletions
@@ -1,5 +1,5 @@
import {MockAgent, setGlobalDispatcher} from 'undici' import {MockAgent, setGlobalDispatcher} from 'undici'
import {createStorageRecord} from '../src/attest' import {createStorageRecord} from '../src/artifact-metadata'
describe('createStorageRecord', () => { describe('createStorageRecord', () => {
const originalEnv = process.env const originalEnv = process.env
@@ -12,7 +12,7 @@ describe('createStorageRecord', () => {
digest: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", digest: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
} }
const registryParams = { const registryParams = {
registry_url: "https://my-registry.org", registryUrl: 'https://my-registry.org',
} }
@@ -89,7 +89,7 @@ describe('createStorageRecord', () => {
path: '/repos/foo/bar/attestations', path: '/repos/foo/bar/attestations',
method: 'POST', method: 'POST',
headers: {authorization: `token ${token}`}, headers: {authorization: `token ${token}`},
body: JSON.stringify({...artifactParams, ...registryParams}) body: JSON.stringify({...artifactParams, registry_url: registryParams.registryUrl})
}) })
.reply(500, 'oops') .reply(500, 'oops')
.times(1) .times(1)
@@ -101,7 +101,7 @@ describe('createStorageRecord', () => {
headers: {authorization: `token ${token}`}, headers: {authorization: `token ${token}`},
body: JSON.stringify({}) body: JSON.stringify({})
}) })
.reply(201, {id: '123'}) .reply(201, {storage_records: [{id: '123'}, {id: '456'}]})
.times(1) .times(1)
}) })
+15 -12
View File
@@ -15,7 +15,7 @@ export type ArtifactParams = {
export type PackageRegistryParams = { export type PackageRegistryParams = {
registryUrl: string registryUrl: string
artifactUrl?: string artifactUrl?: string
registryRepo?: string repo?: string
path?: string path?: string
} }
@@ -33,28 +33,20 @@ export type WriteOptions = {
* @returns The ID of the storage record. * @returns The ID of the storage record.
* @throws Error if the storage record fails to persist. * @throws Error if the storage record fails to persist.
*/ */
export const createStorageRecord = async ( export async function createStorageRecord(
artifactParams: ArtifactParams, artifactParams: ArtifactParams,
packageRegistryParams: PackageRegistryParams, packageRegistryParams: PackageRegistryParams,
token: string, token: string,
options: WriteOptions = {} options: WriteOptions = {}
): Promise<Array<string>> => { ): Promise<Array<string>> {
const retries = options.retry ?? DEFAULT_RETRY_COUNT const retries = options.retry ?? DEFAULT_RETRY_COUNT
const octokit = github.getOctokit(token, {retry: {retries}}, retry) const octokit = github.getOctokit(token, {retry: {retries}}, retry)
try { try {
const response = await octokit.request(CREATE_STORAGE_RECORD_REQUEST, { const response = await octokit.request(CREATE_STORAGE_RECORD_REQUEST, {
owner: github.context.repo.owner, owner: github.context.repo.owner,
repo: github.context.repo.repo,
headers: options.headers, headers: options.headers,
artifact_digest: artifactParams.digest, ...buildRequestParams(artifactParams, packageRegistryParams),
artifact_name: artifactParams.name,
artifact_status: artifactParams.status,
artifact_url: packageRegistryParams.artifactUrl,
artifact_version: artifactParams.version,
path: packageRegistryParams.path,
registry_repo: packageRegistryParams.registryRepo,
registry_url: packageRegistryParams.registryUrl,
}) })
const data = const data =
@@ -68,3 +60,14 @@ export const createStorageRecord = async (
throw new Error(`Failed to persist storage record: ${message}`) throw new Error(`Failed to persist storage record: ${message}`)
} }
} }
const buildRequestParams = (artifactParams: ArtifactParams, registryParams: PackageRegistryParams) => {
const { registryUrl, artifactUrl, ...rest } = registryParams
return {
...artifactParams,
...rest,
// rename parameters to match API expectations
artifact_url: artifactUrl,
registry_url: registryUrl,
}
}