jenkinsci / slack-plugin

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

slackSend returns null. #633

Closed AnotherStream closed 4 years ago

AnotherStream commented 4 years ago

Your checklist for this issue

Description

I want to make a thread posting to Slak using threadId with slackSend step.

I referred to the following site and other issues related to threading.

https://edekler.nl/api-mngmt/enabling-threaded-slack-messages-while-using-the-jenkins-slack-plugin/

Would you give me some advice?

I created a Slack Custom App on Slack and generated a Slack App Bot User OAuth Access Token. I registered this token in Jenkins Credentials in the Secret Text format with the ID name SLACK_APP_BOT_USER_OAUTH_ACCESS_TOKEN.

And I called it with slackSend as follows.The return value SlackResponse object returns null. Is there anything wrong with it?

Code

def slackThreadId = null
def message = 'My Message'
def teamDomain = 'myTeamDomain'
def channel = slackTrheadId ? slackTrheadId : '#my_channel'

def response = slackSend(
    tokenCredentialId: 'SLACK_APP_BOT_USER_OAUTH_ACCESS_TOKEN',
    channel: channel,
    teamDomain: teamDomain,
    message: "${message} Thread Root",
    botUser: true,
)
assert (response != null)

println "response.threadId: ${response.threadId}"
if (response.threadId) {
    slackTrheadId = response.threadId
}

channel = slackTrheadId ? slackTrheadId : '#my_channel'
response = slackSend(
    tokenCredentialId: 'SLACK_APP_BOT_USER_OAUTH_ACCESS_TOKEN',
    channel: channel,
    teamDomain: teamDomain,
    message: "${message} in Thread",
    botUser: true,
)
assert (response != null)

println "response.threadId: ${response.threadId}"
if (response.threadId) {
    slackTrheadId = response.threadId
}

The following error is output to the Jenkins console.

Console Output

[Pipeline] slackSend
Slack Send Pipeline step running, values are - baseUrl: https://myTeamDomain.slack.com/services/hooks/jenkins-ci/, teamDomain: myTeamDomain, channel: #my_channel, color: , botUser: true, tokenCredentialId: SLACK_APP_BOT_USER_OAUTH_ACCESS_TOKEN, notifyCommitters: false, iconEmoji: , username: 
ERROR: Slack notification failed. See Jenkins logs for details.

The system log shows the following output.

Jenkins System Log

Processing build test » test_slack_notification » develop #1
10 21, 2019 3:18:45 PM INFO org.jenkinsci.plugins.githubautostatus.GithubBuildStatusGraphListener log
Processing build test » test_slack_notification » develop #1
10 21, 2019 3:18:45 PM WARN jenkins.plugins.slack.StandardSlackService correctMisconfigurationOfBaseUrl
Overriding base url to team domain 'myTeamDomain' this is due to mis-configuration, you don't need to set base url unless you're using a slack compatible app like mattermost
10 21, 2019 3:18:45 PM WARN jenkins.plugins.slack.StandardSlackService publish
Slack post may have failed. Response: null
10 21, 2019 3:18:45 PM WARN jenkins.plugins.slack.StandardSlackService publish
Response Code: 404
10 21, 2019 3:18:46 PM INFO org.jenkinsci.plugins.workflow.job.WorkflowRun finish
test/test1/test_slack_notification/develop #1 completed: FAILURE
10 21, 2019 3:18:46 PM INFO org.jenkinsci.plugins.githubautostatus.BuildStatusJobListener log
Build Completed
timja commented 4 years ago

Try remove your baseUrl entry

AnotherStream commented 4 years ago

I confirmed in other issues that baseUrl and teamDomain should not be set at the same time. Therefore, baseUrl is not specified in the slackSend parameter as above. I haven't set it up, but the Jenkins console output seems to be running with baseUrl set.

I tried the following, but baseUrl was automatically interpolated.

def response = slackSend(
    tokenCredentialId: 'SLACK_APP_BOT_USER_OAUTH_ACCESS_TOKEN',
    channel: channel,
    baseUrl: null,
    teamDomain: teamDomain,
    message: "${message} Thread Root",
    botUser: true,
)

I don't explicitly specify it as a parameter. How can I remove baseUrl?

timja commented 4 years ago

Are you sure it's not set globally? It won't be set by default

AnotherStream commented 4 years ago

I'm sorry. Confirmation leaked because it was hidden below the advanced settings.

BaseUrl was set in Jenkins settings.

By removing this, it was confirmed that SlackResponse was returned and that the threadId was set. Thank you very much.