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'
|
|
|
|
|
|
|
|
|
|
const CREATE_STORAGE_RECORD_REQUEST = 'POST /orgs/{owner}/artifacts/metadata/storage-record'
|
|
|
|
|
const DEFAULT_RETRY_COUNT = 5
|
|
|
|
|
|
2025-12-08 11:02:59 -08:00
|
|
|
export type ArtifactParams = {
|
|
|
|
|
name: string
|
|
|
|
|
digest: string
|
|
|
|
|
version?: string
|
|
|
|
|
status?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type PackageRegistryParams = {
|
|
|
|
|
registryUrl: string
|
|
|
|
|
artifactUrl?: string
|
|
|
|
|
registryRepo?: string
|
|
|
|
|
path?: string
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 10:49:24 -08:00
|
|
|
export type WriteOptions = {
|
|
|
|
|
retry?: number
|
|
|
|
|
headers?: RequestHeaders
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-08 11:02:59 -08:00
|
|
|
* Writes a storage record on behalf of an artifact that has been attested
|
|
|
|
|
* @param artifactParams - parameters for the artifact.
|
|
|
|
|
* @param packageRegistryParams - parameters for the package registry.
|
2025-12-08 10:49:24 -08:00
|
|
|
* @param token - The GitHub token for authentication.
|
2025-12-08 11:02:59 -08:00
|
|
|
* @param options - Optional parameters for the write operation.
|
2025-12-08 10:49:24 -08:00
|
|
|
* @returns The ID of the storage record.
|
|
|
|
|
* @throws Error if the storage record fails to persist.
|
|
|
|
|
*/
|
|
|
|
|
export const createStorageRecord = async (
|
2025-12-08 11:02:59 -08:00
|
|
|
artifactParams: ArtifactParams,
|
|
|
|
|
packageRegistryParams: PackageRegistryParams,
|
2025-12-08 10:49:24 -08:00
|
|
|
token: string,
|
|
|
|
|
options: WriteOptions = {}
|
2025-12-08 13:17:08 -08:00
|
|
|
): Promise<Array<string>> => {
|
2025-12-08 10:49:24 -08:00
|
|
|
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,
|
2025-12-08 11:02:59 -08:00
|
|
|
artifact_digest: artifactParams.digest,
|
2025-12-08 13:17:08 -08:00
|
|
|
artifact_name: artifactParams.name,
|
2025-12-08 11:02:59 -08:00
|
|
|
artifact_status: artifactParams.status,
|
|
|
|
|
artifact_url: packageRegistryParams.artifactUrl,
|
2025-12-08 13:17:08 -08:00
|
|
|
artifact_version: artifactParams.version,
|
|
|
|
|
path: packageRegistryParams.path,
|
2025-12-08 11:02:59 -08:00
|
|
|
registry_repo: packageRegistryParams.registryRepo,
|
2025-12-08 13:17:08 -08:00
|
|
|
registry_url: packageRegistryParams.registryUrl,
|
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
|
|
|
|
|
|
|
|
return data?.storage_records.map((r: { id: any }) => 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}`)
|
|
|
|
|
}
|
|
|
|
|
}
|