Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-47214 globally disable autofavorite using system property (#13)
-DBLUEOCEAN_FEATURE_AUTOFAVORITE_ENABLED=false
  • Loading branch information
James William Dumay committed Nov 29, 2017
1 parent 95ee516 commit 80930fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -23,6 +23,7 @@
import hudson.util.LogTaskListener;
import io.jenkins.blueocean.autofavorite.user.FavoritingUserProperty;
import jenkins.branch.MultiBranchProject;
import jenkins.util.SystemProperties;
import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.errors.MissingObjectException;
import org.jenkinsci.plugins.gitclient.Git;
Expand All @@ -46,6 +47,11 @@ public class FavoritingScmListener extends SCMListener {

@Override
public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListener listener, @CheckForNull File changelogFile, @CheckForNull SCMRevisionState pollingBaseline) throws Exception {

if (!isEnabled()) {
return;
}

// Look for the first build of a multibranch job
if (!(build instanceof WorkflowRun
&& ((WorkflowRun) build).getParent().getParent() instanceof MultiBranchProject
Expand Down Expand Up @@ -157,4 +163,10 @@ private GitChangeSet getChangeSet(GitSCM scm, FilePath workspace, Revision lastB
}
return Iterables.getOnlyElement(changeSets, null);
}

static boolean isEnabled() {
return SystemProperties.getBoolean(BLUEOCEAN_FEATURE_AUTOFAVORITE_ENABLED_PROPERTY, true);
}

static final String BLUEOCEAN_FEATURE_AUTOFAVORITE_ENABLED_PROPERTY = "BLUEOCEAN_FEATURE_AUTOFAVORITE_ENABLED";
}
Expand Up @@ -13,6 +13,7 @@
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand Down Expand Up @@ -46,6 +47,27 @@ public void testAutoFavoriteForRegisteredUserWhenDisabled() throws Exception {
assertFalse(Favorites.isFavorite(user, job));
}

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

// Disable autofavorite
assertTrue(FavoritingScmListener.isEnabled());
System.setProperty(FavoritingScmListener.BLUEOCEAN_FEATURE_AUTOFAVORITE_ENABLED_PROPERTY, "false");
assertFalse(FavoritingScmListener.isEnabled());

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

@After
public void enableFeature() {
System.setProperty(FavoritingScmListener.BLUEOCEAN_FEATURE_AUTOFAVORITE_ENABLED_PROPERTY, "true");
}

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

0 comments on commit 80930fe

Please sign in to comment.