Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watcher: Jira fields do not use template rendering for every field #30068

Closed
elasticmachine opened this issue Feb 15, 2018 · 1 comment
Closed

Comments

@elasticmachine
Copy link
Collaborator

Original comment by @spinscale:

The following example jira configuration will result in no changes in the components array due to the current implementation, regular string fields are correctly replaced

    "actions": {
      "create-jira-issue": {
        "jira": {
          "account": "monitoring",
          "fields": {
            "summary": "correctly replaced {{ctx.payload}}",
            "issuetype": {
              "name": "Alert"
            },
            "components": [
              {
                "name": "not replaced {{ctx.payload}}"
              }
            ],
            "project": {
              "key": "MMA"
            },
            "customfield_10200": 2
          }
        }
      }
    }
  }

The reason for this is the broken ExecutableJiraAction.merge method, because it does not do the right thing when a list, that is not comprised of a string value is found - which for a map is simple to add.

This unit test resembles the broken behaviour and fails on the last assert

    public void testMerge() {
        Map<String, Object> writeableMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("foo", "bar");
        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("COMPONENTS"), instanceOf(List.class));
        List<Map<String, Object>> components = (List<Map<String, Object>>) result.get("COMPONENTS");
        assertThat(components.get(0), hasEntry("NAME", "VALUE"));
    }

@elasticmachine
Copy link
Collaborator Author

Original comment by @spinscale:

this is a preliminary fix, but the full PR should also contain proper unit testing for the merge method itself, as it is untested it seems

--- a/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraAction.java
+++ b/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/actions/jira/ExecutableJiraAction.java
@@ -100,6 +100,8 @@ public class ExecutableJiraAction extends ExecutableAction<JiraAction> {
                     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);
                         }

jakelandis pushed a commit that referenced this issue Jan 29, 2019
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant