Skip to content

Commit

Permalink
chore: add validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jun 7, 2024
1 parent 844b867 commit ff05224
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"@sinclair/typebox": "^0.32.5",
"@supabase/supabase-js": "2.42.0",
"dotenv": "^16.4.4",
"ms": "^2.1.3"
"ms": "^2.1.3",
"typebox-validators": "^0.3.5"
},
"devDependencies": {
"@commitlint/cli": "^18.6.1",
Expand Down
21 changes: 12 additions & 9 deletions src/handlers/shared/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ export async function start(context: Context, issue: Context["payload"]["issue"]
throw new Error("Issue is a parent issue");
}

let commitHash = null;

commitHash = await context.octokit.repos.getCommit({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
ref: context.payload.repository.default_branch,
});

commitHash = commitHash.data.sha;
let commitHash: string | null = null;

try {
const hashResponse = await context.octokit.repos.getCommit({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
ref: context.payload.repository.default_branch,
});
commitHash = hashResponse.data.sha;
} catch (e) {
console.error("Error while getting commit hash", e);
}

// check max assigned issues

Expand Down
4 changes: 0 additions & 4 deletions src/types/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ const repositorySchema = Type.Object({
web_commit_signoff_required: Type.Boolean(),
});

export type GitHubRepository = Static<typeof repositorySchema>;

const organizationSchema = Type.Object({
login: Type.String(),
id: Type.Number(),
Expand Down Expand Up @@ -235,6 +233,4 @@ export const payloadSchema = Type.Object({
changes: Type.Optional(changesSchema),
});

export type GitHubPayload = Static<typeof payloadSchema>;

export type HandlerReturnValuesNoVoid = null | string | LogReturn;
2 changes: 2 additions & 0 deletions src/types/plugin-input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SupportedEvents, SupportedEventsU } from "./context";
import { StaticDecode, Type as T } from "@sinclair/typebox";
import { StandardValidator } from "typebox-validators";

export interface PluginInputs<T extends SupportedEventsU = SupportedEventsU, TU extends SupportedEvents[T] = SupportedEvents[T]> {
stateId: string;
Expand Down Expand Up @@ -30,3 +31,4 @@ export const startStopSchema = T.Object({
});

export type StartStopSettings = StaticDecode<typeof startStopSchema>;
export const startStopSettingsValidator = new StandardValidator(startStopSchema);
7 changes: 6 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Value } from "@sinclair/typebox/value";
import { startStopBounty } from "./plugin";
import { Env } from "./types/env";
import { startStopSchema } from "./types/plugin-input";
import { startStopSchema, startStopSettingsValidator } from "./types/plugin-input";

export default {
async fetch(request: Request, env: Env): Promise<Response> {
Expand All @@ -22,6 +22,11 @@ export default {

const webhookPayload = await request.json();
const settings = Value.Decode(startStopSchema, Value.Default(startStopSchema, JSON.parse(webhookPayload.settings)));

if (!startStopSettingsValidator.test(settings)) {
throw new Error("Invalid settings provided");
}

webhookPayload.eventPayload = JSON.parse(webhookPayload.eventPayload);
webhookPayload.settings = settings;
await startStopBounty(webhookPayload, env);
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6794,6 +6794,11 @@ type-fest@^4.9.0:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-4.10.3.tgz#ff01cb0a1209f59583d61e1312de9715e7ea4874"
integrity sha512-JLXyjizi072smKGGcZiAJDCNweT8J+AuRxmPZ1aG7TERg4ijx9REl8CNhbr36RV4qXqL1gO1FF9HL8OkVmmrsA==

typebox-validators@^0.3.5:
version "0.3.5"
resolved "https://registry.yarnpkg.com/typebox-validators/-/typebox-validators-0.3.5.tgz#b913bad0a87571ffe0edd01d2b6090a268e1ecc9"
integrity sha512-FXrmSUAN6bSGxDANResNCZQ8VRRLr5bSyy73/HyqSXGdiVuogppGAoRocy7NTVZY4Wc2sWUofmWwwIXE6OxS6Q==

typed-array-buffer@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/typed-array-buffer/-/typed-array-buffer-1.0.1.tgz#0608ffe6bca71bf15a45bff0ca2604107a1325f5"
Expand Down Expand Up @@ -7228,4 +7233,4 @@ [email protected]:
zod@^3.20.6:
version "3.23.8"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d"
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==

0 comments on commit ff05224

Please sign in to comment.