Repository: slackapi/slack-github-action
Pull request #408 (2025-03-12 16:47:16)

refactor: setup the logger using core configuration values
### Summary

This PR refactors the `logger` setup used for `@slack/web-api` to a standalone file to test that the configured action inputs are used when constructing the `WebClient` 👾 

Hopefully no changes to functionality, but an improvement to testing for following changes!

### Requirements

* [x] I've read and understood the [Contributing Guidelines](https://github.com/slackapi/slack-github-action/blob/main/.github/contributing.md) and have done my best effort to follow them.
* [x] I've read and agree to the [Code of Conduct](https://slackhq.github.io/code-of-conduct).

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/slackapi/slack-github-action/pull/408?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=slackapi) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 99.85%. Comparing base [(`1ebd736`)](https://app.codecov.io/gh/slackapi/slack-github-action/commit/1ebd7369aaf3a6d504deead6fff697aa960eea9c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=slackapi) to head [(`1ee5b59`)](https://app.codecov.io/gh/slackapi/slack-github-action/commit/1ee5b59bbfff078006418addf2660c8c9b89c3be?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=slackapi).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #408      +/-   ##
==========================================
+ Coverage   99.70%   99.85%   +0.15%     
==========================================
  Files           6        7       +1     
  Lines         669      699      +30     
==========================================
+ Hits          667      698      +31     
+ Misses          2        1       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/slackapi/slack-github-action/pull/408?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=slackapi).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=slackapi).

<details><summary>🚀 New features to boost your workflow: </summary>

- ❄ [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- 📦 [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
zimeg: @WilliamBergamin Thanks so much! I'm hoping this `logger` might find use elsewhere too, but I'm most excited for the testing improvements TBH 🌲 ✨ 

Patch:
diff --git a/package-lock.json b/package-lock.json
index 183d83e..2383274 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -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",
diff --git a/package.json b/package.json
index e352134..c141538 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/client.js b/src/client.js
index bb59d31..8d371b2 100644
--- a/src/client.js
+++ b/src/client.js
@@ -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 {
       /**
diff --git a/src/config.js b/src/config.js
index 52d5757..9a3ff11 100644
--- a/src/config.js
+++ b/src/config.js
@@ -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"),
diff --git a/src/logger.js b/src/logger.js
new file mode 100644
index 0000000..79a2601
--- /dev/null
+++ b/src/logger.js
@@ -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) => {},
+    };
+  }
+}


Test patch:
diff --git a/test/client.spec.js b/test/client.spec.js
index 1561f89..5bfff66 100644
--- a/test/client.spec.js
+++ b/test/client.spec.js
@@ -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", () => {
diff --git a/test/index.spec.js b/test/index.spec.js
index 4e21eae..009a2f2 100644
--- a/test/index.spec.js
+++ b/test/index.spec.js
@@ -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");
   }
diff --git a/test/logger.spec.js b/test/logger.spec.js
new file mode 100644
index 0000000..09fb0de
--- /dev/null
+++ b/test/logger.spec.js
@@ -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);
+    });
+  });
+});