Skip to content

Commit 468bdc4

Browse files
committed
fix cannot create empty flow
1 parent a077bca commit 468bdc4

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

core/src/main/java/com/flowci/core/flow/domain/CreateOption.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@
2222
import lombok.Data;
2323
import lombok.experimental.Accessors;
2424

25+
import java.util.Objects;
26+
2527
/**
2628
* @author yang
2729
*/
2830
@Data
2931
@Accessors(chain = true)
3032
public class CreateOption {
3133

34+
private static final String TEMPLATE_BLANK = "_blank_";
35+
3236
private String groupName;
3337

3438
@JsonProperty("title")
@@ -40,6 +44,10 @@ public class CreateOption {
4044
@JsonProperty("yml")
4145
private String rawYaml;
4246

47+
public boolean isBlank() {
48+
return Objects.equals(templateTitle, TEMPLATE_BLANK);
49+
}
50+
4351
public boolean hasGroupName() {
4452
return StringHelper.hasValue(groupName);
4553
}

core/src/main/java/com/flowci/core/flow/service/FlowServiceImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ public void onGitHookEvent(GitHookEvent event) {
279279
// ====================================================================
280280

281281
private String getYmlContent(CreateOption option) {
282+
if (option.isBlank()) {
283+
return StringHelper.EMPTY;
284+
}
285+
282286
if (option.hasTemplateTitle()) {
283287
try {
284288
return loadYmlFromTemplate(option.getTemplateTitle());

core/src/main/java/com/flowci/core/flow/service/YmlService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public interface YmlService {
5656
/**
5757
* Create or update yml for flow
5858
*
59+
* @param yml could be null or empty string
5960
* @throws com.flowci.exception.ArgumentException if yml string is empty or null
6061
*/
6162
Yml saveYml(Flow flow, String name, String yml);

core/src/main/java/com/flowci/core/flow/service/YmlServiceImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ public String getYmlString(String flowId, String name) {
105105

106106
@Override
107107
public Yml saveYml(Flow flow, String name, String yaml) {
108-
if (!StringHelper.hasValue(yaml)) {
109-
throw new ArgumentException("YAML content cannot be null or empty");
110-
}
111-
112108
FlowNode root = YmlParser.load(yaml);
113109
NodeTree tree = NodeTree.create(root);
114110

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

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

1919
import com.flowci.exception.YmlException;
2020
import com.flowci.tree.yml.FlowYml;
21+
import com.flowci.util.StringHelper;
2122
import com.flowci.util.YamlHelper;
2223
import org.yaml.snakeyaml.Yaml;
2324
import org.yaml.snakeyaml.error.YAMLException;
@@ -33,9 +34,12 @@ public class YmlParser {
3334
* Create Node instance from yml
3435
*/
3536
public static synchronized FlowNode load(String yml) {
36-
Yaml yaml = YamlHelper.create(FlowYml.class);
37+
if (!StringHelper.hasValue(yml)) {
38+
return new FlowNode(DEFAULT_ROOT_NAME);
39+
}
3740

3841
try {
42+
Yaml yaml = YamlHelper.create(FlowYml.class);
3943
FlowYml root = yaml.load(yml);
4044
root.setName(DEFAULT_ROOT_NAME);
4145
return root.toNode(null);

tree/src/test/java/com/flowci/tree/test/YmlParserTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public void init() throws IOException {
4747
content = loadContent("flow.yml");
4848
}
4949

50+
@Test
51+
public void should_parse_empty_string() {
52+
FlowNode node = YmlParser.load("");
53+
Assert.assertNotNull(node);
54+
}
55+
5056
@Test(expected = YmlException.class)
5157
public void should_yml_exception_if_name_is_invalid() throws IOException {
5258
content = loadContent("flow-with-invalid-name.yml");

0 commit comments

Comments
 (0)