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

feat: improvements #169

Merged
Merged
Show file tree
Hide file tree
Changes from 13 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
Binary file modified bun.lockb
Binary file not shown.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"typings": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
}
},
"files": [
Expand Down Expand Up @@ -53,6 +53,7 @@
"@cfworker/json-schema": "2.0.1",
"@octokit/auth-app": "7.1.0",
"@octokit/core": "6.1.2",
"@octokit/plugin-paginate-graphql": "^5.2.4",
"@octokit/plugin-paginate-rest": "11.3.3",
"@octokit/plugin-rest-endpoint-methods": "13.2.4",
"@octokit/plugin-retry": "7.1.1",
Expand All @@ -61,7 +62,7 @@
"@octokit/types": "^13.5.0",
"@octokit/webhooks": "13.3.0",
"@octokit/webhooks-types": "7.5.1",
"@sinclair/typebox": "0.32.35",
"@sinclair/typebox": "^0.33.17",
"@ubiquity-os/ubiquity-os-logger": "^1.3.2",
"dotenv": "16.4.5",
"hono": "4.4.13",
Expand Down
1 change: 0 additions & 1 deletion revert-pr-108.txt

This file was deleted.

2 changes: 2 additions & 0 deletions src/github/handlers/push-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async function checkPluginConfigurations(context: GitHubContext<"push">, config:
value: JSON.stringify(plugin),
type: 0,
schema: configSchema,
errors: [],
});
} else {
const validator = new Validator(manifest.configuration, "7", false);
Expand All @@ -113,6 +114,7 @@ async function checkPluginConfigurations(context: GitHubContext<"push">, config:
value: JSON.stringify(value),
type: 0,
schema: configSchema,
errors: [],
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/github/types/plugin-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const configSchema = T.Object(
plugins: handlerSchema,
},
{
additionalProperties: false,
additionalProperties: true,
}
);

Expand Down
22 changes: 16 additions & 6 deletions src/sdk/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks";
import { Type as T, TAnySchema } from "@sinclair/typebox";
import { TAnySchema, 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 { config } from "dotenv";
Expand Down Expand Up @@ -58,14 +58,24 @@ export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TSu

let config: TConfig;
if (pluginOptions.settingsSchema) {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, JSON.parse(inputs.settings)));
try {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, JSON.parse(inputs.settings)));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.settingsSchema, JSON.parse(inputs.settings)), { depth: null });
throw e;
}
} else {
config = JSON.parse(inputs.settings) as TConfig;
}

let env: TEnv;
if (pluginOptions.envSchema) {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, process.env));
try {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, process.env));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.envSchema, process.env), { depth: null });
throw e;
}
} else {
env = process.env as TEnv;
}
Expand Down Expand Up @@ -99,12 +109,12 @@ export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TSu
}

if (pluginOptions.postCommentOnError && loggerError) {
await postComment(context, loggerError);
await postErrorComment(context, loggerError);
}
}
}

async function postComment(context: Context, error: LogReturn) {
async function postErrorComment(context: Context, error: LogReturn) {
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved
if ("issue" in context.payload && context.payload.repository?.owner?.login) {
await context.octokit.rest.issues.createComment({
owner: context.payload.repository.owner.login,
Expand All @@ -113,7 +123,7 @@ async function postComment(context: Context, error: LogReturn) {
body: `${error.logMessage.diff}\n<!--\n${getGithubWorkflowRunUrl()}\n${sanitizeMetadata(error.metadata)}\n-->`,
});
} else {
context.logger.info("Cannot post comment because issue is not found in the payload");
context.logger.info("Cannot post error comment because issue is not found in the payload");
}
}

Expand Down
48 changes: 48 additions & 0 deletions src/sdk/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Context } from "./context";
import { LogReturn } from "@ubiquity-os/ubiquity-os-logger";
import { sanitizeMetadata } from "./util";

const HEADER_NAME = "Ubiquity";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused on the purpose of this but if the full header is how we'll search for and parse the structured metadata then we can pull the org via the payload


/**
* Posts a comment on a GitHub issue if the issue exists in the context payload, embedding structured metadata to it.
*/
export async function postComment(context: Context, message: LogReturn) {
if ("issue" in context.payload && context.payload.repository?.owner?.login) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add if("pull_request" in ...) as well and use pull_request.number?

I'm not 100% if payloads with "pull_request" in it always have "issue" too

const metadata = createStructuredMetadata(message.metadata?.name, message);
Copy link
Member

@Keyrxng Keyrxng Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message.metadata?.name will require we update the logger. I implemented pluginName in this logger PR, is that what name represents here?

No, name represents className not pluginName sorry, I'm confused what className represents now is it the header by which to parse the structuredMetadata e.g ask-llm-response

await context.octokit.rest.issues.createComment({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
body: [message.logMessage.diff, metadata].join("\n"),
});
} else {
context.logger.info("Cannot post comment because issue is not found in the payload");
}
}

