Skip to content
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

Merge development into main #38

Merged
merged 2 commits into from
Dec 3, 2024
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
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 { 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 @@
},
});
});

it("Should return the proper Kernel Key", () => {
let options = getPluginOptions({ kernelPublicKey: "" });

Check warning on line 411 in tests/sdk.test.ts

View workflow job for this annotation

GitHub Actions / check-for-empty-strings

Detected an empty string. If this is during variable initialization, consider using a different approach. For more information, visit: https://www.github.com/ubiquity/ts-template/issues/31
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");
});
});
Loading