Skip to content

Commit 3f68873

Browse files
gaobinlongjakelandis
authored andcommitted
Fix passing params to template or script failed in watcher (elastic#58559)
The main changes are: * Fix custom params are missing when using template or script in watcher's logging action or jira action. * Add yaml tests to test passing params to template or script successfully. Relates to elastic#57625
1 parent dbb78e1 commit 3f68873

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
setup:
3+
- do:
4+
cluster.health:
5+
wait_for_status: yellow
6+
7+
---
8+
teardown:
9+
- do:
10+
watcher.delete_watch:
11+
id: "test_watch"
12+
ignore: 404
13+
14+
---
15+
"Test executing logging action using template with params":
16+
- do:
17+
put_script:
18+
id: log-action
19+
body: >
20+
{
21+
"script":
22+
{
23+
"lang": "mustache",
24+
"source": "{{color}} alert"
25+
}
26+
}
27+
28+
- do:
29+
watcher.put_watch:
30+
id: "test_watch"
31+
body: >
32+
{
33+
"trigger": {
34+
"schedule" : { "interval": "1m" }
35+
},
36+
"input": {
37+
"simple": {
38+
}
39+
},
40+
"condition": {
41+
"always": {}
42+
},
43+
"actions": {
44+
"log" : {
45+
"logging" : {
46+
"text" : {
47+
"id": "log-action",
48+
"params": {
49+
"color": "yellow"
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
- match: { _id: "test_watch" }
57+
- match: { created: true }
58+
59+
- do:
60+
watcher.execute_watch:
61+
id: "test_watch"
62+
63+
- match: { watch_record.watch_id: "test_watch" }
64+
- match: { watch_record.result.actions.0.id: "log" }
65+
- match: { watch_record.result.actions.0.type: "logging" }
66+
- match: { watch_record.result.actions.0.status: "success" }
67+
- match: { watch_record.result.actions.0.logging.logged_text: "yellow alert" }

x-pack/plugin/watcher/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/painless/10_basic.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,61 @@
248248
- match: { "watch_record.result.actions.0.type" : "email" }
249249
- match: { "watch_record.result.actions.0.email.message.subject" : "404 recently encountered" }
250250

251+
---
252+
"Test executing logging action using scripts with params":
253+
- do:
254+
cluster.health:
255+
wait_for_status: yellow
256+
257+
- do:
258+
put_script:
259+
id: log-action
260+
body: >
261+
{
262+
"script":
263+
{
264+
"lang": "painless",
265+
"source": "params.color + \" alert\""
266+
}
267+
}
268+
269+
- do:
270+
watcher.put_watch:
271+
id: "test_watch"
272+
body: >
273+
{
274+
"trigger": {
275+
"schedule" : { "interval": "1m" }
276+
},
277+
"input": {
278+
"simple": {
279+
}
280+
},
281+
"condition": {
282+
"always": {}
283+
},
284+
"actions": {
285+
"log" : {
286+
"logging" : {
287+
"text" : {
288+
"id": "log-action",
289+
"params": {
290+
"color": "yellow"
291+
}
292+
}
293+
}
294+
}
295+
}
296+
}
297+
- match: { _id: "test_watch" }
298+
- match: { created: true }
299+
300+
- do:
301+
watcher.execute_watch:
302+
id: "test_watch"
303+
304+
- match: { watch_record.watch_id: "test_watch" }
305+
- match: { watch_record.result.actions.0.id: "log" }
306+
- match: { watch_record.result.actions.0.type: "logging" }
307+
- match: { watch_record.result.actions.0.status: "success" }
308+
- match: { watch_record.result.actions.0.logging.logged_text: "yellow alert" }

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/common/text/TextTemplateEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public String render(TextTemplate textTemplate, Map<String, Object> model) {
5555
Script script = new Script(textTemplate.getType(),
5656
textTemplate.getType() == ScriptType.STORED ? null : "mustache", template, options, mergedModel);
5757
TemplateScript.Factory compiledTemplate = service.compile(script, Watcher.SCRIPT_TEMPLATE_CONTEXT);
58-
return compiledTemplate.newInstance(model).execute();
58+
return compiledTemplate.newInstance(mergedModel).execute();
5959
}
6060

6161
private String trimContentType(TextTemplate textTemplate) {

0 commit comments

Comments
 (0)