function createStructuredMetadata(className: string | undefined, logReturn: LogReturn) {
const logMessage = logReturn.logMessage;
const metadata = logReturn.metadata;

const jsonPretty = sanitizeMetadata(metadata);
const stack = logReturn.metadata?.stack;
const stackLine = (Array.isArray(stack) ? stack.join("\n") : stack)?.split("\n")[2] ?? "";
const caller = stackLine.match(/at (\S+)/)?.[1] ?? "";
Copy link
Member

@Keyrxng Keyrxng Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The QA images I grabbed show that caller is always undefined no matter if it's new Error or throw logger

You can take caller from LogReturn it should already have it and so should Error too.

Personally I don't see the value in sticking the invoking fn name in the header used for search, but maybe that's short sighted of me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of that code was some copy paste of some function you implemented in another project I think, forgot which one, will update that.

Copy link
Member

@Keyrxng Keyrxng Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's implemented in command-ask but I only added it last week and took it from this PR. It does look familiar tho I can agree with that, akin to the logger internals I thought at first. Possible I wrote it somewhere else but I can't recall

https://github.com/ubiquity-os-marketplace/command-ask/blame/eb5487d4879e48757ff758ed3fa90728469edcc1/src/handlers/comment-created-callback.ts#L63

Copy link
Member

@Keyrxng Keyrxng Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's from Ubiquibot V1 actually as far back as that

const ubiquityMetadataHeader = `<!-- ${HEADER_NAME} - ${className} - ${caller} - ${metadata?.revision}`;

let metadataSerialized: string;
const metadataSerializedVisible = ["```json", jsonPretty, "```"].join("\n");
const metadataSerializedHidden = [ubiquityMetadataHeader, jsonPretty, "-->"].join("\n");

if (logMessage?.type === "fatal") {
// if the log message is fatal, then we want to show the metadata
metadataSerialized = [metadataSerializedVisible, metadataSerializedHidden].join("\n");
Copy link
Member

@Keyrxng Keyrxng Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this how it should be displayed? Why don't we just replace <!-- --!> with backticks and show the structured data with the header, is there a reason?

image

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think because it allows us to search for the metadata

Copy link
Member

@Keyrxng Keyrxng Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought search would be based on the components of the header not including the HTML comment syntax <!-- at the start of the header. Bit harder to parse I guess without it, my point was duplicating the metadata but it's not a real problem


It's very unlikely we'll ever search by caller or by revision I think, what about you guys?

If we used just the headers we could do something like this UbiquityOS - <orgName> - <pluginName>. First item being the name of the bot, just to make the query a bit more specific. If className === LogLevel then that gives us a type of filter for search if we include it.

Is there a spec for search/parse implementation yet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we search we need to search for the comment syntax otherwise it can match other things, so we need to search for <-- UbiquityOS ... <metadata> ... -->

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't resolve this but think it can be if double metadata being posted is not a problem

} else {
// otherwise we want to hide it
metadataSerialized = metadataSerializedHidden;
}

// Add carriage returns to avoid any formatting issue
return `\n${metadataSerialized}\n`;
}
2 changes: 2 additions & 0 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { createPlugin } from "./server";
export { createActionsPlugin } from "./actions";
export { postComment } from "./comment";
export type { Context } from "./context";
export * from "./constants";
export type { Manifest } from "../types/manifest";
3 changes: 2 additions & 1 deletion src/sdk/octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { paginateRest } from "@octokit/plugin-paginate-rest";
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
import { retry } from "@octokit/plugin-retry";
import { throttling } from "@octokit/plugin-throttling";
import { paginateGraphQL } from "@octokit/plugin-paginate-graphql";

const defaultOptions = {
throttle: {
Expand All @@ -22,6 +23,6 @@ const defaultOptions = {
},
};

export const customOctokit = Octokit.plugin(throttling, retry, paginateRest, restEndpointMethods).defaults((instanceOptions: object) => {
export const customOctokit = Octokit.plugin(throttling, retry, paginateRest, restEndpointMethods, paginateGraphQL).defaults((instanceOptions: object) => {
return Object.assign({}, defaultOptions, instanceOptions);
});
30 changes: 14 additions & 16 deletions src/sdk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { KERNEL_PUBLIC_KEY } from "./constants";
import { Context } from "./context";
import { customOctokit } from "./octokit";
import { verifySignature } from "./signature";
import { sanitizeMetadata } from "./util";
import { env as honoEnv } from "hono/adapter";
import { postComment } from "./comment";
import { Type as T } from "@sinclair/typebox";

interface Options {
Expand Down Expand Up @@ -62,14 +63,24 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents

let config: TConfig;
if (pluginOptions.settingsSchema) {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, inputs.settings));
try {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, inputs.settings));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.settingsSchema, inputs.settings), { depth: null });
throw e;
}
} else {
config = inputs.settings as TConfig;
}

let env: TEnv;
if (pluginOptions.envSchema) {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, ctx.env));
try {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, honoEnv(ctx)));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.envSchema, ctx.env), { depth: null });
throw e;
}
} else {
env = ctx.env as TEnv;
}
Expand Down Expand Up @@ -108,16 +119,3 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents

return app;
}

async function postComment(context: Context, error: LogReturn) {
if ("issue" in context.payload && context.payload.repository?.owner?.login) {
await context.octokit.rest.issues.createComment({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
body: `${error.logMessage.diff}\n<!--\n${sanitizeMetadata(error.metadata)}\n-->`,
});
} else {
context.logger.info("Cannot post comment because issue is not found in the payload");
}
}
6 changes: 4 additions & 2 deletions tests/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ describe("SDK worker tests", () => {
body: `\`\`\`diff
! test error
\`\`\`
<!--

<!-- Ubiquity - undefined - - undefined
{
"caller": "error"
}
-->`,
-->
`,
});
});
it("Should accept correct request", async () => {
Expand Down