Skip to content

Commit 99838da

Browse files
committed
support condition as groovy script
1 parent 9d4a8d8 commit 99838da

File tree

13 files changed

+125
-30
lines changed

13 files changed

+125
-30
lines changed

core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
<artifactId>minio</artifactId>
6565
</dependency>
6666

67+
<dependency>
68+
<groupId>org.codehaus.groovy</groupId>
69+
<artifactId>groovy-all</artifactId>
70+
<type>pom</type>
71+
</dependency>
72+
6773
<dependency>
6874
<groupId>com.github.ben-manes.caffeine</groupId>
6975
<artifactId>caffeine</artifactId>

core/src/main/java/com/flowci/core/agent/domain/ShellIn.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.flowci.core.agent.domain;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.flowci.domain.DockerOption;
45
import com.flowci.domain.StringVars;
56
import com.flowci.domain.Vars;
7+
import com.flowci.util.StringHelper;
68
import com.google.common.base.Strings;
79
import lombok.Getter;
810
import lombok.Setter;
@@ -31,6 +33,9 @@ public final class ShellIn extends CmdIn {
3133

3234
private List<DockerOption> dockers;
3335

36+
@JsonIgnore
37+
private String condition;
38+
3439
private List<String> scripts = new LinkedList<>();
3540

3641
private int timeout = 1800;
@@ -57,4 +62,9 @@ public void addEnvFilters(Set<String> exports) {
5762
public void addInputs(StringVars vars) {
5863
inputs.putAll(vars);
5964
}
65+
66+
@JsonIgnore
67+
public boolean hasCondition() {
68+
return StringHelper.hasValue(condition);
69+
}
6070
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.flowci.core.job.manager;
2+
3+
import com.flowci.core.agent.domain.CmdIn;
4+
import groovy.util.ScriptException;
5+
6+
public interface ConditionManager {
7+
8+
boolean run(CmdIn in) throws ScriptException;
9+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.flowci.core.job.manager;
2+
3+
import com.flowci.core.agent.domain.CmdIn;
4+
import com.flowci.core.agent.domain.ShellIn;
5+
import groovy.lang.Binding;
6+
import groovy.lang.GroovyRuntimeException;
7+
import groovy.lang.GroovyShell;
8+
import groovy.util.ScriptException;
9+
import lombok.extern.log4j.Log4j2;
10+
import org.codehaus.groovy.control.CompilationFailedException;
11+
import org.springframework.stereotype.Component;
12+
13+
@Log4j2
14+
@Component
15+
public class ConditionManagerImpl implements ConditionManager {
16+
17+
@Override
18+
public boolean run(CmdIn in) throws ScriptException {
19+
if (!(in instanceof ShellIn)) {
20+
return true;
21+
}
22+
23+
ShellIn shell = (ShellIn) in;
24+
if (!shell.hasCondition()) {
25+
return true;
26+
}
27+
28+
Binding binding = new Binding();
29+
shell.getInputs().forEach(binding::setVariable);
30+
31+
try {
32+
GroovyShell groovy = new GroovyShell(binding);
33+
Object value = groovy.evaluate(shell.getCondition());
34+
if (value instanceof Boolean) {
35+
return (Boolean) value;
36+
}
37+
throw new ScriptException("The return type is not boolean");
38+
} catch (GroovyRuntimeException e) {
39+
throw new ScriptException(e.getMessage());
40+
}
41+
}
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.flowci.core.test.job;
2+
3+
import com.flowci.core.agent.domain.ShellIn;
4+
import com.flowci.core.job.manager.ConditionManager;
5+
import com.flowci.core.test.SpringScenario;
6+
import groovy.util.ScriptException;
7+
import org.junit.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.testng.Assert;
10+
11+
public class ConditionManagerTest extends SpringScenario {
12+
13+
@Autowired
14+
private ConditionManager conditionManager;
15+
16+
@Test
17+
public void should_run_groovy_condition() throws ScriptException {
18+
ShellIn in = new ShellIn();
19+
in.getInputs().put("foo", "helloword");
20+
in.setCondition(
21+
"println \"$foo\"; "
22+
+ "return true;"
23+
);
24+
25+
Assert.assertTrue(conditionManager.run(in));
26+
}
27+
28+
@Test(expected = ScriptException.class)
29+
public void should_throw_exception_if_wrong_return_type() throws ScriptException {
30+
ShellIn in = new ShellIn();
31+
in.getInputs().put("foo", "helloword");
32+
in.setCondition(
33+
"println \"$foo\"; "
34+
+ "hello = \"1234\";"
35+
+ "return hello;"
36+
);
37+
38+
conditionManager.run(in);
39+
}
40+
}

pom.xml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,11 @@
3535
<swagger2.version>2.7.0</swagger2.version>
3636
<jgit.version>5.0.3.201809091024-r</jgit.version>
3737
<jsch.version>0.1.55</jsch.version>
38-
<groovy-all.version>2.4.12</groovy-all.version>
3938
<docker-java.version>3.2.1</docker-java.version>
4039
<cron-utils.version>7.0.5</cron-utils.version>
4140
<amqp-client.version>5.9.0</amqp-client.version>
4241
<java-jwt.version>3.8.1</java-jwt.version>
4342
<minio.version>6.0.11</minio.version>
44-
<protobuf-java.version>3.11.4</protobuf-java.version>
4543
<log4j-core.version>2.11.2</log4j-core.version>
4644
</properties>
4745

@@ -155,18 +153,6 @@
155153
<version>${java-jwt.version}</version>
156154
</dependency>
157155

158-
<dependency>
159-
<groupId>com.google.protobuf</groupId>
160-
<artifactId>protobuf-java</artifactId>
161-
<version>${protobuf-java.version}</version>
162-
</dependency>
163-
164-
<dependency>
165-
<groupId>org.codehaus.groovy</groupId>
166-
<artifactId>groovy-all</artifactId>
167-
<version>${groovy-all.version}</version>
168-
</dependency>
169-
170156
<dependency>
171157
<groupId>com.cronutils</groupId>
172158
<artifactId>cron-utils</artifactId>
@@ -314,6 +300,13 @@
314300
<version>${curator.version}</version>
315301
</dependency>
316302

303+
<dependency>
304+
<groupId>org.codehaus.groovy</groupId>
305+
<artifactId>groovy-all</artifactId>
306+
<version>3.0.4</version>
307+
<type>pom</type>
308+
</dependency>
309+
317310
<dependency>
318311
<groupId>org.apache.curator</groupId>
319312
<artifactId>curator-test</artifactId>
@@ -334,7 +327,6 @@
334327
<version>${wiremock.version}</version>
335328
<scope>test</scope>
336329
</dependency>
337-
338330
</dependencies>
339331
</dependencyManagement>
340332

tree/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828
<artifactId>snakeyaml</artifactId>
2929
</dependency>
3030

31-
<dependency>
32-
<groupId>org.codehaus.groovy</groupId>
33-
<artifactId>groovy-all</artifactId>
34-
</dependency>
35-
3631
<dependency>
3732
<groupId>junit</groupId>
3833
<artifactId>junit</artifactId>

tree/src/main/java/com/flowci/tree/StepNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class StepNode extends Node {
1919
/**
2020
* Node before groovy script;
2121
*/
22-
private String before;
22+
private String condition;
2323

2424
/**
2525
* Node execute script, can be null
@@ -50,7 +50,7 @@ public boolean hasPlugin() {
5050
}
5151

5252
@JsonIgnore
53-
public boolean hasBefore() {
54-
return !Strings.isNullOrEmpty(before);
53+
public boolean hasCondition() {
54+
return !Strings.isNullOrEmpty(condition);
5555
}
5656
}

tree/src/main/java/com/flowci/tree/yml/FlowYml.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.flowci.exception.YmlException;
2020
import com.flowci.tree.*;
21-
import com.flowci.util.ObjectsHelper;
2221
import lombok.Getter;
2322
import lombok.NoArgsConstructor;
2423
import lombok.NonNull;

tree/src/main/java/com/flowci/tree/yml/StepYml.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.flowci.exception.YmlException;
2020
import com.flowci.tree.NodePath;
2121
import com.flowci.tree.StepNode;
22-
import com.flowci.util.ObjectsHelper;
2322
import com.flowci.util.StringHelper;
2423
import com.google.common.collect.Sets;
2524
import lombok.Getter;
@@ -39,7 +38,10 @@ public class StepYml extends YmlBase<StepNode> {
3938

4039
private static final String DefaultStepPrefix = "step-";
4140

42-
private String before;
41+
/**
42+
* Groovy script
43+
*/
44+
private String condition;
4345

4446
private String script;
4547

@@ -59,7 +61,7 @@ public class StepYml extends YmlBase<StepNode> {
5961

6062
public StepNode toNode(int index) {
6163
StepNode node = new StepNode(buildName(index));
62-
node.setBefore(before);
64+
node.setCondition(condition);
6365
node.setScript(script);
6466
node.setPlugin(plugin);
6567
node.setExports(Sets.newHashSet(exports));

0 commit comments

Comments
 (0)