Skip to content

Commit

Permalink
Merge pull request #38 from ubiquity-os/development
Browse files Browse the repository at this point in the history
Merge development into main
  • Loading branch information
gentlementlegen authored Dec 3, 2024
2 parents 70d83d1 + cdbdd62 commit 14b3aa6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 53 deletions.
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down
32 changes: 5 additions & 27 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -44,15 +30,7 @@ export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TCo
handler: (context: Context<TConfig, TEnv, TCommand, TSupportedEvents>) => 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) {
Expand Down
29 changes: 4 additions & 25 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -42,15 +29,7 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno
manifest: Manifest,
options?: Options
) {
const pluginOptions = {
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,
};
const pluginOptions = getPluginOptions(options);

const app = new Hono();

Expand Down
30 changes: 29 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { LogReturn } from "@ubiquity-os/ubiquity-os-logger";
import { TAnySchema } from "@sinclair/typebox";
import { LOG_LEVEL, LogLevel, LogReturn } from "@ubiquity-os/ubiquity-os-logger";
import { KERNEL_PUBLIC_KEY } from "./constants";

export 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;
}

export function sanitizeMetadata(obj: LogReturn["metadata"]): string {
return JSON.stringify(obj, null, 2).replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/--/g, "&#45;&#45;");
}

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,
};
}
11 changes: 11 additions & 0 deletions tests/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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");
});
});

0 comments on commit 14b3aa6

Please sign in to comment.