Skip to content

Commit

Permalink
JENKINS-47214 Give ability to opt out of autofavoriting (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
James William Dumay committed Nov 29, 2017
1 parent 73db17e commit 45f6a79
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
Expand Up @@ -21,6 +21,7 @@
import hudson.scm.SCM;
import hudson.scm.SCMRevisionState;
import hudson.util.LogTaskListener;
import io.jenkins.blueocean.autofavorite.user.FavoritingUserProperty;
import jenkins.branch.MultiBranchProject;
import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.errors.MissingObjectException;
Expand Down Expand Up @@ -98,6 +99,11 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene
return;
}

// If the user has disabled auto-favoriting then we should bail out
if (!FavoritingUserProperty.from(author).isAutofavoriteEnabled()) {
return;
}

// If the user has already favorited then unfavorited it we should not favorite it again
if (Favorites.hasFavorite(author, job) && !Favorites.isFavorite(author, job)) {
return;
Expand Down
@@ -0,0 +1,53 @@
package io.jenkins.blueocean.autofavorite.user;

import com.google.common.annotations.VisibleForTesting;
import hudson.Extension;
import hudson.model.User;
import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;
import io.jenkins.blueocean.autofavorite.Messages;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;

public class FavoritingUserProperty extends UserProperty {

private Boolean autofavoriteEnabled;

@DataBoundConstructor
public FavoritingUserProperty(Boolean autofavoriteEnabled) {
this.autofavoriteEnabled = autofavoriteEnabled;
}

public boolean isAutofavoriteEnabled() {
return autofavoriteEnabled;
}

@VisibleForTesting
public void setAutofavoriteEnabled(boolean autofavoriteEnabled) {
this.autofavoriteEnabled = autofavoriteEnabled;
}

@Extension
public static class DescriptorImpl extends UserPropertyDescriptor {

public DescriptorImpl() {
super(FavoritingUserProperty.class);
}

@Override
public UserProperty newInstance(User user) {
return new FavoritingUserProperty(true);
}

@Nonnull
@Override
public String getDisplayName() {
return Messages.autofavorite_name();
}
}

public static FavoritingUserProperty from(User user) {
return user.getProperty(FavoritingUserProperty.class);
}
}
@@ -0,0 +1 @@
autofavorite.name = Automatically favorite pipelines
@@ -0,0 +1,9 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:block>
<f:entry name="auto-fav-enabled" field="autofavoriteEnabled">
<f:checkbox title="${%autofavorite.enabled}" name="autofavoriteEnabled" checked="${instance.autofavoriteEnabled}"/>
</f:entry>
</f:block>
</j:jelly>
@@ -0,0 +1 @@
autofavorite.enabled = when your email address appears in the change list of a run
Expand Up @@ -4,6 +4,8 @@
import hudson.model.User;
import hudson.plugins.favorite.Favorites;
import java.util.concurrent.TimeUnit;

import io.jenkins.blueocean.autofavorite.user.FavoritingUserProperty;
import jenkins.branch.BranchSource;
import jenkins.branch.MultiBranchProject.BranchIndexing;
import jenkins.plugins.git.GitSCMSource;
Expand All @@ -29,6 +31,21 @@ public void testAutoFavoriteForRegisteredUser() throws Exception {
assertTrue(Favorites.isFavorite(user, job));
}

@Test
public void testAutoFavoriteForRegisteredUserWhenDisabled() throws Exception {
User jdumay = User.getById("jdumay", true);
assertNotNull(jdumay);

// Disable autofavorite
FavoritingUserProperty.from(jdumay).setAutofavoriteEnabled(false);
jdumay.save();

WorkflowJob job = createAndRunPipeline();
User user = User.getById("jdumay", false);
assertNotNull(user);
assertFalse(Favorites.isFavorite(user, job));
}

// @Test
/** Disabled because of https://issues.jenkins-ci.org/browse/JENKINS-39694 **/
public void testAutoFavoriteForNonRegisteredUser() throws Exception {
Expand Down

0 comments on commit 45f6a79

Please sign in to comment.