javajsonjenkinsgroovyjelly

jelly: Create a table using JSON object


I am trying to build a Jenkins Post build plugin where I have to process JSON file (contains test results) and show it in tabular format in Jenkins once build is executed.

Following are the steps done till now:

  1. Created Jenkins plugin
  2. Able to retrieve JSON file content and read it as Google gson JSONElement.
  3. Built BuildAction (extends Action) to show the results.
  4. In index.jelly (view for BuildAction) corresponding to BuildAction, trying to show each record in JSON file, as a row.

JSON File sample:

{
    "records": [{
        "objectProps": {
            "OTYPE": "TEST",
            "NAME": "testMethodError",

        }
    },
    {
        "objectProps": {
            "OTYPE": "TEST",
            "NAME": "testMethodFail",
        }
    }]
}

BuildAction class:

public class BuildAction implements Action {
    private JsonElement results;
    private Run<?, ?> build;
    TaskListener listener;

    // this value referred as `it.results` in `index.jelly`
    public JsonArray getResults(){
        return results.getAsJsonObject().get("records").getAsJsonArray();
    }

}

current index.jelly for above BuildAction class

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout">
    <l:layout>
        <st:include it="${it.build}" page="sidepanel.jelly"/>
        <l:main-panel>
            <table> Test - Wise Results
             <j:forEach items="${it.results}" var="i">
                <tr><td>Test case name: ${i}</td></tr>
             </j:forEach>
            </table>         
        </l:main-panel>

    </l:layout>
</j:jelly>

Actual behaviour:

As of now, ${results} value is of JSONArray type. forEach in jelly, I am able iterate over and get the record using var i (syntax ${i}). i refers to each record in records JSONArray. Now, I want to access objectProps.NAME field using i, I don't know the syntax in Jelly to achieve the same.

Expected behaviour:

I wan to iterate through records array in JSON file and each child/jsonObject as one table row (and its values as corresponding columns).

something similar to this (to access NAME value):

<j:forEach items="${it.results}" var="i">
   <tr><td>Test case name: ${i}."objectProps"."NAME"</td></tr>
</j:forEach>

Need help in building the table out of a JSON using Jelly. Any other way of achieving the same also welcome (please post code samples when suggesting the same).

Note: Groovy related answer also welcome as Jenkins support both Jelly and Groovy for View.


Solution

  • I am interested in solving you problem, but might not have a 100% certain answer as I can't test locally.

    Have you tried to use ${i.objectProps.NAME}or ${i."objectProps"."NAME"} instead of ${i}."objectProps"."NAME"in your example?

    You could also see if g:evaluate is available, as jelly might not evaluate your variable without explicitly telling it to do so. You can find some documentation on g:evaluate here.