-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-62079] Add
contributor()
recipient provider which pulls in…
…formation from `ContributorMetadataAction` (#361)
- Loading branch information
Showing
4 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...java/hudson/plugins/emailext/plugins/recipients/ContributorMetadataRecipientProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
.../hudson/plugins/emailext/plugins/recipients/ContributorMetadataRecipientProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |