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 ubiquibot logger support #16

Merged
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
6 changes: 6 additions & 0 deletions .dev.vars.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
WEBHOOK_PROXY_URL=https://smee.io/new
WEBHOOK_SECRET=xxxxxx

SUPABASE_URL=
SUPABASE_KEY=

LOG_RETRY_LIMIT=
LOG_LEVEL=
Copy link
Member

Choose a reason for hiding this comment

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

Also should include documentation around this. I'm assuming verbose is fine

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{

"typescript.enablePromptUseWorkspaceTsdk": true,
"cSpell.words": ["smee"]
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ The bot kernel is designed to:
## Environment variables

- PRIVATE_KEY
You need to get a private key from Github App settings and convert it to PKCS#8 using this command:
`openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in pkcs8.key -out pkcs8.key`
You need to get a private key from Github App settings and convert it to PKCS#8 using this command:
`openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in pkcs8.key -out pkcs8.key`

- WEBHOOK_SECRET
You need to set it in Github App settings and also set it here.
You need to set it in Github App settings and also set it here.

- APP_ID
You can find this in Github App settings.
You can find this in Github App settings.

- WEBHOOK_PROXY_URL (only for development)
You need to get a webhook URL at <https://smee.io/> and set it in the Github App settings
You need to get a webhook URL at <https://smee.io/> and set it in the Github App settings

### Quick Start

Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
"@octokit/webhooks": "^12.0.10",
"@octokit/webhooks-types": "^7.3.1",
"@sinclair/typebox": "^0.32.5",
"@supabase/supabase-js": "^2.4.0",
"create-cloudflare": "^2.8.3",
"octokit": "^3.1.2",
"smee-client": "^2.0.0"
"smee-client": "^2.0.0",
"ubiquibot-logger": "^0.3.5"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240117.0",
Expand All @@ -53,7 +55,7 @@
"prettier": "^3.1.0",
"tsx": "^4.6.2",
"typescript": "^5.0.4",
"wrangler": "^3.23.0"
"wrangler": "^3.25.0"
},
"lint-staged": {
"*.ts": [
Expand Down
24 changes: 24 additions & 0 deletions src/event-handler.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
import { Webhooks } from "@octokit/webhooks";
import { Context, SimplifiedContext } from "./context";
import { customOctokit } from "./octokit";
import { createClient } from "@supabase/supabase-js";
import { Database } from "./types/database";
import { Logs } from "ubiquibot-logger";
import { LogLevel } from "ubiquibot-logger/pretty-logs";

export type Options = {
webhookSecret: string;
appId: string | number;
privateKey: string;
supabaseUrl: string;
supabaseKey: string;
logRetryLimit: number;
logLevel: LogLevel;
};

export class EventHandler {
public webhooks: Webhooks<SimplifiedContext>;
public on: Webhooks<SimplifiedContext>["on"];
public onAny: Webhooks<SimplifiedContext>["onAny"];
public onError: Webhooks<SimplifiedContext>["onError"];
public log: Logs;

private _webhookSecret: string;
private _privateKey: string;
private _appId: number;
private _supabaseUrl: string;
private _supabaseKey: string;
private _logRetryLimit: number;
private _logLevel: LogLevel;

constructor(options: Options) {
this._privateKey = options.privateKey;
this._appId = Number(options.appId);
this._webhookSecret = options.webhookSecret;
this._supabaseKey = options.supabaseKey;
this._supabaseUrl = options.supabaseUrl;
this._logRetryLimit = options.logRetryLimit;
this._logLevel = options.logLevel;

this.webhooks = new Webhooks<SimplifiedContext>({
secret: this._webhookSecret,
Expand All @@ -42,11 +59,18 @@ export class EventHandler {
},
});

const supabaseClient = createClient<Database>(this._supabaseUrl, this._supabaseKey, {
auth: { persistSession: false },
});

this.log = new Logs(supabaseClient, this._logRetryLimit, this._logLevel, null);

this.on = this.webhooks.on;
this.onAny = this.webhooks.onAny;
this.onError = this.webhooks.onError;

this.onAny((event) => {
this.log.info(`Event ${event.name} received (id: ${event.id})`, { id: event.id, name: event.name });
console.log(`Event ${event.name} received (id: ${event.id})`);
});
this.onError((error) => {
Expand Down
Loading
Loading