Skip to content
Permalink
Browse files

JENKINS-47214 Give ability to opt out of autofavoriting (#12)

  • Loading branch information
i386 committed Nov 29, 2017
1 parent 73db17e commit 45f6a79e76632e9a785c392662785a8c1c42d933
@@ -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;
@@ -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;
@@ -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
@@ -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;
@@ -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 {

0 comments on commit 45f6a79

Please sign in to comment.
You can’t perform that action at this time.