Skip to content

Commit

Permalink
Contributing the actual JSON/XML #30
Browse files Browse the repository at this point in the history
* Will now contribute the actual JSON, or XML, in the configured variable, if expression does not resolve to a string. So that it is possible to contribute the, or a part of, the JSON/XML that was read.
* Correctly readning streams with UTF-8, not default encodings.
* Adding Violations Maven Plugin to monitor Findbugs findings.
  • Loading branch information
tomasbjerre committed Jan 3, 2018
1 parent 21733a0 commit 4a60c3c
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 73 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Generic Webhook Plugin Changelog
Changelog of Generic Webhook Plugin.
## Unreleased
### GitHub [#30](https://github.com/jenkinsci/generic-webhook-trigger-plugin/issues/30) Request: Supporting of JSON header without flattening

**Contributing the actual JSON/XML #30**

* Will now contribute the actual JSON, or XML, in the configured variable, if expression does not resolve to a string. So that it is possible to contribute the, or a part of, the JSON/XML that was read.
* Correctly readning streams with UTF-8, not default encodings.
* Adding Violations Maven Plugin to monitor Findbugs findings.

[bb32a7cc82d7174](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/bb32a7cc82d7174) Tomas Bjerre *2018-01-03 12:10:05*


## 1.21 (2017-12-20 18:52:26)
### GitHub [#26](https://github.com/jenkinsci/generic-webhook-trigger-plugin/issues/26) JSONPath processing seems to not handle "," operator

**Test case with comma operator #26**
Expand All @@ -14,7 +26,7 @@ Changelog of Generic Webhook Plugin.
**More information in log if variable cannot be resolved #29**


[41d34ee48f05639](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/41d34ee48f05639) Tomas Bjerre *2017-12-20 18:49:24*
[19443b4e072acc4](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/19443b4e072acc4) Tomas Bjerre *2017-12-20 18:51:29*


