jenkinsci / slack-plugin

A Jenkins plugin for posting notifications to a Slack channel
https://plugins.jenkins.io/slack/
MIT License
670 stars 413 forks source link

How to use both a bot user and JenkinsCI bot? #584

Closed IdanAdar closed 5 years ago

IdanAdar commented 5 years ago

Your checklist for this issue

Description

Until now, in order to post messages from Jenkins to Slack, I used the Jenkins CI App provided by Slack. I generated a token, saved this token as a Jenkins credential and used it in the Global Slack Notifier Settings. I then use the slackSend step to write messages in various channels.

Now I want to also create a thread for a message in whichever channel and write into it. In order to do this using slackSend, I need to create a bot and use its oauth token.

However it seems I can only use one of these at a time? The settings only allow for 1 credential to be set and used.

  1. Is this at all possible? If not, what am I to do?
  2. If I do switch to the credentials of the bot user and set bot user as true, no messages are posted at all...

e.g.

def slackResponse = slackSend(channel: "myChannel", color: '#199515',message: "*$JOB_NAME*, <$BUILD_URL|Build #$BUILD_NUMBER>: my message")

slackSend(channel: "${slackResponse.threadId}", color: '#199515',message: "*$JOB_NAME*, <$BUILD_URL|Build #$BUILD_NUMBER>: my message 1")

When using the Jenkins CI App the message is created but the thread does not. When using the bot use the message is not created at all.

timja commented 5 years ago

If you pass all of the variable in pipeline it will work i.e.

def slackResponse = slackSend(channel: "myChannel", color: '#199515',message: "*$JOB_NAME*, <$BUILD_URL|Build #$BUILD_NUMBER>: my message",botUser: true, tokenCredentialId: myBotUserToken)
IdanAdar commented 5 years ago

Thanks.

  1. I've reverted the Global Slack Notifier Settings back to using the Jenkins CI App credential.
  2. In the Jenkinsfile, I changed to this:

    def slackResponse = slackSend(channel: "myChannel", color: '#199515',message: "*$JOB_NAME*, <$BUILD_URL|Build #$BUILD_NUMBER>: my message.")
    slackSend(channel: "${slackResponse.threadId}", color: '#199515',message: "*$JOB_NAME*, <$BUILD_URL|Build #$BUILD_NUMBER>: my message 1.", botUser: true, tokenCredentialId: "slackbot-token-for-writing-into-slack-threads")

The message is written, but the thread is not created still.

timja commented 5 years ago

you need to do it on both slackSends, looks like you only did it on one

IdanAdar commented 5 years ago

if I add those two to the original slackSend then it is again not sent to Slack. It's basically the same same issue as in (2).

timja commented 5 years ago

is the bot invited to your channel?

IdanAdar commented 5 years ago

Yes.

timja commented 5 years ago

Add a log recorder for this class: https://github.com/jenkinsci/slack-plugin/blob/master/src/main/java/jenkins/plugins/slack/StandardSlackService.java#L194 Needs fine level logging, see if that helps

IdanAdar commented 5 years ago

Sorry, didn't understand what is the exact value I should put in Logger:

Screen Shot 2019-06-17 at 15 57 58
timja commented 5 years ago

jenkins.plugins.slack.StandardSlackService

IdanAdar commented 5 years ago

The log (some wording redacted):

Jun 17, 2019 4:58:23 PM FINE jenkins.plugins.slack.StandardSlackService
Posting: to myChannel on **** using https://slack.com/api/chat.postMessage?token=****&channel=myChannel&link_names=1&as_user=true&attachments=%5B%7B%22fallback%22%3A%22*Common-Updater-Repositories*%2C+%3Chttps%3A%2F%2F****%2Fjob%2FCommon-Updater-Repositories%2F96%2F%7CBuild+%2396%3E%3A+Starting+to+update+dependnecies.%22%2C%22color%22%3A%22%23199515%22%2C%22fields%22%3A%5B%7B%22short%22%3Afalse%2C%22value%22%3A%22*Common-Updater-Repositories*%2C+%3Chttps%3A%2F%2F****%2Fjob%2FCommon-Updater-Repositories%2F96%2F%7CBuild+%2396%3E%3A+Starting+to+update+dependnecies.%22%7D%5D%2C%22mrkdwn_in%22%3A%5B%22pretext%22%2C%22text%22%2C%22fields%22%5D%7D%5D: [{"fallback":"*Common-Updater-Repositories*, <https://****/job/Common-Updater-Repositories/96/|Build #96>: Starting to update dependnecies.","color":"#199515","fields":[{"short":false,"value":"*Common-Updater-Repositories*, <https://****/job/Common-Updater-Repositories/96/|Build #96>: Starting to update dependnecies."}],"mrkdwn_in":["pretext","text","fields"]}] #199515
Jun 17, 2019 4:58:23 PM FINE jenkins.plugins.slack.StandardSlackService
Posting succeeded

It says success, but nothing is actually posted.

timja commented 5 years ago

There's a fix on master which should give the actual error. I'll release it now

timja commented 5 years ago

Can you try with version 2.25 please

IdanAdar commented 5 years ago

The build log shows:

