Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get e-mail address of current Jenkins user to use in groovy script

I've created a groovy script for the new Jenkins Workflow Plugin, https://github.com/jenkinsci/workflow-plugin. I want it to send a mail to the user who started the job when it needs input for the next step. I've tried to search the API but I can't find anything about getting the users email address.

I would think of something like this.

import hudson.model.User
def user = User.current()
def mailAddress = user.getMailAddress()

Is there a way to get the current Jenkins user' address in groovy?

like image 229
mipmip Avatar asked Jan 20 '15 13:01

mipmip


2 Answers

I found a way:

import hudson.model.AbstractProject
import hudson.tasks.Mailer
import hudson.model.User

def item = hudson.model.Hudson.instance.getItem(env.JOB_NAME) 
def build = item.getLastBuild()
def cause = build.getCause(hudson.model.Cause.UserIdCause.class)
def id = cause.getUserId()
User u = User.get(id)
def umail = u.getProperty(Mailer.UserProperty.class)
print umail.getAddress()
like image 127
mipmip Avatar answered Oct 14 '22 21:10

mipmip


You can access the object of the current user with the method current()

def user = hudson.model.User.current();

The email address can be retrieved in the same way as to what you have done in your answer.

print user.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress();
like image 28
satrun77 Avatar answered Oct 14 '22 20:10

satrun77