Skip to content

Commit 21be76f

Browse files
authored
refactor: setup the logger using core configuration values (#408)
1 parent 1ebd736 commit 21be76f

8 files changed

Lines changed: 111 additions & 12 deletions

File tree

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"@actions/core": "^1.11.1",
3737
"@actions/github": "^6.0.0",
38+
"@slack/logger": "^4.0.0",
3839
"@slack/web-api": "^7.8.0",
3940
"axios": "^1.8.2",
4041
"axios-retry": "^4.5.0",

src/client.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import webapi, { LogLevel } from "@slack/web-api";
1+
import webapi from "@slack/web-api";
22
import { HttpsProxyAgent } from "https-proxy-agent";
33
import Config from "./config.js";
44
import SlackError from "./errors.js";
@@ -24,18 +24,8 @@ export default class Client {
2424
}
2525
const client = new config.webapi.WebClient(config.inputs.token, {
2626
agent: this.proxies(config)?.httpsAgent,
27+
logger: config.logger,
2728
retryConfig: this.retries(config.inputs.retries),
28-
logger: {
29-
debug: config.core.debug,
30-
info: config.core.info,
31-
warn: config.core.warning,
32-
error: config.core.error,
33-
getLevel: () => {
34-
return config.core.isDebug() ? LogLevel.DEBUG : LogLevel.INFO;
35-
},
36-
setLevel: (_level) => {},
37-
setName: (_name) => {},
38-
},
3929
});
4030
try {
4131
/**

src/config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import webapi from "@slack/web-api";
33
import axios from "axios";
44
import Content from "./content.js";
55
import SlackError from "./errors.js";
6+
import Logger from "./logger.js";
67

78
/**
89
* Options and settings set as inputs to this action.
@@ -73,6 +74,12 @@ export default class Config {
7374
*/
7475
core;
7576

77+
/**
78+
* The logger of outputs.
79+
* @type {import("@slack/logger").Logger}
80+
*/
81+
logger;
82+
7683
/**
7784
* @type {import("@slack/web-api")} - Slack API client.
7885
*/
@@ -91,6 +98,7 @@ export default class Config {
9198
constructor(core) {
9299
this.axios = axios;
93100
this.core = core;
101+
this.logger = new Logger(core).logger;
94102
this.webapi = webapi;
95103
this.inputs = {
96104
errors: core.getBooleanInput("errors"),

src/logger.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { LogLevel } from "@slack/logger";
2+
3+
/**
4+
* The Logger class creates a Logger to output debug messages and errors.
5+
*
6+
* @see {@link https://tools.slack.dev/node-slack-sdk/web-api/#logging}
7+
*/
8+
export default class Logger {
9+
/**
10+
* The logger for outputs.
11+
* @type {import("@slack/logger").Logger}
12+
*/
13+
logger;
14+
15+
/**
16+
* Shared utilities specific to the GitHub action workflow.
17+
* @param {import("@actions/core")} core - GitHub Actions core utilities.
18+
*/
19+
constructor(core) {
20+
this.logger = {
21+
debug: core.debug,
22+
info: core.info,
23+
warn: core.warning,
24+
error: core.error,
25+
getLevel: () => {
26+
return core.isDebug() ? LogLevel.DEBUG : LogLevel.INFO;
27+
},
28+
setLevel: (_level) => {},
29+
setName: (_name) => {},
30+
};
31+
}
32+
}

test/client.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import core from "@actions/core";
22
import webapi from "@slack/web-api";
33
import errors from "@slack/web-api/dist/errors.js";
44
import { assert } from "chai";
5+
import sinon from "sinon";
56
import Client from "../src/client.js";
67
import Config from "../src/config.js";
78
import SlackError from "../src/errors.js";
9+
import Logger from "../src/logger.js";
810
import send from "../src/send.js";
911
import { mocks } from "./index.spec.js";
1012

@@ -58,6 +60,35 @@ describe("client", () => {
5860
}
5961
}
6062
});
63+
64+
it("uses input arguments when constructing the web api client", async () => {
65+
const spy = sinon.spy(mocks.webapi, "WebClient");
66+
/**
67+
* @type {Config}
68+
*/
69+
const config = {
70+
content: {
71+
values: {},
72+
},
73+
core: core,
74+
logger: new Logger(core).logger,
75+
inputs: {
76+
method: "pins.add",
77+
retries: "10",
78+
token: "xoxb-example-002",
79+
},
80+
webapi: mocks.webapi,
81+
};
82+
await new Client().post(config);
83+
assert.isTrue(spy.calledWithNew());
84+
assert.isTrue(
85+
spy.calledWith("xoxb-example-002", {
86+
agent: undefined,
87+
logger: config.logger,
88+
retryConfig: webapi.retryPolicies.tenRetriesInAboutThirtyMinutes,
89+
}),
90+
);
91+
});
6192
});
6293

6394
describe("success", () => {

test/index.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ export class Mock {
4747
this.axios = this.sandbox.stub(axios);
4848
this.core = this.sandbox.stub(core);
4949
this.fs = this.sandbox.stub(fs);
50+
this.webapi = {
51+
WebClient: function () {
52+
this.apiCall = () => ({
53+
ok: true,
54+
});
55+
},
56+
};
5057
this.core.getInput.withArgs("errors").returns("false");
5158
this.core.getInput.withArgs("retries").returns("5");
5259
}

test/logger.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import core from "@actions/core";
2+
import { LogLevel } from "@slack/logger";
3+
import { assert } from "chai";
4+
import Logger from "../src/logger.js";
5+
import { mocks } from "./index.spec.js";
6+
7+
describe("logger", () => {
8+
beforeEach(() => {
9+
mocks.reset();
10+
});
11+
12+
describe("level", () => {
13+
it("debug", () => {
14+
mocks.core.isDebug = () => true;
15+
const { logger } = new Logger(core);
16+
const actual = logger.getLevel();
17+
const expected = LogLevel.DEBUG;
18+
assert.equal(actual, expected);
19+
});
20+
21+
it("info", () => {
22+
mocks.core.isDebug = () => false;
23+
const { logger } = new Logger(core);
24+
const actual = logger.getLevel();
25+
const expected = LogLevel.INFO;
26+
assert.equal(actual, expected);
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)