-
Notifications
You must be signed in to change notification settings - Fork 0
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: worker log and revision #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -80,13 +80,16 @@ export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TSu | |||
env = process.env as TEnv; | ||||
} | ||||
|
||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
const context: Context<TConfig, TEnv, TSupportedEvents> = { | ||||
eventName: inputs.eventName as TSupportedEvents, | ||||
payload: JSON.parse(inputs.eventPayload), | ||||
octokit: new customOctokit({ auth: inputs.authToken }), | ||||
config: config, | ||||
env: env, | ||||
logger: new Logs(pluginOptions.logLevel), | ||||
pluginDeploymentDetails: getGithubWorkflowRunUrl() | ||||
}; | ||||
|
||||
try { | ||||
|
@@ -120,7 +123,7 @@ async function postErrorComment(context: Context, error: LogReturn) { | |||
owner: context.payload.repository.owner.login, | ||||
repo: context.payload.repository.name, | ||||
issue_number: context.payload.issue.number, | ||||
body: `${error.logMessage.diff}\n<!--\n${getGithubWorkflowRunUrl()}\n${sanitizeMetadata(error.metadata)}\n-->`, | ||||
body: `${error.logMessage.diff}\n<!--\n${context.pluginDeploymentDetails}\n${sanitizeMetadata(error.metadata)}\n-->`, | ||||
}); | ||||
} else { | ||||
context.logger.info("Cannot post error comment because issue is not found in the payload"); | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import { EmitterWebhookEventName as WebhookEventName } from "@octokit/webhooks"; | |
import { TAnySchema } from "@sinclair/typebox"; | ||
import { Value } from "@sinclair/typebox/value"; | ||
import { LOG_LEVEL, LogLevel, LogReturn, Logs } from "@ubiquity-os/ubiquity-os-logger"; | ||
import { Hono } from "hono"; | ||
import { Env, Hono } from "hono"; | ||
import { HTTPException } from "hono/http-exception"; | ||
import { Manifest } from "../types/manifest"; | ||
import { KERNEL_PUBLIC_KEY } from "./constants"; | ||
|
@@ -31,6 +31,11 @@ const inputSchema = T.Object({ | |
signature: T.String(), | ||
}); | ||
|
||
export type CloudflareEnvBindings = { | ||
CLOUDFLARE_ACCOUNT_ID: string, | ||
CLOUDFLARE_API_TOKEN: string | ||
} | ||
|
||
export function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>( | ||
handler: (context: Context<TConfig, TEnv, TSupportedEvents>) => Promise<Record<string, unknown> | undefined>, | ||
manifest: Manifest, | ||
|
@@ -44,7 +49,8 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents | |
envSchema: options?.envSchema, | ||
}; | ||
|
||
const app = new Hono(); | ||
|
||
const app = new Hono<{ Bindings: CloudflareEnvBindings }>(); | ||
|
||
app.get("/manifest.json", (ctx) => { | ||
return ctx.json(manifest); | ||
|
@@ -92,32 +98,33 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TSupportedEvents | |
env = ctx.env as TEnv; | ||
} | ||
|
||
const workerUrl = new URL(inputs.ref).origin; | ||
let workerName; | ||
|
||
if (workerUrl.includes("localhost")) { | ||
workerName = "localhost"; | ||
} else { | ||
workerName = `${workerUrl.split("//")[1].split(".")[0]}`; | ||
} | ||
|
||
const context: Context<TConfig, TEnv, TSupportedEvents> = { | ||
eventName: inputs.eventName as TSupportedEvents, | ||
payload: inputs.eventPayload, | ||
octokit: new customOctokit({ auth: inputs.authToken }), | ||
config: config, | ||
env: env, | ||
logger: new Logs(pluginOptions.logLevel), | ||
pluginDeploymentDetails: `${workerName}`, | ||
}; | ||
|
||
try { | ||
const result = await handler(context); | ||
return ctx.json({ stateId: inputs.stateId, output: result }); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
let loggerError: LogReturn | null; | ||
if (error instanceof Error) { | ||
loggerError = context.logger.error(`Error: ${error}`, { error: error }); | ||
} else if (error instanceof LogReturn) { | ||
loggerError = error; | ||
} else { | ||
loggerError = context.logger.error(`Error: ${error}`); | ||
} | ||
|
||
const loggerError = error as LogReturn | Error | null; | ||
if (pluginOptions.postCommentOnError && loggerError) { | ||
await postComment(context, loggerError); | ||
await postComment(context, loggerError, honoEnvironment); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've passed it as a param here instead of building it into At least that was my intention, I hope that it holds true. |
||
} | ||
|
||
throw new HTTPException(500, { message: "Unexpected error" }); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.