Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from synopsys-arc-oss/JENKINS-26119-fix
[FIXED JENKINS-26119] - Restore the legacy variable escaping approach
- Loading branch information
Showing
5 changed files
with
215 additions
and
64 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/java/hudson/plugins/perforce/utils/JobSubstitutionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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.MatrixConfiguration; | ||
import hudson.matrix.MatrixProject; | ||
import hudson.model.Job; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.AbstractProject; | ||
import hudson.model.Hudson; | ||
import hudson.model.ParameterDefinition; | ||
import hudson.model.ParameterValue; | ||
import hudson.model.ParametersDefinitionProperty; | ||
import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
import hudson.slaves.NodeProperty; | ||
import java.util.Map; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Default substitutions for {@link Job}s and {@link Build}s. | ||
* @author Oleg Nenashev <o.v.nenashev@gmail.com> | ||
* @since 1.3.32 | ||
*/ | ||
public class JobSubstitutionHelper { | ||
|
||
/**package*/ static void getDefaultSubstitutions( | ||
@Nonnull AbstractProject project, | ||
@Nonnull Map<String, String> subst) { | ||
|
||
subst.put("JOB_NAME", JobSubstitutionHelper.getSafeJobName(project)); | ||
String rootUrl = Hudson.getInstance().getRootUrl(); | ||
if (rootUrl != null) { | ||
subst.put("JOB_URL", rootUrl + project.getUrl()); | ||
} | ||
|
||
for (NodeProperty nodeProperty : Hudson.getInstance().getGlobalNodeProperties()) { | ||
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) { | ||
subst.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars()); | ||
} | ||
} | ||
ParametersDefinitionProperty pdp = (ParametersDefinitionProperty) project.getProperty(hudson.model.ParametersDefinitionProperty.class); | ||
if (pdp != null) { | ||
for (ParameterDefinition pd : pdp.getParameterDefinitions()) { | ||
try { | ||
ParameterValue defaultValue = pd.getDefaultParameterValue(); | ||
if (defaultValue != null) { | ||
String name = defaultValue.getName(); | ||
String value = defaultValue.createVariableResolver(null).resolve(name); | ||
subst.put(name, value); | ||
} | ||
} catch (Exception e) { | ||
// Do nothing | ||
} | ||
} | ||
} | ||
|
||
// Handle Matrix Axes | ||
if (project instanceof MatrixConfiguration) { | ||
MatrixConfiguration matrixConfiguration = (MatrixConfiguration) project; | ||
subst.putAll(matrixConfiguration.getCombination()); | ||
} | ||
if (project instanceof MatrixProject) { | ||
MatrixProject matrixProject = (MatrixProject) project; | ||
for (Axis axis : matrixProject.getAxes()) { | ||
subst.put(axis.name, axis.size() >0 ? axis.value(0) : ""); | ||
} | ||
} | ||
} | ||
|
||
public static String getSafeJobName(@Nonnull AbstractBuild build) { | ||
return getSafeJobName(build.getProject()); | ||
} | ||
|
||
public static String getSafeJobName(@Nonnull AbstractProject project) { | ||
return project.getFullName().replace('/', '-').replace('=', '-').replace(',', '-'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/java/hudson/plugins/perforce/utils/JobSubstitutionHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(",")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters