Skip to content

Commit

Permalink
JENKINS-41839 Substitue build parameters, number in topic and payload
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuin committed Feb 8, 2017
1 parent b165068 commit 1510113
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
25 changes: 24 additions & 1 deletion src/main/java/jenkins/plugins/mqttnotification/MqttNotifier.java
Expand Up @@ -247,7 +247,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
}

/**
* Replace both static and environment variables defined in the given rawString
* Replace both static and environment variables, build parameters defined in the given rawString
* @param rawString The string containing variables to be replaced
* @param build The current build
* @param listener The current buildListener
Expand All @@ -256,6 +256,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
private String replaceVariables(final String rawString, final AbstractBuild build, BuildListener listener) {
String result = replaceStaticVariables(rawString, build);
result = replaceEnvironmentVariables(result, build, listener);
result = replaceBuildVariables(result, build, listener);
return result;
}

Expand All @@ -265,6 +266,7 @@ private String replaceVariables(final String rawString, final AbstractBuild buil
* <li>BUILD_RESULT</li> The build result (e.g. SUCCESS)
* <li>PROJECT_URL</li> The URL to the project
* <li>CULPRITS</li> The culprits responsible for the build
* <li>BUILD_NUMBER</li> The build number
* </ul>
* @param rawString The string containing variables to be replaced
* @param build The current build
Expand All @@ -273,6 +275,8 @@ private String replaceVariables(final String rawString, final AbstractBuild buil
private String replaceStaticVariables(final String rawString, final AbstractBuild build) {
String result = rawString.replaceAll("\\$PROJECT_URL", build.getProject().getUrl());
result = result.replaceAll("\\$BUILD_RESULT", build.getResult().toString());
String number = "" + build.number;
result = result.replaceAll("\\$BUILD_NUMBER", number);
if (rawString.contains("$CULPRITS")) {
StringBuilder culprits = new StringBuilder();
String delim = "";
Expand Down Expand Up @@ -312,4 +316,23 @@ private String replaceEnvironmentVariables(final String rawString, final Abstrac
return result;
}

/**
*
* @param rawString The string containing variables to be replaced
* @param build The current build
* @param listener The current buildListener
* @return a new String with variables replaced
*/
private String replaceBuildVariables(final String rawString, final AbstractBuild build, BuildListener listener) {
final PrintStream logger = listener.getLogger();
String result = rawString;
Map<String, String> buildVarMap = build.getBuildVariables();
for (Map.Entry<String, String> buildVarEntry : buildVarMap.entrySet()) {
String key = "\\$" + buildVarEntry.getKey();
String value = buildVarEntry.getValue();
result = result.replaceAll(key, value);
}
return result;
}

}
@@ -1,7 +1,8 @@
<p>The "payload" for the MQTT message.</p>
<p>In addition to environment variables, 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 "$"):
<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>
<li>CULPRITS - Comma-separated list of the culprits responsible for the current build result</li>
</ul></p>
<li>BUILD_NUMBER - the build number</li>
</ul></p>
@@ -1,8 +1,9 @@
<p>The topic to which to publish the notification.
If no value is specified then the default value ("jenkins/$JOB_URL" e.g. "jenkins/job/my-build") is used.</p>
<p>In addition to environment variables, 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 "$"):
<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>
<li>CULPRITS - Comma-separated list of the culprits responsible for the current build result</li>
</ul></p>
<li>BUILD_NUMBER - the build number</li>
</ul></p>

0 comments on commit 1510113

Please sign in to comment.