2024-02-18 03:14:10 +00:00
|
|
|
import * as github from '@actions/github'
|
2024-04-24 18:31:11 +00:00
|
|
|
import {retry} from '@octokit/plugin-retry'
|
2024-08-14 18:21:15 +00:00
|
|
|
import {RequestHeaders} from '@octokit/types'
|
2024-02-18 03:14:10 +00:00
|
|
|
|
|
|
|
const CREATE_ATTESTATION_REQUEST = 'POST /repos/{owner}/{repo}/attestations'
|
2024-04-24 18:31:11 +00:00
|
|
|
const DEFAULT_RETRY_COUNT = 5
|
2024-02-18 03:14:10 +00:00
|
|
|
|
2024-04-24 18:31:11 +00:00
|
|
|
export type WriteOptions = {
|
|
|
|
retry?: number
|
2024-08-14 18:21:15 +00:00
|
|
|
headers?: RequestHeaders
|
2024-04-24 18:31:11 +00:00
|
|
|
}
|
2024-02-18 03:14:10 +00:00
|
|
|
/**
|
|
|
|
* Writes an attestation to the repository's attestations endpoint.
|
|
|
|
* @param attestation - The attestation to write.
|
|
|
|
* @param token - The GitHub token for authentication.
|
|
|
|
* @returns The ID of the attestation.
|
|
|
|
* @throws Error if the attestation fails to persist.
|
|
|
|
*/
|
|
|
|
export const writeAttestation = async (
|
|
|
|
attestation: unknown,
|
2024-04-24 18:31:11 +00:00
|
|
|
token: string,
|
|
|
|
options: WriteOptions = {}
|
2024-02-18 03:14:10 +00:00
|
|
|
): Promise<string> => {
|
2024-04-24 18:31:11 +00:00
|
|
|
const retries = options.retry ?? DEFAULT_RETRY_COUNT
|
|
|
|
const octokit = github.getOctokit(token, {retry: {retries}}, retry)
|
2024-02-18 03:14:10 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await octokit.request(CREATE_ATTESTATION_REQUEST, {
|
|
|
|
owner: github.context.repo.owner,
|
|
|
|
repo: github.context.repo.repo,
|
2024-08-14 18:21:15 +00:00
|
|
|
headers: options.headers,
|
2024-02-18 03:14:10 +00:00
|
|
|
data: {bundle: attestation}
|
|
|
|
})
|
|
|
|
|
2024-04-10 23:53:17 +00:00
|
|
|
const data =
|
|
|
|
typeof response.data == 'string'
|
|
|
|
? JSON.parse(response.data)
|
|
|
|
: response.data
|
|
|
|
return data?.id
|
2024-02-18 03:14:10 +00:00
|
|
|
} catch (err) {
|
|
|
|
const message = err instanceof Error ? err.message : err
|
|
|
|
throw new Error(`Failed to persist attestation: ${message}`)
|
|
|
|
}
|
|
|
|
}
|