Skip to content

Commit 8876593

Browse files
authored
Fix missing channels in the configuration of the Slack integration (#907)
* Make separate slack requests for channels' filtering * changeset * Make requests concurrent
1 parent a295590 commit 8876593

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

.changeset/fine-heads-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-slack': minor
3+
---
4+
5+
Try to fix missing channels in Slack's configuration

bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@
357357
},
358358
"integrations/mermaid": {
359359
"name": "@gitbook/integration-mermaid",
360-
"version": "0.3.4",
360+
"version": "0.4.0",
361361
"dependencies": {
362362
"@gitbook/api": "*",
363363
"@gitbook/runtime": "*",
@@ -419,7 +419,7 @@
419419
},
420420
"integrations/posthog": {
421421
"name": "@gitbook/integration-posthog",
422-
"version": "0.4.2",
422+
"version": "1.0.0",
423423
"dependencies": {
424424
"@gitbook/api": "*",
425425
"@gitbook/runtime": "*",

integrations/slack/src/slack.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ type SlackChannel = {
1515
};
1616

1717
/**
18-
* Cloudflare workers have a maximum number of subrequests we can call (20 according to my
19-
* tests) https://developers.cloudflare.com/workers/platform/limits/#how-many-subrequests-can-i-make
20-
* TODO: Test with 50
18+
* Cloudflare workers have a maximum number of subrequests we can call (50 on a free account)
19+
* https://developers.cloudflare.com/workers/platform/limits/#how-many-subrequests-can-i-make
2120
*/
22-
const maximumSubrequests = 20;
21+
const maximumSubrequests = 50;
2322

2423
/**
25-
* Executes a Slack API request to fetch channels, handles pagination, then returns the merged
26-
* results.
24+
* Helper function to fetch channels by type with pagination
2725
*/
28-
export async function getChannelsPaginated(context: SlackRuntimeContext) {
26+
async function fetchChannelsByType(context: SlackRuntimeContext, types: string) {
2927
const channels: SlackChannel[] = [];
3028

3129
let response = await slackAPI<{
@@ -39,7 +37,7 @@ export async function getChannelsPaginated(context: SlackRuntimeContext) {
3937
payload: {
4038
limit: 1000,
4139
exclude_archived: true,
42-
types: 'public_channel,private_channel',
40+
types,
4341
},
4442
});
4543
channels.push(...response?.channels);
@@ -57,16 +55,34 @@ export async function getChannelsPaginated(context: SlackRuntimeContext) {
5755
payload: {
5856
limit: 1000,
5957
exclude_archived: true,
60-
types: 'public_channel,private_channel',
58+
types,
6159
cursor: response.response_metadata.next_cursor,
6260
},
6361
});
6462
channels.push(...response?.channels);
6563
numberOfCalls++;
6664
}
6765

66+
return channels;
67+
}
68+
69+
/**
70+
* Executes a Slack API request to fetch channels, handles pagination, then returns the merged
71+
* results.
72+
*/
73+
export async function getChannelsPaginated(context: SlackRuntimeContext) {
74+
// Make separate calls for public and private channels due to inconsistent Slack API filtering
75+
// when requesting multiple types together - some channels are missing from the response
76+
const [publicChannels, privateChannels] = await Promise.all([
77+
fetchChannelsByType(context, 'public_channel'),
78+
fetchChannelsByType(context, 'private_channel'),
79+
]);
80+
81+
// Combine results
82+
const allChannels = [...publicChannels, ...privateChannels];
83+
6884
// Remove any duplicate as the pagination API could return duplicated channels
69-
return channels.filter(
85+
return allChannels.filter(
7086
(value, index, self) => index === self.findIndex((t) => t.id === value.id),
7187
);
7288
}

0 commit comments

Comments
 (0)