generated from ghalactic/action-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
321 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getInput } from "@actions/core"; | ||
import { load } from "js-yaml"; | ||
import { errorMessage } from "../error.js"; | ||
import type { AppInput } from "../type/input.js"; | ||
import { validateApps } from "./validation.js"; | ||
|
||
export function readAppsInput(): AppInput[] { | ||
const yaml = getInput("apps"); | ||
let parsed; | ||
|
||
try { | ||
parsed = load(yaml); | ||
} catch (error) { | ||
throw new Error( | ||
`Parsing of apps action input failed: ${errorMessage(error)}`, | ||
); | ||
} | ||
|
||
try { | ||
return validateApps(parsed); | ||
} catch (error) { | ||
throw new Error( | ||
`Validation of apps action input failed: ${errorMessage(error)}`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import ajvModule, { ErrorObject } from "ajv"; | ||
import appsSchema from "../schema/apps.v1.schema.json"; | ||
import type { AppInput } from "../type/input.js"; | ||
|
||
// see https://github.com/ajv-validator/ajv/issues/2132 | ||
const Ajv = ajvModule.default; | ||
|
||
const ajv = new Ajv({ | ||
schemas: [appsSchema], | ||
allErrors: true, | ||
useDefaults: true, | ||
}); | ||
|
||
export const validateApps = createValidate<AppInput[]>( | ||
appsSchema.$id, | ||
"apps input", | ||
); | ||
|
||
class ValidateError extends Error { | ||
public errors: ErrorObject[]; | ||
|
||
constructor(message: string, errors: ErrorObject[]) { | ||
super(message); | ||
|
||
this.errors = errors; | ||
} | ||
} | ||
|
||
function createValidate<T>( | ||
schemaId: string, | ||
label: string, | ||
): (value: unknown) => T { | ||
return function validate(value) { | ||
const validator = ajv.getSchema(schemaId); | ||
|
||
/* v8 ignore start */ | ||
if (!validator) { | ||
throw new Error(`Invariant violation: Undefined schema ${schemaId}`); | ||
} | ||
/* v8 ignore stop */ | ||
|
||
if (validator(value)) return value as T; | ||
|
||
/* v8 ignore start - never seen errors be nullish */ | ||
const errors = validator.errors ?? []; | ||
/* v8 ignore stop */ | ||
|
||
const error = new ValidateError( | ||
`Invalid ${label}:\n${renderErrors(errors)}`, | ||
errors, | ||
); | ||
|
||
throw error; | ||
}; | ||
} | ||
|
||
function renderErrors(errors: ErrorObject[]): string { | ||
return ` - ${errors.map(renderError).join("\n - ")}\n`; | ||
} | ||
|
||
function renderError(error: ErrorObject): string { | ||
const { instancePath, message } = error; | ||
const subject = instancePath && ` (${instancePath})`; | ||
|
||
return `${message}${subject}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function errorMessage(error: unknown): string { | ||
/* v8 ignore start - never seen non-error */ | ||
return error instanceof Error ? error.message : "unknown cause"; | ||
/* v8 ignore stop */ | ||
} | ||
|
||
export function errorStack(error: unknown): string { | ||
/* v8 ignore start - never seen non-error */ | ||
return (error instanceof Error ? error.stack : undefined) ?? "unknown cause"; | ||
/* v8 ignore stop */ | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "https://ghalactic.github.io/provision-github-tokens/schema/apps.v1.schema.json", | ||
"title": "Provision GitHub Tokens (apps)", | ||
"description": "Apps to use for provisioning tokens.", | ||
"type": "array", | ||
"items": { | ||
"description": "An app to use for provisioning tokens.", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"required": ["appId", "privateKey"], | ||
"properties": { | ||
"appId": { | ||
"description": "The GitHub app ID.", | ||
"type": "integer" | ||
}, | ||
"privateKey": { | ||
"description": "The GitHub app private key in PEM format.", | ||
"type": "string", | ||
"minLength": 1 | ||
}, | ||
"roles": { | ||
"description": "The roles of the app.", | ||
"type": "array", | ||
"uniqueItems": true, | ||
"default": [], | ||
"items": { | ||
"description": "An app role.", | ||
"type": "string", | ||
"minLength": 1 | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type AppInput = { | ||
appId: number; | ||
privateKey: string; | ||
roles: string[]; | ||
}; |
Oops, something went wrong.