Skip to content

Commit 1ba8cf8

Browse files
Alexander Falkensternfalkena
authored andcommitted
[JENKINS-54649] Add trigger response as environment variable
1 parent b17ceab commit 1ba8cf8

File tree

10 files changed

+93
-31
lines changed

10 files changed

+93
-31
lines changed

Readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ Plugin](https://plugins.jenkins.io/fstrigger/)
4141

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

44+
## Notes
45+
46+
To determine the URL caused invocation - use the environment variable
47+
`URL_TRIGGER_CAUSE` - e.g. `${env.URL_TRIGGER_CAUSE}`
48+
49+
To determine the response caused invocation - use the environment variable
50+
`URL_TRIGGER_RESPONSE` - e.g. `${env.URL_TRIGGER_RESPONSE}`
51+
4452
## Declarative Pipeline Syntax
4553
Example:
4654

pom.xml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.jenkins-ci.plugins</groupId>
77
<artifactId>plugin</artifactId>
8-
<version>4.12</version>
8+
<version>4.27</version>
99
</parent>
1010

1111
<artifactId>urltrigger</artifactId>
@@ -41,14 +41,13 @@
4141
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4242
<maven.compiler.source>1.8</maven.compiler.source>
4343
<maven.compiler.target>1.8</maven.compiler.target>
44-
<java.version>1.8.0</java.version>
4544
<java.level>8</java.level>
4645
<xtrigger.lib.version>0.35</xtrigger.lib.version>
4746
<jersey.client.version>1.19.4</jersey.client.version>
48-
<json.path.version>2.4.0</json.path.version>
47+
<json.path.version>2.6.0</json.path.version>
4948
<jackson.mapper.as1.version>1.8.3</jackson.mapper.as1.version>
5049
<mockito.version>1.8.5</mockito.version>
51-
<jenkins.version>2.204.6</jenkins.version>
50+
<jenkins.version>2.263.4</jenkins.version>
5251
</properties>
5352

5453
<scm>
@@ -73,8 +72,8 @@
7372
<dependencies>
7473
<dependency>
7574
<groupId>io.jenkins.tools.bom</groupId>
76-
<artifactId>bom-2.204.x</artifactId>
77-
<version>16</version>
75+
<artifactId>bom-2.263.x</artifactId>
76+
<version>950.v396cb834de1e</version>
7877
<scope>import</scope>
7978
<type>pom</type>
8079
</dependency>
@@ -142,12 +141,7 @@
142141
<dependency>
143142
<groupId>commons-net</groupId>
144143
<artifactId>commons-net</artifactId>
145-
<version>3.6</version>
146-
</dependency>
147-
148-
<dependency>
149-
<groupId>commons-lang</groupId>
150-
<artifactId>commons-lang</artifactId>
144+
<version>3.8.0</version>
151145
</dependency>
152146

153147
<dependency>

src/main/java/org/jenkinsci/plugins/urltrigger/URLTrigger.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws XTri
255255
if (modified) {
256256
this.buildCause = new URLTriggerCause( true ) ;
257257
this.buildCause.setUrlTrigger(entry.getUrl());
258+
for(final URLTriggerContentType contentType: entry.getContentTypes()) {
259+
this.buildCause.addTriggerResponse(contentType.getTriggeringResponse());
260+
}
258261
return true;
259262
}
260263
}

src/main/java/org/jenkinsci/plugins/urltrigger/URLTriggerCause.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.jenkinsci.plugins.urltrigger;
22

33
import java.io.Serializable;
4+
import java.util.*;
45

6+
import org.apache.commons.lang3.StringUtils;
57
import org.jenkinsci.lib.xtrigger.XTriggerCause;
68

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

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

