From d07f1ea7ff4009eba4b6dc9ca083feb846beaa8a Mon Sep 17 00:00:00 2001 From: Koji Nakamaru Date: Wed, 5 Jun 2024 05:00:11 +0900 Subject: [PATCH] Modify `getChannelId` and `convertChannelNameToId` for efficient channel ID retrieval (#977) --- .../slack/cache/SlackChannelIdCache.java | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java b/src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java index a1606a61..d473d861 100644 --- a/src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java +++ b/src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java @@ -73,6 +73,9 @@ public long getRetryInterval() { } public static String getChannelId(String botUserToken, String channelName) throws ExecutionException, InterruptedException, AbortException { + if (channelName.matches("^(C[A-Z0-9]{8}|G[A-Z0-9]{10}||D[A-Z0-9]{8})$")) { + return channelName; + } Map channelNameToIdMap = CHANNEL_METADATA_CACHE.get(botUserToken); String channelId = channelNameToIdMap.get(channelName); @@ -93,10 +96,53 @@ public static String getChannelId(String botUserToken, String channelName) throw } private static Map convertChannelNameToId(CloseableHttpClient client, String token, Map channels, String cursor) throws IOException { + convertPublicChannelNameToId(client, token, channels, cursor); + convertPrivateChannelNameToId(client, token, channels, cursor); + return channels; + } + + private static Map convertPublicChannelNameToId(CloseableHttpClient client, String token, Map channels, String cursor) throws IOException { + RequestBuilder requestBuilder = RequestBuilder.get("https://slack.com/api/conversations.list") + .addHeader("Authorization", "Bearer " + token) + .addParameter("exclude_archived", "true") + .addParameter("types", "public_channel") + .addParameter("limit", "999"); + + if (cursor != null) { + requestBuilder.addParameter("cursor", cursor); + } + ResponseHandler standardResponseHandler = getStandardResponseHandler(); + JSONObject result = client.execute(requestBuilder.build(), standardResponseHandler); + + if (!result.getBoolean("ok")) { + logger.warning("Couldn't convert channel name to ID in Slack: " + result); + return channels; + } + + JSONArray channelsArray = result.getJSONArray("channels"); + for (int i = 0; i < channelsArray.length(); i++) { + JSONObject channel = channelsArray.getJSONObject(i); + + String channelName = channel.getString("name"); + String channelId = channel.getString("id"); + + channels.put(channelName, channelId); + } + + cursor = result.getJSONObject("response_metadata").getString("next_cursor"); + if (cursor != null && !cursor.isEmpty()) { + return convertPublicChannelNameToId(client, token, channels, cursor); + } + + return channels; + } + + private static Map convertPrivateChannelNameToId(CloseableHttpClient client, String token, Map channels, String cursor) throws IOException { RequestBuilder requestBuilder = RequestBuilder.get("https://slack.com/api/conversations.list") .addHeader("Authorization", "Bearer " + token) .addParameter("exclude_archived", "true") - .addParameter("types", "public_channel,private_channel"); + .addParameter("types", "private_channel") + .addParameter("limit", "999"); if (cursor != null) { requestBuilder.addParameter("cursor", cursor); @@ -121,7 +167,7 @@ private static Map convertChannelNameToId(CloseableHttpClient cl cursor = result.getJSONObject("response_metadata").getString("next_cursor"); if (cursor != null && !cursor.isEmpty()) { - return convertChannelNameToId(client, token, channels, cursor); + return convertPrivateChannelNameToId(client, token, channels, cursor); } return channels;