Skip to content

Commit 0188f46

Browse files
committed
JENKINS-57117 - support pipeline jobs
1 parent 98d306c commit 0188f46

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

src/main/java/org/jenkinsci/plugins/scripttrigger/AbstractTrigger.java

100644100755
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package org.jenkinsci.plugins.scripttrigger;
2424

2525
import antlr.ANTLRException;
26+
import com.google.common.base.Charsets;
2627
import hudson.Util;
2728
import hudson.model.Action;
2829
import hudson.model.BuildableItem;
@@ -71,7 +72,7 @@ protected String getName() {
7172
@Override
7273
protected String getCause() {
7374
try {
74-
String scriptContent = Util.loadFile(getLogFile());
75+
String scriptContent = Util.loadFile(getLogFile(), Charsets.UTF_8);
7576
String cause = extractRootCause(scriptContent);
7677
if (cause == null) {
7778
return getDefaultMessageCause();
@@ -92,12 +93,12 @@ private String extractRootCause(String content) {
9293
protected Action[] getScheduledActions(Node pollingNode, XTriggerLog log) {
9394
String scriptContent;
9495
try {
95-
scriptContent = Util.loadFile(getLogFile());
96+
scriptContent = Util.loadFile(getLogFile(), Charsets.UTF_8);
9697
} catch (IOException e) {
9798
return new Action[0];
9899
}
99100

100-
List<Action> actionList = new ArrayList<Action>();
101+
List<Action> actionList = new ArrayList<>();
101102
String description = extractDescription(scriptContent);
102103
if (description != null) {
103104
actionList.add(new ScriptTriggerRunAction(description));

src/main/java/org/jenkinsci/plugins/scripttrigger/ScriptTrigger.java

100644100755
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.nio.charset.Charset;
4040
import java.util.Collection;
4141
import java.util.Collections;
42+
import java.util.HashMap;
4243
import java.util.Map;
4344

4445
/**
@@ -123,12 +124,16 @@ private boolean checkIfModifiedWithScriptsEvaluation(Node executingNode, int exp
123124

124125
ScriptTriggerExecutor executor = getScriptTriggerExecutor(log);
125126

126-
EnvVarsResolver envVarsResolver = new EnvVarsResolver();
127127
Map<String, String> envVars;
128-
try {
129-
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, executingNode);
130-
} catch (EnvInjectException e) {
131-
throw new ScriptTriggerException(e);
128+
if (job instanceof AbstractProject) {
129+
EnvVarsResolver envVarsResolver = new EnvVarsResolver();
130+
try {
131+
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, executingNode);
132+
} catch (EnvInjectException e) {
133+
throw new ScriptTriggerException(e);
134+
}
135+
} else {
136+
envVars = new HashMap<>();
132137
}
133138

134139
if (script != null) {
@@ -189,8 +194,8 @@ public InternalScriptTriggerAction(String actionTitle) {
189194
}
190195

191196
@SuppressWarnings("unused")
192-
public AbstractProject<?, ?> getOwner() {
193-
return (AbstractProject) job;
197+
public BuildableItem getOwner() {
198+
return job;
194199
}
195200

196201
@Override

src/main/java/org/jenkinsci/plugins/scripttrigger/groovy/GroovyScriptTrigger.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,28 @@ protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws Scri
169169
try {
170170

171171
GroovyScriptTriggerExecutor executor = getGroovyScriptTriggerExecutor(log);
172-
final AbstractProject proj = (AbstractProject) job;
173172

174-
EnvVarsResolver envVarsResolver = new EnvVarsResolver();
175173
Map<String, String> envVars;
176-
try {
177-
envVars = envVarsResolver.getPollingEnvVars(proj, pollingNode);
178-
} catch (EnvInjectException e) {
179-
throw new ScriptTriggerException(e);
174+
if (job instanceof AbstractProject) {
175+
EnvVarsResolver envVarsResolver = new EnvVarsResolver();
176+
try {
177+
envVars = envVarsResolver.getPollingEnvVars((AbstractProject) job, pollingNode);
178+
} catch (EnvInjectException e) {
179+
throw new ScriptTriggerException(e);
180+
}
181+
} else {
182+
envVars = new HashMap<>();
180183
}
181184

182185
if (groovyExpression != null) {
183-
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, proj, getGroovyExpression(), envVars, groovySystemScript);
186+
boolean evaluationSucceed = executor.evaluateGroovyScript(pollingNode, job, getGroovyExpression(), envVars, groovySystemScript);
184187
if (evaluationSucceed) {
185188
return true;
186189
}
187190
}
188191

189192
if (groovyFilePath != null) {
190-
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, proj, Util.replaceMacro(groovyFilePath, envVars), envVars, groovySystemScript);
193+
boolean evaluationSucceed = executor.evaluateGroovyScriptFilePath(pollingNode, job, Util.replaceMacro(groovyFilePath, envVars), envVars, groovySystemScript);
191194
if (evaluationSucceed) {
192195
return true;
193196
}
@@ -232,8 +235,8 @@ public InternalGroovyScriptTriggerAction(String actionTitle) {
232235
}
233236

234237
@SuppressWarnings("unused")
235-
public AbstractProject<?, ?> getOwner() {
236-
return (AbstractProject) job;
238+
public BuildableItem getOwner() {
239+
return job;
237240
}
238241

239242
@Override

src/main/java/org/jenkinsci/plugins/scripttrigger/groovy/GroovyScriptTriggerExecutor.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import groovy.lang.GroovyShell;
2626
import hudson.PluginManager;
2727
import hudson.Util;
28-
import hudson.model.AbstractProject;
29-
import hudson.model.Hudson;
30-
import hudson.model.Node;
28+
import hudson.model.*;
3129
import hudson.remoting.Callable;
3230
import hudson.util.IOUtils;
3331
import org.jenkinsci.lib.xtrigger.XTriggerLog;
@@ -49,15 +47,15 @@ public GroovyScriptTriggerExecutor(XTriggerLog log) {
4947
super(log);
5048
}
5149

52-
public boolean evaluateGroovyScript(Node executingNode, final AbstractProject proj, final String scriptContent, final Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
50+
public boolean evaluateGroovyScript(Node executingNode, final BuildableItem items, final String scriptContent, final Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
5351

5452
if (scriptContent == null) {
5553
throw new NullPointerException("The script content object must be set.");
5654
}
5755
try {
5856
if (groovySystemScript) {
5957
log.info("Running as system script");
60-
return evaluateGroovyScript(proj, scriptContent, envVars);
58+
return evaluateGroovyScript(items, scriptContent, envVars);
6159
}
6260

6361
return executingNode.getRootPath().act(new Callable<Boolean, ScriptTriggerException>() {
@@ -85,7 +83,7 @@ public Boolean call() throws ScriptTriggerException {
8583
}
8684
}
8785

88-
private boolean evaluateGroovyScript(final AbstractProject proj, final String scriptContent, final Map<String, String> envVars) {
86+
private boolean evaluateGroovyScript(final BuildableItem item, final String scriptContent, final Map<String, String> envVars) {
8987
if (envVars != null) {
9088
final StringBuilder envDebug = new StringBuilder("Replacing script vars using:");
9189
for (final Map.Entry<String, String> envEntry : envVars.entrySet()) {
@@ -113,8 +111,8 @@ private boolean evaluateGroovyScript(final AbstractProject proj, final String sc
113111

114112
shell.setVariable("log", log);
115113
shell.setVariable("out", log.getListener().getLogger());
116-
if (proj != null) {
117-
shell.setVariable("project", proj);
114+
if (item != null) {
115+
shell.setVariable("project", item);
118116
}
119117

120118
//Evaluate the new script content
@@ -153,7 +151,7 @@ protected ClassLoader getClassLoader() {
153151
return cl;
154152
}
155153

156-
public boolean evaluateGroovyScriptFilePath(Node executingNode, AbstractProject proj, String scriptFilePath, Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
154+
public boolean evaluateGroovyScriptFilePath(Node executingNode, BuildableItem item, String scriptFilePath, Map<String, String> envVars, boolean groovySystemScript) throws ScriptTriggerException {
157155

158156
if (scriptFilePath == null) {
159157
throw new NullPointerException("The scriptFilePath object must be set.");
@@ -194,7 +192,7 @@ public boolean evaluateGroovyScriptFilePath(Node executingNode, AbstractProject
194192
scriptContent = getStringContent(executingNode, scriptFilePath);
195193
}
196194

197-
return evaluateGroovyScript(executingNode, proj, scriptContent, envVars, groovySystemScript);
195+
return evaluateGroovyScript(executingNode, item, scriptContent, envVars, groovySystemScript);
198196
}
199197

200198
}

src/main/resources/org/jenkinsci/plugins/scripttrigger/groovy/GroovyScriptTrigger/help-groovySystemScript.html

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div>
22
<p>
33
If checked run the groovy script as a system script, the script will have access to the same
4-
variables as the Groovy Console. The AbstractProject is also bound to the project variable.
4+
variables as the Groovy Console. The BuildableItem is also bound to the project variable.
55
<br/>
66
If not checked run the groovy script on the executor node, the script will not have access
77
to the hudson or job model.

0 commit comments

Comments
 (0)