From d4e83d677700a7bf02ec230864a1874258dae18e Mon Sep 17 00:00:00 2001 From: "@zimeg" Date: Thu, 31 Oct 2024 19:04:38 -0700 Subject: [PATCH] fix: return the channel id from methods that return it as channel.id --- src/client.js | 9 ++++++--- test/client.spec.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/client.js b/src/client.js index e5fce2b..49aa799 100644 --- a/src/client.js +++ b/src/client.js @@ -54,7 +54,7 @@ export default class Client { }); try { /** - * @type {webapi.WebAPICallResult & MessageResult=} + * @type {webapi.WebAPICallResult & import("@slack/web-api").ChatPostMessageResponse & import("@slack/web-api").ConversationsCreateResponse} */ const response = await client.apiCall( config.inputs.method, @@ -62,8 +62,11 @@ export default class Client { ); config.core.setOutput("ok", response.ok); config.core.setOutput("response", JSON.stringify(response)); - if (response.channel) { - config.core.setOutput("channel_id", response.channel); + if (response.channel?.id ?? response.channel) { + config.core.setOutput( + "channel_id", + response.channel?.id ?? response.channel, + ); } if (response.message?.thread_ts) { config.core.setOutput("thread_ts", response.message.thread_ts); diff --git a/test/client.spec.js b/test/client.spec.js index bbddd79..90e744e 100644 --- a/test/client.spec.js +++ b/test/client.spec.js @@ -107,6 +107,44 @@ describe("client", () => { } }); + it("calls 'conversations.create' with the given token and content", async () => { + try { + const args = { + name: "pull-request-review-010101", + }; + const response = { + ok: true, + channel: { + id: "C0101010101", + name: "pull-request-review-010101", + is_channel: true, + created: 1730425428, + }, + }; + mocks.core.getInput.withArgs("method").returns("chat.postMessage"); + mocks.core.getInput.withArgs("token").returns("xoxb-example"); + mocks.core.getInput.withArgs("payload").returns(JSON.stringify(args)); + mocks.api.resolves(response); + await send(mocks.core); + assert.deepEqual(mocks.api.getCall(0).firstArg, "chat.postMessage"); + assert.deepEqual(mocks.api.getCall(0).lastArg, args); + assert.equal(mocks.core.setOutput.getCall(0).firstArg, "ok"); + assert.equal(mocks.core.setOutput.getCall(0).lastArg, true); + assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response"); + assert.equal( + mocks.core.setOutput.getCall(1).lastArg, + JSON.stringify(response), + ); + assert.equal(mocks.core.setOutput.getCall(2).firstArg, "channel_id"); + assert.equal(mocks.core.setOutput.getCall(2).lastArg, "C0101010101"); + assert.equal(mocks.core.setOutput.getCall(3).firstArg, "time"); + assert.equal(mocks.core.setOutput.getCalls().length, 4); + } catch (error) { + console.error(error); + assert.fail("Unexpected error when calling the method"); + } + }); + it("calls 'files.uploadV2' with the provided token and content", async () => { try { const args = {