From 49d234e98aa72f2f79c92a56ffbf0c14861030ee Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Tue, 3 Dec 2024 15:57:13 +0900 Subject: [PATCH] fix: refactor getPluginOptions function and default kernel public key --- eslint.config.mjs | 2 ++ src/actions.ts | 32 +++++--------------------------- src/server.ts | 29 ++++------------------------- src/util.ts | 30 +++++++++++++++++++++++++++++- tests/sdk.test.ts | 11 +++++++++++ 5 files changed, 51 insertions(+), 53 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 37fa4d3..0f6adbb 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -65,6 +65,8 @@ export default tsEslint.config({ "sonarjs/no-element-overwrite": "error", "sonarjs/no-identical-conditions": "error", "sonarjs/no-identical-expressions": "error", + "sonarjs/new-cap": "off", + "sonarjs/prefer-nullish-coalescing": "off", "@typescript-eslint/naming-convention": [ "error", { diff --git a/src/actions.ts b/src/actions.ts index 551a696..8772c61 100644 --- a/src/actions.ts +++ b/src/actions.ts @@ -1,34 +1,20 @@ import * as core from "@actions/core"; import * as github from "@actions/github"; import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; -import { TAnySchema, Type as T } from "@sinclair/typebox"; +import { Type as T } from "@sinclair/typebox"; import { Value } from "@sinclair/typebox/value"; -import { LOG_LEVEL, LogLevel, LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger"; +import { LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger"; import { config } from "dotenv"; import { Context } from "./context"; import { customOctokit } from "./octokit"; -import { sanitizeMetadata } from "./util"; import { verifySignature } from "./signature"; -import { KERNEL_PUBLIC_KEY } from "./constants"; -import { jsonType } from "./types/util"; import { commandCallSchema } from "./types/command"; import { HandlerReturn } from "./types/sdk"; +import { jsonType } from "./types/util"; +import { getPluginOptions, Options, sanitizeMetadata } from "./util"; config(); -interface Options { - logLevel?: LogLevel; - postCommentOnError?: boolean; - settingsSchema?: TAnySchema; - envSchema?: TAnySchema; - commandSchema?: TAnySchema; - kernelPublicKey?: string; - /** - * @deprecated This disables signature verification - only for local development - */ - bypassSignatureVerification?: boolean; -} - const inputSchema = T.Object({ stateId: T.String(), eventName: T.String(), @@ -44,15 +30,7 @@ export async function createActionsPlugin) => HandlerReturn, options?: Options ) { - const pluginOptions = { - logLevel: options?.logLevel ?? LOG_LEVEL.INFO, - postCommentOnError: options?.postCommentOnError ?? true, - settingsSchema: options?.settingsSchema, - envSchema: options?.envSchema, - commandSchema: options?.commandSchema, - kernelPublicKey: options?.kernelPublicKey ?? KERNEL_PUBLIC_KEY, - bypassSignatureVerification: options?.bypassSignatureVerification || false, - }; + const pluginOptions = getPluginOptions(options); const pluginGithubToken = process.env.PLUGIN_GITHUB_TOKEN; if (!pluginGithubToken) { diff --git a/src/server.ts b/src/server.ts index aef78dd..42e59a4 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,30 +1,17 @@ import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; -import { TAnySchema, Type as T } from "@sinclair/typebox"; +import { Type as T } from "@sinclair/typebox"; import { Value } from "@sinclair/typebox/value"; -import { LOG_LEVEL, LogLevel, LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger"; +import { LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger"; import { Hono } from "hono"; import { env as honoEnv } from "hono/adapter"; import { HTTPException } from "hono/http-exception"; import { postComment } from "./comment"; -import { KERNEL_PUBLIC_KEY } from "./constants"; import { Context } from "./context"; import { customOctokit } from "./octokit"; import { verifySignature } from "./signature"; import { Manifest } from "./types/manifest"; import { HandlerReturn } from "./types/sdk"; - -interface Options { - kernelPublicKey?: string; - logLevel?: LogLevel; - postCommentOnError?: boolean; - settingsSchema?: TAnySchema; - envSchema?: TAnySchema; - commandSchema?: TAnySchema; - /** - * @deprecated This disables signature verification - only for local development - */ - bypassSignatureVerification?: boolean; -} +import { getPluginOptions, Options } from "./util"; const inputSchema = T.Object({ stateId: T.String(), @@ -42,15 +29,7 @@ export function createPlugin/g, ">").replace(/--/g, "--"); } + +export function getPluginOptions(options: Options | undefined) { + return { + // Important to use || and not ?? to not consider empty strings + kernelPublicKey: options?.kernelPublicKey || KERNEL_PUBLIC_KEY, + logLevel: options?.logLevel ?? LOG_LEVEL.INFO, + postCommentOnError: options?.postCommentOnError ?? true, + settingsSchema: options?.settingsSchema, + envSchema: options?.envSchema, + commandSchema: options?.commandSchema, + bypassSignatureVerification: options?.bypassSignatureVerification || false, + }; +} diff --git a/tests/sdk.test.ts b/tests/sdk.test.ts index f2faa4e..87181c8 100644 --- a/tests/sdk.test.ts +++ b/tests/sdk.test.ts @@ -2,9 +2,11 @@ import { afterAll, afterEach, beforeAll, describe, expect, it, jest } from "@jes import { EmitterWebhookEventName } from "@octokit/webhooks"; import * as crypto from "crypto"; import { http, HttpResponse } from "msw"; +import { KERNEL_PUBLIC_KEY } from "../src/constants"; import { Context } from "../src/context"; import { createPlugin } from "../src/server"; import { signPayload } from "../src/signature"; +import { getPluginOptions } from "../src/util"; import { server } from "./__mocks__/node"; import issueCommented from "./__mocks__/requests/issue-comment-post.json"; import { CommandCall } from "../src/types/command"; @@ -404,4 +406,13 @@ describe("SDK actions tests", () => { }, }); }); + + it("Should return the proper Kernel Key", () => { + let options = getPluginOptions({ kernelPublicKey: "" }); + expect(options.kernelPublicKey).toEqual(KERNEL_PUBLIC_KEY); + options = getPluginOptions({}); + expect(options.kernelPublicKey).toEqual(KERNEL_PUBLIC_KEY); + options = getPluginOptions({ kernelPublicKey: "1234" }); + expect(options.kernelPublicKey).toEqual("1234"); + }); });