[Email-ext] Resolving email address from Cause.UserIdCause

471 views
Skip to first unread message

Stuart Rowe

unread,
Mar 11, 2014, 2:24:37 PM3/11/14
to jenkins...@googlegroups.com
Hi, does anyone know of a way to look up a user's email address from their user ID? I can't make the assumption that the email is "use...@company.com" because I know this isn't always the case.

We're using the Active Directory plugin for security which is where the email addresses for users are coming from. I need to manually add the requester to the recipient list because this doesn't seem to be propagated by BuildFlow FlowCauses. The relevant parts of build pipeline in this case is:

BuildFlow project A (scheduled by the logged in user) --> Build Flow project B --> Free Style Project with an Editable Email Notification post build step.

Thanks in advance, 

Stuart

Slide

unread,
Mar 11, 2014, 2:33:47 PM3/11/14
to Jenkins User Mailing List
You could use the mailmap-resolver-plugin, but I may be missing something because there seems to be a discrepancy with what you are saying. First you need to look up the user's email address from their user ID, but then you say you are getting email addresses from Active Directory? Can you clarify.

Thanks,

slide


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Website: http://earl-of-code.com

Stuart Rowe

unread,
Mar 11, 2014, 2:56:39 PM3/11/14
to jenkins...@googlegroups.com
Sorry for the confusion:

If a user schedules a single project that has an email post build step than the email address is resolved automatically by the Active Directory plugin.

I'm having problems with a more complicated build pipeline where the requesting user isn't propagated to a down stream build that has an email step. I'm attempting to write a groovy script that will set the email recipients to the requester of the top level build. I've managed to look up the user ID from the UserIdCause of the top level build, but I haven't found a way to resolve an email address from that user id.

I hope that's more clear now :).

Slide

unread,
Mar 11, 2014, 3:29:10 PM3/11/14
to Jenkins User Mailing List

How are you planning, in your groovy script to add the users as recipients? Just a curiosity. You could call the MailAddressResolver resolve method yourself from the groovy script in each recipient, but you may not need to do that depending on how you are adding the recipients.

Stuart Rowe

unread,
Mar 11, 2014, 3:44:16 PM3/11/14
to jenkins...@googlegroups.com
I just found the MailAddressResolver class on my own and it's solved the problem.

Currently I'm adding recipients by referencing a groovy script directly in the project recipients edit box (e.g. ${SCRIPT, script="myscript.groovy"} ). The script recursively looks up the requesting userId from the top level build and that is used to get a Jenkins User which is passed to MailAddressResolver.resolve(). The script returns the address returned by resolve(). 

Is this the best approach? It'd be nice to keep the default recipient and append the resolved recipient - can I just put $DEFAULT_RECIPIENT,${SCRIPT, script="myscript.groovy"} in the edit box? 

Thanks for your help!

Slide

unread,
Mar 11, 2014, 4:15:42 PM3/11/14
to Jenkins User Mailing List
You don't need to resolve the addresses yourself if you are using a SCRIPT token, the email-ext plugin will resolve them after it replaces the tokens. Yes, you can have the $DEFAULT_RECIPIENTS and ${SCRIPT...} tokens in the edit box and it will work just fine.

Stuart Rowe

unread,
Mar 11, 2014, 4:31:04 PM3/11/14
to jenkins...@googlegroups.com
Perfect, thanks again.

Nick Dierauf

unread,
Oct 28, 2014, 9:17:01 PM10/28/14
to jenkins...@googlegroups.com
Stuart, can you post the groovy script that you use to determine the email address (ie, "myscript.groovy")?
Thanks!
Nick.

Stuart Rowe

unread,
Oct 29, 2014, 3:43:56 PM10/29/14
to jenkins...@googlegroups.com
Here you go :)


import javax.mail.Message
import hudson.model.*
import com.cloudbees.plugins.flow.*
import hudson.tasks.MailAddressResolver

def getUpstreamBuild(AbstractBuild curBuild)
{
    upStreamBuild = null
    if(curBuild != null)
    {
        // find a cause that will lead to an upstream build
        for( cause in curBuild.causes )
        {
            if(cause instanceof Cause.UpstreamCause)
            {
                upStreamBuild = Hudson.instance.getItem(cause.upstreamProject).getBuildByNumber(cause.upstreamBuild)
                break
            }
            else if(cause instanceof FlowCause)
            {
                upStreamBuild = cause.getFlowRun()
                break
            }
        }
    }
    return upStreamBuild
}

def getUserIdCause(AbstractBuild curBuild)
{
    def userIdCause = null
    if (curBuild != null)
    {
        for( cause in curBuild.causes )
        {
            if(cause instanceof Cause.UserIdCause)
            {
                userIdCause = cause
                break
            }
        }
    }

    return userIdCause
}

def getEmailFromUserId(userId)
{
    email = null
    user = User.get(userId, false, [:])
    if(user)
    {
        email = MailAddressResolver.resolve(user)
    }
    return email
}

def getRootRequesterUserEmail()
{
    try
    {
        // try to find a user ID cause for the current build
        def userIdCause = getUserIdCause(build)
        if(userIdCause != null)
        {
            return getEmailFromUserId(userIdCause.getUserId())
        }

        def rootBuild = null
        def curUpstreamBuild = getUpstreamBuild(build)

        // find the top level build
        while(curUpstreamBuild)
        {
            rootBuild = curUpstreamBuild
            curUpstreamBuild = getUpstreamBuild(curUpstreamBuild)
        }

        // try to find a user ID cause from the top level build
        userIdCause = getUserIdCause(rootBuild)
        if(userIdCause != null)
        {
            return getEmailFromUserId(userIdCause.getUserId())
        }
    }
    catch (e)
    {
        println e
    }
    return  null
}

// update the recipients with the requester from the top level build
def userEmail = getRootRequesterUserEmail()
return userEmail


--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/nQtro7RisO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.

Nick Dierauf

unread,
Nov 1, 2014, 3:21:02 AM11/1/14
to jenkins...@googlegroups.com
Thanks Stuart!
Nick.

Stuart Rowe

unread,
Nov 3, 2014, 11:00:23 PM11/3/14
to jenkins...@googlegroups.com
BTW, it looks like this PR: https://github.com/jenkinsci/build-flow-plugin/pull/53 will fix this properly so no more hacky scripts will be needed
Reply all
Reply to author
Forward
0 new messages