fix function exporting and test results

Signed-off-by: Meredith Lancaster <malancas@github.com>
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 {createStorageRecord} from '../src/attest'
import {createStorageRecord} from '../src/artifact-metadata'
describe('createStorageRecord', () => {
const originalEnv = process.env
@@ -12,7 +12,7 @@ describe('createStorageRecord', () => {
digest: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
}
const registryParams = {
registry_url: "https://my-registry.org",
registryUrl: 'https://my-registry.org',
}
@@ -89,7 +89,7 @@ describe('createStorageRecord', () => {
path: '/repos/foo/bar/attestations',
method: 'POST',
headers: {authorization: `token ${token}`},
body: JSON.stringify({...artifactParams, ...registryParams})
body: JSON.stringify({...artifactParams, registry_url: registryParams.registryUrl})
})
.reply(500, 'oops')
.times(1)
@@ -101,7 +101,7 @@ describe('createStorageRecord', () => {
headers: {authorization: `token ${token}`},
body: JSON.stringify({})
})
.reply(201, {id: '123'})
.reply(201, {storage_records: [{id: '123'}, {id: '456'}]})
.times(1)
})
+15 -12
View File
@@ -15,7 +15,7 @@ export type ArtifactParams = {
export type PackageRegistryParams = {
registryUrl: string
artifactUrl?: string
registryRepo?: string
repo?: string
path?: string
}
@@ -33,28 +33,20 @@ export type WriteOptions = {
* @returns The ID of the storage record.
* @throws Error if the storage record fails to persist.
*/
export const createStorageRecord = async (
export async function createStorageRecord(
artifactParams: ArtifactParams,
packageRegistryParams: PackageRegistryParams,
token: string,
options: WriteOptions = {}
): Promise<Array<string>> => {
): Promise<Array<string>> {
const retries = options.retry ?? DEFAULT_RETRY_COUNT
const octokit = github.getOctokit(token, {retry: {retries}}, retry)
try {
const response = await octokit.request(CREATE_STORAGE_RECORD_REQUEST, {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
headers: options.headers,
artifact_digest: artifactParams.digest,
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,
...buildRequestParams(artifactParams, packageRegistryParams),
})
const data =
@@ -68,3 +60,14 @@ export const createStorageRecord = async (
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,
}
}