Skip to content

Commit

Permalink
Merge pull request #20 from gentlementlegen/feat/schema-validation
Browse files Browse the repository at this point in the history
feat: added schema validation workflow
  • Loading branch information
gentlementlegen authored Sep 20, 2024
2 parents 8afbfad + 447f421 commit fdffadb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/validate-schema.yml
Original file line number Diff line number Diff line change
@@ -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
30 changes: 5 additions & 25 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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, {});
}
Expand Down
30 changes: 30 additions & 0 deletions src/helpers/validator.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
12 changes: 12 additions & 0 deletions src/validate-schema.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit fdffadb

Please sign in to comment.