Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid sync config files from scm working dir. #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.409</version>
<version>1.424</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -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()+"] !");
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -312,7 +312,7 @@ private List<File> syncDirectories(File from, String relative) throws IOExceptio
List<File> l = new ArrayList<File>();
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
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,7 +121,7 @@ public List<String> getManualSynchronizationIncludes(){
public void start() throws Exception {
super.start();

Hudson.XSTREAM.registerConverter(new ScmSyncConfigurationXStreamConverter());
Jenkins.XSTREAM.registerConverter(new ScmSyncConfigurationXStreamConverter());

this.load();

Expand Down Expand Up @@ -265,13 +265,13 @@ public List<String> 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){
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,7 +52,7 @@ public String getSCMClassName() {
}

public Descriptor<? extends hudson.scm.SCM> getSCMDescriptor(){
return Hudson.getInstance().getDescriptorByName(getSCMClassName());
return Jenkins.getInstance().getDescriptorByName(getSCMClassName());
}

public String getRepositoryUrlHelpPath() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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;
import jenkins.model.Jenkins;
import hudson.model.Item;
import hudson.model.Saveable;
import hudson.plugins.scm_sync_configuration.model.MessageWeight;
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;
Expand All @@ -21,7 +23,13 @@ public abstract class AbstractScmSyncStrategy implements ScmSyncStrategy {

private static final Function<String,File> PATH_TO_FILE_IN_HUDSON = new Function<String, File>() {
public File apply(@Nullable String path) {
return new File(Hudson.getInstance().getRootDir()+File.separator+path);
return new File(Jenkins.getInstance().getRootDir()+File.separator+path);
}
};

private static final Predicate<File> FILTER_PATH_TO_SCM_DIR = new Predicate<File>() {
public boolean apply(@Nullable File file) {
return !file.getAbsolutePath().startsWith(ScmSyncConfigurationBusiness.getCheckoutScmDirectoryAbsolutePath());
}
};

Expand Down Expand Up @@ -54,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());
Expand All @@ -70,9 +78,9 @@ public PageMatcher getPageMatcherMatching(String url){
}

public List<File> createInitializationSynchronizedFileset() {
File hudsonRoot = Hudson.getInstance().getRootDir();
File hudsonRoot = Jenkins.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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down