Skip to content

Commit 00f9591

Browse files
authored
Remove # consistently from channel name in file upload (#1004)
1 parent e9a39ec commit 00f9591

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ public long getRetryInterval() {
7272
}
7373
}
7474

75-
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;
75+
public static String getChannelId(String botUserToken, String channel) throws ExecutionException, InterruptedException, AbortException {
76+
if (channel.matches("^(C[A-Z0-9]{8}|G[A-Z0-9]{10}||D[A-Z0-9]{8})$")) {
77+
return channel;
7878
}
79+
80+
String channelName = cleanChannelName(channel);
81+
7982
Map<String, String> channelNameToIdMap = CHANNEL_METADATA_CACHE.get(botUserToken);
8083
String channelId = channelNameToIdMap.get(channelName);
8184

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

101+
private static String cleanChannelName(String channelName) {
102+
String[] splitForThread = channelName.split(":", 2);
103+
String channel = channelName;
104+
if (splitForThread.length == 2) {
105+
channel = splitForThread[0];
106+
}
107+
if (channel.startsWith("#")) {
108+
return channel.substring(1);
109+
}
110+
return channel;
111+
}
112+
113+
98114
private static Map<String, String> convertChannelNameToId(CloseableHttpClient client, String token, Map<String, String> channels, String cursor) throws IOException {
99115
convertPublicChannelNameToId(client, token, channels, cursor);
100116
convertPrivateChannelNameToId(client, token, channels, cursor);

src/main/java/jenkins/plugins/slack/pipeline/SlackUploadFileStep.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,10 @@ protected Void run() throws IOException, InterruptedException, ExecutionExceptio
141141

142142
String channelId;
143143
try {
144-
String channelName = cleanChannelName(channel);
145-
channelId = SlackChannelIdCache.getChannelId(populatedToken, channelName);
144+
channelId = SlackChannelIdCache.getChannelId(populatedToken, channel);
146145
if (channelId == null) {
147146
// possibly a user ID which won't be found in the channel ID cache
148-
channelId = channelName;
147+
channelId = channel;
149148
}
150149
} catch (CompletionException | SlackChannelIdCache.HttpStatusCodeException e) {
151150
throw new AbortException("Failed uploading file to slack, channel not found: " + channel + ", error: " + e.getMessage());
@@ -182,17 +181,4 @@ private static String getThreadTs(String channelName) {
182181
}
183182
return null;
184183
}
185-
186-
private static String cleanChannelName(String channelName) {
187-
String[] splitForThread = channelName.split(":", 2);
188-
String channel = channelName;
189-
if (splitForThread.length == 2) {
190-
channel = splitForThread[0];
191-
if (channel.startsWith("#")) {
192-
return channel.substring(1);
193-
}
194-
return channel;
195-
}
196-
return channelName;
197-
}
198184
}

0 commit comments

Comments
 (0)