Get a request to create a request using a Groovy script (Jenkins / email-ext)
I want to get the username and / or email address of the creator of the build request in a post-build script.
Sidenote: I want the requester to be able to configure the email sender for the post-build email message dynamically in the email-ext script plugin pre-submission.
AbstractBuild
has built-in support for AbstractBuild.getCulprits()
and AbstractBuild.hasParticipant(User user)
, but I can't find a method to get the request, I can't find a useful link in the list of links to the class User
.
source to share
I managed to solve it using an Cause
assembly as recommended in this answer .
The reason the build request can be found in the build reason makes sense when you think about it: not every build is directly initiated by the user. If triggered by a user, the list of reasons for the build contains Cause.UserIdCause
, with a user id and name.
This piece of code worked for me. It extracts the username from the assembly for a reason and sets the From and ReplyTo headers. I am using it in a pre-send script email-ext Jenkins plugin.
import javax.mail.internet.InternetAddress
cause = build.getCause(hudson.model.Cause.UserIdCause.class);
username = cause.getUserName()
id = cause.getUserId()
email = new InternetAddress(String.format("%s <%s@example.com>", username, id))
msg.setFrom(email)
msg.setReplyTo(email);
source to share