Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline currentBuild.changeSets and retrieving emails per repository

I have a Jenkins pipeline job, that check outs three repositories.

When the build fails, depending on where it fails, I want to send emails to the developers which caused the last commits/changes.

I can retrieve the authors full names with this:

def changeSet = script.currentBuild.changeSets[0];
Set authors = [];
if (changeSet != null) {
    for (change in changeSet.items) {
        authors.add(change.author.fullName)
    }
}

But I cannot figure out:

  1. How can I get the authors email?
  2. How can I distinguish the authors for different repositories?
like image 281
Nathan Avatar asked Sep 06 '17 08:09

Nathan


2 Answers

You can get the author name and then use it for an example on a mailing registry or something like that:

def author = ""
def changeSet = currentBuild.rawBuild.changeSets               
for (int i = 0; i < changeSet.size(); i++) 
{
   def entries = changeSet[i].items;
   for (int i = 0; i < changeSet.size(); i++) 
            {
                       def entries = changeSet[i].items;
                       def entry = entries[0]
                       author += "${entry.author}"
            } 
 }
 print author;
like image 142
Mohamed Amine Kharrez Avatar answered Nov 15 '22 17:11

Mohamed Amine Kharrez


See more here: How to get e-mail address of current Jenkins user to use in groovy script

echo 'author email:' + change.author.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress()

But it required disable groovy sandbox :(

Possibly solution has been add this to Jenkins Pipeline Shared Libraries: https://jenkins.io/doc/book/pipeline/shared-libraries/

Like this:

$ cat GetUserEmail.groovy 
#!/usr/bin/groovy

def call(body) {
    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    def User = config.get('user')
    return User.getProperty(hudson.tasks.Mailer.UserProperty.class).getAddress()
}

And use like this:

def changeSet = script.currentBuild.changeSets[0];
Set authors = [];
if (changeSet != null) {
    for (change in changeSet.items) {
        authors.add(GetUserEmail{user=change.author})
    }
}
like image 36
METAJIJI Avatar answered Nov 15 '22 15:11

METAJIJI