Skip to content

feat: added schema validation workflow #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Loading