4
4
import com .ionos .go .plugin .notifier .message .GoPluginApiRequestHandler ;
5
5
import com .ionos .go .plugin .notifier .message .incoming .StageStatusRequest ;
6
6
import com .ionos .go .plugin .notifier .message .outgoing .StageAndAgentStatusChangedResponse ;
7
+ import com .ionos .go .plugin .notifier .template .TemplateContext ;
7
8
import com .ionos .go .plugin .notifier .template .TemplateHandler ;
8
9
import com .ionos .go .plugin .notifier .util .Helper ;
9
10
import com .thoughtworks .go .plugin .api .logging .Logger ;
13
14
import lombok .NonNull ;
14
15
15
16
import java .io .IOException ;
17
+ import java .util .HashMap ;
16
18
import java .util .Map ;
17
19
18
20
import static com .ionos .go .plugin .notifier .util .JsonUtil .fromJsonString ;
19
21
import static com .ionos .go .plugin .notifier .util .JsonUtil .toJsonString ;
20
22
import static com .thoughtworks .go .plugin .api .response .DefaultGoPluginApiResponse .success ;
21
23
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
+ * */
22
30
public class StageStatusHandler implements GoPluginApiRequestHandler {
23
31
24
32
private static final Logger LOGGER = Logger .getLoggerFor (StageStatusHandler .class );
25
33
34
+ /** Server info for mapping into the template context. */
26
35
private final Map <String , String > serverInfo ;
27
36
37
+ /** Plugin settings.. */
28
38
private final Map <String , String > settings ;
29
39
30
40
StageStatusHandler (@ NonNull Map <String , String > serverInfo , @ NonNull Map <String , String > settings ) {
@@ -49,7 +59,7 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) {
49
59
50
60
try {
51
61
TemplateHandler conditionHandler = new TemplateHandler ("condition" , condition );
52
- String conditionValue = conditionHandler .eval (stageStatus , serverInfo );
62
+ String conditionValue = conditionHandler .eval (new TemplateContext ( stageStatus , serverInfo ) );
53
63
LOGGER .debug ("Instance condition: " + conditionValue );
54
64
55
65
conditionEval = Boolean .parseBoolean (conditionValue );
@@ -64,21 +74,27 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) {
64
74
}
65
75
66
76
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 );
67
89
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 ());
80
93
}
94
+ } catch (TemplateException | IOException e ) {
95
+ LOGGER .warn ("Exception for template " + condition , e );
96
+ response = new StageAndAgentStatusChangedResponse (StageAndAgentStatusChangedResponse .Status .failure , "Template problem: " + e .getMessage ());
81
97
}
82
- return success ( toJsonString ( response )) ;
98
+ return response ;
83
99
}
84
100
}
0 commit comments