Skip to content

Commit 676c6b5

Browse files
committed
load template in backend since CORS
1 parent 3fef748 commit 676c6b5

File tree

14 files changed

+157
-50
lines changed

14 files changed

+157
-50
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.flowci.core.common.domain;
2+
3+
import com.fasterxml.jackson.annotation.JsonAlias;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.flowci.util.StringHelper;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
import java.io.Serializable;
10+
11+
@Getter
12+
@Setter
13+
public abstract class SourceWithDomain implements Serializable {
14+
15+
private static final String DOMAIN_CN = "cn";
16+
17+
@JsonAlias("url")
18+
private String source;
19+
20+
@JsonAlias("url_cn")
21+
@JsonProperty("source_cn")
22+
private String sourceCn;
23+
24+
public String getSourceWithDomain(String domain) {
25+
if (!StringHelper.hasValue(domain)) return source;
26+
return DOMAIN_CN.equalsIgnoreCase(domain) ? sourceCn : source;
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.flowci.core.common.manager;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.HttpClient;
5+
import org.apache.http.client.config.RequestConfig;
6+
import org.apache.http.client.methods.HttpGet;
7+
import org.apache.http.impl.client.HttpClientBuilder;
8+
import org.apache.http.util.EntityUtils;
9+
import org.springframework.stereotype.Component;
10+
11+
import javax.annotation.PostConstruct;
12+
import java.io.IOException;
13+
14+
@Component("httpManager")
15+
public class HttpRequestManager {
16+
17+
private final static int DefaultTimeout = 30 * 1000;
18+
19+
private final static String DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36";
20+
21+
private HttpClient client;
22+
23+
@PostConstruct
24+
public void init() {
25+
RequestConfig config = RequestConfig.custom()
26+
.setConnectTimeout(DefaultTimeout)
27+
.setConnectionRequestTimeout(DefaultTimeout)
28+
.setSocketTimeout(DefaultTimeout)
29+
.build();
30+
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
31+
}
32+
33+
public String get(String url) throws IOException {
34+
HttpGet request = new HttpGet(url);
35+
request.setHeader("User-Agent", DefaultUserAgent);
36+
37+
HttpResponse execute = client.execute(request);
38+
return EntityUtils.toString(execute.getEntity());
39+
}
40+
}

core/src/main/java/com/flowci/core/flow/controller/YmlController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ public void saveYml(@PathVariable String flowName,
6262
@RequestBody RequestMessage<String> body) {
6363
Flow flow = flowService.get(flowName);
6464
String yamlInB64 = body.getData();
65-
ymlService.saveYml(flow, ymlName, yamlInB64);
65+
ymlService.saveYmlFromB64(flow, ymlName, yamlInB64);
6666
}
6767

6868
@GetMapping(value = "/{flowName}/yml/{ymlName}", produces = MediaType.APPLICATION_JSON_VALUE)
6969
@Action(FlowAction.GET_YML)
7070
public String getYml(@PathVariable String flowName, @PathVariable String ymlName) {
7171
Flow flow = flowService.get(flowName);
72-
String yamlInB64 = ymlService.getYmlString(flow.getId(), ymlName);
73-
return yamlInB64;
72+
return ymlService.getYmlString(flow.getId(), ymlName);
7473
}
7574
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.flowci.core.flow.domain;
1818

19+
import com.fasterxml.jackson.annotation.JsonProperty;
1920
import com.flowci.util.StringHelper;
2021
import lombok.Data;
2122
import lombok.experimental.Accessors;
@@ -27,6 +28,8 @@
2728
@Accessors(chain = true)
2829
public class ConfirmOption {
2930

31+
private static final String DESC_BLANK = "blank";
32+
3033
private String gitUrl;
3134

3235
private String secret;
@@ -36,6 +39,12 @@ public class ConfirmOption {
3639
*/
3740
private String yaml = StringHelper.EMPTY;
3841

42+
/**
43+
* Template name (desc), yaml property will be ignored if it is defined
44+
*/
45+
@JsonProperty("desc")
46+
private String templateDesc;
47+
3948
public boolean hasGitUrl() {
4049
return StringHelper.hasValue(gitUrl);
4150
}
@@ -47,4 +56,12 @@ public boolean hasSecret() {
4756
public boolean hasYml() {
4857
return StringHelper.hasValue(yaml);
4958
}
59+
60+
public boolean hasTemplateDesc() {
61+
return StringHelper.hasValue(templateDesc);
62+
}
63+
64+
public boolean hasBlankTemplate() {
65+
return DESC_BLANK.equalsIgnoreCase(templateDesc);
66+
}
5067
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.flowci.core.flow.domain;
22

3-
import com.fasterxml.jackson.annotation.JsonProperty;
3+
import com.flowci.core.common.domain.SourceWithDomain;
44
import lombok.Getter;
55
import lombok.Setter;
66

77
@Getter
88
@Setter
9-
public final class Template {
10-
11-
private String url;
12-
13-
@JsonProperty("url_cn")
14-
private String urlCn;
9+
public final class Template extends SourceWithDomain {
1510

1611
private String desc;
1712

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616

1717
package com.flowci.core.flow.service;
1818

19+
import com.flowci.core.common.config.AppProperties;
1920
import com.flowci.core.common.domain.Variables;
21+
import com.flowci.core.common.manager.HttpRequestManager;
2022
import com.flowci.core.common.manager.SessionManager;
2123
import com.flowci.core.common.manager.SpringEventManager;
2224
import com.flowci.core.flow.dao.FlowDao;
2325
import com.flowci.core.flow.dao.FlowUserDao;
24-
import com.flowci.core.flow.domain.ConfirmOption;
25-
import com.flowci.core.flow.domain.Flow;
26+
import com.flowci.core.flow.domain.*;
2627
import com.flowci.core.flow.domain.Flow.Status;
27-
import com.flowci.core.flow.domain.WebhookStatus;
28-
import com.flowci.core.flow.domain.Yml;
2928
import com.flowci.core.flow.event.FlowCreatedEvent;
3029
import com.flowci.core.flow.event.FlowDeletedEvent;
3130
import com.flowci.core.flow.event.FlowInitEvent;
@@ -79,22 +78,27 @@ public class FlowServiceImpl implements FlowService {
7978
@Autowired
8079
private FileManager fileManager;
8180

81+
@Autowired
82+
private HttpRequestManager httpRequestManager;
83+
8284
@Autowired
8385
private YmlService ymlService;
8486

8587
@Autowired
8688
private CronService cronService;
8789

90+
@Autowired
91+
private List<Template> templates;
92+
93+
@Autowired
94+
private AppProperties appProperties;
95+
8896
// ====================================================================
8997
// %% Public function
9098
// ====================================================================
9199

92100
@Override
93101
public List<Flow> list(Status status) {
94-
if (sessionManager.get().isAdmin()) {
95-
return flowDao.findAll();
96-
}
97-
98102
String email = sessionManager.getUserEmail();
99103
List<String> flowIds = flowUserDao.findAllFlowsByUserEmail(email);
100104

@@ -197,9 +201,24 @@ public Flow confirm(String name, ConfirmOption option) {
197201

198202
flow.setStatus(Status.CONFIRMED);
199203

204+
if (option.hasBlankTemplate()) {
205+
return flowDao.save(flow);
206+
}
207+
208+
// load YAML from template
209+
if (option.hasTemplateDesc()) {
210+
try {
211+
String template = loadYmlFromTemplate(option.getTemplateDesc());
212+
ymlService.saveYml(flow, Yml.DEFAULT_NAME, template);
213+
return flow;
214+
} catch (IOException e) {
215+
throw new NotFoundException("Unable to load template {0} content", option.getTemplateDesc());
216+
}
217+
}
218+
200219
// flow instance will be saved in saveYml
201220
if (option.hasYml()) {
202-
ymlService.saveYml(flow, Yml.DEFAULT_NAME, option.getYaml());
221+
ymlService.saveYmlFromB64(flow, Yml.DEFAULT_NAME, option.getYaml());
203222
return flow;
204223
}
205224

@@ -340,4 +359,15 @@ private void setupDefaultVars(Flow flow) {
340359
Vars<VarValue> localVars = flow.getLocally();
341360
localVars.put(Variables.Flow.Name, VarValue.of(flow.getName(), VarType.STRING, false));
342361
}
362+
363+
private String loadYmlFromTemplate(String desc) throws IOException {
364+
for (Template t : templates) {
365+
if (t.getDesc().equals(desc)) {
366+
String source = t.getSourceWithDomain(appProperties.getResourceDomain());
367+
return httpRequestManager.get(source);
368+
}
369+
}
370+
371+
throw new NotFoundException("Unable to load template {0} content", desc);
372+
}
343373
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public interface YmlService {
5858
*
5959
* @throws com.flowci.exception.ArgumentException if yml string is empty or null
6060
*/
61-
Yml saveYml(Flow flow, String name, String ymlInB64);
61+
Yml saveYml(Flow flow, String name, String yml);
62+
63+
Yml saveYmlFromB64(Flow flow, String name, String ymlInB64);
6264

6365
/**
6466
* Delete all yaml of flow

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,11 @@ public String getYmlString(String flowId, String name) {
104104
}
105105

106106
@Override
107-
public Yml saveYml(Flow flow, String name, String ymlInB64) {
108-
if (!StringHelper.hasValue(ymlInB64)) {
107+
public Yml saveYml(Flow flow, String name, String yaml) {
108+
if (!StringHelper.hasValue(yaml)) {
109109
throw new ArgumentException("YAML content cannot be null or empty");
110110
}
111111

112-
String yaml = StringHelper.fromBase64(ymlInB64);
113112
FlowNode root = YmlParser.load(yaml);
114113
NodeTree tree = NodeTree.create(root);
115114

@@ -120,7 +119,7 @@ public Yml saveYml(Flow flow, String name, String ymlInB64) {
120119
}
121120
}
122121

123-
Yml ymlObj = getOrCreate(flow.getId(), name, ymlInB64);
122+
Yml ymlObj = getOrCreate(flow.getId(), name, StringHelper.toBase64(yaml));
124123
try {
125124
ymlDao.save(ymlObj);
126125
} catch (DuplicateKeyException e) {
@@ -138,6 +137,12 @@ public Yml saveYml(Flow flow, String name, String ymlInB64) {
138137
return ymlObj;
139138
}
140139

140+
@Override
141+
public Yml saveYmlFromB64(Flow flow, String name, String ymlInB64) {
142+
String yaml = StringHelper.fromBase64(ymlInB64);
143+
return saveYml(flow, name, yaml);
144+
}
145+
141146
@Override
142147
public void delete(String flowId) {
143148
ymlDao.deleteAllByFlowId(flowId);

core/src/main/java/com/flowci/core/plugin/domain/PluginRepoInfo.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
package com.flowci.core.plugin.domain;
1818

19-
import com.fasterxml.jackson.annotation.JsonProperty;
19+
import com.flowci.core.common.domain.SourceWithDomain;
2020
import com.flowci.domain.Version;
2121
import lombok.EqualsAndHashCode;
2222
import lombok.Getter;
2323
import lombok.Setter;
2424
import lombok.ToString;
2525
import org.springframework.data.mongodb.core.index.Indexed;
2626

27-
import java.io.Serializable;
2827
import java.util.HashSet;
2928
import java.util.Set;
3029

@@ -33,18 +32,13 @@
3332
*/
3433
@Getter
3534
@Setter
36-
@EqualsAndHashCode(of = {"name"})
35+
@EqualsAndHashCode(of = {"name"}, callSuper = false)
3736
@ToString(of = {"name", "version", "branch"})
38-
public class PluginRepoInfo implements Serializable {
37+
public class PluginRepoInfo extends SourceWithDomain {
3938

4039
@Indexed(name = "index_plugins_name", unique = true)
4140
private String name;
4241

43-
private String source; // git repo url
44-
45-
@JsonProperty("source_cn")
46-
private String sourceCn; // git repo url for cn
47-
4842
private String branch = "master";
4943

5044
private String description;

core/src/main/java/com/flowci/core/plugin/service/PluginServiceImpl.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,14 @@ private Plugin clone(PluginRepoInfo repo) throws Exception {
250250
log.info("Start to load plugin: {}", repo);
251251
Path dir = getPluginRepoDir(repo.getName(), repo.getVersion().toString());
252252

253-
String rd = appProperties.getResourceDomain().toLowerCase();
254-
String resource = repo.getSource();
255-
if (rd.equals("cn")) {
256-
resource = repo.getSourceCn();
257-
}
253+
String rd = appProperties.getResourceDomain();
254+
String source = repo.getSourceWithDomain(rd);
258255

259-
if (!StringHelper.hasValue(resource)) {
256+
if (!StringHelper.hasValue(source)) {
260257
throw new NotFoundException("Plugin {0} source is missing for domain {1}", repo.getName(), rd);
261258
}
262259

263-
GitClient client = new GitClient(resource, null, null);
260+
GitClient client = new GitClient(source, null, null);
264261
client.klone(dir, repo.getBranch());
265262

266263
return load(dir.toFile(), repo);

0 commit comments

Comments
 (0)