MackProgramsAlot
MackProgramsAlot

Reputation: 593

How to retrieve (or re-create) jenkins mailer plugin post build email text

In the event of a Jenkins build failure, an email is sent out to all developers using the Jenkins Mailer Plugin. See https://wiki.jenkins.io/display/JENKINS/Mailer

This email is extremely helpful in quickly identifying those authors whom have recently contributed to the source code and may be the culprits for breaking the build.

My goal is simple:

String emailText = "Email Content Here";

i.e. I wish to retrieve the email text for my own needs and perform my own parsing on it.

My initial attempt at solving this problem was to fetch the console output using basic auth:

    // Credentials
    String username = "some.username";
    String password = "USER_API_TOKEN";

    // Jenkins url
    String url = "https://[my-company-domain]/job/Team-Servers/job/teamserver-asc/85/consoleText";

    String consoleText = getConsoleText(username, password, url);

This worked great! I was able to get the console Text. However it does not provide the authors as the email does... any idea on how to retrieve this email for programmatic purposes. Or re-create it?

The format of the email is:

Authors:
 John Doe

Changes:
    John Doe: ce1ec6623567802d2fbc2cb5fb194d927835e466 
    - src/com/a/somePath/someFile.java
    - ivy.xml


Build Log:
  [...truncated 147.59 KB...]
     [java] [GC 3779195K->1885262K(4954624K), 0.2512120 secs]
     ...

BUILD FAILED
/u/jenkins/workspace/somePath/build.xml:1761: Java returned: 1

Total time: 52 minutes 56 seconds
Build step 'Invoke Ant' marked build as failure
Email was triggered for: Failure - Any
Sending email for trigger: Failure - Any

Upvotes: 3

Views: 288

Answers (2)

Ken
Ken

Reputation: 1985

There's a getCulprits() function that you can call for a build (http://javadoc.jenkins-ci.org/hudson/model/AbstractBuild.html)

Example

import hudson.tasks.Mailer;

def build = Jenkins.instance.getItemByFullName("TestBuild").getBuildByNumber(4)
def culprits = build.getCulprits()

for(culprit in culprits) {
    println culprit 
    def id = culprit.getId()
    User u = User.get(id)
    def umail = u.getProperty(Mailer.UserProperty.class)
    println "Email address is " + umail.getAddress()
}

This will print output like

First_Last
Email address is [email protected]
Foo_Bar
Email address is [email protected]

If you combine this with the email text you already have, I think this does what you want.

Upvotes: 1

rohit thomas
rohit thomas

Reputation: 2312

How exactly are you achieving this ?? using Groovy scripting, Pipeline or using curl ??

Based on the info you have provided
"https://[my-company-domain]/job/Team-Servers/job/teamserver-asc/85/consoleText"

You are directly pointing to the buildNumber(85) and reading the consoleText which works fine, so why not point to
"https://[my-company-domain]/job/Team-Servers/job/teamserver-asc/85/Changes" to get the Change implementor, if the data is not present then move to the parent folder like so
"https://[my-company-domain]/job/Team-Servers/Changes"
guessing(Team-Servers is the build name) you will definitely see the latest Change implementors here.

Again need to know how your coding to provide better information

Upvotes: 1

Related Questions