Skip to content

Commit 6ed35fb

Browse files
Likejakelandis
authored andcommitted
Support merge nested Map in list for JIRA configurations (#37634)
This commit allows JIRA API fields that require a list of key/value pairs (maps), such as JIRA "components" to use use template snippets (e.g. {{ctx.payload.foo}}). Prior to this change the templated value (not the de-referenced value) would be sent via the API and error. Closes #30068
1 parent 09b6028 commit 6ed35fb

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ static Map<String, Object> merge(final Map<String, Object> fields, final Map<Str
8888
for (Object v : (List) value) {
8989
if (v instanceof String) {
9090
newValues.add(fn.apply((String) v));
91+
} else if (v instanceof Map) {
92+
newValues.add(merge(new HashMap<>(), (Map<String, ?>) v, fn));
9193
} else {
9294
newValues.add(v);
9395
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraActionTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
import org.joda.time.DateTimeZone;
2828
import org.mockito.ArgumentCaptor;
2929

30+
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.Collections;
3233
import java.util.HashMap;
34+
import java.util.List;
3335
import java.util.Locale;
3436
import java.util.Map;
3537
import java.util.function.Function;
@@ -311,4 +313,54 @@ public String render(TextTemplate textTemplate, Map<String, Object> model) {
311313
return textTemplate.getTemplate().toUpperCase(Locale.ROOT);
312314
}
313315
}
316+
317+
public void testMerge() {
318+
Map<String, Object> writeableMap = new HashMap<>();
319+
Map<String, Object> mergeNull = ExecutableJiraAction.merge(writeableMap, null, s -> s);
320+
assertTrue(mergeNull.isEmpty());
321+
Map<String, Object> map = new HashMap<>();
322+
map.put("foo", "bar");
323+
map.put("list", Arrays.asList("test1", "test2"));
324+
Map<String, Object> valueMap = new HashMap<>();
325+
valueMap.put("var", "abc");
326+
map.put("map", valueMap);
327+
Map<String, Object> componentMap = new HashMap<>();
328+
componentMap.put("name", "value");
329+
List<Map<String, Object>> list = new ArrayList<>();
330+
list.add(componentMap);
331+
map.put("components", list);
332+
Map<String, Object> result = ExecutableJiraAction.merge(writeableMap, map, s -> s.toUpperCase(Locale.ROOT));
333+
assertThat(result, hasEntry("FOO", "BAR"));
334+
assertThat(result.get("LIST"), instanceOf(List.class));
335+
List<String> mergedList = (List<String>) result.get("LIST");
336+
assertEquals(2, mergedList.size());
337+
assertEquals("TEST1", mergedList.get(0));
338+
assertEquals("TEST2", mergedList.get(1));
339+
Map<String, Object> mergedMap = (Map<String, Object>) result.get("MAP");
340+
assertEquals(1, mergedMap.size());
341+
assertEquals("ABC", mergedMap.get("VAR"));
342+
assertThat(result.get("COMPONENTS"), instanceOf(List.class));
343+
List<Map<String, Object>> components = (List<Map<String, Object>>) result.get("COMPONENTS");
344+
assertThat(components.get(0), hasEntry("NAME", "VALUE"));
345+
346+
// test the fields is not overwritten
347+
Map<String, Object> fields = new HashMap<>();
348+
fields.put("FOO", "bob");
349+
fields.put("LIST", Arrays.asList("test3"));
350+
fields.put("MAP", new HashMap<>());
351+
fields.put("COMPONENTS", new ArrayList<>());
352+
353+
result = ExecutableJiraAction.merge(fields, map, s -> s.toUpperCase(Locale.ROOT));
354+
assertThat(result, hasEntry("FOO", "bob"));
355+
assertThat(result.get("LIST"), instanceOf(List.class));
356+
mergedList = (List<String>) result.get("LIST");
357+
assertEquals(1, mergedList.size());
358+
assertEquals("test3", mergedList.get(0));
359+
mergedMap = (Map<String, Object>) result.get("MAP");
360+
assertTrue(mergedMap.isEmpty());
361+
assertThat(result.get("COMPONENTS"), instanceOf(List.class));
362+
components = (List<Map<String, Object>>) result.get("COMPONENTS");
363+
assertTrue(components.isEmpty());
364+
}
365+
314366
}

0 commit comments

Comments
 (0)