From 33354b6353d44ae731ab56030716cad5978c6ea4 Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Thu, 9 May 2024 08:40:32 +0100 Subject: [PATCH] Allow user file upload to work --- README.md | 12 ++++++++++++ .../plugins/slack/pipeline/SlackUploadFileStep.java | 12 ++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5ca8ba17..d7a49df6 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,18 @@ node { This feature requires [botUser](#bot-user-mode) mode. +##### File upload to a user channel + +You can upload files to a user channel by messaging the user first and then using the channel ID from the message response: + +```groovy +node { + sh "echo hey > blah.txt" + def slackResponse = slackSend channel: '$userId', message: 'Hey', sendAsText: true + slackUploadFile filePath: "*.txt", channel: slackResponse.channelId +} +``` + #### Threads Support You can send a message and create a thread on that message using the pipeline step. diff --git a/src/main/java/jenkins/plugins/slack/pipeline/SlackUploadFileStep.java b/src/main/java/jenkins/plugins/slack/pipeline/SlackUploadFileStep.java index 7a267e7f..60fa756d 100644 --- a/src/main/java/jenkins/plugins/slack/pipeline/SlackUploadFileStep.java +++ b/src/main/java/jenkins/plugins/slack/pipeline/SlackUploadFileStep.java @@ -141,15 +141,11 @@ protected Void run() throws IOException, InterruptedException, ExecutionExceptio String channelId; try { - channelId = SlackChannelIdCache.getChannelId(populatedToken, cleanChannelName(channel)); + String channelName = cleanChannelName(channel); + channelId = SlackChannelIdCache.getChannelId(populatedToken, channelName); if (channelId == null) { - String message = "Failed uploading file to slack, channel not found: " + channel; - if (step.failOnError) { - throw new AbortException(message); - } else { - listener.error(message); - return null; - } + // possibly a user ID which won't be found in the channel ID cache + channelId = channelName; } } catch (CompletionException | SlackChannelIdCache.HttpStatusCodeException e) { throw new AbortException("Failed uploading file to slack, channel not found: " + channel + ", error: " + e.getMessage());