Skip to content

Commit

Permalink
Support merge nested Map in list for JIRA configurations (#37634)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Like authored and jakelandis committed Jan 29, 2019
1 parent 09b6028 commit 6ed35fb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ static Map<String, Object> merge(final Map<String, Object> fields, final Map<Str
for (Object v : (List) value) {
if (v instanceof String) {
newValues.add(fn.apply((String) v));
} else if (v instanceof Map) {
newValues.add(merge(new HashMap<>(), (Map<String, ?>) v, fn));
} else {
newValues.add(v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import org.joda.time.DateTimeZone;
import org.mockito.ArgumentCaptor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -311,4 +313,54 @@ public String render(TextTemplate textTemplate, Map<String, Object> model) {
return textTemplate.getTemplate().toUpperCase(Locale.ROOT);
}
}

public void testMerge() {
Map<String, Object> writeableMap = new HashMap<>();
Map<String, Object> mergeNull = ExecutableJiraAction.merge(writeableMap, null, s -> s);
assertTrue(mergeNull.isEmpty());
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("list", Arrays.asList("test1", "test2"));
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("var", "abc");
map.put("map", valueMap);
Map<String, Object> componentMap = new HashMap<>();
componentMap.put("name", "value");
List<Map<String, Object>> list = new ArrayList<>();
list.add(componentMap);
map.put("components", list);
Map<String, Object> result = ExecutableJiraAction.merge(writeableMap, map, s -> s.toUpperCase(Locale.ROOT));
assertThat(result, hasEntry("FOO", "BAR"));
assertThat(result.get("LIST"), instanceOf(List.class));
List<String> mergedList = (List<String>) result.get("LIST");
assertEquals(2, mergedList.size());
assertEquals("TEST1", mergedList.get(0));
assertEquals("TEST2", mergedList.get(1));
Map<String, Object> mergedMap = (Map<String, Object>) result.get("MAP");
assertEquals(1, mergedMap.size());
assertEquals("ABC", mergedMap.get("VAR"));
assertThat(result.get("COMPONENTS"), instanceOf(List.class));
List<Map<String, Object>> components = (List<Map<String, Object>>) result.get("COMPONENTS");
assertThat(components.get(0), hasEntry("NAME", "VALUE"));

// test the fields is not overwritten
Map<String, Object> fields = new HashMap<>();
fields.put("FOO", "bob");
fields.put("LIST", Arrays.asList("test3"));
fields.put("MAP", new HashMap<>());
fields.put("COMPONENTS", new ArrayList<>());

result = ExecutableJiraAction.merge(fields, map, s -> s.toUpperCase(Locale.ROOT));
assertThat(result, hasEntry("FOO", "bob"));
assertThat(result.get("LIST"), instanceOf(List.class));
mergedList = (List<String>) result.get("LIST");
assertEquals(1, mergedList.size());
assertEquals("test3", mergedList.get(0));
mergedMap = (Map<String, Object>) result.get("MAP");
assertTrue(mergedMap.isEmpty());
assertThat(result.get("COMPONENTS"), instanceOf(List.class));
components = (List<Map<String, Object>>) result.get("COMPONENTS");
assertTrue(components.isEmpty());
}

}

0 comments on commit 6ed35fb

Please sign in to comment.