Skip to content

Commit

Permalink
[JENKINS-62079] Add contributor() recipient provider which pulls in…
Browse files Browse the repository at this point in the history
…formation from `ContributorMetadataAction` (#361)
  • Loading branch information
slide authored Apr 24, 2022
1 parent 27cacee commit 64739c8
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
</dependency>

<!-- workflow stuff -->
<dependency>
Expand Down Expand Up @@ -188,11 +192,6 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!-- StepConfigTester -->
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package hudson.plugins.emailext.plugins.recipients;

import java.io.PrintStream;
import java.util.Collections;
import java.util.Set;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.User;
import hudson.plugins.emailext.ExtendedEmailPublisherContext;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.plugins.emailext.Messages;
import hudson.plugins.emailext.plugins.RecipientProvider;
import hudson.plugins.emailext.plugins.RecipientProviderDescriptor;
import jenkins.model.Jenkins;
import jenkins.scm.api.metadata.ContributorMetadataAction;

public class ContributorMetadataRecipientProvider extends RecipientProvider {

@DataBoundConstructor
public ContributorMetadataRecipientProvider() {

}

@Override
public void addRecipients(ExtendedEmailPublisherContext context, EnvVars env, Set<InternetAddress> to,
Set<InternetAddress> cc, Set<InternetAddress> bcc) {

final class Debug implements RecipientProviderUtilities.IDebug {
private final ExtendedEmailPublisherDescriptor descriptor
= Jenkins.get().getDescriptorByType(ExtendedEmailPublisherDescriptor.class);

private final PrintStream logger = context.getListener().getLogger();

public void send(final String format, final Object... args) {
descriptor.debug(logger, format, args);
}
}

final Debug debug = new Debug();

ContributorMetadataAction action = context.getRun().getAction(ContributorMetadataAction.class);
if(action != null) {
User user = findUser(debug, action.getContributor(), action.getContributorEmail());
if(user != null) {
RecipientProviderUtilities.addUsers(Collections.singleton(user), context, env, to, cc, bcc, debug);
}
} else {
debug.send("No ContributorMetadataAction is available");
context.getListener().getLogger().print(Messages.ContributorMetadataRecipientProvider_NoContributorInformationAvailable());
}
}

public User findUser(RecipientProviderUtilities.IDebug debug, String author, String authorEmail) {
// first we look for a user based on the user id (author), then if
// that fails, we look for one based on the email.
User user = null;
if(!StringUtils.isBlank(author)) {
debug.send("Trying username to get user account from Jenkins");
user = User.get(author, false, Collections.emptyMap());
}

if(user == null) {
if(!StringUtils.isBlank(authorEmail)) {
debug.send("Trying email address to get user account from Jenkins");
user = User.get(authorEmail, false, Collections.emptyMap());
if (user == null) {
debug.send("Looking through all users for a matching email address");
for (User existingUser : User.getAll()) {
if (authorEmail.equalsIgnoreCase(getMail(existingUser))) {
user = existingUser;
break;
}
}
}
}
}

if(user == null) {
debug.send("Could not find user with information provided");
}

return user;
}

private String getMail(User user) {
hudson.tasks.Mailer.UserProperty property = user.getProperty(hudson.tasks.Mailer.UserProperty.class);
if (property == null) {
return null;
}
if (!property.hasExplicitlyConfiguredAddress()) {
return null;
}
return property.getExplicitlyConfiguredAddress();
}

@Extension
@Symbol("contributor")
public static final class DescriptorImpl extends RecipientProviderDescriptor {

@NonNull
@Override
public String getDisplayName() {
return Messages.ContributorMetadataRecipientProvider_DisplayName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ ListRecipientProvider.DisplayName=Recipient List
RequesterRecipientProvider.DisplayName=Requestor
BuildUserRecipientProvider.DisplayName=Build User
UpstreamComitterRecipientProvider.DisplayName=Upstream Committers
ContributorMetadataRecipientProvider.DisplayName=Contributor

ContributorMetadataRecipientProvider.ErrorAddingContributorAddress=Error adding contributor email address: {0}
ContributorMetadataRecipientProvider.NoContributorInformationAvailable=No contributor information available

contentType.plainText=Plain Text (text/plain)
contentType.html=HTML (text/html)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package hudson.plugins.emailext.plugins.recipients;

import hudson.model.Result;
import hudson.model.User;
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor;
import hudson.tasks.Mailer;
import jenkins.model.Jenkins;
import jenkins.scm.api.metadata.ContributorMetadataAction;

import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.mock_javamail.Mailbox;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.util.Collections;

public class ContributorMetadataRecipientProviderTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@After
public void tearDown() {
Mailbox.clearAll();
}

@Test
public void testAddRecipients() throws Exception {
User user = User.get("someone", true, Collections.emptyMap());
user.addProperty(new Mailer.UserProperty("someone@DOMAIN"));

WorkflowJob job = j.createProject(WorkflowJob.class, "test");
WorkflowRun run = job.scheduleBuild2(0).get();
run.addAction(new ContributorMetadataAction("someone", "Some One", "someone@DOMAIN" ));

TestUtilities.checkRecipients(run, new ContributorMetadataRecipientProvider(), "someone");
}

@Test
public void testAddRecipients_NotUser() throws Exception {
WorkflowJob job = j.createProject(WorkflowJob.class, "test");
WorkflowRun run = job.scheduleBuild2(0).get();
run.addAction(new ContributorMetadataAction("someoneelse", "Some One Else", "someoneelse@DOMAIN" ));

TestUtilities.checkRecipients(run, new ContributorMetadataRecipientProvider());
}
}

0 comments on commit 64739c8

Please sign in to comment.