Skip to content

Commit

Permalink
feat(Octokit): add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Jun 23, 2023
1 parent 8cb06f7 commit 67ba239
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/octocat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@ijsblokje/server": "*",
"@ijsblokje/utils": "*",
"@octokit/request": "7.0.0",
"@snowcrystals/icicle": "2.0.4",
"glob": "^10.2.7",
"redis": "^4.6.7",
"zod": "^3.21.4"
Expand Down
4 changes: 3 additions & 1 deletion packages/octocat/src/lib/Octocat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InstallationManager } from "./managers/InstallationManager.js";
import { createClient } from "redis";
import { Server } from "@ijsblokje/server";
import { EventManager } from "./managers/EventManager.js";
import { Logger } from "@snowcrystals/icicle";

export class Octocat {
public readonly installations: InstallationManager;
Expand All @@ -23,7 +24,8 @@ export class Octocat {
appId: options.appId,
privateKey: options.privateKey,
clientId: options.clientId,
clientSecret: options.clientSecret
clientSecret: options.clientSecret,
logger: new Logger({ name: "Octokit" })
});

this.installations = new InstallationManager(this, options.allowedInstallations);
Expand Down
1 change: 1 addition & 0 deletions packages/octokit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@octokit/plugin-throttling": "^6.1.0"
},
"devDependencies": {
"@snowcrystals/icicle": "^2.0.4",
"@types/node": "^18.16.18",
"eslint": "^8.43.0",
"prettier": "^2.8.8",
Expand Down
12 changes: 11 additions & 1 deletion packages/octokit/src/Octokit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createAppAuth } from "@octokit/auth-app";
import { Octokit as CoreOctokit } from "@octokit/core";
import { throttling } from "@octokit/plugin-throttling";
import type { Logger } from "@snowcrystals/icicle";

const ExtendableOctokit = CoreOctokit.plugin(throttling) as typeof CoreOctokit;

Expand All @@ -23,12 +24,16 @@ export class Octokit extends ExtendableOctokit {
/** The request user-agent */
public readonly userAgent: string;

/** The @snowcrystals/icicle logger instance */
public readonly logger: Logger;

public get options() {
const options: OctokitOptions = {
appId: this.appId,
privateKey: this.privateKey,
clientId: this.clientId,
clientSecret: this.clientSecret
clientSecret: this.clientSecret,
logger: this.logger
};

if (this.installationId) options.installationId = this.installationId;
Expand All @@ -41,6 +46,7 @@ export class Octokit extends ExtendableOctokit {
userAgent,
auth: options,
authStrategy: createAppAuth,
log: options.logger,
throttle: {
enabled: true,
onRateLimit: Octokit.onRateLimit.bind(Octokit),
Expand All @@ -55,6 +61,7 @@ export class Octokit extends ExtendableOctokit {
this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
this.installationId = options.installationId;
this.logger = options.logger;
}

/**
Expand Down Expand Up @@ -113,4 +120,7 @@ export interface OctokitOptions {

/** The installation id */
installationId?: number;

/** The @snowcrystals/icicle logger instance */
logger: Logger;
}
4 changes: 3 additions & 1 deletion packages/octokit/tests/Octokit.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Logger } from "@snowcrystals/icicle";
import { Octokit, type OctokitOptions } from "../src/index.js";

/** Checks whether the input is a class or not */
Expand All @@ -14,7 +15,8 @@ describe("Octokit", () => {
appId: 15483,
clientId: "",
clientSecret: "",
privateKey: "-----BEGIN RSA PRIVATE KEY-----\ntest test test\n-----END RSA PRIVATE KEY-----"
privateKey: "-----BEGIN RSA PRIVATE KEY-----\ntest test test\n-----END RSA PRIVATE KEY-----",
logger: new Logger({ name: "Octokit" })
};

test("Without installationId", () => {
Expand Down
4 changes: 3 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ __metadata:
"@octokit/openapi-types": ^18.0.0
"@octokit/request": 7.0.0
"@octokit/types": ^10.0.0
"@snowcrystals/icicle": 2.0.4
"@types/node": ^18.16.18
eslint: ^8.43.0
glob: ^10.2.7
Expand All @@ -536,6 +537,7 @@ __metadata:
"@octokit/auth-app": 5.0.5
"@octokit/core": 4.2.4
"@octokit/plugin-throttling": ^6.1.0
"@snowcrystals/icicle": ^2.0.4
"@types/node": ^18.16.18
eslint: ^8.43.0
prettier: ^2.8.8
Expand Down Expand Up @@ -1021,7 +1023,7 @@ __metadata:
languageName: node
linkType: hard

"@snowcrystals/icicle@npm:^2.0.4":
"@snowcrystals/icicle@npm:2.0.4, @snowcrystals/icicle@npm:^2.0.4":
version: 2.0.4
resolution: "@snowcrystals/icicle@npm:2.0.4"
dependencies:
Expand Down

0 comments on commit 67ba239

Please sign in to comment.