Files
toolkit/packages/attest/src/artifactMetadata.ts
T

93 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-12-08 10:49:24 -08:00
import * as github from '@actions/github'
import {retry} from '@octokit/plugin-retry'
import {RequestHeaders} from '@octokit/types'
import {getUserAgent} from './internal/utils.js'
2025-12-08 10:49:24 -08:00
2025-12-08 16:22:59 -08:00
const CREATE_STORAGE_RECORD_REQUEST =
'POST /orgs/{owner}/artifacts/metadata/storage-record'
2025-12-08 10:49:24 -08:00
const DEFAULT_RETRY_COUNT = 5
2025-12-08 15:25:34 -08:00
/**
* Options for creating a storage record for an attested artifact.
*/
export type ArtifactOptions = {
2025-12-08 15:25:34 -08:00
// Includes details about the attested artifact
// The name of the artifact
name: string
// The digest of the artifact
digest: string
// The version of the artifact
version?: string
// The status of the artifact
status?: string
}
2025-12-09 11:40:40 -08:00
// Includes details about the package registry the artifact was published to
export type PackageRegistryOptions = {
// The URL of the package registry
registryUrl: string
// The URL of the artifact in the package registry
artifactUrl?: string
// The package registry repository the artifact was published to.
repo?: string
// The path of the artifact in the package registry repository.
path?: string
2025-12-08 10:49:24 -08:00
}
/**
2025-12-08 11:02:59 -08:00
* Writes a storage record on behalf of an artifact that has been attested
* @param artifactOptions - parameters for the storage record API request.
* @param packageRegistryOptions - parameters for the package registry API request.
* @param token - GitHub token used to authenticate the request.
2025-12-09 11:39:12 -08:00
* @param retryAttempts - The number of retries to attempt if the request fails.
* @param headers - Additional headers to include in the request.
2025-12-09 11:40:40 -08:00
*
2025-12-08 10:49:24 -08:00
* @returns The ID of the storage record.
* @throws Error if the storage record fails to persist.
*/
2025-12-08 16:22:59 -08:00
export async function createStorageRecord(
artifactOptions: ArtifactOptions,
packageRegistryOptions: PackageRegistryOptions,
token: string,
2025-12-09 11:39:12 -08:00
retryAttempts?: number,
headers?: RequestHeaders
2025-12-09 08:02:38 -08:00
): Promise<number[]> {
2025-12-09 11:39:12 -08:00
const retries = retryAttempts ?? DEFAULT_RETRY_COUNT
const octokit = github.getOctokit(token, {retry: {retries}}, retry)
const headersWithUserAgent = {
'User-Agent': getUserAgent(),
...headers
}
2025-12-08 10:49:24 -08:00
try {
const response = await octokit.request(CREATE_STORAGE_RECORD_REQUEST, {
owner: github.context.repo.owner,
headers: headersWithUserAgent,
...buildRequestParams(artifactOptions, packageRegistryOptions)
2025-12-08 10:49:24 -08:00
})
const data =
typeof response.data == 'string'
? JSON.parse(response.data)
: response.data
2025-12-08 13:17:08 -08:00
2025-12-09 08:02:38 -08:00
return data?.storage_records.map((r: {id: number}) => r.id)
2025-12-08 10:49:24 -08:00
} catch (err) {
const message = err instanceof Error ? err.message : err
throw new Error(`Failed to persist storage record: ${message}`)
}
}
2025-12-08 15:39:26 -08:00
2025-12-08 19:19:11 -08:00
function buildRequestParams(
artifactOptions: ArtifactOptions,
packageRegistryOptions: PackageRegistryOptions
2025-12-08 19:19:11 -08:00
): Record<string, unknown> {
const {registryUrl, artifactUrl, ...rest} = packageRegistryOptions
2025-12-08 15:39:26 -08:00
return {
...artifactOptions,
2025-12-08 15:39:26 -08:00
registry_url: registryUrl,
artifact_url: artifactUrl,
2025-12-08 16:22:59 -08:00
...rest
2025-12-08 15:39:26 -08:00
}
}