From 8dcaf63d1ff8801a94f724ba545cac42c44aeba1 Mon Sep 17 00:00:00 2001 From: Alexandre Adolphe Date: Mon, 6 Jul 2015 15:14:40 +0200 Subject: [PATCH 1/3] Avoid sync config files from scm working dir. Avoiding possible recursive sync of "jobs" folder. --- .../strategies/AbstractScmSyncStrategy.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java b/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java index c218c015..9b4f95eb 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java @@ -1,6 +1,7 @@ package hudson.plugins.scm_sync_configuration.strategies; import com.google.common.base.Function; +import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import hudson.XmlFile; import hudson.model.Hudson; @@ -10,6 +11,7 @@ import hudson.plugins.scm_sync_configuration.model.WeightedMessage; import hudson.plugins.scm_sync_configuration.strategies.model.ConfigurationEntityMatcher; import hudson.plugins.scm_sync_configuration.strategies.model.PageMatcher; +import hudson.plugins.scm_sync_configuration.ScmSyncConfigurationBusiness; import javax.annotation.Nullable; import java.io.File; @@ -25,6 +27,12 @@ public File apply(@Nullable String path) { } }; + private static final Predicate FILTER_PATH_TO_SCM_DIR = new Predicate() { + public boolean apply(@Nullable File file) { + return !file.getAbsolutePath().startsWith(ScmSyncConfigurationBusiness.getCheckoutScmDirectoryAbsolutePath()); + } + }; + protected static class DefaultCommitMessageFactory implements CommitMessageFactory { public WeightedMessage getMessageWhenSaveableUpdated(Saveable s, XmlFile file) { return new WeightedMessage("Modification on configuration(s)", MessageWeight.MINIMAL); @@ -72,7 +80,7 @@ public PageMatcher getPageMatcherMatching(String url){ public List createInitializationSynchronizedFileset() { File hudsonRoot = Hudson.getInstance().getRootDir(); String[] matchingFilePaths = createConfigEntityMatcher().matchingFilesFrom(hudsonRoot); - return new ArrayList(Collections2.transform(Arrays.asList(matchingFilePaths), PATH_TO_FILE_IN_HUDSON)); + return new ArrayList(Collections2.filter(Collections2.transform(Arrays.asList(matchingFilePaths), PATH_TO_FILE_IN_HUDSON), FILTER_PATH_TO_SCM_DIR)); } public boolean isCurrentUrlApplicable(String url) { From 838f26221b0a41e0c213a90c6227617d5513e5b6 Mon Sep 17 00:00:00 2001 From: Alexandre Adolphe Date: Mon, 6 Jul 2015 15:31:41 +0200 Subject: [PATCH 2/3] Update pom.xml to support Java 7. Change org.jenkins-ci.plugins:plugin version to 1.424. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 229aaf63..3a3185f0 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jenkins-ci.plugins plugin - 1.409 + 1.424 ../pom.xml From 6687b57c3142c86660b991cefa21b606550a0630 Mon Sep 17 00:00:00 2001 From: Alexandre Adolphe Date: Mon, 6 Jul 2015 19:04:50 +0200 Subject: [PATCH 3/3] Use jenkins model instead of hudson. --- .../JenkinsFilesHelper.java | 6 +++--- .../ScmSyncConfigurationBusiness.java | 12 ++++++------ .../ScmSyncConfigurationPlugin.java | 8 ++++---- .../ScmSyncConfigurationStatusManager.java | 6 +++--- .../scm_sync_configuration/scms/SCM.java | 4 ++-- .../strategies/AbstractScmSyncStrategy.java | 8 ++++---- .../model/PatternsEntityMatcher.java | 2 +- .../basic/ScmSyncConfigurationBasicTest.java | 12 ++++++------ .../util/ScmSyncConfigurationBaseTest.java | 18 +++++++++--------- 9 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/JenkinsFilesHelper.java b/src/main/java/hudson/plugins/scm_sync_configuration/JenkinsFilesHelper.java index a12de734..8cd9da2e 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/JenkinsFilesHelper.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/JenkinsFilesHelper.java @@ -1,13 +1,13 @@ package hudson.plugins.scm_sync_configuration; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import java.io.File; public class JenkinsFilesHelper { public static String buildPathRelativeToHudsonRoot(File file){ - File hudsonRoot = Hudson.getInstance().getRootDir(); + File hudsonRoot = Jenkins.getInstance().getRootDir(); if(!file.getAbsolutePath().startsWith(hudsonRoot.getAbsolutePath())){ throw new IllegalArgumentException("Err ! File ["+file.getAbsolutePath()+"] seems not to reside in ["+hudsonRoot.getAbsolutePath()+"] !"); } @@ -16,7 +16,7 @@ public static String buildPathRelativeToHudsonRoot(File file){ } public static File buildFileFromPathRelativeToHudsonRoot(String pathRelativeToHudsonRoot){ - File hudsonRoot = Hudson.getInstance().getRootDir(); + File hudsonRoot = Jenkins.getInstance().getRootDir(); return new File(hudsonRoot.getAbsolutePath()+File.separator+pathRelativeToHudsonRoot); } } diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java index 8e43be65..64181ab8 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationBusiness.java @@ -1,7 +1,7 @@ package hudson.plugins.scm_sync_configuration; import com.google.common.io.Files; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import hudson.model.User; import hudson.plugins.scm_sync_configuration.exceptions.LoggableException; import hudson.plugins.scm_sync_configuration.model.*; @@ -312,7 +312,7 @@ private List syncDirectories(File from, String relative) throws IOExceptio List l = new ArrayList(); for(File f : from.listFiles()) { String newRelative = relative + File.separator + f.getName(); - File jenkinsFile = new File(Hudson.getInstance().getRootDir() + newRelative); + File jenkinsFile = new File(Jenkins.getInstance().getRootDir() + newRelative); if (f.getName().equals(scmManipulator.getScmSpecificFilename())) { // nothing to do } @@ -351,20 +351,20 @@ private void signal(String operation, boolean result) { } public static String getCheckoutScmDirectoryAbsolutePath(){ - return Hudson.getInstance().getRootDir().getAbsolutePath()+WORKING_DIRECTORY_PATH+CHECKOUT_SCM_DIRECTORY; + return Jenkins.getInstance().getRootDir().getAbsolutePath()+WORKING_DIRECTORY_PATH+CHECKOUT_SCM_DIRECTORY; } public void purgeFailLogs() { - Hudson.getInstance().checkPermission(purgeFailLogPermission()); + Jenkins.getInstance().checkPermission(purgeFailLogPermission()); scmSyncConfigurationStatusManager.purgeFailLogs(); } public boolean canCurrentUserPurgeFailLogs() { - return Hudson.getInstance().hasPermission(purgeFailLogPermission()); + return Jenkins.getInstance().hasPermission(purgeFailLogPermission()); } private static Permission purgeFailLogPermission(){ // Only administrators should be able to purge logs - return Hudson.ADMINISTER; + return Jenkins.ADMINISTER; } } diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java index 95b9ed11..22054e89 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin.java @@ -5,7 +5,7 @@ import hudson.Plugin; import hudson.model.Descriptor; import hudson.model.Descriptor.FormException; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import hudson.model.Saveable; import hudson.model.User; import hudson.plugins.scm_sync_configuration.extensions.ScmSyncConfigurationFilter; @@ -121,7 +121,7 @@ public List getManualSynchronizationIncludes(){ public void start() throws Exception { super.start(); - Hudson.XSTREAM.registerConverter(new ScmSyncConfigurationXStreamConverter()); + Jenkins.XSTREAM.registerConverter(new ScmSyncConfigurationXStreamConverter()); this.load(); @@ -265,13 +265,13 @@ public List getDefaultIncludes(){ private User getCurrentUser(){ User user = null; try { - user = Hudson.getInstance().getMe(); + user = Jenkins.getInstance().getMe(); }catch(AccessDeniedException e){} return user; } public static ScmSyncConfigurationPlugin getInstance(){ - return Hudson.getInstance().getPlugin(ScmSyncConfigurationPlugin.class); + return Jenkins.getInstance().getPlugin(ScmSyncConfigurationPlugin.class); } public ScmSyncStrategy getStrategyForSaveable(Saveable s, File f){ diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationStatusManager.java b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationStatusManager.java index ed26afe2..75017787 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationStatusManager.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationStatusManager.java @@ -1,6 +1,6 @@ package hudson.plugins.scm_sync_configuration; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import java.io.File; import java.io.IOException; @@ -21,8 +21,8 @@ public class ScmSyncConfigurationStatusManager { private File success; public ScmSyncConfigurationStatusManager() { - fail = new File(Hudson.getInstance().getRootDir().getAbsolutePath()+File.separator+LOG_FAIL_FILENAME); - success = new File(Hudson.getInstance().getRootDir().getAbsolutePath()+File.separator+LOG_SUCCESS_FILENAME); + fail = new File(Jenkins.getInstance().getRootDir().getAbsolutePath()+File.separator+LOG_FAIL_FILENAME); + success = new File(Jenkins.getInstance().getRootDir().getAbsolutePath()+File.separator+LOG_SUCCESS_FILENAME); } public String getLastFail() { diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/scms/SCM.java b/src/main/java/hudson/plugins/scm_sync_configuration/scms/SCM.java index f473948d..3b8d8994 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/scms/SCM.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/scms/SCM.java @@ -1,7 +1,7 @@ package hudson.plugins.scm_sync_configuration.scms; import hudson.model.Descriptor; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import java.util.ArrayList; import java.util.List; @@ -52,7 +52,7 @@ public String getSCMClassName() { } public Descriptor getSCMDescriptor(){ - return Hudson.getInstance().getDescriptorByName(getSCMClassName()); + return Jenkins.getInstance().getDescriptorByName(getSCMClassName()); } public String getRepositoryUrlHelpPath() { diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java b/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java index 9b4f95eb..745a42ac 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java @@ -4,7 +4,7 @@ import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import hudson.XmlFile; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import hudson.model.Item; import hudson.model.Saveable; import hudson.plugins.scm_sync_configuration.model.MessageWeight; @@ -23,7 +23,7 @@ public abstract class AbstractScmSyncStrategy implements ScmSyncStrategy { private static final Function PATH_TO_FILE_IN_HUDSON = new Function() { public File apply(@Nullable String path) { - return new File(Hudson.getInstance().getRootDir()+File.separator+path); + return new File(Jenkins.getInstance().getRootDir()+File.separator+path); } }; @@ -62,7 +62,7 @@ public boolean isSaveableApplicable(Saveable saveable, File file) { } public PageMatcher getPageMatcherMatching(String url){ - String rootUrl = Hudson.getInstance().getRootUrlFromRequest(); + String rootUrl = Jenkins.getInstance().getRootUrlFromRequest(); String cleanedUrl = null; if(url.startsWith(rootUrl)){ cleanedUrl = url.substring(rootUrl.length()); @@ -78,7 +78,7 @@ public PageMatcher getPageMatcherMatching(String url){ } public List createInitializationSynchronizedFileset() { - File hudsonRoot = Hudson.getInstance().getRootDir(); + File hudsonRoot = Jenkins.getInstance().getRootDir(); String[] matchingFilePaths = createConfigEntityMatcher().matchingFilesFrom(hudsonRoot); return new ArrayList(Collections2.filter(Collections2.transform(Arrays.asList(matchingFilePaths), PATH_TO_FILE_IN_HUDSON), FILTER_PATH_TO_SCM_DIR)); } diff --git a/src/main/java/hudson/plugins/scm_sync_configuration/strategies/model/PatternsEntityMatcher.java b/src/main/java/hudson/plugins/scm_sync_configuration/strategies/model/PatternsEntityMatcher.java index 3bccded2..7c18f9f8 100644 --- a/src/main/java/hudson/plugins/scm_sync_configuration/strategies/model/PatternsEntityMatcher.java +++ b/src/main/java/hudson/plugins/scm_sync_configuration/strategies/model/PatternsEntityMatcher.java @@ -1,6 +1,6 @@ package hudson.plugins.scm_sync_configuration.strategies.model; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import hudson.model.Saveable; import hudson.plugins.scm_sync_configuration.JenkinsFilesHelper; import org.apache.tools.ant.DirectoryScanner; diff --git a/src/test/java/hudson/plugins/scm_sync_configuration/basic/ScmSyncConfigurationBasicTest.java b/src/test/java/hudson/plugins/scm_sync_configuration/basic/ScmSyncConfigurationBasicTest.java index 0e15fc1e..ed8441fb 100644 --- a/src/test/java/hudson/plugins/scm_sync_configuration/basic/ScmSyncConfigurationBasicTest.java +++ b/src/test/java/hudson/plugins/scm_sync_configuration/basic/ScmSyncConfigurationBasicTest.java @@ -5,7 +5,7 @@ import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertThat; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import hudson.plugins.scm_sync_configuration.util.ScmSyncConfigurationBaseTest; import hudson.plugins.test.utils.scms.ScmUnderTestSubversion; @@ -21,16 +21,16 @@ public ScmSyncConfigurationBasicTest() { @Test public void shouldRetrieveMockedHudsonInstanceCorrectly() throws Throwable { - Hudson hudsonInstance = Hudson.getInstance(); - assertThat(hudsonInstance, is(notNullValue())); - assertThat(hudsonInstance.toString().split("@")[0], is(not(equalTo("hudson.model.Hudson")))); + Jenkins jenkinsInstance = Jenkins.getInstance(); + assertThat(jenkinsInstance, is(notNullValue())); + assertThat(jenkinsInstance.toString().split("@")[0], is(not(equalTo("jenkins.model.Jenkins")))); } @Test public void shouldVerifyIfHudsonRootDirectoryExists() throws Throwable { - Hudson hudsonInstance = Hudson.getInstance(); - File hudsonRootDir = hudsonInstance.getRootDir(); + Jenkins jenkinsInstance = Jenkins.getInstance(); + File hudsonRootDir = jenkinsInstance.getRootDir(); assertThat(hudsonRootDir, is(not(equalTo(null)))); assertThat(hudsonRootDir.exists(), is(true)); } diff --git a/src/test/java/hudson/plugins/scm_sync_configuration/util/ScmSyncConfigurationBaseTest.java b/src/test/java/hudson/plugins/scm_sync_configuration/util/ScmSyncConfigurationBaseTest.java index 5de847fa..3a1da32a 100644 --- a/src/test/java/hudson/plugins/scm_sync_configuration/util/ScmSyncConfigurationBaseTest.java +++ b/src/test/java/hudson/plugins/scm_sync_configuration/util/ScmSyncConfigurationBaseTest.java @@ -2,7 +2,7 @@ import hudson.Plugin; import hudson.PluginWrapper; -import hudson.model.Hudson; +import jenkins.model.Jenkins; import hudson.model.User; import hudson.plugins.scm_sync_configuration.SCMManagerFactory; import hudson.plugins.scm_sync_configuration.SCMManipulator; @@ -46,7 +46,7 @@ @RunWith(PowerMockRunner.class) @PowerMockIgnore({ "org.tmatesoft.svn.*" }) -@PrepareForTest({Hudson.class, SCM.class, ScmSyncSubversionSCM.class, PluginWrapper.class}) +@PrepareForTest({Jenkins.class, SCM.class, ScmSyncSubversionSCM.class, PluginWrapper.class}) public abstract class ScmSyncConfigurationBaseTest { @Rule protected TestName testName = new TestName(); @@ -105,14 +105,14 @@ public void setup() throws Throwable { // Mocking Hudson singleton instance ... // Warning : this line will only work on Objenesis supported VMs : // http://code.google.com/p/objenesis/wiki/ListOfCurrentlySupportedVMs - Hudson hudsonMockedInstance = spy((Hudson) new ObjenesisStd().getInstantiatorOf(Hudson.class).newInstance()); - PowerMockito.doReturn(currentHudsonRootDirectory).when(hudsonMockedInstance).getRootDir(); - PowerMockito.doReturn(mockedUser).when(hudsonMockedInstance).getMe(); - PowerMockito.doReturn(scmSyncConfigPluginInstance).when(hudsonMockedInstance).getPlugin(ScmSyncConfigurationPlugin.class); + Jenkins jenkinsMockedInstance = spy((Jenkins) new ObjenesisStd().getInstantiatorOf(Jenkins.class).newInstance()); + PowerMockito.doReturn(currentHudsonRootDirectory).when(jenkinsMockedInstance).getRootDir(); + PowerMockito.doReturn(mockedUser).when(jenkinsMockedInstance).getMe(); + PowerMockito.doReturn(scmSyncConfigPluginInstance).when(jenkinsMockedInstance).getPlugin(ScmSyncConfigurationPlugin.class); - PowerMockito.mockStatic(Hudson.class); - PowerMockito.doReturn(hudsonMockedInstance).when(Hudson.class); Hudson.getInstance(); - //when(Hudson.getInstance()).thenReturn(hudsonMockedInstance); + PowerMockito.mockStatic(Jenkins.class); + PowerMockito.doReturn(jenkinsMockedInstance).when(Jenkins.class); Jenkins.getInstance(); + //when(Jenkins.getInstance()).thenReturn(jenkinsMockedInstance); } @After