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

Added Mercurial (HG) support #9

Open
wants to merge 5 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
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>subversion</artifactId>
<version>1.39</version>
</dependency>
</dependency>

<!-- providers declaration -->
<dependency>
<groupId>com.google.code.maven-scm-provider-svnjava</groupId>
Expand All @@ -159,6 +160,11 @@
<version>1.6</version>
</dependency>

<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-hg</artifactId>
<version>1.8</version>
</dependency>

<!-- TEST DEPENDENCIES -->
<!-- powermock to mock static methods.. especially Hudson.getInstance() -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,16 @@ public boolean checkout(File checkoutDirectory){
return checkoutOk;
}

public List<File> deleteHierarchy(File hierarchyToDelete){
public List<File> deleteHierarchy(File hierarchyToDelete, String hierarchyPath){
if(!expectScmRepositoryInitiated()){
return null;
}

File enclosingDirectory = hierarchyToDelete.getParentFile();

LOGGER.fine("Deleting SCM hierarchy ["+hierarchyToDelete.getAbsolutePath()+"] from SCM ...");
String absolutePath = hierarchyToDelete.getAbsolutePath();
String basePath = absolutePath.substring(0, absolutePath.lastIndexOf(hierarchyPath));
File enclosingDirectory = new File(basePath);

LOGGER.fine("Deleting SCM hierarchy ["+absolutePath+"] from SCM ...");

File commitFile = hierarchyToDelete;
while(! commitFile.isDirectory()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public List<File> deleteHierarchy(ScmContext scmContext, Path hierarchyPath){


File rootHierarchyTranslatedInScm = hierarchyPath.getScmFile();
List<File> filesToCommit = scmManipulator.deleteHierarchy(rootHierarchyTranslatedInScm);
List<File> filesToCommit = scmManipulator.deleteHierarchy(rootHierarchyTranslatedInScm, hierarchyPath.getPath());

// Once done, we should delete path in scm if it is a directory
if(hierarchyPath.isDirectory()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class SCM {
add(new ScmSyncNoSCM());
add(new ScmSyncSubversionSCM());
add(new ScmSyncGitSCM());
add(new ScmSyncHgSCM());
} };

transient protected String title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hudson.plugins.scm_sync_configuration.scms;

import org.kohsuke.stapler.StaplerRequest;

public class ScmSyncHgSCM extends SCM {

private static final String SCM_URL_PREFIX="scm:hg:";

ScmSyncHgSCM(){
super("Mercurial", "hg/config.jelly", "hudson.plugins.mercurial.MercurialSCM", "/hudson/plugins/scm_sync_configuration/ScmSyncConfigurationPlugin/scms/hg/url-help.jelly");
}

public String createScmUrlFromRequest(StaplerRequest req) {
String repoURL = req.getParameter("hgRepositoryUrl");
if(repoURL == null){
return null;
}
else {
return SCM_URL_PREFIX+repoURL;
}
}

public String extractScmUrlFrom(String scmUrl) {
return scmUrl.substring(SCM_URL_PREFIX.length());
}

public SCMCredentialConfiguration extractScmCredentials(String scmUrl) {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<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" xmlns:p="/lib/hudson/project">

<f:entry title="${%Repository URL}" help="${rootURL}/plugin/scm-sync-configuration/helpForRepositoryUrl?scm=${scm.id}">
<f:textbox id="hgRepositoryUrl" name="hgRepositoryUrl" value="${it.isScmSelected(scm) ? it.scmUrl : null}"/>
</f:entry>

</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Repository\ URL=URL du repository
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
The MIT License

Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout">
<l:ajax>
<div>
${%description.1}<br/>
${%description.2}
</div>
</l:ajax>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The MIT License
#
# Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

description.1=\
Specify the hg repository URL to synchronize your configuration files with, such as "[email protected]:mycompany/jenkins-config"
description.2=\
Note that, for the moment, your MUST reference your Hg repository root
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The MIT License
#
# Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Eric Lefevre-Ardant
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

description.1=\
Sp�cifier l''url du repository Hg vers lequel synchroniser vos fichiers de configuration, tel que "[email protected]/mycompany/jenkins-config"
description.2=\
A noter qu''il est pour le moment obligatoire de r�f�rencer la racine du repository
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hudson.plugins.scm_sync_configuration.repository;

import hudson.plugins.test.utils.scms.ScmUnderTestHg;
import org.junit.Ignore;

public class HudsonExtensionsHgTest extends HudsonExtensionsTest {

public HudsonExtensionsHgTest() {
super(new ScmUnderTestHg());
}

@Ignore("Should be re-activated once maven-scm-api 1.9 is released (see JENKINS-15128)")
public void shouldJobRenameBeCorrectlyImpactedOnSCM() throws Throwable {
super.shouldJobRenameBeCorrectlyImpactedOnSCM();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void shouldReloadAllFilesUpdateScmAndReloadAllFiles() throws Throwable {

final File configJobFile = new File(checkoutDirectoryForVerifications.getAbsolutePath() + "/jobs/fakeJob/config.xml");
FileUtils.fileAppend(configJobFile.getAbsolutePath(), "titi");
scmManipulator.checkinFiles(checkoutDirectoryForVerifications, "external commit on jonb file");
scmManipulator.checkinFiles(checkoutDirectoryForVerifications, "external commit on job file");

verifyCurrentScmContentMatchesCurrentHudsonDir(false);

Expand All @@ -303,6 +303,7 @@ public void shouldReloadAllFilesUpdateScmAndReloadAllFiles() throws Throwable {

verifyCurrentScmContentMatchesCurrentHudsonDir(true);

assertAndRemoveHgSpecificFile(syncedFiles);
assertThat(syncedFiles.size(), is(2));
assertThat(syncedFiles.contains(new File(getCurrentHudsonRootDirectory().getAbsolutePath() + "/config.xml")), is(true));
assertThat(syncedFiles.contains(new File(getCurrentHudsonRootDirectory().getAbsolutePath() + "/jobs/fakeJob/config.xml")), is(true));
Expand Down Expand Up @@ -345,6 +346,7 @@ public void shouldReloadAllFilesUpdateScmAndReloadAllFilesWithFileAdd() throws T

verifyCurrentScmContentMatchesCurrentHudsonDir(true);

assertAndRemoveHgSpecificFile(syncedFiles);
assertThat(syncedFiles.size(), is(2));
assertThat(syncedFiles.contains(new File(getCurrentHudsonRootDirectory().getAbsolutePath() + "/myConfigFile.xml")), is(true));
assertThat(syncedFiles.contains(new File(getCurrentHudsonRootDirectory().getAbsolutePath() + "/jobs/myJob")), is(true));
Expand Down Expand Up @@ -378,4 +380,13 @@ private void assertStrategy(Class<? extends ScmSyncStrategy> expectedStrategyCla
}
}

private void assertAndRemoveHgSpecificFile(List<File> syncedFiles) {
// HG Support: If we encounter the dummy file used to initialise HG repo then we know
// we are dealing with a HG repo, so remove it so other tests can pass.
if(this instanceof HudsonExtensionsHgTest) {
File file = new File(getCurrentHudsonRootDirectory().getAbsolutePath() + "/hg_dummy.txt");
assertThat(syncedFiles.contains(file), is(true));
syncedFiles.remove(syncedFiles.indexOf(file));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package hudson.plugins.scm_sync_configuration.repository;

import hudson.plugins.test.utils.scms.ScmUnderTestHg;



public class InitRepositoryHgTest extends InitRepositoryTest {

public InitRepositoryHgTest() {
super(new ScmUnderTestHg());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ public void shouldResetCheckoutConfigurationDirectoryWhenAsked() throws Throwabl

// Reseting the repository, without cleanup
sscBusiness.initializeRepository(scmContext, false);
assertThat(fileWhichShouldBeDeletedAfterReset.exists(), is(true));
if (this instanceof InitRepositoryHgTest) {
// HG Support: HG behaves differently and always destroys files that aren't yet in source control.
// Note: Should this be allowed in the first place? If a reset is requested why should it
// allow untracked files to linger around?
assertThat(fileWhichShouldBeDeletedAfterReset.exists(), is(false));
} else {
assertThat(fileWhichShouldBeDeletedAfterReset.exists(), is(true));
}

// Reseting the repository with cleanup
sscBusiness.initializeRepository(scmContext, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ protected static List<Pattern> getSpecialSCMDirectoryExcludePattern(){
return new ArrayList<Pattern>(){{
add(Pattern.compile("\\.svn"));
add(Pattern.compile("\\.git.*"));
add(Pattern.compile("\\.hg.*"));
add(Pattern.compile("hg_dummy.txt"));
add(Pattern.compile("scm-sync-configuration\\..*\\.log"));
add(Pattern.compile("scm-sync-configuration"));
}};
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/hudson/plugins/test/utils/scms/ScmUnderTestHg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package hudson.plugins.test.utils.scms;

import hudson.plugins.scm_sync_configuration.scms.SCM;
import hudson.plugins.scm_sync_configuration.scms.ScmSyncHgSCM;

import java.io.File;

import org.codehaus.plexus.util.FileUtils;

public class ScmUnderTestHg implements ScmUnderTest {

public void initRepo(File path) throws Exception {
// HG support: Unlike Git, Mercurial does not appear to support "bare" repositories.
// So we have to initialise the repository by committing a dummy file.

final String dummyFileName = "hg_dummy.txt";
File dummyFile = new File(path, dummyFileName);
FileUtils.fileWrite(dummyFile.getAbsolutePath(), "This is a dummy file");

ProcessBuilder[] commands = new ProcessBuilder[] {
new ProcessBuilder("hg", "init"),
new ProcessBuilder("hg", "add", dummyFileName),
new ProcessBuilder("hg", "commit", "-m", "\"initial commit\"")
};

for (ProcessBuilder pb : commands) {
pb.directory(path);
Process p = pb.start();
if (p.waitFor() != 0) {
throw new Exception("Unable to init hg repo in " + path.getAbsolutePath());
}
}
}

public String createUrl(String url) {
return "scm:hg:" + url;
}

public Class<? extends SCM> getClazz() {
return ScmSyncHgSCM.class;
}

public boolean useCredentials() {
return false;
}

public String getSuffixForTestFiles() {
return ".hg";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson>
<version>1.339</version>
<numExecutors>2</numExecutors>
<mode>NORMAL</mode>
<useSecurity>true</useSecurity>
<authorizationStrategy class="hudson.security.FullControlOnceLoggedInAuthorizationStrategy"/>
<securityRealm class="hudson.security.HudsonPrivateSecurityRealm">
<disableSignup>true</disableSignup>
</securityRealm>
<systemMessage>Welcome !</systemMessage>
<jdks />
<clouds/>
<slaves/>
<quietPeriod>5</quietPeriod>
<scmCheckoutRetryCount>0</scmCheckoutRetryCount>
<views>
<hudson.model.AllView>
<owner class="hudson" reference="../../.."/>
<name>All</name>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
</hudson.model.AllView>
</views>
<primaryView>All</primaryView>
<slaveAgentPort>0</slaveAgentPort>
<label></label>
<nodeProperties/>
<globalNodeProperties/>
<disabledAdministrativeMonitors/>
</hudson>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson.tasks.Shell_-DescriptorImpl>
<helpRedirect/>
<shell>/bin/bash</shell>
</hudson.tasks.Shell_-DescriptorImpl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<hudson.plugins.scm__sync__configuration.ScmSyncConfigurationPlugin version="1">
<scmRepositoryUrl>scm:svn:https://myrepo/synchronizedDirectory/</scmRepositoryUrl>
<scm class="hudson.plugins.scm_sync_configuration.scms.ScmSyncSubversionSCM"/>
</hudson.plugins.scm__sync__configuration.ScmSyncConfigurationPlugin>