Skip to content

Commit 7e4468e

Browse files
authored
Merge pull request #406 from FlowCI/feature/1548
Feature/1548
2 parents 9faa0c7 + 676c6b5 commit 7e4468e

File tree

21 files changed

+168
-75
lines changed

21 files changed

+168
-75
lines changed

core/src/main/java/com/flowci/core/common/config/AppProperties.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
2525
import org.springframework.context.annotation.PropertySource;
26+
import org.springframework.core.io.Resource;
2627
import org.springframework.validation.annotation.Validated;
2728

2829
import javax.validation.constraints.NotBlank;
@@ -51,6 +52,9 @@ public class AppProperties {
5152
@Length(max = 16, min = 16)
5253
private String secret;
5354

55+
// indicate load yaml template and plugin from where
56+
private String resourceDomain;
57+
5458
private boolean autoLocalAgentHost;
5559

5660
private boolean defaultSmtpConfig;
@@ -106,7 +110,7 @@ public Minio minio() {
106110
@Data
107111
public static class Flow {
108112

109-
private String templatesUrl;
113+
private Resource templatesUrl;
110114
}
111115

112116
@Data
@@ -118,7 +122,7 @@ public static class Job {
118122
@Data
119123
public static class Plugin {
120124

121-
private String defaultRepo;
125+
private Resource defaultRepo;
122126

123127
private Boolean autoUpdate;
124128
}

core/src/main/java/com/flowci/core/common/domain/Settings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public static class Action {
3232

3333
private String serverUrl;
3434

35+
/**
36+
* Indicate load resource(cn) from where
37+
*/
38+
private String source;
39+
3540
public Settings() {
3641
setId(DefaultId);
3742
}
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+
}

core/src/main/java/com/flowci/core/common/domain/Variables.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public abstract static class App {
2929

3030
public static final String Host = "FLOWCI_SERVER_HOST";
3131

32+
public static final String ResourceDomain = "FLOWCI_RESOURCE_DOMAIN";
3233
}
3334

3435
public abstract static class Flow {

core/src/main/java/com/flowci/core/common/service/SettingServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.flowci.core.common.service;
22

3+
import com.flowci.core.common.config.AppProperties;
34
import com.flowci.core.common.dao.SettingsDao;
45
import com.flowci.core.common.domain.Settings;
56
import com.flowci.core.common.domain.Variables;
@@ -18,6 +19,9 @@ public class SettingServiceImpl implements SettingService {
1819
@Autowired
1920
private Environment environment;
2021

22+
@Autowired
23+
private AppProperties appProperties;
24+
2125
@Autowired
2226
private ServerProperties serverProperties;
2327

@@ -49,6 +53,7 @@ public void setDefaultValue() {
4953
}
5054

5155
s.setServerUrl(serverUrl);
56+
s.setSource(appProperties.getResourceDomain());
5257
settingsDao.save(s);
5358
});
5459
}

core/src/main/java/com/flowci/core/flow/config/FlowConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.flowci.core.common.config.AppProperties;
2222
import com.flowci.core.common.helper.CacheHelper;
23-
import com.flowci.core.common.manager.HttpRequestManager;
2423
import com.flowci.core.flow.domain.Template;
2524
import com.flowci.tree.NodeTree;
2625
import com.github.benmanes.caffeine.cache.Cache;
2726
import lombok.extern.log4j.Log4j2;
2827
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.context.annotation.Bean;
3029
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.io.Resource;
3131
import org.springframework.scheduling.TaskScheduler;
3232
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
3333

@@ -58,13 +58,13 @@ public Cache<String, List<String>> gitBranchCache() {
5858
}
5959

6060
@Bean("templates")
61-
public List<Template> getTemplates(HttpRequestManager httpManager) throws IOException {
62-
String body = httpManager.get(flowProperties.getTemplatesUrl());
61+
public List<Template> getTemplates() throws IOException {
62+
Resource r = flowProperties.getTemplatesUrl();
6363

6464
TypeReference<List<Template>> typeRef = new TypeReference<List<Template>>() {
6565
};
6666

67-
List<Template> list = objectMapper.readValue(body, typeRef);
67+
List<Template> list = objectMapper.readValue(r.getInputStream(), typeRef);
6868
log.info("Templates is loaded from {}", flowProperties.getTemplatesUrl());
6969
return list;
7070
}

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.flowci.core.flow.domain;
22

3+
import com.flowci.core.common.domain.SourceWithDomain;
34
import lombok.Getter;
45
import lombok.Setter;
56

67
@Getter
78
@Setter
8-
public final class Template {
9-
10-
private String url;
9+
public final class Template extends SourceWithDomain {
1110

1211
private String desc;
1312

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
}

0 commit comments

Comments
 (0)