Skip to content

Commit

Permalink
[JENKINS-54649] Add trigger response as environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Falkenstern authored and falkena committed Oct 19, 2021
1 parent b17ceab commit 1ba8cf8
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 31 deletions.
8 changes: 8 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ Plugin](https://plugins.jenkins.io/fstrigger/)

![configuration screen](docs/images/URLTRIGGER_CONF_1.png)

## Notes

To determine the URL caused invocation - use the environment variable
`URL_TRIGGER_CAUSE` - e.g. `${env.URL_TRIGGER_CAUSE}`

To determine the response caused invocation - use the environment variable
`URL_TRIGGER_RESPONSE` - e.g. `${env.URL_TRIGGER_RESPONSE}`

## Declarative Pipeline Syntax
Example:

Expand Down
18 changes: 6 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.12</version>
<version>4.27</version>
</parent>

<artifactId>urltrigger</artifactId>
Expand Down Expand Up @@ -41,14 +41,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8.0</java.version>
<java.level>8</java.level>
<xtrigger.lib.version>0.35</xtrigger.lib.version>
<jersey.client.version>1.19.4</jersey.client.version>
<json.path.version>2.4.0</json.path.version>
<json.path.version>2.6.0</json.path.version>
<jackson.mapper.as1.version>1.8.3</jackson.mapper.as1.version>
<mockito.version>1.8.5</mockito.version>
<jenkins.version>2.204.6</jenkins.version>
<jenkins.version>2.263.4</jenkins.version>
</properties>

<scm>
Expand All @@ -73,8 +72,8 @@
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.204.x</artifactId>
<version>16</version>
<artifactId>bom-2.263.x</artifactId>
<version>950.v396cb834de1e</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down Expand Up @@ -142,12 +141,7 @@
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>3.8.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws XTri
if (modified) {
this.buildCause = new URLTriggerCause( true ) ;
this.buildCause.setUrlTrigger(entry.getUrl());
for(final URLTriggerContentType contentType: entry.getContentTypes()) {
this.buildCause.addTriggerResponse(contentType.getTriggeringResponse());
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jenkinsci.plugins.urltrigger;

import java.io.Serializable;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.lib.xtrigger.XTriggerCause;

import hudson.model.Cause;
Expand All @@ -18,6 +20,7 @@ public class URLTriggerCause extends XTriggerCause implements Serializable {

public static final String NAME = "URLTrigger";
public static final String CAUSE = "A change within the response URL invocation";
private Map<String, String> triggerResponse;
private String urlTrigger;

protected URLTriggerCause() {
Expand All @@ -29,7 +32,19 @@ protected URLTriggerCause( boolean logEnabled ) {
}

protected URLTriggerCause(String triggerName, String causeFrom, boolean logEnabled) {
super( triggerName , causeFrom , logEnabled ) ;
super(triggerName , causeFrom , logEnabled) ;
}

public void addTriggerResponse(Map<String, String> response) {
if (triggerResponse != null) {
triggerResponse.putAll(response);
} else {
setTriggerResponse(response);
}
}

public void setTriggerResponse(Map<String, String> response) {
triggerResponse = response;
}

public void setUrlTrigger(String url) {
Expand All @@ -41,6 +56,14 @@ public String getShortDescription() {
return String.format("[%s] - %s of %s", NAME, CAUSE, urlTrigger);
}

public String getTriggerResponse() {
List<String> result = new ArrayList<>();
if (triggerResponse != null) {
triggerResponse.forEach((key, value) -> result.add(String.format("\"%s\": %s", key, value)));
}
return StringUtils.joinWith(",", result);
}

public String getUrlTrigger() {
return urlTrigger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class JSONContentType extends URLTriggerContentType {

private transient Map<String, Object> results = null;

private List<JSONContentEntry> jsonPaths = new ArrayList<JSONContentEntry>();
private List<JSONContentEntry> jsonPaths = new ArrayList<>();

@DataBoundConstructor
public JSONContentType(List<JSONContentEntry> jsonPaths) {
Expand All @@ -51,7 +51,7 @@ protected void initForContentType(String content, XTriggerLog log) throws XTrigg
}

private Map<String, Object> readJsonPath(String content) throws XTriggerException {
Map<String, Object> results = new HashMap<String, Object>(jsonPaths.size());
Map<String, Object> results = new HashMap<>(jsonPaths.size());
for (JSONContentEntry jsonContentEntry : jsonPaths) {
String jsonPath = jsonContentEntry.getJsonPath();
try {
Expand All @@ -66,6 +66,15 @@ private Map<String, Object> readJsonPath(String content) throws XTriggerExceptio
return results;
}

@Override
public Map<String, String> getTriggeringResponse() {
Map<String, String> payload = new HashMap<>();
if (results != null) {
results.forEach((key, value) -> payload.put(key, value.toString()));
}
return payload;
}

@Override
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {

Expand Down Expand Up @@ -96,7 +105,6 @@ protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) t
}

for (Map.Entry<String, Object> entry : results.entrySet()) {

String jsonPath = entry.getKey();
Object initValue = entry.getValue();
Object newValue = newResults.get(jsonPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import hudson.Extension;
import hudson.Util;

import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.lib.xtrigger.XTriggerException;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.kohsuke.stapler.DataBoundConstructor;

import java.util.HashMap;
import java.util.Map;

/**
* @author Gregory Boissinot
*/
Expand All @@ -26,6 +30,13 @@ protected void initForContentType(String content, XTriggerLog log) throws XTrigg
this.md5 = Util.getDigestOf(content);
}

@Override
public Map<String, String> getTriggeringResponse() {
Map<String, String> payload = new HashMap<>();
payload.put("MD5", md5 != null ? md5 : "");
return payload;
}

@Override
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hudson.Extension;

import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.lib.xtrigger.XTriggerException;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
Expand All @@ -24,7 +25,7 @@ public class TEXTContentType extends URLTriggerContentType {

private static final long serialVersionUID = 3560292914545953855L;

private List<TEXTContentEntry> regExElements = new ArrayList<TEXTContentEntry>();
private List<TEXTContentEntry> regExElements = new ArrayList<>();

private transient Map<String, List<String>> capturedValues;

Expand All @@ -45,6 +46,15 @@ protected void initForContentType(String content, XTriggerLog log) throws XTrigg
capturedValues = getMatchedValue(content);
}

@Override
public Map<String, String> getTriggeringResponse() {
Map<String, String> payload = new HashMap<>();
if (capturedValues != null) {
capturedValues.forEach((key, value) -> payload.put(key, StringUtils.joinWith(",", value)));
}
return payload;
}

@Override
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {

Expand Down Expand Up @@ -100,7 +110,7 @@ protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) t

private Map<String, List<String>> getMatchedValue(String content) throws XTriggerException {

Map<String, List<String>> capturedValues = new HashMap<String, List<String>>();
Map<String, List<String>> capturedValues = new HashMap<>();

StringReader stringReader = null;
BufferedReader bufferedReader = null;
Expand Down Expand Up @@ -140,7 +150,7 @@ private Map<String, List<String>> getMatchedValue(String content) throws XTrigge
private Map<String, List<String>> addMatchedValue(Map<String, List<String>> capturedValues, String regEx, String group) {
List<String> values = capturedValues.get(regEx);
if (values == null) {
values = new ArrayList<String>();
values = new ArrayList<>();
values.add(group);
capturedValues.put(regEx, values);
return capturedValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import jenkins.model.Jenkins;

import org.jenkinsci.lib.xtrigger.XTriggerException;
import org.jenkinsci.lib.xtrigger.XTriggerLog;

import java.io.Serializable;
import java.util.Map;

/**
* @author Gregory Boissinot
Expand All @@ -33,7 +33,6 @@ public void initForContent(String content, XTriggerLog log) throws XTriggerExcep
initForContentType(content, log);
}


/**
* These methods have to be overridden in each trigger implementation
*
Expand All @@ -47,5 +46,7 @@ public boolean isTriggering(String content, XTriggerLog log) throws XTriggerExce
return isTriggeringBuildForContent(content, log);
}

public abstract Map<String, String> getTriggeringResponse();

protected abstract boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class XMLContentType extends URLTriggerContentType {

private transient Map<String, Object> results = null;

private transient Document xmlDocument;

private List<XMLContentEntry> xPaths = new ArrayList<XMLContentEntry>();
private List<XMLContentEntry> xPaths = new ArrayList<>();

@DataBoundConstructor
public XMLContentType(List<XMLContentEntry> xPaths) {
Expand All @@ -50,7 +48,7 @@ public List<XMLContentEntry> getXPaths() {

@Override
protected void initForContentType(String content, XTriggerLog log) throws XTriggerException {
xmlDocument = initXMLFile(content);
Document xmlDocument = initXMLFile(content);
results = readXMLPath(xmlDocument);
}

Expand All @@ -63,18 +61,14 @@ private Document initXMLFile(String content) throws XTriggerException {
xmlDocFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) ;
xmlDocument = xmlDocFactory.newDocumentBuilder().parse(inputSource);
stringReader.close();
} catch (SAXException e) {
throw new XTriggerException(e);
} catch (IOException e) {
throw new XTriggerException(e);
} catch (ParserConfigurationException e) {
} catch (SAXException | IOException | ParserConfigurationException e) {
throw new XTriggerException(e);
}
return xmlDocument;
}

private Map<String, Object> readXMLPath(Document document) throws XTriggerException {
Map<String, Object> results = new HashMap<String, Object>(xPaths.size());
Map<String, Object> results = new HashMap<>(xPaths.size());
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
try {
Expand All @@ -90,6 +84,15 @@ private Map<String, Object> readXMLPath(Document document) throws XTriggerExcept
return results;
}

@Override
public Map<String, String> getTriggeringResponse() {
Map<String, String> payload = new HashMap<>();
if (results != null) {
results.forEach((key, value) -> payload.put(key, value.toString()));
}
return payload;
}

@Override
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull
}
if (cause != null) {
envs.override("URL_TRIGGER_CAUSE", cause.getUrlTrigger());
envs.override("URL_TRIGGER_RESPONSE", cause.getTriggerResponse());
}
}
}

0 comments on commit 1ba8cf8

Please sign in to comment.