### No issue
Expand Down
36 changes: 35 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -122,6 +123,7 @@
<gitHubToken>${GITHUB_OAUTH2TOKEN}</gitHubToken>
<gitHubIssuePattern>#([0-9]*)</gitHubIssuePattern>
<file>CHANGELOG.md</file>
<jiraIssuePattern>\bJENKINS-([0-9]+)\b</jiraIssuePattern>
<templateContent>
<![CDATA[
# Generic Webhook Plugin Changelog
Expand Down Expand Up @@ -172,6 +174,33 @@ Changelog of Generic Webhook Plugin.
</execution>
</executions>
</plugin>
<plugin>
<groupId>se.bjurr.violations</groupId>
<artifactId>violations-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<minSeverity>INFO</minSeverity>
<detailLevel>VERBOSE</detailLevel>
<maxViolations>99999999</maxViolations>
<printViolations>true</printViolations>
<violations>
<violation>
<parser>FINDBUGS</parser>
<reporter>Findbugs</reporter>
<folder>.</folder>
<pattern>.*/findbugsXml.*\.xml$</pattern>
</violation>
</violations>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>violations</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
Expand All @@ -198,6 +227,11 @@ Changelog of Generic Webhook Plugin.
<artifactId>json-path</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>

<!-- test // -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package org.jenkinsci.plugins.gwt;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.TaskListener;
import hudson.model.EnvironmentContributor;
import hudson.model.Run;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

import javax.annotation.Nonnull;

import com.google.common.base.Charsets;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.Run;
import hudson.model.TaskListener;

@Extension
public class GenericWebhookEnvironmentContributor extends EnvironmentContributor {
private static final String CONTRIBUTING_VARIABLES = "Contributing variables:";

@SuppressWarnings("unchecked")
@Override
public void buildEnvironmentFor(
@SuppressWarnings("rawtypes") @Nonnull Run r,
@Nonnull EnvVars envs,
@Nonnull TaskListener listener)
@SuppressWarnings("rawtypes") @Nonnull final Run r,
@Nonnull final EnvVars envs,
@Nonnull final TaskListener listener)
throws IOException, InterruptedException {
boolean shouldLog = shouldLog(r);
GenericCause cause = (GenericCause) r.getCause(GenericCause.class);
final boolean shouldLog = shouldLog(r);
final GenericCause cause = (GenericCause) r.getCause(GenericCause.class);
if (cause != null) {
if (shouldLog) {
listener
Expand All @@ -36,12 +39,12 @@ public void buildEnvironmentFor(
+ cause.getPostContent()
+ "\n\n");
}
Map<String, String> resolvedVariables = cause.getResolvedVariables();
final Map<String, String> resolvedVariables = cause.getResolvedVariables();
if (shouldLog) {
listener.getLogger().println(CONTRIBUTING_VARIABLES + "\n");
}
for (String variable : resolvedVariables.keySet()) {
String resolved = cause.getResolvedVariables().get(variable);
for (final String variable : resolvedVariables.keySet()) {
final String resolved = cause.getResolvedVariables().get(variable);
if (shouldLog) {
listener.getLogger().println(" " + variable + " = " + resolved);
}
Expand All @@ -53,8 +56,10 @@ public void buildEnvironmentFor(
}
}

private boolean shouldLog(@SuppressWarnings("rawtypes") Run r) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(r.getLogFile()))) {
private boolean shouldLog(@SuppressWarnings("rawtypes") final Run r) throws IOException {
try (BufferedReader br =
new BufferedReader(
new InputStreamReader(new FileInputStream(r.getLogFile()), Charsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
if (line.contains(CONTRIBUTING_VARIABLES)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,51 @@
import java.util.Map;
import java.util.Map.Entry;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonFlattener {
private static Gson GSON = new GsonBuilder().create();

public JsonFlattener() {}

public Map<String, String> flattenJson(
final String key, final String regexFilter, final Object resolved) {
final Map<String, String> resolvedVariables = newHashMap();
doFlatternJson(key, regexFilter, resolved, resolvedVariables);

if (resolved != null && !(resolved instanceof String)) {
final String variableName = toVariableName(key);
resolvedVariables.put(variableName, filter(GSON.toJson(resolved).toString(), regexFilter));
}

return resolvedVariables;
}

@SuppressWarnings("unchecked")
public Map<String, String> flattenJson(String key, String regexFilter, Object resolved) {
Map<String, String> resolvedVariables = newHashMap();
private void doFlatternJson(
final String key,
final String regexFilter,
final Object resolved,
final Map<String, String> resolvedVariables) {
if (resolved instanceof List) {
int i = 0;
for (Object o : (List<?>) resolved) {
resolvedVariables.putAll(flattenJson(key + "_" + i, regexFilter, o));
for (final Object o : (List<?>) resolved) {
doFlatternJson(key + "_" + i, regexFilter, o, resolvedVariables);
i++;
}
} else if (resolved instanceof Map) {
for (Entry<String, Object> entry : ((Map<String, Object>) resolved).entrySet()) {
resolvedVariables.putAll(
flattenJson(key + "_" + entry.getKey(), regexFilter, entry.getValue()));
for (final Entry<String, Object> entry : ((Map<String, Object>) resolved).entrySet()) {
doFlatternJson(
key + "_" + entry.getKey(), regexFilter, entry.getValue(), resolvedVariables);
}
} else if (resolved != null) {
String variableName = toVariableName(key);
resolvedVariables.put(variableName, filter(resolved.toString(), regexFilter));
final String variableName = toVariableName(key);
String string = resolved.toString();
if (!(resolved instanceof String)) {
string = GSON.toJson(resolved).toString();
}
resolvedVariables.put(variableName, filter(string, regexFilter));
}
return resolvedVariables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@
import static org.jenkinsci.plugins.gwt.ExpressionType.XPath;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.jenkinsci.plugins.gwt.GenericVariable;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.google.common.base.Charsets;
import com.jayway.jsonpath.JsonPath;

public class PostContentParameterResolver {
Expand All @@ -38,7 +35,7 @@ public class PostContentParameterResolver {
public PostContentParameterResolver() {}

public Map<String, String> getPostContentParameters(
List<GenericVariable> configuredGenericVariables, String incomingPostContent) {
final List<GenericVariable> configuredGenericVariables, final String incomingPostContent) {
final Map<String, String> resolvedVariables = newHashMap();
if (configuredGenericVariables != null) {
for (final GenericVariable gv : configuredGenericVariables) {
Expand All @@ -48,7 +45,7 @@ public Map<String, String> getPostContentParameters(
return resolvedVariables;
}

private Map<String, String> resolve(String incomingPostContent, GenericVariable gv) {
private Map<String, String> resolve(final String incomingPostContent, final GenericVariable gv) {
try {
if (gv != null && gv.getExpression() != null && !gv.getExpression().isEmpty()) {
if (gv.getExpressionType() == JSONPath) {
Expand All @@ -59,7 +56,7 @@ private Map<String, String> resolve(String incomingPostContent, GenericVariable
throw new IllegalStateException("Not recognizing " + gv.getExpressionType());
}
}
} catch (final Exception e) {
} catch (final Throwable e) {
LOGGER.log(
INFO,
"Unable to resolve "
Expand All @@ -75,17 +72,18 @@ private Map<String, String> resolve(String incomingPostContent, GenericVariable
return new HashMap<>();
}

private Map<String, String> resolveJsonPath(String incomingPostContent, GenericVariable gv) {
private Map<String, String> resolveJsonPath(
final String incomingPostContent, final GenericVariable gv) {
final Object resolved = JsonPath.read(incomingPostContent, gv.getExpression());
return jsonFlattener.flattenJson(gv.getVariableName(), gv.getRegexpFilter(), resolved);
}

private Map<String, String> resolveXPath(String incomingPostContent, GenericVariable gv)
throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
private Map<String, String> resolveXPath(
final String incomingPostContent, final GenericVariable gv) throws Exception {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final InputSource inputSource =
new InputSource(new ByteArrayInputStream(incomingPostContent.getBytes()));
new InputSource(new ByteArrayInputStream(incomingPostContent.getBytes(Charsets.UTF_8)));
final Document doc = builder.parse(inputSource);
final XPathFactory xPathfactory = XPathFactory.newInstance();
final XPath xpath = xPathfactory.newXPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,58 @@
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.TEXT_NODE;

import java.io.StringWriter;
import java.util.Map;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jenkinsci.plugins.gwt.GenericVariable;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlFlattener {
public XmlFlattener() {}

public Map<String, String> flatternXmlNode(GenericVariable gv, NodeList nodeList) {
public Map<String, String> flatternXmlNode(final GenericVariable gv, final NodeList nodeList)
throws Exception {
final Map<String, String> resolvedVariables = newHashMap();
if (nodeList.getLength() > 0) {
final boolean singleElementInNodeList = nodeList.getLength() == 1 ? true : false;
resolvedVariables.put(gv.getVariableName(), toXmlString(nodeList.item(0)));
for (int i = 0; i < nodeList.getLength(); i++) {
final boolean fromRootLevel = nodeList.getLength() == 1 ? true : false;
resolvedVariables.putAll(
flattenXmlNode(
gv.getVariableName(), gv.getRegexpFilter(), nodeList.item(i), i, fromRootLevel));
gv.getVariableName(),
gv.getRegexpFilter(),
nodeList.item(i),
i,
singleElementInNodeList));
}
} else {
resolvedVariables.put(gv.getVariableName(), "");
}
return resolvedVariables;
}

private String toXmlString(final Node elem) throws Exception {
final StringWriter buf = new StringWriter();
final Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xform.setOutputProperty(OutputKeys.INDENT, "yes");
xform.transform(new DOMSource(elem), new StreamResult(buf));
return buf.toString();
}

private Map<String, String> flattenXmlNode(
String parentKey, String regexFilter, Node node, int level, boolean fromRootLevel) {
final String parentKey,
final String regexFilter,
final Node node,
final int level,
final boolean fromRootLevel) {
final Map<String, String> resolvedVariables = newHashMap();
if (isXmlLeafNode(node)) {
final String noWhitespaces = toVariableName(parentKey);
Expand All @@ -57,14 +83,14 @@ private Map<String, String> flattenXmlNode(
return resolvedVariables;
}

private boolean isXmlLeafNode(Node node) {
private boolean isXmlLeafNode(final Node node) {
return node != null
&& (node.getNodeType() == ELEMENT_NODE || node.getNodeType() == ATTRIBUTE_NODE)
&& node.getChildNodes().getLength() == 1
&& node.getFirstChild().getNodeType() == TEXT_NODE;
}

private String expandKey(String key, int level, boolean fromRootLevel) {
private String expandKey(final String key, final int level, final boolean fromRootLevel) {
if (fromRootLevel) {
return key;
} else {
Expand Down
Loading

0 comments on commit 4a60c3c

Please sign in to comment.