Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove # consistently from channel name in file upload #1004

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@
}
}

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 @@
return channelId;
}

private static String cleanChannelName(String channelName) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to the cache so both freestyle and pipeline share the same common code

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("#")) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously was only removed if a thread_ts was attached which was probably more common in pipeline but not always there.

return channel.substring(1);
}
return channel;
}
return channelName;
}
}