Skip to content

Commit

Permalink
[JENKINS-41974] Allow defining variables between brackets to avoid am…
Browse files Browse the repository at this point in the history
…biguity

This change maintains also the old way of defining variables for compatibility.
  • Loading branch information
mhuin committed Feb 13, 2017
1 parent eb2d3cf commit 1450c98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/main/java/jenkins/plugins/mqttnotification/MqttNotifier.java
Expand Up @@ -276,19 +276,23 @@ private String replaceVariables(final String rawString, final AbstractBuild buil
* @return a new String with variables replaced
*/
private String replaceStaticVariables(final String rawString, final AbstractBuild build) {
String result = rawString.replaceAll("\\$PROJECT_URL", build.getProject().getUrl());
String result = rawString.replaceAll("\\$\\{PROJECT_URL\\}", build.getProject().getUrl());
result = rawString.replaceAll("\\$PROJECT_URL", build.getProject().getUrl());
Result buildResult = build.getResult();
if (buildResult != null) {
result = result.replaceAll("\\$\\{BUILD_RESULT\\}", buildResult.toString());
result = result.replaceAll("\\$BUILD_RESULT", buildResult.toString());
}
result = result.replaceAll("\\$\\{BUILD_NUMBER\\}", Integer.toString(build.getNumber()));
result = result.replaceAll("\\$BUILD_NUMBER", Integer.toString(build.getNumber()));
if (rawString.contains("$CULPRITS")) {
if (rawString.contains("$\\{CULPRITS\\}") || rawString.contains("$CULPRITS")) {
StringBuilder culprits = new StringBuilder();
String delim = "";
for (Object userObject : build.getCulprits()) {
culprits.append(delim).append(userObject.toString());
delim = ",";
}
result = result.replaceAll("\\$\\{CULPRITS\\}", culprits.toString());
result = result.replaceAll("\\$CULPRITS", culprits.toString());
}
return result;
Expand All @@ -300,9 +304,11 @@ private String replaceEnvironmentVariables(final String rawString, final Abstrac
try {
EnvVars environment = build.getProject().getEnvironment(build.getBuiltOn(), listener);
for (Map.Entry<String, String> envVarEntry : environment.entrySet()) {
String key = "\\$" + envVarEntry.getKey();
String key1 = "\\$\\{" + envVarEntry.getKey() + "\\}";
String key2 = "\\$" + envVarEntry.getKey();
String value = envVarEntry.getValue();
result = result.replaceAll(key, value);
result = result.replaceAll(key1, value);
result = result.replaceAll(key2, value);
}
} catch (IOException ioe) {
logger.println("ERROR: Caught IOException while trying to replace environment variables: " + ioe.getMessage());
Expand All @@ -318,9 +324,11 @@ private String replaceBuildVariables(final String rawString, final AbstractBuild
String result = rawString;
Map<String, String> buildVarMap = build.getBuildVariables();
for (Map.Entry<String, String> buildVarEntry : buildVarMap.entrySet()) {
String key = "\\$" + buildVarEntry.getKey();
String key1 = "\\$\\{" + buildVarEntry.getKey() + "\\}";
String key2 = "\\$" + buildVarEntry.getKey();
String value = buildVarEntry.getValue();
result = result.replaceAll(key, value);
result = result.replaceAll(key1, value);
result = result.replaceAll(key2, value);
}
return result;
}
Expand Down
@@ -1,5 +1,5 @@
<p>The "payload" for the MQTT message.</p>
<p>In addition to environment variables and build parameters, the following variables can also be used (all variables must be prefix with "$"):
<p>In addition to environment variables and build parameters, the following variables can also be used (all variables must be prefix with "$", or encapsulated within "${}"):
<ul>
<li>BUILD_RESULT - The result of the build (e.g. SUCCESS, FAILURE, ABORTED, etc.)</li>
<li>PROJECT_URL - The job project's URL (e.g. "job/my-build")</li>
Expand Down

0 comments on commit 1450c98

Please sign in to comment.