Skip to content

Commit

Permalink
[FIXED JENKINS-26119] - Inject custom JOB_NAME during the environment…
Browse files Browse the repository at this point in the history
… substitution

Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Dec 27, 2014
1 parent e02ccbb commit 7381efb
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
Expand Up @@ -90,11 +90,11 @@ public class JobSubstitutionHelper {
}
}

/**package*/ static String getSafeJobName(@Nonnull AbstractBuild build) {
public static String getSafeJobName(@Nonnull AbstractBuild build) {
return getSafeJobName(build.getProject());
}

/**package*/ static String getSafeJobName(@Nonnull AbstractProject project) {
public static String getSafeJobName(@Nonnull AbstractProject project) {
return project.getFullName().replace('/', '-').replace('=', '-').replace(',', '-');
}
}
Expand Up @@ -25,20 +25,12 @@
package hudson.plugins.perforce.utils;

import hudson.EnvVars;
import hudson.matrix.Axis;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.TaskListener;
import hudson.plugins.perforce.PerforceSCM;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -273,6 +265,11 @@ private static String substituteParametersNoCheck(
}
String result = inputString;

// Substitute variables with custom values (escaping, etc)
final Map<String, String> customVars = new TreeMap<String, String>();
customVars.put("JOB_NAME", JobSubstitutionHelper.getSafeJobName(build));
result = MacroStringHelper.substituteParametersNoCheck(result, customVars);

// Try to build the full environment. Nested calls count is handled in PerforceSCM::buildEnvVars()
Map<String, String> environmentVarsFromExtensions = new TreeMap<String, String>();
try {
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/hudson/plugins/perforce/PerforceSCMTest.java
@@ -1,5 +1,12 @@
package hudson.plugins.perforce;

import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.Combination;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.MatrixRun;
import hudson.model.AbstractProject;
import hudson.model.FreeStyleBuild;
import hudson.plugins.perforce.config.DepotType;
import hudson.model.FreeStyleProject;
Expand All @@ -13,9 +20,13 @@
import hudson.plugins.perforce.config.CleanTypeConfig;
import hudson.plugins.perforce.config.MaskViewConfig;
import hudson.plugins.perforce.config.WorkspaceCleanupConfig;
import hudson.plugins.perforce.utils.JobSubstitutionHelper;
import static hudson.plugins.perforce.utils.JobSubstitutionHelperTest.assertNoSpecialSymbols;
import hudson.plugins.perforce.utils.MacroStringHelper;
import hudson.plugins.perforce.utils.ParameterSubstitutionException;
import hudson.slaves.DumbSlave;
import hudson.tools.ToolProperty;
import java.io.IOException;
import java.net.MalformedURLException;

import java.net.URL;
Expand All @@ -26,6 +37,7 @@
import java.util.concurrent.Future;
import javax.annotation.Nonnull;
import static junit.framework.Assert.assertNotNull;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;

import org.jvnet.hudson.test.HudsonTestCase;
Expand Down Expand Up @@ -472,6 +484,45 @@ public void testExecutorNumberSubstitutionInClientName() throws Exception {
slave.getNodeName(), 0, // first executor
build.getBuiltOn().getNodeName().hashCode()), build);
}

@Bug(26119)
public void testSubstituteVarsForMatrixAxis()
throws Exception, InterruptedException {

final String CLIENTNAME_FORMAT = "test_%s";

// Tool stub
PerforceToolInstallation stubInstallation = new PerforceToolInstallation("p4_stub", "echo", new LinkedList<ToolProperty<?>>());
PerforceToolInstallation.DescriptorImpl descriptor = (PerforceToolInstallation.DescriptorImpl) Hudson.getInstance().getDescriptor(PerforceToolInstallation.class);
descriptor.setInstallations(new PerforceToolInstallation[] { stubInstallation });
descriptor.save();

// Project initialization
final MatrixProject prj = createMatrixProject("test");
PerforceSCM scm = createPerforceSCMStub();
scm.setP4Client(String.format(CLIENTNAME_FORMAT, "${JOB_NAME}"));
scm.setP4Tool("p4_stub");
prj.setScm(scm);
final AxisList axes = new AxisList(new Axis("TEST_AXIS", "val1", "val2"));
prj.setAxes(axes);

final Combination configuration = new Combination(axes, "val1");

// Run MatrixBuild
Future<MatrixBuild> fBuild = prj.scheduleBuild2(0);
assertNotNull(fBuild);
final MatrixBuild build = fBuild.get();

// Check JobSubstitutionHelper::getSafeJobName()
MatrixRun run = build.getRun(configuration);
final String safeConfigName = JobSubstitutionHelper.getSafeJobName(prj.getItem(configuration));
assertNoSpecialSymbols(safeConfigName);
assertLogContains(safeConfigName, run);

// Check substitution via explicit call
String substitutedJobName = MacroStringHelper.substituteParameters("${JOB_NAME}", scm, run, null);
assertEquals(safeConfigName, substitutedJobName);
}

/**
* Creates {@link PerforceSCM} with default fields.
Expand Down
@@ -0,0 +1,49 @@
/*
* The MIT License
*
* Copyright 2014 Oleg Nenashev <o.v.nenashev@gmail.com>.
*
* 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.
*/

package hudson.plugins.perforce.utils;

import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.Combination;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractProject;
import java.io.IOException;
import javax.annotation.Nonnull;
import junit.framework.Assert;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;

/**
*
* @author Oleg Nenashev <o.v.nenashev@gmail.com>
*/
public class JobSubstitutionHelperTest {

public static void assertNoSpecialSymbols(@Nonnull String value) {
Assert.assertFalse(value.contains("/"));
Assert.assertFalse(value.contains("="));
Assert.assertFalse(value.contains(","));
}
}
Expand Up @@ -21,11 +21,18 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.synopsys.arc.jenkinsci.plugins.perforce;
package hudson.plugins.perforce.utils;

import hudson.EnvVars;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.Combination;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractProject;
import hudson.plugins.perforce.utils.JobSubstitutionHelper;
import hudson.plugins.perforce.utils.MacroStringHelper;
import hudson.plugins.perforce.utils.ParameterSubstitutionException;
import java.io.IOException;
import javax.annotation.Nonnull;
import junit.framework.Assert;
import org.junit.Test;
Expand Down

0 comments on commit 7381efb

Please sign in to comment.