Slack Send Pipeline step running, values are - baseUrl: <empty>, teamDomain: ****, channel: ****, color: #199515, botUser: true, tokenCredentialId: slackbot-token-for-writing-into-slack-threads, iconEmoji <empty>, username <empty>
ERROR: Slack notification failed with exception: {"ok":false,"error":"not_in_channel","warning":"missing_charset","response_metadata":{"warnings":["missing_charset"]}}
ERROR: Slack notification failed. See Jenkins logs for details.

And:

Java.lang.NullPointerException: Cannot get property 'threadId' on null object

The log shows:

Jun 17, 2019 6:13:38 PM WARNING jenkins.plugins.slack.StandardSlackService publish
Response Code: 200

But the channel does have a notification that the bot user was added.

added an integration to this channel: certsbot
timja commented 5 years ago

that's different, you need to invite the bot user to the channel

/invite @jenkins-bot #channel-name
IdanAdar commented 5 years ago

Okay, that worked and now the first message is posted by the app, but messages into the thread fail:

Build log:

Slack Send Pipeline step running, values are - baseUrl: <empty>, teamDomain: ***, channel: C4GEHN657:1560785714.005200, color: #199515, botUser: true, tokenCredentialId: slackbot-token-for-writing-into-slack-threads, iconEmoji <empty>, username <empty>
ERROR: Slack notification failed with exception: {"ok":false,"error":"invalid_thread_ts","warning":"missing_charset","response_metadata":{"messages":["[ERROR] 0 is not a valid thread_ts"],"warnings":["missing_charset"]}}
ERROR: Slack notification failed. See Jenkins logs for details.

System log:

Jun 17, 2019 6:35:37 PM FINE jenkins.plugins.slack.StandardSlackService
Posting: to C4GEHN657 on ibm-cloudplatform using https://slack.com/api/chat.postMessage: [{"fallback":"*Common-Updater-Repositories*, <https://***/job/Common-Updater-Repositories/99/|Build #99>: certificate-manager-service-api - Common code dependencies match the latest package versions. Nothing to update.","color":"#199515","fields":[{"short":false,"value":"*Common-Updater-Repositories*, <https://***/job/Common-Updater-Repositories/99/|Build #99>: certificate-manager-service-api - Common code dependencies match the latest package versions. Nothing to update."}],"mrkdwn_in":["pretext","text","fields"]}] #199515
Jun 17, 2019 6:35:37 PM WARNING jenkins.plugins.slack.StandardSlackService publish
Slack post may have failed. Response: {"ok":false,"error":"invalid_thread_ts","warning":"missing_charset","response_metadata":{"messages":["[ERROR] 0 is not a valid thread_ts"],"warnings":["missing_charset"]}}
Jun 17, 2019 6:35:37 PM WARNING jenkins.plugins.slack.StandardSlackService publish
Response Code: 200
timja commented 5 years ago

can you echo the thread id? it seems to think its sending 0 which isn't right

IdanAdar commented 5 years ago

It seems to print a full value: *** thread id is: C4GEHN657:1560786919.005300

But fails with:

Slack Send Pipeline step running, values are - baseUrl: <empty>, teamDomain: ***, channel: C4GEHN657:1560786919.005300, color: #199515, botUser: true, tokenCredentialId: slackbot-token-for-writing-into-slack-threads, iconEmoji <empty>, username <empty>
ERROR: Slack notification failed with exception: {"ok":false,"error":"invalid_thread_ts","warning":"missing_charset","response_metadata":{"messages":["[ERROR] 0 is not a valid thread_ts"],"warnings":["missing_charset"]}}
ERROR: Slack notification failed. See Jenkins logs for details.

System log:

Jun 17, 2019 6:55:40 PM FINE jenkins.plugins.slack.StandardSlackService
Posting: to C4GEHN657 on ibm-cloudplatform using https://slack.com/api/chat.postMessage: [{"fallback":"*Common-Updater-Repositories*, <https://wcp-certificate-manager-jenkins.swg-devops.com/job/Common-Updater-Repositories/100/|Build #100>: certificate-manager-service-api - Common code dependencies match the latest package versions. Nothing to update.","color":"#199515","fields":[{"short":false,"value":"*Common-Updater-Repositories*, <https://wcp-certificate-manager-jenkins.swg-devops.com/job/Common-Updater-Repositories/100/|Build #100>: certificate-manager-service-api - Common code dependencies match the latest package versions. Nothing to update."}],"mrkdwn_in":["pretext","text","fields"]}] #199515
Jun 17, 2019 6:55:40 PM WARNING jenkins.plugins.slack.StandardSlackService publish
Slack post may have failed. Response: {"ok":false,"error":"invalid_thread_ts","warning":"missing_charset","response_metadata":{"messages":["[ERROR] 0 is not a valid thread_ts"],"warnings":["missing_charset"]}}
Jun 17, 2019 6:55:40 PM WARNING jenkins.plugins.slack.StandardSlackService publish
Response Code: 200
timja commented 5 years ago

I've reproduced that, and tested on 2.24, it works on the old version. Downgrade and I'll try get a fix out tonight / tomorrow

IdanAdar commented 5 years ago

Many thanks @timja. Looking forward to 2.26.

timja commented 5 years ago

It has been released now

IdanAdar commented 5 years ago

Awesome. Works. :)