2326
protected URLTriggerCause() {
@@ -29,7 +32,19 @@ protected URLTriggerCause( boolean logEnabled ) {
2932
}
3033

3134
protected URLTriggerCause(String triggerName, String causeFrom, boolean logEnabled) {
32-
super( triggerName , causeFrom , logEnabled ) ;
35+
super(triggerName , causeFrom , logEnabled) ;
36+
}
37+
38+
public void addTriggerResponse(Map<String, String> response) {
39+
if (triggerResponse != null) {
40+
triggerResponse.putAll(response);
41+
} else {
42+
setTriggerResponse(response);
43+
}
44+
}
45+
46+
public void setTriggerResponse(Map<String, String> response) {
47+
triggerResponse = response;
3348
}
3449

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

59+
public String getTriggerResponse() {
60+
List<String> result = new ArrayList<>();
61+
if (triggerResponse != null) {
62+
triggerResponse.forEach((key, value) -> result.add(String.format("\"%s\": %s", key, value)));
63+
}
64+
return StringUtils.joinWith(",", result);
65+
}
66+
4467
public String getUrlTrigger() {
4568
return urlTrigger;
4669
}

src/main/java/org/jenkinsci/plugins/urltrigger/content/JSONContentType.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class JSONContentType extends URLTriggerContentType {
2525

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

28-
private List<JSONContentEntry> jsonPaths = new ArrayList<JSONContentEntry>();
28+
private List<JSONContentEntry> jsonPaths = new ArrayList<>();
2929

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

5353
private Map<String, Object> readJsonPath(String content) throws XTriggerException {
54-
Map<String, Object> results = new HashMap<String, Object>(jsonPaths.size());
54+
Map<String, Object> results = new HashMap<>(jsonPaths.size());
5555
for (JSONContentEntry jsonContentEntry : jsonPaths) {
5656
String jsonPath = jsonContentEntry.getJsonPath();
5757
try {
@@ -66,6 +66,15 @@ private Map<String, Object> readJsonPath(String content) throws XTriggerExceptio
6666
return results;
6767
}
6868

69+
@Override
70+
public Map<String, String> getTriggeringResponse() {
71+
Map<String, String> payload = new HashMap<>();
72+
if (results != null) {
73+
results.forEach((key, value) -> payload.put(key, value.toString()));
74+
}
75+
return payload;
76+
}
77+
6978
@Override
7079
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {
7180

@@ -96,7 +105,6 @@ protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) t
96105
}
97106

98107
for (Map.Entry<String, Object> entry : results.entrySet()) {
99-
100108
String jsonPath = entry.getKey();
101109
Object initValue = entry.getValue();
102110
Object newValue = newResults.get(jsonPath);

src/main/java/org/jenkinsci/plugins/urltrigger/content/SimpleContentType.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import hudson.Extension;
44
import hudson.Util;
55

6+
import org.apache.commons.lang3.StringUtils;
67
import org.jenkinsci.Symbol;
78
import org.jenkinsci.lib.xtrigger.XTriggerException;
89
import org.jenkinsci.lib.xtrigger.XTriggerLog;
910
import org.kohsuke.stapler.DataBoundConstructor;
1011

12+
import java.util.HashMap;
13+
import java.util.Map;
14+
1115
/**
1216
* @author Gregory Boissinot
1317
*/
@@ -26,6 +30,13 @@ protected void initForContentType(String content, XTriggerLog log) throws XTrigg
2630
this.md5 = Util.getDigestOf(content);
2731
}
2832

33+
@Override
34+
public Map<String, String> getTriggeringResponse() {
35+
Map<String, String> payload = new HashMap<>();
36+
payload.put("MD5", md5 != null ? md5 : "");
37+
return payload;
38+
}
39+
2940
@Override
3041
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {
3142

src/main/java/org/jenkinsci/plugins/urltrigger/content/TEXTContentType.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hudson.Extension;
44

5+
import org.apache.commons.lang3.StringUtils;
56
import org.jenkinsci.Symbol;
67
import org.jenkinsci.lib.xtrigger.XTriggerException;
78
import org.jenkinsci.lib.xtrigger.XTriggerLog;
@@ -24,7 +25,7 @@ public class TEXTContentType extends URLTriggerContentType {
2425

2526
private static final long serialVersionUID = 3560292914545953855L;
2627

27-
private List<TEXTContentEntry> regExElements = new ArrayList<TEXTContentEntry>();
28+
private List<TEXTContentEntry> regExElements = new ArrayList<>();
2829

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

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

49+
@Override
50+
public Map<String, String> getTriggeringResponse() {
51+
Map<String, String> payload = new HashMap<>();
52+
if (capturedValues != null) {
53+
capturedValues.forEach((key, value) -> payload.put(key, StringUtils.joinWith(",", value)));
54+
}
55+
return payload;
56+
}
57+
4858
@Override
4959
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {
5060

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

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

103-
Map<String, List<String>> capturedValues = new HashMap<String, List<String>>();
113+
Map<String, List<String>> capturedValues = new HashMap<>();
104114

105115
StringReader stringReader = null;
106116
BufferedReader bufferedReader = null;
@@ -140,7 +150,7 @@ private Map<String, List<String>> getMatchedValue(String content) throws XTrigge
140150
private Map<String, List<String>> addMatchedValue(Map<String, List<String>> capturedValues, String regEx, String group) {
141151
List<String> values = capturedValues.get(regEx);
142152
if (values == null) {
143-
values = new ArrayList<String>();
153+
values = new ArrayList<>();
144154
values.add(group);
145155
capturedValues.put(regEx, values);
146156
return capturedValues;

src/main/java/org/jenkinsci/plugins/urltrigger/content/URLTriggerContentType.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import hudson.ExtensionPoint;
44
import hudson.model.Describable;
55
import hudson.model.Descriptor;
6-
import hudson.model.Hudson;
76
import jenkins.model.Jenkins;
87

98
import org.jenkinsci.lib.xtrigger.XTriggerException;
109
import org.jenkinsci.lib.xtrigger.XTriggerLog;
1110

1211
import java.io.Serializable;
12+
import java.util.Map;
1313

1414
/**
1515
* @author Gregory Boissinot
@@ -33,7 +33,6 @@ public void initForContent(String content, XTriggerLog log) throws XTriggerExcep
3333
initForContentType(content, log);
3434
}
3535

36-
3736
/**
3837
* These methods have to be overridden in each trigger implementation
3938
*
@@ -47,5 +46,7 @@ public boolean isTriggering(String content, XTriggerLog log) throws XTriggerExce
4746
return isTriggeringBuildForContent(content, log);
4847
}
4948

49+
public abstract Map<String, String> getTriggeringResponse();
50+
5051
protected abstract boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException;
5152
}

src/main/java/org/jenkinsci/plugins/urltrigger/content/XMLContentType.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ public class XMLContentType extends URLTriggerContentType {
3232

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

35-
private transient Document xmlDocument;
36-
37-
private List<XMLContentEntry> xPaths = new ArrayList<XMLContentEntry>();
35+
private List<XMLContentEntry> xPaths = new ArrayList<>();
3836

3937
@DataBoundConstructor
4038
public XMLContentType(List<XMLContentEntry> xPaths) {
@@ -50,7 +48,7 @@ public List<XMLContentEntry> getXPaths() {
5048

5149
@Override
5250
protected void initForContentType(String content, XTriggerLog log) throws XTriggerException {
53-
xmlDocument = initXMLFile(content);
51+
Document xmlDocument = initXMLFile(content);
5452
results = readXMLPath(xmlDocument);
5553
}
5654

@@ -63,18 +61,14 @@ private Document initXMLFile(String content) throws XTriggerException {
6361
xmlDocFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) ;
6462
xmlDocument = xmlDocFactory.newDocumentBuilder().parse(inputSource);
6563
stringReader.close();
66-
} catch (SAXException e) {
67-
throw new XTriggerException(e);
68-
} catch (IOException e) {
69-
throw new XTriggerException(e);
70-
} catch (ParserConfigurationException e) {
64+
} catch (SAXException | IOException | ParserConfigurationException e) {
7165
throw new XTriggerException(e);
7266
}
7367
return xmlDocument;
7468
}
7569

7670
private Map<String, Object> readXMLPath(Document document) throws XTriggerException {
77-
Map<String, Object> results = new HashMap<String, Object>(xPaths.size());
71+
Map<String, Object> results = new HashMap<>(xPaths.size());
7872
XPathFactory xPathFactory = XPathFactory.newInstance();
7973
XPath xPath = xPathFactory.newXPath();
8074
try {
@@ -90,6 +84,15 @@ private Map<String, Object> readXMLPath(Document document) throws XTriggerExcept
9084
return results;
9185
}
9286

87+
@Override
88+
public Map<String, String> getTriggeringResponse() {
89+
Map<String, String> payload = new HashMap<>();
90+
if (results != null) {
91+
results.forEach((key, value) -> payload.put(key, value.toString()));
92+
}
93+
return payload;
94+
}
95+
9396
@Override
9497
protected boolean isTriggeringBuildForContent(String content, XTriggerLog log) throws XTriggerException {
9598

src/main/java/org/jenkinsci/plugins/urltrigger/environment/URLTriggerEnvironmentContributor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull
3030
}
3131
if (cause != null) {
3232
envs.override("URL_TRIGGER_CAUSE", cause.getUrlTrigger());
33+
envs.override("URL_TRIGGER_RESPONSE", cause.getTriggerResponse());
3334
}
3435
}
3536
}

0 commit comments

Comments
 (0)