Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit 4f1230c

Browse files
committed
Add TemplateContext to make it more obvious what the context is
Javadoc
1 parent 7c620e8 commit 4f1230c

File tree

5 files changed

+69
-22
lines changed

5 files changed

+69
-22
lines changed

src/main/java/com/ionos/go/plugin/notifier/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ionos.go.plugin.notifier;
22

3+
/** Constants that are re-used within the plugin. */
34
public class Constants {
45
private Constants() {
56
// no instance
@@ -28,5 +29,4 @@ private Constants() {
2829
static final String PLUGIN_GET_VIEW = "go.plugin-settings.get-view";
2930
static final String SERVER_PLUGIN_SETTINGS_GET = "go.processor.plugin-settings.get";
3031
static final String SERVER_SERVER_INFO_GET = "go.processor.server-info.get";
31-
3232
}

src/main/java/com/ionos/go/plugin/notifier/StageStatusHandler.java

+30-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.ionos.go.plugin.notifier.message.GoPluginApiRequestHandler;
55
import com.ionos.go.plugin.notifier.message.incoming.StageStatusRequest;
66
import com.ionos.go.plugin.notifier.message.outgoing.StageAndAgentStatusChangedResponse;
7+
import com.ionos.go.plugin.notifier.template.TemplateContext;
78
import com.ionos.go.plugin.notifier.template.TemplateHandler;
89
import com.ionos.go.plugin.notifier.util.Helper;
910
import com.thoughtworks.go.plugin.api.logging.Logger;
@@ -13,18 +14,27 @@
1314
import lombok.NonNull;
1415

1516
import java.io.IOException;
17+
import java.util.HashMap;
1618
import java.util.Map;
1719

1820
import static com.ionos.go.plugin.notifier.util.JsonUtil.fromJsonString;
1921
import static com.ionos.go.plugin.notifier.util.JsonUtil.toJsonString;
2022
import static com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse.success;
2123

24+
/**
25+
* Processes an update of a stage status. Will
26+
* check all preconditions, instantiate templates
27+
* and send a GChat message.
28+
* @see Constants#PLUGIN_STAGE_STATUS
29+
* */
2230
public class StageStatusHandler implements GoPluginApiRequestHandler {
2331

2432
private static final Logger LOGGER = Logger.getLoggerFor(StageStatusHandler.class);
2533

34+
/** Server info for mapping into the template context. */
2635
private final Map<String, String> serverInfo;
2736

37+
/** Plugin settings.. */
2838
private final Map<String, String> settings;
2939

3040
StageStatusHandler(@NonNull Map<String, String> serverInfo, @NonNull Map<String, String> settings) {
@@ -49,7 +59,7 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) {
4959

5060
try {
5161
TemplateHandler conditionHandler = new TemplateHandler("condition", condition);
52-
String conditionValue = conditionHandler.eval(stageStatus, serverInfo);
62+
String conditionValue = conditionHandler.eval(new TemplateContext(stageStatus, serverInfo));
5363
LOGGER.debug("Instance condition: " + conditionValue);
5464

5565
conditionEval = Boolean.parseBoolean(conditionValue);
@@ -64,21 +74,27 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) {
6474
}
6575

6676
if (conditionEval) {
77+
response = prepareAndSendGChatMessage(stageStatus, response, condition, template, webhookUrl, proxyUrl);
78+
}
79+
return success(toJsonString(response));
80+
}
81+
82+
private StageAndAgentStatusChangedResponse prepareAndSendGChatMessage(StageStatusRequest stageStatus, StageAndAgentStatusChangedResponse response, String condition, String template, String webhookUrl, String proxyUrl) {
83+
String instanceTemplate;
84+
try {
85+
TemplateHandler templateHandler = new TemplateHandler("template", template);
86+
instanceTemplate = templateHandler.eval(new TemplateContext(stageStatus, serverInfo));
87+
LOGGER.debug("Instance template: " + instanceTemplate);
88+
GoogleChatWebhookSender googleChatWebhookSender = new GoogleChatWebhookSender(proxyUrl);
6789
try {
68-
TemplateHandler templateHandler = new TemplateHandler("template", template);
69-
instanceTemplate = templateHandler.eval(stageStatus, serverInfo);
70-
LOGGER.debug("Instance template: " + instanceTemplate);
71-
GoogleChatWebhookSender googleChatWebhookSender = new GoogleChatWebhookSender(proxyUrl);
72-
try {
73-
googleChatWebhookSender.send(webhookUrl, instanceTemplate);
74-
} catch (IOException e) {
75-
response = new StageAndAgentStatusChangedResponse(StageAndAgentStatusChangedResponse.Status.failure, "GChat sending problem: " + e.getMessage());
76-
}
77-
} catch (TemplateException | IOException e) {
78-
LOGGER.warn("Exception for template " + condition, e);
79-
response = new StageAndAgentStatusChangedResponse(StageAndAgentStatusChangedResponse.Status.failure, "Template problem: " + e.getMessage());
90+
googleChatWebhookSender.send(webhookUrl, instanceTemplate);
91+
} catch (IOException e) {
92+
response = new StageAndAgentStatusChangedResponse(StageAndAgentStatusChangedResponse.Status.failure, "GChat sending problem: " + e.getMessage());
8093
}
94+
} catch (TemplateException | IOException e) {
95+
LOGGER.warn("Exception for template " + condition, e);
96+
response = new StageAndAgentStatusChangedResponse(StageAndAgentStatusChangedResponse.Status.failure, "Template problem: " + e.getMessage());
8197
}
82-
return success(toJsonString(response));
98+
return response;
8399
}
84100
}

src/main/java/com/ionos/go/plugin/notifier/ValidateConfigurationHandler.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.ionos.go.plugin.notifier.message.incoming.StageStatusRequest;
66
import com.ionos.go.plugin.notifier.message.incoming.ValidateConfigurationRequest;
77
import com.ionos.go.plugin.notifier.message.outgoing.ValidateConfigurationResponse;
8+
import com.ionos.go.plugin.notifier.template.TemplateContext;
89
import com.ionos.go.plugin.notifier.template.TemplateHandler;
910
import com.ionos.go.plugin.notifier.util.Helper;
1011
import com.ionos.go.plugin.notifier.util.JsonUtil;
@@ -24,6 +25,9 @@
2425
import static com.ionos.go.plugin.notifier.util.JsonUtil.toJsonString;
2526
import static com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse.success;
2627

28+
/** Handles the validation of plugin settings.
29+
* @see Constants#PLUGIN_VALIDATE_CONFIGURATION
30+
* */
2731
class ValidateConfigurationHandler implements GoPluginApiRequestHandler {
2832
private static final Logger LOGGER = Logger.getLoggerFor(GoNotifierPlugin.class);
2933

@@ -124,7 +128,7 @@ private void validateTemplate(ValidateConfigurationRequest validateRequest, List
124128
try {
125129
TemplateHandler handler = new TemplateHandler("template", template);
126130
for (StageStatusRequest sample : newSampleStageStatusRequests()) {
127-
handler.eval(sample, serverInfo);
131+
handler.eval(new TemplateContext(sample, serverInfo));
128132
}
129133
}
130134
catch (Exception e) {
@@ -143,7 +147,7 @@ private void validateCondition(ValidateConfigurationRequest validateRequest, Lis
143147
String nonTrueOrFalse = null;
144148
TemplateHandler handler = new TemplateHandler(Constants.PARAM_CONDITION, condition);
145149
for (StageStatusRequest sample : newSampleStageStatusRequests()) {
146-
String shouldBeBool = handler.eval(sample, serverInfo);
150+
String shouldBeBool = handler.eval(new TemplateContext(sample, serverInfo));
147151
if (!(shouldBeBool.equals("true") || shouldBeBool.equals("false"))) {
148152
nonTrueOrFalse = shouldBeBool;
149153
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ionos.go.plugin.notifier.template;
2+
3+
import com.ionos.go.plugin.notifier.message.incoming.StageStatusRequest;
4+
import lombok.Getter;
5+
import lombok.Value;
6+
7+
import java.util.Map;
8+
9+
/** The context mapping for the template instantiation.
10+
* @see TemplateHandler
11+
* */
12+
@Value
13+
@Getter
14+
public class TemplateContext {
15+
private final StageStatusRequest stageStatus;
16+
private final Map<String, String> serverInfo;
17+
}

src/main/java/com/ionos/go/plugin/notifier/template/TemplateHandler.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@
1616
import java.util.Map;
1717
import java.util.TimeZone;
1818

19+
/** A wrapper around Freemarker to instantiate a template for
20+
* either text expansion or condition evaluation.
21+
* */
1922
public class TemplateHandler {
2023

2124
private static final Logger LOGGER = Logger.getLoggerFor(TemplateHandler.class);
2225

26+
/** The template text to expand. */
2327
private final String templateString;
28+
29+
/** The template instance object after compiling templateString. */
2430
private final Template template;
2531

32+
/** Creates a new instance.
33+
* @param templateName the name of the template for re-using already compared Freemarker templates.
34+
* Needs to be the same for the same template text.
35+
* @param template the freemarker syntax template to expand.
36+
* */
2637
public TemplateHandler(
2738
@NonNull String templateName,
2839
@NonNull String template) throws IOException {
@@ -40,14 +51,13 @@ public TemplateHandler(
4051
cfg);
4152
}
4253

43-
public String eval(@NonNull StageStatusRequest stageStatusRequest, Map<String,String> serverInfo) throws TemplateException, IOException {
54+
/** Expands the template with the given parameters.
55+
* */
56+
public String eval(@NonNull TemplateContext context) throws TemplateException, IOException {
4457

4558
Writer out = new StringWriter();
46-
Map<Object, Object> model = new HashMap<>();
47-
model.put("stageStatus", stageStatusRequest);
48-
model.put("serverInfo", serverInfo);
4959

50-
template.process(model, out);
60+
template.process(context, out);
5161

5262
final String value = out.toString();
5363
LOGGER.debug("Value is: " + value);

0 commit comments

Comments
 (0)