Skip to content

Commit

Permalink
Remove # consistently from channel name in file upload (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Aug 29, 2024
1 parent e9a39ec commit 00f9591
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
22 changes: 19 additions & 3 deletions src/main/java/jenkins/plugins/slack/cache/SlackChannelIdCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ 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;
public static String getChannelId(String botUserToken, String channel) throws ExecutionException, InterruptedException, AbortException {
if (channel.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 channel;

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
}

String channelName = cleanChannelName(channel);

Map<String, String> channelNameToIdMap = CHANNEL_METADATA_CACHE.get(botUserToken);
String channelId = channelNameToIdMap.get(channelName);

Expand All @@ -95,6 +98,19 @@ public static String getChannelId(String botUserToken, String channelName) throw
return channelId;
}

private static String cleanChannelName(String channelName) {
String[] splitForThread = channelName.split(":", 2);
String channel = channelName;
if (splitForThread.length == 2) {

Check warning on line 104 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 104 is only partially covered, one branch is missing
channel = splitForThread[0];

Check warning on line 105 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 105 is not covered by tests
}
if (channel.startsWith("#")) {

Check warning on line 107 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 107 is only partially covered, one branch is missing
return channel.substring(1);
}
return channel;

Check warning on line 110 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 110 is not covered by tests
}


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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ protected Void run() throws IOException, InterruptedException, ExecutionExceptio

String channelId;
try {
String channelName = cleanChannelName(channel);
channelId = SlackChannelIdCache.getChannelId(populatedToken, channelName);
channelId = SlackChannelIdCache.getChannelId(populatedToken, channel);
if (channelId == null) {
// possibly a user ID which won't be found in the channel ID cache
channelId = channelName;
channelId = channel;
}
} catch (CompletionException | SlackChannelIdCache.HttpStatusCodeException e) {
throw new AbortException("Failed uploading file to slack, channel not found: " + channel + ", error: " + e.getMessage());
Expand Down Expand Up @@ -182,17 +181,4 @@ private static String getThreadTs(String channelName) {
}
return null;
}

private static String cleanChannelName(String channelName) {
String[] splitForThread = channelName.split(":", 2);
String channel = channelName;
if (splitForThread.length == 2) {
channel = splitForThread[0];
if (channel.startsWith("#")) {
return channel.substring(1);
}
return channel;
}
return channelName;
}
}

0 comments on commit 00f9591

Please sign in to comment.