Skip to content

Commit

Permalink
feat: ubiquibot-logger integration
Browse files Browse the repository at this point in the history
The ubiquibot-logger integration enables supabase logging for
ubiquibot-kernel.

On top of SUPABASE_KEY and SUPABASE_URL environment variables,
LOG_RETRY_LIMIT and LOG_LEVEL must be specified.

Example:

LOG_RETRY_LIMIT=3
LOG_LEVEL=DEBUG

Resolves: #5
  • Loading branch information
gitcoindev committed Jan 30, 2024
1 parent c65bb90 commit 0585355
Show file tree
Hide file tree
Showing 11 changed files with 648 additions and 9 deletions.
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=
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.
4 changes: 3 additions & 1 deletion 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 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

0 comments on commit 0585355

Please sign in to comment.