Files
attest/src/predicate.ts
T

33 lines
926 B
TypeScript
Raw Normal View History

2024-02-22 07:53:51 -08:00
import fs from 'fs'
2024-02-29 17:02:56 -08:00
2024-02-22 07:53:51 -08:00
import type { Predicate } from '@actions/attest'
export type PredicateInputs = {
predicateType: string
predicate: string
predicatePath: string
}
2024-02-22 07:53:51 -08:00
// Returns the predicate specified by the action's inputs. The predicate value
// may be specified as a path to a file or as a string.
export const predicateFromInputs = (inputs: PredicateInputs): Predicate => {
const { predicateType, predicate, predicatePath } = inputs
if (!predicateType) {
throw new Error('predicate-type must be provided')
}
2024-02-22 07:53:51 -08:00
if (!predicatePath && !predicate) {
2024-02-22 07:53:51 -08:00
throw new Error('One of predicate-path or predicate must be provided')
}
if (predicatePath && predicate) {
2024-02-29 17:02:56 -08:00
throw new Error('Only one of predicate-path or predicate may be provided')
}
2024-02-22 07:53:51 -08:00
const params = predicatePath
? fs.readFileSync(predicatePath, 'utf-8')
: predicate
2024-02-22 07:53:51 -08:00
return { type: predicateType, params: JSON.parse(params) }
}