-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from gentlementlegen/feat/schema-validation
feat: schema validation
- Loading branch information
Showing
13 changed files
with
196 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
name: "Update Configuration" | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
|
||
jobs: | ||
update: | ||
name: "Update Configuration in manifest.json" | ||
runs-on: ubuntu-latest | ||
permissions: write-all | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20.10.0" | ||
|
||
- name: Install deps and run configuration update | ||
run: | | ||
yarn install --immutable --immutable-cache --check-cache | ||
yarn tsc --noCheck --project tsconfig.json | ||
- name: Update manifest configuration using GitHub Script | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
(async () => { | ||
const fs = await import('fs/promises'); | ||
const path = await import('path'); | ||
const { pluginSettingsSchema } = await import("${{ github.workspace }}/src/types/plugin-inputs.js"); | ||
const manifestPath = path.resolve("${{ github.workspace }}", './manifest.json'); | ||
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8')); | ||
const configuration = JSON.stringify(pluginSettingsSchema); | ||
manifest["configuration"] = JSON.parse(configuration); | ||
const updatedManifest = JSON.stringify(manifest, null, 2); | ||
console.log('Updated manifest:', updatedManifest); | ||
await fs.writeFile(manifestPath, updatedManifest); | ||
})(); | ||
- name: Commit and Push generated types | ||
run: | | ||
git config --global user.name 'ubiquity-os[bot]' | ||
git config --global user.email 'ubiquity-os[bot]@users.noreply.github.com' | ||
git add ./manifest.json | ||
if [ -n "$(git diff-index --cached --name-only HEAD)" ]; then | ||
git commit -m "chore: updated generated configuration" || echo "Lint-staged check failed" | ||
git push origin HEAD:${{ github.ref_name }} | ||
else | ||
echo "No changes to commit" | ||
fi | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
File renamed without changes.
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 |
---|---|---|
@@ -1,5 +1,41 @@ | ||
{ | ||
"name": "User activity watcher", | ||
"description": "Watches user activity on issues, sends reminders on deadlines, and unassign inactive users.", | ||
"ubiquity:listeners": ["pull_request_review_comment.created", "issue_comment.created", "push"] | ||
} | ||
"ubiquity:listeners": [ | ||
"pull_request_review_comment.created", | ||
"issue_comment.created", | ||
"push" | ||
], | ||
"configuration": { | ||
"type": "object", | ||
"properties": { | ||
"warning": { | ||
"default": "3.5 days", | ||
"type": "string" | ||
}, | ||
"watch": { | ||
"type": "object", | ||
"properties": { | ||
"optOut": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"optOut" | ||
] | ||
}, | ||
"disqualification": { | ||
"default": "7 days", | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"warning", | ||
"watch", | ||
"disqualification" | ||
] | ||
} | ||
} |
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,50 @@ | ||
import * as github from "@actions/github"; | ||
import { Octokit } from "@octokit/rest"; | ||
import { TransformDecodeCheckError, TransformDecodeError, Value, ValueError } from "@sinclair/typebox/value"; | ||
import { Env, envSchema, envValidator, pluginSettingsValidator, PluginSettings, pluginSettingsSchema } from "../types/plugin-inputs"; | ||
|
||
export function validateAndDecodeSchemas(rawEnv: object, rawSettings: object) { | ||
const errors: ValueError[] = []; | ||
|
||
const env = Value.Default(envSchema, rawEnv) as Env; | ||
if (!envValidator.test(env)) { | ||
for (const error of envValidator.errors(env)) { | ||
errors.push(error); | ||
} | ||
} | ||
|
||
const settings = Value.Default(pluginSettingsSchema, rawSettings) as PluginSettings; | ||
if (!pluginSettingsValidator.test(settings)) { | ||
for (const error of pluginSettingsValidator.errors(settings)) { | ||
errors.push(error); | ||
} | ||
} | ||
|
||
if (errors.length) { | ||
throw { errors }; | ||
} | ||
|
||
try { | ||
const decodedSettings = Value.Decode(pluginSettingsSchema, settings); | ||
const decodedEnv = Value.Decode(envSchema, rawEnv || {}); | ||
return { decodedEnv, decodedSettings }; | ||
} catch (e) { | ||
if (e instanceof TransformDecodeCheckError || e instanceof TransformDecodeError) { | ||
throw { errors: [e.error] }; | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
export async function returnDataToKernel(repoToken: string, stateId: string, output: object, eventType = "return-data-to-ubiquity-os-kernel") { | ||
const octokit = new Octokit({ auth: repoToken }); | ||
return octokit.repos.createDispatchEvent({ | ||
owner: github.context.repo.owner, | ||
repo: github.context.repo.repo, | ||
event_type: eventType, | ||
client_payload: { | ||
state_id: stateId, | ||
output: JSON.stringify(output), | ||
}, | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import * as github from "@actions/github"; | ||
import { Value } from "@sinclair/typebox/value"; | ||
import { config } from "dotenv"; | ||
import { PluginInputs, userActivityWatcherSettingsSchema } from "../types/plugin-inputs"; | ||
import { validateAndDecodeSchemas } from "../helpers/validator"; | ||
import { PluginInputs } from "../types/plugin-inputs"; | ||
|
||
config(); | ||
|
||
const webhookPayload = github.context.payload.inputs; | ||
const settings = Value.Decode(userActivityWatcherSettingsSchema, Value.Default(userActivityWatcherSettingsSchema, JSON.parse(webhookPayload.settings))); | ||
const { decodedSettings } = validateAndDecodeSchemas(JSON.parse(webhookPayload.settings), process.env); | ||
|
||
const program: PluginInputs = { | ||
stateId: webhookPayload.stateId, | ||
eventName: webhookPayload.eventName, | ||
authToken: webhookPayload.authToken, | ||
ref: webhookPayload.ref, | ||
eventPayload: JSON.parse(webhookPayload.eventPayload), | ||
settings, | ||
settings: decodedSettings, | ||
}; | ||
|
||
export default program; |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { EmitterWebhookEvent as WebhookEvent, EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | ||
import { Octokit } from "@octokit/rest"; | ||
import { SupportedEvents, UserActivityWatcherSettings } from "./plugin-inputs"; | ||
import { SupportedEvents, PluginSettings } from "./plugin-inputs"; | ||
import { Logs } from "@ubiquity-dao/ubiquibot-logger"; | ||
|
||
export interface Context<T extends SupportedEvents = SupportedEvents> { | ||
eventName: T; | ||
payload: WebhookEvent<T>["payload"]; | ||
octokit: InstanceType<typeof Octokit>; | ||
config: UserActivityWatcherSettings; | ||
config: PluginSettings; | ||
logger: Logs; | ||
} |
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,9 @@ | ||
declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
GITHUB_TOKEN: string; | ||
} | ||
} | ||
} | ||
|
||
export {}; |
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
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 |
---|---|---|
|
@@ -5189,10 +5189,15 @@ type-fest@^4.9.0: | |
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-4.18.3.tgz#5249f96e7c2c3f0f1561625f54050e343f1c8f68" | ||
integrity sha512-Q08/0IrpvM+NMY9PA2rti9Jb+JejTddwmwmVQGskAlhtcrw1wsRzoR6ode6mR+OAabNa75w/dxedSUY2mlphaQ== | ||
|
||
typescript@^5.4.5: | ||
version "5.5.4" | ||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.5.4.tgz#d9852d6c82bad2d2eda4fd74a5762a8f5909e9ba" | ||
integrity sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q== | ||
[email protected]: | ||
version "0.3.5" | ||
resolved "https://registry.yarnpkg.com/typebox-validators/-/typebox-validators-0.3.5.tgz#b913bad0a87571ffe0edd01d2b6090a268e1ecc9" | ||
integrity sha512-FXrmSUAN6bSGxDANResNCZQ8VRRLr5bSyy73/HyqSXGdiVuogppGAoRocy7NTVZY4Wc2sWUofmWwwIXE6OxS6Q== | ||
|
||
[email protected]: | ||
version "5.6.2" | ||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.2.tgz#d1de67b6bef77c41823f822df8f0b3bcff60a5a0" | ||
integrity sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw== | ||
|
||
undici-types@~5.26.4: | ||
version "5.26.5" | ||
|