Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 2 additions & 12 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 {
/**
Expand Down
8 changes: 8 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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"),
Expand Down
32 changes: 32 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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", () => {
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
29 changes: 29 additions & 0 deletions test/logger.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Loading