如何在Jenkins activeChoices插件的Groovy脚本中迭代嵌套的Map或列表?

chy5wohz  于 2023-03-17  发布在  Jenkins
关注(0)|答案(1)|浏览(219)

我的第一份工作使用activeChoicesParameter插件,和相当大的挑战。我想实现的是一个复选框列表,分组为“主要”和“特定”与正确格式化的HTML代码。可能的选择列表(由复选框表示)应该从Map到一个特定的标识符,由'单选'下拉列表表示。
因此,我希望在构建页面中获得的复选框列表应该如下所示:
Expected list of choices
目前为止,我尝试的是:

test_group_map = [
  "id_1": [
    [test_group : "group_1", test_subgroup : ["subgroup_1", "subgroup_2", "subgroup_3"] ],
    [test_group : "group_2", test_subgroup : ["subgroup_4", "subgroup_5"] ],
  ],
]

html_to_be_rendered = ""

test_groups = test_group_map[ID]
test_groups.each { groups ->
  html_to_be_rendered = """
    ${html_to_be_rendered}
    <li style=\"list-style: none\">
      <input name=\"value\" alt=\"${groups.test_group}\" json=\"${groups.test_group}\" type=\"checkbox\" class=\" \" id=\"option\">
      <label title=\"${groups.test_group}\" class=\" \" for=\"option\">${groups.test_group}</label>
      <ul style=\"list-style: none\">
        <li>
          <input name=\"value\" alt=\"${groups.test_subgroup}\" json=\"${groups.test_subgroup}\" type=\"checkbox\" class=\"subOption\">
          <label title=\"${groups.test_subgroup}\" class=\" \">${groups.test_subgroup}</label>
        </li>
      </ul>
    </li>
"""
}

html_to_be_rendered = "${html_to_be_rendered}"

return html_to_be_rendered

其结果是:
Approach with list of values
我的第二个办法如下:

test_group_map = [
  "id_1": [
    [test_group : "group_1", test_subgroup : "subgroup_1", test_subgroup : "subgroup_2", test_subgroup : "subgroup_3" ],
    [test_group : "group_2", test_subgroup : "subgroup_4", test_subgroup :  "subgroup_5" ],
  ],
]

html_to_be_rendered = ""

test_groups = test_group_map[ID]
test_groups.each { groups ->
  html_to_be_rendered = """
    ${html_to_be_rendered}
    <li style=\"list-style: none\">
      <input name=\"value\" alt=\"${groups.test_group}\" json=\"${groups.test_group}\" type=\"checkbox\" class=\" \" id=\"option\">
      <label title=\"${groups.test_group}\" class=\" \" for=\"option\">${groups.test_group}</label>
      <ul style=\"list-style: none\">
        <li>
          <input name=\"value\" alt=\"${groups.test_subgroup}\" json=\"${groups.test_subgroup}\" type=\"checkbox\" class=\"subOption\">
          <label title=\"${groups.test_subgroup}\" class=\" \">${groups.test_subgroup}</label>
        </li>
      </ul>
    </li>
"""
}

html_to_be_rendered = "${html_to_be_rendered}"

return html_to_be_rendered

得到了以下结果:
Approach with many values for one key
(Note封装主要部分html_to_be_rendered变量实际上包含一些格式化HTML标记,因此使用它们;我只是为了可读性而删减了值)
我还尝试过迭代'subgroup'键的值,但Jenkins什么也没给我(这意味着从脚本生成的HTML代码不正确,或者据我所知,Groovy脚本在运行时失败,不返回任何HTML)。
所以最后我相信无论是迭代呈现的Map还是Map本身都有问题。任何建议都将受到赞赏。

kmpatx3s

kmpatx3s1#

如果我没理解错的话,在你的第一个版本中你想要迭代groups.test_subgroup,我把这个迭代添加到了你的第一个版本中:

test_group_map = [
        "id_1": [
                [test_group : "group_1", test_subgroup : ["subgroup_1", "subgroup_2", "subgroup_3"] ],
                [test_group : "group_2", test_subgroup : ["subgroup_4", "subgroup_5"] ],
        ],
]

html_to_be_rendered = ""

String ID = "id_1"
test_groups = test_group_map[ID]

test_groups.each { groups ->
    html_to_be_rendered = """
    ${html_to_be_rendered}
    <li style=\"list-style: none\">
      <input name=\"value\" alt=\"${groups.test_group}\" json=\"${groups.test_group}\" type=\"checkbox\" class=\" \" id=\"option\">
      <label title=\"${groups.test_group}\" class=\" \" for=\"option\">${groups.test_group}</label>
      <ul style=\"list-style: none\">
        <li>""" +
            groups.test_subgroup.collect { name -> """
          <input name=\"value\" alt=\"${name}\" json=\"${name}\" type=\"checkbox\" class=\"subOption\">
          <label title=\"${name}\" class=\" \">${name}</label>"""}.join('<br>') + """
        </li>
      </ul>
    </li>
"""
}

html_to_be_rendered = "${html_to_be_rendered}"

println html_to_be_rendered

如果这太难看了,可以考虑使用HTML MarkupBuilder

def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(writer)

builder.html {
    head {}
    body {
        mkp.yieldUnescaped html_to_be_rendered

        test_groups.each { groups ->
            li(style: "list-style: none") {
                input(name: "value", alt: groups.test_group, json: groups.test_group, type: "checkbox", class: " ", id: "option")
                label(title: groups.test_group, class: " ", for: "option", groups.test_group)
                ul(style: "list-style: none"){
                    li {
                        groups.test_subgroup.each { name ->
                            input(name: "value", alt: name, json: name, type: "checkbox", class: "subOption")
                            label(title: name, class: " ", name)
                            br()
                        }
                    }
                }
            }
        }
    }
}

println(writer.toString())

我没有在Jenkins身上做过任何测试。

相关问题