Skip to content
Browse files
[JENKINS-17742] - Pick Up Committer from Upstream Job
  • Loading branch information
christiangalsterer committed Jul 18, 2014
1 parent 526b444 commit 763ecc19519e6d14b35e0be4150f55d99a028850
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
@@ -0,0 +1,79 @@
package hudson.plugins.emailext.plugins.recipients;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.*;
import hudson.plugins.emailext.EmailRecipientUtils;
import hudson.plugins.emailext.ExtendedEmailPublisherContext;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.RecipientProviderDescriptor;
import hudson.scm.ChangeLogSet;
import hudson.tasks.Mailer;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.mail.internet.InternetAddress;
import java.util.Set;

* Sends emails to committers of upstream builds which triggered this build.
public class UpstreamComitterRecipientProvider extends RecipientProvider {

public UpstreamComitterRecipientProvider() {

public void addRecipients(ExtendedEmailPublisherContext context, EnvVars env, Set<InternetAddress> to, Set<InternetAddress> cc, Set<InternetAddress> bcc) {
ExtendedEmailPublisherDescriptor descriptor = Jenkins.getInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class);
descriptor.debug(context.getListener().getLogger(), "Sending email to upstream committer(s).");

AbstractBuild<?, ?> cur;
Cause.UpstreamCause upc = context.getBuild().getCause(Cause.UpstreamCause.class);
while (upc != null) {
// UpstreamCause.getUpstreamProject() returns the full name, so use getItemByFullName
AbstractProject<?, ?> p = (AbstractProject<?, ?>) Jenkins.getInstance().getItemByFullName(upc.getUpstreamProject());
if(p == null)
cur = p.getBuildByNumber(upc.getUpstreamBuild());
upc = cur.getCause(Cause.UpstreamCause.class);
addUpstreamCommittersTriggeringBuild(cur, to, cc, bcc, env, context.getListener());

* Adds for the given upstream build the committers to the recipient list for each commit in the upstream build.
* @param build the upstream build
* @param to the to recipient list
* @param cc the cc recipient list
* @param bcc the bcc recipient list
* @param env
* @param listener
private void addUpstreamCommittersTriggeringBuild(AbstractBuild<?, ?> build, Set<InternetAddress> to, Set<InternetAddress> cc, Set<InternetAddress> bcc, EnvVars env, TaskListener listener) {
listener.getLogger().println(String.format("Adding upstream committer from job %s with build number %s", build.getProject().getDisplayName(), build.getNumber()));
for (ChangeLogSet.Entry change : build.getChangeSet()) {
User user = change.getAuthor();
String email = user.getProperty(Mailer.UserProperty.class).getAddress();
if (email != null) {
listener.getLogger().println(String.format("Adding upstream committer %s to recipient list with email %s", user.getFullName(), email));
EmailRecipientUtils.addAddressesFromRecipientList(to, cc, bcc, email, env, listener);
} else {
listener.getLogger().println(String.format("The user %s does not have a configured email email, trying the user's id", user.getFullName()));
EmailRecipientUtils.addAddressesFromRecipientList(to, cc, bcc, user.getId(), env, listener);

public static final class DescriptorImpl extends RecipientProviderDescriptor {

public String getDisplayName() {
return "Upstream Committer";
@@ -0,0 +1,3 @@
Sends email to the list of users who committed changes in upstream builds that triggered this build.

0 comments on commit 763ecc1

Please sign in to comment.