fix linter issues

Signed-off-by: Meredith Lancaster <[email protected]>
This commit is contained in:
Meredith Lancaster
2025-12-08 16:22:59 -08:00
parent 8eca440361
commit 10d3b034e0
2 changed files with 40 additions and 35 deletions
@@ -3,21 +3,20 @@ import {createStorageRecord} from '../src/artifactMetadata'
describe('createStorageRecord', () => { describe('createStorageRecord', () => {
const originalEnv = process.env const originalEnv = process.env
const attestation = {foo: 'bar '}
const token = 'token' const token = 'token'
const headers = {'X-GitHub-Foo': 'true'} const headers = {'X-GitHub-Foo': 'true'}
const options = { const options = {
artifactOptions: { artifactOptions: {
name: "my-lib", name: 'my-lib',
version: "1.0.0", version: '1.0.0',
digest: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", digest: `sha256:${'a'.repeat(64)}`
}, },
packageRegistryOptions: { packageRegistryOptions: {
registryUrl: 'https://my-registry.org', registryUrl: 'https://my-registry.org'
}, },
token, token,
writeOptions: {headers}, writeOptions: {headers}
} }
const mockAgent = new MockAgent() const mockAgent = new MockAgent()
@@ -43,19 +42,20 @@ describe('createStorageRecord', () => {
method: 'POST', method: 'POST',
headers: {authorization: `token ${token}`, ...headers}, headers: {authorization: `token ${token}`, ...headers},
body: JSON.stringify({ body: JSON.stringify({
name: "my-lib", name: 'my-lib',
version: "1.0.0", version: '1.0.0',
digest: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", digest: `sha256:${'a'.repeat(64)}`,
registry_url: "https://my-registry.org" registry_url: 'https://my-registry.org'
}) })
}) })
.reply(201, {storage_records: [{id: '123'}, {id: '456'}]}) .reply(201, {storage_records: [{id: '123'}, {id: '456'}]})
}) })
it('persists the storage record', async () => { it('persists the storage record', async () => {
await expect( await expect(createStorageRecord(options)).resolves.toEqual([
createStorageRecord(options) '123',
).resolves.toEqual(['123', '456']) '456'
])
}) })
}) })
@@ -68,10 +68,10 @@ describe('createStorageRecord', () => {
method: 'POST', method: 'POST',
headers: {authorization: `token ${token}`}, headers: {authorization: `token ${token}`},
body: JSON.stringify({ body: JSON.stringify({
name: "my-lib", name: 'my-lib',
version: "1.0.0", version: '1.0.0',
digest: "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", digest: `sha256:${'a'.repeat(64)}`,
registry_url: "https://my-registry.org" registry_url: 'https://my-registry.org'
}) })
}) })
.reply(500, 'oops') .reply(500, 'oops')
@@ -81,7 +81,7 @@ describe('createStorageRecord', () => {
await expect( await expect(
createStorageRecord({ createStorageRecord({
...options, ...options,
writeOptions: {retry: 0}, writeOptions: {retry: 0}
}) })
).rejects.toThrow(/oops/) ).rejects.toThrow(/oops/)
}) })
@@ -98,7 +98,7 @@ describe('createStorageRecord', () => {
headers: {authorization: `token ${token}`}, headers: {authorization: `token ${token}`},
body: JSON.stringify({ body: JSON.stringify({
...options.artifactOptions, ...options.artifactOptions,
registry_url: options.packageRegistryOptions.registryUrl, registry_url: options.packageRegistryOptions.registryUrl
}) })
}) })
.reply(500, 'oops') .reply(500, 'oops')
@@ -111,18 +111,20 @@ describe('createStorageRecord', () => {
headers: {authorization: `token ${token}`}, headers: {authorization: `token ${token}`},
body: JSON.stringify({ body: JSON.stringify({
...options.artifactOptions, ...options.artifactOptions,
registry_url: options.packageRegistryOptions.registryUrl, registry_url: options.packageRegistryOptions.registryUrl
}) })
}) })
.reply(201, {storage_records: [{id: '123'}, {id: '456'}]}) .reply(201, {storage_records: [{id: '123'}, {id: '456'}]})
.times(1) .times(1)
}) })
it('persists the attestation', async () => { it('persists the storage record', async () => {
await expect(createStorageRecord({ await expect(
...options, createStorageRecord({
writeOptions: {}, ...options,
})).resolves.toEqual(['123', '456']) writeOptions: {}
})
).resolves.toEqual(['123', '456'])
}) })
}) })
}) })
+13 -10
View File
@@ -2,7 +2,8 @@ import * as github from '@actions/github'
import {retry} from '@octokit/plugin-retry' import {retry} from '@octokit/plugin-retry'
import {RequestHeaders} from '@octokit/types' import {RequestHeaders} from '@octokit/types'
const CREATE_STORAGE_RECORD_REQUEST = 'POST /orgs/{owner}/artifacts/metadata/storage-record' const CREATE_STORAGE_RECORD_REQUEST =
'POST /orgs/{owner}/artifacts/metadata/storage-record'
const DEFAULT_RETRY_COUNT = 5 const DEFAULT_RETRY_COUNT = 5
/** /**
@@ -19,7 +20,7 @@ export type StorageRecordOptions = {
version?: string version?: string
// The status of the artifact // The status of the artifact
status?: string status?: string
}, }
// Includes details about the package registry the artifact was published to // Includes details about the package registry the artifact was published to
packageRegistryOptions: { packageRegistryOptions: {
// The URL of the package registry // The URL of the package registry
@@ -30,14 +31,14 @@ export type StorageRecordOptions = {
repo?: string repo?: string
// The path of the artifact in the package registry repository. // The path of the artifact in the package registry repository.
path?: string path?: string
}, }
// GitHub token for writing attestations. // GitHub token for writing attestations.
token: string token: string
// Optional parameters for the write operation. // Optional parameters for the write operation.
writeOptions: { writeOptions: {
// The number of times to retry the request. // The number of times to retry the request.
retry?: number retry?: number
// HTTP headers to include in request to Artifact Metadata API. // HTTP headers to include in request to Artifact Metadata API.
headers?: RequestHeaders headers?: RequestHeaders
} }
} }
@@ -48,7 +49,9 @@ export type StorageRecordOptions = {
* @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 async function createStorageRecord(options: StorageRecordOptions): Promise<Array<string>> { export async function createStorageRecord(
options: StorageRecordOptions
): Promise<string[]> {
const retries = options.writeOptions.retry ?? DEFAULT_RETRY_COUNT const retries = options.writeOptions.retry ?? DEFAULT_RETRY_COUNT
const octokit = github.getOctokit(options.token, {retry: {retries}}, retry) const octokit = github.getOctokit(options.token, {retry: {retries}}, retry)
@@ -56,7 +59,7 @@ export async function createStorageRecord(options: StorageRecordOptions): Promis
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,
headers: options.writeOptions.headers, headers: options.writeOptions.headers,
...buildRequestParams(options), ...buildRequestParams(options)
}) })
const data = const data =
@@ -64,19 +67,19 @@ export async function createStorageRecord(options: StorageRecordOptions): Promis
? JSON.parse(response.data) ? JSON.parse(response.data)
: response.data : response.data
return data?.storage_records.map((r: { id: any }) => r.id) return data?.storage_records.map((r: {id: number}) => String(r.id))
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : err const message = err instanceof Error ? err.message : err
throw new Error(`Failed to persist storage record: ${message}`) throw new Error(`Failed to persist storage record: ${message}`)
} }
} }
const buildRequestParams = (options: StorageRecordOptions) => { function buildRequestParams(options: StorageRecordOptions): Object {
const { registryUrl, artifactUrl, ...rest } = options.packageRegistryOptions const {registryUrl, artifactUrl, ...rest} = options.packageRegistryOptions
return { return {
...options.artifactOptions, ...options.artifactOptions,
registry_url: registryUrl, registry_url: registryUrl,
artifact_url: artifactUrl, artifact_url: artifactUrl,
...rest, ...rest
} }
} }