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

slackUserIdFromEmail() Doesn't Work #767

Open pporee opened 3 years ago

pporee commented 3 years ago

Version report

Jenkins 2.277.3 Slack Notification Plugin 2.48

Issue

Hi,

I got the same issue reported here https://github.com/jenkinsci/slack-plugin/issues/763, the func return null and no errors or debug informations are available.

Scopes

Here the scopes for my slack app. chat:write files:write users:read users:read.email

Token

token

Config as Code

slackNotifier:
    botUser: true
    room: "xxx-notifications"
    sendAs: "Jenkins"
    sendAsText: false
    slackUserIdResolver: "email"
    teamDomain: "xxx"
    tokenCredentialId: "slack-token"

thanks for your help. Below my code:

script {
  def lastCommiterEmail = sh(returnStdout: true, script: 'git log --format="%ae" | head -1').trim()
  def slackUserId = slackUserIdFromEmail("${lastCommiterEmail}")
  slackSend(botUser: true, tokenCredentialId: 'slack-token', color: "good", message: "<@${slackUserId} test message")
}

Result:

result

Reproduction steps

I tested directly with my slack email in the slackUserIdFromEmail function but got the same result.

timja commented 3 years ago

Please provide more information, i.e. scopes, what resolution strategy you have configured in your global config?

pporee commented 3 years ago

@timja Added scopes, creds and my config as code, if you need more tell me :). Regards

timja commented 3 years ago

the resolution strategy for slack IDs? it's in the advanced settings on the global configuration

pporee commented 3 years ago

As my app is part of my organisation i only found that parameter under my app scope. Is it what you need or do i need to ask to slack administrators ?

global
timja commented 3 years ago

No in your jenkins configuration, system configuration page

pporee commented 3 years ago

sorry i can't find more informations, i gave you all i got :(

Dorsosiv commented 3 years ago

Talked to Slack about the same problem. They have changed the way their REST API receives the OAuth token. Because of that the plugin does not work with getting the emails.

https://api.slack.com/changelog/2020-11-no-more-tokens-in-querystrings-for-newly-created-apps

I do not know to program in java but if someone that can , would change the way the OAuth token is sent to Slack then it would work @timja

timja commented 3 years ago

We use the header not the query string: https://github.com/jenkinsci/slack-plugin/blob/955756034a9addb3fbbed7b741c564a36b112a4b/src/main/java/jenkins/plugins/slack/user/EmailSlackUserIdResolver.java#L137

marco-luzzara commented 3 years ago

A couple of things you could try:

Finally, FYI, you can limit the number of git commits shown by git log like this:

git log -1 --format="%ae"

No need to bother the head command👍

4ekki commented 2 years ago

Faced the same issue today. Do you have any updates on it?

Pexers commented 1 year ago

+1 Experiencing the same issue.

My app has the required permissions and the Email User resolver option is enabled on Jenkins.

sarah-utilityapi commented 1 year ago

I found the same issue. My issue was that when I printed out the git committer it was a jenkins user. When I added

options {
  skipDefaultCheckout()
}
stages {
  stage('Checkout') {
    steps { checkout scm }
  }
}

then I had a real committer.

paoloyx commented 7 months ago

We ran into this same issue. We're not able to resolve Slack userIds from email.

I've tried with a simple snippet like this one

def sendSlackNotification() {
  // test with 'slackUserIdFromEmail'
  def lastCommitterEmail = sh(returnStdout: true, script: 'git log --format="%ae" | head -1').trim()
  echo "commit author ${lastCommitterEmail}"
  def slackUserId = slackUserIdFromEmail("${lastCommitterEmail}")
  slackSend(color: "good", message: "@${slackUserId} 'slackUserIdFromEmail' message", channel: channel)

  // test with 'slackUserIdsFromCommitters'
  def userIds = slackUserIdsFromCommitters()
  def userIdsString = userIds.collect { "<@$it>" }.join(' ')
  slackSend(color: colorCode, message: "${userIdsString} 'slackUserIdsFromCommitters' message", channel: channel)
}

but I get nothing, see image below. image

If I use the Slack userId directly it does work i.e.

def sendSlackNotification() {
  // test with userId
  def userId = "@{my-slack-user-id}"
  slackSend(color: colorCode, message: "${userId} 'userId' message", channel: channel)
}

then it does work. Any hint? In master node's /var/log/jenkins/jenkins.log file I can only see this warning, nothing more useful

2024-02-26 15:19:56.398+0000 [id=17689022]  WARNING o.j.r.u.AnonymousClassWarnings#warn: Attempt to (de-)serialize synthetic class jenkins.plugins.slack.user.EmailSlackUserIdResolver$$Lambda$621/540202168 in file:/var/lib/jenkins/plugins/slack/WEB-INF/lib/slack.jar; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
alant94 commented 5 months ago

We experience similar issue as posted above with the more recent versions as well:

Enabled additional logging as described here but in the resulting log there are no details about the api calls being made by slack plugin to slack and their results. It only posts log messages like the one below:

Apr 23, 2024 3:39:19 PM FINE jenkins.plugins.slack.StandardSlackService

Posting: to #channel_name on :  {"attachments":[{"fallback":"null, message text","color":"#FFFF00","fields":[{"short":false,"value":"null, message text"}],"mrkdwn_in":["pretext","text","fields"]}],"link_names":"1","unfurl_links":"true","unfurl_media":"true","channel":"#channel_name","as_user":"true"}

Apr 23, 2024 3:39:20 PM FINE jenkins.plugins.slack.StandardSlackService
Posting succeeded

Any guidance on how to troubleshoot it further?