Skip to content

Commit

Permalink
fix: un-deprecated .log() calls to ease transition to Probot v10
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Aug 17, 2020
1 parent 131031b commit 4e7884e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getOctokitThrottleOptions } from "./octokit/get-octokit-throttle-option
import { getProbotOctokitWithDefaults } from "./octokit/get-probot-octokit-with-defaults";
import { DeprecatedLogger, ProbotWebhooks, State } from "./types";
import { webhookEventCheck } from "./helpers/webhook-event-check";
import { deprecateLog } from "./helpers/deprecate-log";
import { aliasLog } from "./helpers/alias-log";
import { getWebhooks } from "./octokit/get-webhooks";

export interface Options {
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Application {
private state: State;

constructor(options: Options) {
this.log = deprecateLog(options.log || getLog());
this.log = aliasLog(options.log || getLog());

// TODO: support redis backend for access token cache if `options.redisConfig || process.env.REDIS_URL`
const cache =
Expand Down
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import yaml from "js-yaml";
import type { Logger } from "pino";

import { ProbotOctokit } from "./octokit/probot-octokit";
import { deprecateLog } from "./helpers/deprecate-log";
import { aliasLog } from "./helpers/alias-log";
import { DeprecatedLogger } from "./types";

type ReposGetContentsParams = Endpoints["GET /repos/:owner/:repo/contents/:path"]["parameters"];
Expand Down Expand Up @@ -88,7 +88,7 @@ export class Context<E extends WebhookPayloadWithRepository = any>
this.payload = event.payload;

this.github = github;
this.log = deprecateLog(log);
this.log = aliasLog(log);
}

// Maintain backward compatibility
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/alias-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Logger } from "pino";

import type { DeprecatedLogger } from "../types";

/**
* `probot.log()`, `app.log()` and `context.log()` are aliasing `.log.info()`.
* We will probably remove the aliasing in future.
*/
export function aliasLog(log: Logger): DeprecatedLogger {
function logInfo() {
// @ts-ignore
log.info(...arguments);
}

for (const key in log) {
// @ts-ignore
logInfo[key] =
typeof log[key] === "function" ? log[key].bind(log) : log[key];
}

// @ts-ignore
return logInfo;
}
29 changes: 0 additions & 29 deletions src/helpers/deprecate-log.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { getErrorHandler } from "./helpers/get-error-handler";
import { DeprecatedLogger, ProbotWebhooks, State } from "./types";
import { getOctokitThrottleOptions } from "./octokit/get-octokit-throttle-options";
import { getProbotOctokitWithDefaults } from "./octokit/get-probot-octokit-with-defaults";
import { deprecateLog } from "./helpers/deprecate-log";
import { aliasLog } from "./helpers/alias-log";
import { logWarningsForObsoleteEnvironmentVariables } from "./helpers/log-warnings-for-obsolete-environment-variables";
import { getWebhooks } from "./octokit/get-webhooks";

Expand Down Expand Up @@ -177,7 +177,7 @@ export class Probot {
options.webhookPath = options.webhookPath || "/";
options.secret = options.secret || "development";

this.log = deprecateLog(options.log || getLog());
this.log = aliasLog(options.log || getLog());

if (options.cert) {
this.log.warn(
Expand Down
35 changes: 1 addition & 34 deletions test/deprecations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Stream from "stream";
import { Webhooks } from "@octokit/webhooks";
import pino from "pino";

import { Application, Context, createProbot, Probot } from "../src";
import { createProbot, Probot } from "../src";

describe("Deprecations", () => {
let output: any;
Expand Down Expand Up @@ -62,37 +62,4 @@ describe("Deprecations", () => {
`[probot] "probot.logger" is deprecated. Use "probot.log" instead`
);
});

it("probot.log()", () => {
const probot = new Probot({ log: pino(streamLogsToOutput) });
probot.log("test");

expect(output.length).toEqual(2);
expect(output[0].msg).toContain(
'[probot] "app.log()" and "context.log()" are deprecated. Use "app.log.info()" and "context.log.info()" instead'
);
});

it("app.log()", () => {
const app = new Application({
log: pino(streamLogsToOutput),
secret: "secret",
});
app.log("test");

expect(output.length).toEqual(2);
expect(output[0].msg).toContain(
'[probot] "app.log()" and "context.log()" are deprecated. Use "app.log.info()" and "context.log.info()" instead'
);
});

it("context.log()", () => {
const context = new Context({} as any, {} as any, pino(streamLogsToOutput));
context.log("test");

expect(output.length).toEqual(2);
expect(output[0].msg).toContain(
'[probot] "app.log()" and "context.log()" are deprecated. Use "app.log.info()" and "context.log.info()" instead'
);
});
});

0 comments on commit 4e7884e

Please sign in to comment.