Skip to content

Commit

Permalink
Modify getChannelId and convertChannelNameToId for efficient chan…
Browse files Browse the repository at this point in the history
…nel ID retrieval (#977)
  • Loading branch information
KojiNakamaru authored Jun 4, 2024
1 parent a2edd2b commit d07f1ea
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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})$")) {

Check warning on line 76 in src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 76 is only partially covered, one branch is missing
return channelName;

Check warning on line 77 in src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 77 is not covered by tests
}
Map<String, String> channelNameToIdMap = CHANNEL_METADATA_CACHE.get(botUserToken);
String channelId = channelNameToIdMap.get(channelName);

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

private static Map<String, String> convertChannelNameToId(CloseableHttpClient client, String token, Map<String, String> channels, String cursor) throws IOException {
convertPublicChannelNameToId(client, token, channels, cursor);
convertPrivateChannelNameToId(client, token, channels, cursor);
return channels;
}

private static Map<String, String> convertPublicChannelNameToId(CloseableHttpClient client, String token, Map<String, String> 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<JSONObject> 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;

Check warning on line 137 in src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 134-137 are not covered by tests
}

private static Map<String, String> convertPrivateChannelNameToId(CloseableHttpClient client, String token, Map<String, String> 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) {

Check warning on line 147 in src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 147 is only partially covered, one branch is missing
requestBuilder.addParameter("cursor", cursor);

Check warning on line 148 in src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 148 is not covered by tests
Expand All @@ -121,7 +167,7 @@ private static Map<String, String> 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);

Check warning on line 170 in src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 158-170 are not covered by tests
}

return channels;
Expand Down

0 comments on commit d07f1ea

Please sign in to comment.