Skip to content

Commit 500e12f

Browse files
authored
Fix passing params to template or script failed in watcher (#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 #57625
1 parent 5065dbc commit 500e12f

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed
Lines changed: 67 additions & 0 deletions
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
@@ -246,3 +246,61 @@
246246
- match: { "watch_record.result.actions.0.type" : "email" }
247247
- match: { "watch_record.result.actions.0.email.message.subject" : "404 recently encountered" }
248248

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