Skip to content

Commit d07f1ea

Browse files
authored
Modify getChannelId and convertChannelNameToId for efficient channel ID retrieval (#977)
1 parent a2edd2b commit d07f1ea

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public long getRetryInterval() {
7373
}
7474

7575
public static String getChannelId(String botUserToken, String channelName) throws ExecutionException, InterruptedException, AbortException {
76+
if (channelName.matches("^(C[A-Z0-9]{8}|G[A-Z0-9]{10}||D[A-Z0-9]{8})$")) {
77+
return channelName;
78+
}
7679
Map<String, String> channelNameToIdMap = CHANNEL_METADATA_CACHE.get(botUserToken);
7780
String channelId = channelNameToIdMap.get(channelName);
7881

@@ -93,10 +96,53 @@ public static String getChannelId(String botUserToken, String channelName) throw
9396
}
9497

9598
private static Map<String, String> convertChannelNameToId(CloseableHttpClient client, String token, Map<String, String> channels, String cursor) throws IOException {
99+
convertPublicChannelNameToId(client, token, channels, cursor);
100+
convertPrivateChannelNameToId(client, token, channels, cursor);
101+
return channels;
102+
}
103+
104+
private static Map<String, String> convertPublicChannelNameToId(CloseableHttpClient client, String token, Map<String, String> channels, String cursor) throws IOException {
105+
RequestBuilder requestBuilder = RequestBuilder.get("https://slack.com/api/conversations.list")
106+
.addHeader("Authorization", "Bearer " + token)
107+
.addParameter("exclude_archived", "true")
108+
.addParameter("types", "public_channel")
109+
.addParameter("limit", "999");
110+
111+
if (cursor != null) {
112+
requestBuilder.addParameter("cursor", cursor);
113+
}
114+
ResponseHandler<JSONObject> standardResponseHandler = getStandardResponseHandler();
115+
JSONObject result = client.execute(requestBuilder.build(), standardResponseHandler);
116+
117+
if (!result.getBoolean("ok")) {
118+
logger.warning("Couldn't convert channel name to ID in Slack: " + result);
119+
return channels;
120+
}
121+
122+
JSONArray channelsArray = result.getJSONArray("channels");
123+
for (int i = 0; i < channelsArray.length(); i++) {
124+
JSONObject channel = channelsArray.getJSONObject(i);
125+
126+
String channelName = channel.getString("name");
127+
String channelId = channel.getString("id");
128+
129+
channels.put(channelName, channelId);
130+
}
131+
132+
cursor = result.getJSONObject("response_metadata").getString("next_cursor");
133+
if (cursor != null && !cursor.isEmpty()) {
134+
return convertPublicChannelNameToId(client, token, channels, cursor);
135+
}
136+
137+
return channels;
138+
}
139+
140+
private static Map<String, String> convertPrivateChannelNameToId(CloseableHttpClient client, String token, Map<String, String> channels, String cursor) throws IOException {
96141
RequestBuilder requestBuilder = RequestBuilder.get("https://slack.com/api/conversations.list")
97142
.addHeader("Authorization", "Bearer " + token)
98143
.addParameter("exclude_archived", "true")
99-
.addParameter("types", "public_channel,private_channel");
144+
.addParameter("types", "private_channel")
145+
.addParameter("limit", "999");
100146

101147
if (cursor != null) {
102148
requestBuilder.addParameter("cursor", cursor);
@@ -121,7 +167,7 @@ private static Map<String, String> convertChannelNameToId(CloseableHttpClient cl
121167

122168
cursor = result.getJSONObject("response_metadata").getString("next_cursor");
123169
if (cursor != null && !cursor.isEmpty()) {
124-
return convertChannelNameToId(client, token, channels, cursor);
170+
return convertPrivateChannelNameToId(client, token, channels, cursor);
125171
}
126172

127173
return channels;

0 commit comments

Comments
 (0)