Skip to content

Commit

Permalink
Merge pull request #16 from alexouzounis/JENKINS-24735
Browse files Browse the repository at this point in the history
[FIXED JENKINS-24735] - Add support for build parameters in Subversion URLs.
  • Loading branch information
hugueschabot committed Oct 24, 2014
2 parents fc6fe53 + c1c458c commit d845cf0
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.466</version>
<version>1.509</version>
</parent>

<artifactId>svnmerge</artifactId>
Expand Down Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>subversion</artifactId>
<version>2.2</version>
<version>2.4.4</version>
</dependency>
</dependencies>

Expand Down
@@ -1,9 +1,11 @@
package jenkins.plugins.svnmerge;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath.FileCallable;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Item;
Expand All @@ -17,6 +19,8 @@
import hudson.scm.SubversionSCM;
import hudson.scm.SvnClientManager;
import hudson.scm.SubversionSCM.ModuleLocation;
import hudson.slaves.NodeProperty;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.util.IOException2;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
Expand Down Expand Up @@ -97,7 +101,10 @@ public ModuleLocation getUpstreamSubversionLocation() {
SCM scm = p.getScm();
if (scm instanceof SubversionSCM) {
SubversionSCM svn = (SubversionSCM) scm;
return svn.getLocations()[0];
ModuleLocation ml = svn.getLocations()[0];
// expand system and node environment variables as well as the project parameters
ml = Utility.getExpandedLocation(ml, p);
return ml;
}
return null;
}
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
Expand Down Expand Up @@ -89,21 +90,23 @@ public RepositoryLayoutInfo getRepositoryLayout() {
//TODO: check for multiple locations ?
SubversionSCM svn = (SubversionSCM) scm;
ModuleLocation firstLocation = svn.getLocations()[0];
// expand system and node environment variables as well as the project parameters
firstLocation = Utility.getExpandedLocation(firstLocation, project);
return getRepositoryLayout(firstLocation);
}

private RepositoryLayoutInfo getRepositoryLayout(ModuleLocation location) {
return new RepositoryLayoutInfo(location.getURL());
}

@RequirePOST
public void doNewBranch(StaplerRequest req, StaplerResponse rsp,
@QueryParameter String name,
@QueryParameter boolean attach,
@QueryParameter String commitMessage,
@QueryParameter String branchLocation,
@QueryParameter boolean createTag,
@QueryParameter String tagLocation) throws ServletException, IOException {
requirePOST();

name = Util.fixEmptyAndTrim(name);

Expand All @@ -127,7 +130,9 @@ public void doNewBranch(StaplerRequest req, StaplerResponse rsp,
// TODO: check for multiple locations
SubversionSCM svn = (SubversionSCM) scm;
ModuleLocation firstLocation = svn.getLocations()[0];

// expand system and node environment variables as well as the project parameters
firstLocation = Utility.getExpandedLocation(firstLocation, project);

RepositoryLayoutInfo layoutInfo = getRepositoryLayout(firstLocation);

branchLocation = Util.fixEmptyAndTrim(branchLocation);
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/jenkins/plugins/svnmerge/Utility.java
@@ -0,0 +1,56 @@
package jenkins.plugins.svnmerge;

import hudson.EnvVars;
import hudson.model.Computer;
import hudson.model.Job;
import hudson.scm.SubversionSCM.ModuleLocation;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Utility {


/**
* Expands the system variables, the node environment variables and the project parameters
*/
public static ModuleLocation getExpandedLocation(ModuleLocation ml, Job<?,?> project) {
ModuleLocation location= ml.getExpandedLocation(project);
// expand system variables
Computer c = Computer.currentComputer();
if (c != null) {
try {
// JVM vars
EnvVars cEnv = c.getEnvironment();
location = location.getExpandedLocation(cEnv);
// node vars
for (NodeProperty<?> nodeProp : c.getNode().getNodeProperties()) {
if (nodeProp instanceof EnvironmentVariablesNodeProperty) {
EnvVars nodeEnvVars = ((EnvironmentVariablesNodeProperty) nodeProp)
.getEnvVars();
location = location
.getExpandedLocation(nodeEnvVars);

}
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to get computer environment",
e);
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "Failed to get computer environment",
e);

}
}
// expand project variables
if(project!=null){
location = location.getExpandedLocation(project);
}
return location;
}

private static final Logger LOGGER = Logger.getLogger(Utility.class.getName());
}
@@ -1,7 +1,27 @@
package jenkins.plugins.svnmerge;

import jenkins.model.Jenkins;

import org.apache.commons.collections.iterators.EntrySetMapIterator;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.HudsonTestCase;

import hudson.EnvVars;
import hudson.model.FreeStyleBuild;
import hudson.model.JobProperty;
import hudson.model.Cause;
import hudson.model.Computer;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.scm.SubversionSCM;
import hudson.slaves.NodeProperty;
import hudson.slaves.EnvironmentVariablesNodeProperty;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
Expand All @@ -24,4 +44,23 @@ public void testConfigRoundtrip2() throws Exception {
assertNotNull(ujp);
assertEquals("xyz",ujp.getUpstream());
}

@Bug(24735)
public void testUpStreamURLwithParams()
throws Exception {
EnvironmentVariablesNodeProperty envNodeProp = new EnvironmentVariablesNodeProperty(new hudson.slaves.EnvironmentVariablesNodeProperty.Entry("ROOT_SVN_URL", "root/"));
Computer.currentComputer().getNode().getNodeProperties().add(envNodeProp);
Jenkins.getInstance().getDescriptorList(JobProperty.class).add(new FeatureBranchProperty.DescriptorImpl());
FreeStyleProject p = createFreeStyleProject();
p.setScm(new SubversionSCM("https://${ROOT_SVN_URL}${REPO}/${PROJECT}/trunk"));
ParameterDefinition def1 = new StringParameterDefinition("REPO", "a");
ParameterDefinition def2 = new StringParameterDefinition("PROJECT", "b");
p.addProperty(new ParametersDefinitionProperty(def1,def2));
FeatureBranchProperty jobProp = new FeatureBranchProperty(p.getName());
FreeStyleProject p2 = createFreeStyleProject();
p2.addProperty(jobProp);
assertEquals( "https://root/a/b/trunk",jobProp.getUpstreamURL().toDecodedString());
}


}

0 comments on commit d845cf0

Please sign in to comment.