refactor: setup the logger using core configuration values (#408)

This commit is contained in:
Eden Zimbelman
2025-03-12 11:27:30 -07:00
committed by GitHub
parent 1ebd7369aa
commit 21be76f4cc
8 changed files with 111 additions and 12 deletions
+1
View File
@@ -11,6 +11,7 @@
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/github": "^6.0.0",
"@slack/logger": "^4.0.0",
"@slack/web-api": "^7.8.0",
"axios": "^1.8.2",
"axios-retry": "^4.5.0",
+1
View File
@@ -35,6 +35,7 @@
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/github": "^6.0.0",
"@slack/logger": "^4.0.0",
"@slack/web-api": "^7.8.0",
"axios": "^1.8.2",
"axios-retry": "^4.5.0",
+2 -12
View File
@@ -1,4 +1,4 @@
import webapi, { LogLevel } from "@slack/web-api";
import webapi from "@slack/web-api";
import { HttpsProxyAgent } from "https-proxy-agent";
import Config from "./config.js";
import SlackError from "./errors.js";
@@ -24,18 +24,8 @@ export default class Client {
}
const client = new config.webapi.WebClient(config.inputs.token, {
agent: this.proxies(config)?.httpsAgent,
logger: config.logger,
retryConfig: this.retries(config.inputs.retries),
logger: {
debug: config.core.debug,
info: config.core.info,
warn: config.core.warning,
error: config.core.error,
getLevel: () => {
return config.core.isDebug() ? LogLevel.DEBUG : LogLevel.INFO;
},
setLevel: (_level) => {},
setName: (_name) => {},
},
});
try {
/**
+8
View File
@@ -3,6 +3,7 @@ import webapi from "@slack/web-api";
import axios from "axios";
import Content from "./content.js";
import SlackError from "./errors.js";
import Logger from "./logger.js";
/**
* Options and settings set as inputs to this action.
@@ -73,6 +74,12 @@ export default class Config {
*/
core;
/**
* The logger of outputs.
* @type {import("@slack/logger").Logger}
*/
logger;
/**
* @type {import("@slack/web-api")} - Slack API client.
*/
@@ -91,6 +98,7 @@ export default class Config {
constructor(core) {
this.axios = axios;
this.core = core;
this.logger = new Logger(core).logger;
this.webapi = webapi;
this.inputs = {
errors: core.getBooleanInput("errors"),
+32
View File
@@ -0,0 +1,32 @@
import { LogLevel } from "@slack/logger";
/**
* The Logger class creates a Logger to output debug messages and errors.
*
* @see {@link https://tools.slack.dev/node-slack-sdk/web-api/#logging}
*/
export default class Logger {
/**
* The logger for outputs.
* @type {import("@slack/logger").Logger}
*/
logger;
/**
* Shared utilities specific to the GitHub action workflow.
* @param {import("@actions/core")} core - GitHub Actions core utilities.
*/
constructor(core) {
this.logger = {
debug: core.debug,
info: core.info,
warn: core.warning,
error: core.error,
getLevel: () => {
return core.isDebug() ? LogLevel.DEBUG : LogLevel.INFO;
},
setLevel: (_level) => {},
setName: (_name) => {},
};
}
}
+31
View File
@@ -2,9 +2,11 @@ import core from "@actions/core";
import webapi from "@slack/web-api";
import errors from "@slack/web-api/dist/errors.js";
import { assert } from "chai";
import sinon from "sinon";
import Client from "../src/client.js";
import Config from "../src/config.js";
import SlackError from "../src/errors.js";
import Logger from "../src/logger.js";
import send from "../src/send.js";
import { mocks } from "./index.spec.js";
@@ -58,6 +60,35 @@ describe("client", () => {
}
}
});
it("uses input arguments when constructing the web api client", async () => {
const spy = sinon.spy(mocks.webapi, "WebClient");
/**
* @type {Config}
*/
const config = {
content: {
values: {},
},
core: core,
logger: new Logger(core).logger,
inputs: {
method: "pins.add",
retries: "10",
token: "xoxb-example-002",
},
webapi: mocks.webapi,
};
await new Client().post(config);
assert.isTrue(spy.calledWithNew());
assert.isTrue(
spy.calledWith("xoxb-example-002", {
agent: undefined,
logger: config.logger,
retryConfig: webapi.retryPolicies.tenRetriesInAboutThirtyMinutes,
}),
);
});
});
describe("success", () => {
+7
View File
@@ -47,6 +47,13 @@ export class Mock {
this.axios = this.sandbox.stub(axios);
this.core = this.sandbox.stub(core);
this.fs = this.sandbox.stub(fs);
this.webapi = {
WebClient: function () {
this.apiCall = () => ({
ok: true,
});
},
};
this.core.getInput.withArgs("errors").returns("false");
this.core.getInput.withArgs("retries").returns("5");
}
+29
View File
@@ -0,0 +1,29 @@
import core from "@actions/core";
import { LogLevel } from "@slack/logger";
import { assert } from "chai";
import Logger from "../src/logger.js";
import { mocks } from "./index.spec.js";
describe("logger", () => {
beforeEach(() => {
mocks.reset();
});
describe("level", () => {
it("debug", () => {
mocks.core.isDebug = () => true;
const { logger } = new Logger(core);
const actual = logger.getLevel();
const expected = LogLevel.DEBUG;
assert.equal(actual, expected);
});
it("info", () => {
mocks.core.isDebug = () => false;
const { logger } = new Logger(core);
const actual = logger.getLevel();
const expected = LogLevel.INFO;
assert.equal(actual, expected);
});
});
});