Skip to content

Commit

Permalink
Support email aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
i386 committed Nov 12, 2016
1 parent 4c13d3b commit 48429ce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<artifactId>favorite</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mail-aliases</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-branch-source</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.blueocean.autofavorite;

import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
Expand All @@ -10,17 +11,23 @@
import hudson.model.User;
import hudson.model.listeners.SCMListener;
import hudson.plugins.favorite.Favorites;
import hudson.plugins.favorite.Favorites.FavoriteException;
import hudson.plugins.git.GitChangeLogParser;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.BuildData;
import hudson.scm.SCM;
import hudson.scm.SCMRevisionState;
import hudson.tasks.Mailer.UserProperty;
import jenkins.branch.MultiBranchProject;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.mailaliases.MailAlias;
import org.jenkinsci.plugins.mailaliases.MailAliasUserSearch;
import org.jenkinsci.plugins.mailaliases.MailAliasUserSearch.OnResult;
import org.jenkinsci.plugins.mailaliases.MailAliasesUserProperty;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -78,8 +85,36 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene
return;
}

Job<?, ?> job = build.getParent();
final Job<?, ?> job = build.getParent();
User author = first.getAuthor();

/**
* Here's some fun - depending on the value of {@link GitSCM#isCreateAccountBasedOnEmail) the user returned by
* {@link GitChangeSet#getAuthor()} will either created in memory or create on disk (making it a user that can be logged into)
* That means if isCreateAccountBasedOnEmail() is true it create any Author it comes across in the Git log as a Jenkins users.
* So in that case we can no-op the linear search for a matching email alias.
*/
if (User.getById(author.getId(), false) == null || !((GitSCM) scm).isCreateAccountBasedOnEmail()) {
UserProperty property = first.getAuthor().getProperty(UserProperty.class);
if (property != null && property.getConfiguredAddress() != null) {
for (User user : User.getAll()) {
MailAliasesUserProperty aliasesUserProperty = user.getProperty(MailAliasesUserProperty.class);
if (aliasesUserProperty != null) {
for (MailAlias alias : aliasesUserProperty.getMailAliases()) {
if (alias != null && alias.getEmail().equalsIgnoreCase(property.getConfiguredAddress())) {
setFavorite(job, user);
break;
}
}
}
}
}
} else {
setFavorite(job, author);
}
}

private void setFavorite(Job<?, ?> job, User author) throws FavoriteException {
if (!User.getUnknown().equals(author) && !Favorites.hasFavorite(author, job) && !Favorites.isFavorite(author, job)) {
Favorites.addFavorite(author, job);
logger.log(Level.INFO, "Automatically favorited " + job.getFullName() + " for " + author);
Expand Down

0 comments on commit 48429ce

Please sign in to comment.