From 447f4215c8fffeb07e86f6c025f649ec1feb30e0 Mon Sep 17 00:00:00 2001 From: Mentlegen <9807008+gentlementlegen@users.noreply.github.com> Date: Fri, 20 Sep 2024 20:39:54 +0900 Subject: [PATCH] feat: added schema validation workflow --- .github/workflows/validate-schema.yml | 25 ++++++++++++++++++++++ src/action.ts | 30 +++++---------------------- src/helpers/validator.ts | 30 +++++++++++++++++++++++++++ src/validate-schema.ts | 12 +++++++++++ 4 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/validate-schema.yml create mode 100644 src/helpers/validator.ts create mode 100644 src/validate-schema.ts diff --git a/.github/workflows/validate-schema.yml b/.github/workflows/validate-schema.yml new file mode 100644 index 00000000..68ed9587 --- /dev/null +++ b/.github/workflows/validate-schema.yml @@ -0,0 +1,25 @@ +name: "Validate Schema" + +on: + workflow_dispatch: + inputs: + settings: + description: "Settings" + +jobs: + validate: + name: "Validate Schema" + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup node + uses: actions/setup-node@v4 + with: + node-version: "20.10.0" + + - name: Install dependencies + run: | + yarn install --immutable --immutable-cache --check-cache + yarn tsx src/validate-schema.ts diff --git a/src/action.ts b/src/action.ts index e5ed458f..a46776bc 100644 --- a/src/action.ts +++ b/src/action.ts @@ -1,8 +1,8 @@ import * as github from "@actions/github"; import { Octokit } from "@octokit/rest"; -import { Value } from "@sinclair/typebox/value"; +import { validateAndDecodeSchemas } from "./helpers/validator"; import { plugin } from "./plugin"; -import { envSchema, envValidator, PluginInputs, pluginSettingsSchema, pluginSettingsValidator } from "./types"; +import { PluginInputs } from "./types"; /** * How a GitHub action executes the plugin. @@ -11,37 +11,17 @@ export async function run() { const payload = github.context.payload.inputs; payload.env = { ...(payload.env || {}), workflowName: github.context.workflow }; - if (!envValidator.test(payload.env)) { - const errors: string[] = []; - for (const error of envValidator.errors(payload.env)) { - console.error(error); - errors.push(`${error.path}: ${error.message}`); - } - throw new Error(`Invalid environment provided:\n${errors.join(";\n")}`); - } - const env = Value.Decode(envSchema, payload.env || {}); - - payload.settings = Value.Default(pluginSettingsSchema, JSON.parse(payload.settings)); - if (!pluginSettingsValidator.test(payload.settings)) { - const errors: string[] = []; - for (const error of pluginSettingsValidator.errors(payload.settings)) { - console.error(error); - errors.push(`${error.path}: ${error.message}`); - } - throw new Error(`Invalid settings provided:\n${errors.join(";\n")}`); - } - - const settings = Value.Decode(pluginSettingsSchema, payload.settings); + const { envDecoded, settingsDecoded } = validateAndDecodeSchemas(payload.env, JSON.parse(payload.settings)); const inputs: PluginInputs = { stateId: payload.stateId, eventName: payload.eventName, eventPayload: JSON.parse(payload.eventPayload), - settings, + settings: settingsDecoded, authToken: payload.authToken, ref: payload.ref, }; - await plugin(inputs, env); + await plugin(inputs, envDecoded); return returnDataToKernel(process.env.GITHUB_TOKEN, inputs.stateId, {}); } diff --git a/src/helpers/validator.ts b/src/helpers/validator.ts new file mode 100644 index 00000000..012067f7 --- /dev/null +++ b/src/helpers/validator.ts @@ -0,0 +1,30 @@ +import { Value } from "@sinclair/typebox/value"; +import { envSchema, envValidator, PluginSettings, pluginSettingsSchema, pluginSettingsValidator } from "../types"; + +export function validateAndDecodeSchemas(env: object, rawSettings: object) { + if (!envValidator.test(env)) { + const errors: object[] = []; + for (const error of envValidator.errors(env)) { + const errorMessage = { path: error.path, message: error.message, value: error.value }; + console.error(errorMessage); + errors.push(errorMessage); + } + throw new Error(`Invalid environment provided. ${errors}`); + } + const envDecoded = Value.Decode(envSchema, env || {}); + + const settings = Value.Default(pluginSettingsSchema, rawSettings) as PluginSettings; + if (!pluginSettingsValidator.test(settings)) { + const errors: object[] = []; + for (const error of pluginSettingsValidator.errors(settings)) { + const errorMessage = { path: error.path, message: error.message, value: error.value }; + console.error(errorMessage); + errors.push(errorMessage); + } + throw new Error(`Invalid settings provided. ${errors}`); + } + + const settingsDecoded = Value.Decode(pluginSettingsSchema, settings); + + return { envDecoded, settingsDecoded }; +} diff --git a/src/validate-schema.ts b/src/validate-schema.ts new file mode 100644 index 00000000..95738567 --- /dev/null +++ b/src/validate-schema.ts @@ -0,0 +1,12 @@ +import * as github from "@actions/github"; +import { validateAndDecodeSchemas } from "./helpers/validator"; + +function main() { + const payload = github.context.payload.inputs; + + payload.env = { ...(payload.env || {}), workflowName: github.context.workflow }; + const decodedSchemas = validateAndDecodeSchemas(payload.env, JSON.parse(payload.settings)); + console.log(decodedSchemas); +} + +main();