Displaying filtered assets on Confluence page with BobSwift "Scripting for Confluence"plugin

This example explains how to search for assets with Jira Api and display results as table in Confluence page.

It is assumed "Scripting for Confluence" is installed on Confluence: https://marketplace.atlassian.com/apps/266/scripting-for-confluence?hosting=server&tab=overview

Add BobSwift Groovy Macro to a page and leave parameters as it is.



Write the script which is at the en of this page

 


Configure the parameters and Groovy

Replace AUTH_HASH with the hash of Jira admin and user pass. You can use https://www.blitter.se/utils/basic-authentication-header-generator/ .


Examples calls asset search API two times and 2 tables are listed:

  • Personal Computers - Filtered by "Operating System"=Fedora Installed 
  • Servers - Filtered by "Device Status"=Active

It is assumed that each rest API call is filtered for one asset type to display it as a table. But it is not crucial. You can display for different types attributes in one column, just HTML coding.


In groovy script, printAssets gets parameters of the search. It is easy to get, just go to Asset Navigator, make filtering and sorting and then inspect the browser and get the "query" network call's "Request Payload". Here is an example screenshot:


Copy the blue selected script and give it as "queryToPostJson1" parameter. 

If you need to display only one table delete last 3 lines about "queryToPostJson2" or copy them if you need more.

Groovy Script

import groovy.json.JsonSlurper
import org.apache.log4j.Level
import org.apache.log4j.Logger

def printAssets(queryToPostJson) {

    def post = new URL("https://jira-test.snapbytes.com/rest/jip-api/1.0/index/query").openConnection();
    def AUTH_HASH = 'Basic YWRtaW46YWRtaW4=';
    // auth hash for authorization. this sample is for user=admin, and password=admin

    post.setRequestMethod("POST")
    post.setDoOutput(true)
    post.setRequestProperty("Content-Type", "application/json")
    post.setRequestProperty("Authorization", AUTH_HASH)
    post.getOutputStream().write(queryToPostJson.getBytes("UTF-8"));
    def postRC = post.getResponseCode();
    def result = '';

    if (postRC.equals(200)) {

        def jsonSlurper = new JsonSlurper()
        def json = jsonSlurper.parseText(post.getInputStream().getText())
        def assets = json.assets;

        def headerRow = '';
        headerRow += '<td>Asset ID</td>';
        headerRow += '<td>Name</td>';
        headerRow += '<td>Type</td>';
        headerRow += '<td>Create Date</td>';

        def isHeaderRowSet = false;
        if (assets != null) {
            assets.each { asset ->
                def oneRow = '';
                oneRow += '<td>' + asset.id + '</td>';
                oneRow += '<td>' + asset.name + '</td>';
                oneRow += '<td>' + asset.formName + '</td>';
                oneRow += '<td>' + asset.createdFormatted + '</td>';
                asset.inventoryItems.each { assetItem ->
                    oneRow += '<td>' + assetItem.textValues + '</td>';
                    if (!isHeaderRowSet) {
                        headerRow += '<td>' + assetItem.attributeName + '</td>';
                    }
                }
                if (!isHeaderRowSet) {
                    isHeaderRowSet = true;
                    result += '<thead><tr style="background-color: #e1e4e8; font-weight:bold">' + headerRow + '</tr></thead>\n';
                }
                result += '<tr>' + oneRow + '</tr>\n';
            }
        }
    }
    println('<table>' + result + '</table>');
}

println('<style> '+
        ' table { '+
        '   border-collapse: collapse; '+
        ' } '+
        ' th, td { '+
        '   border: 1px solid #ededed; '+
        '   padding: 10px; '+
        '   text-align: left; '+
        ' } '+
        ' </style>'); 

println('<h3>Fedora Installed Personal Computers</h3>');
def queryToPostJson1 ='{"searchType":"basic","listType":"list","genericKeyword":null,"queryIndexSearchParams":[{"field":"Operating System","keyword":"","keywords":["Fedora Linux"],"fieldType":"LIST","range":true},{"field":"form.name","keyword":null,"keywords":["Personal Computers"],"fieldType":"LIST","minNum":null,"maxNum":null,"minDate":null,"maxDate":null,"range":false},{"field":"scheme.name","keyword":null,"keywords":[],"fieldType":"LIST","minNum":null,"maxNum":null,"minDate":null,"maxDate":null,"range":false}],"pageNumber":1,"pageSize":50,"sortDirection":"asc","sortField":"asset.name","respTime":1571990917211}';
printAssets(queryToPostJson1);

println('<h3>Servers - Filtered by Device Status=Active</h3>');
def queryToPostJson2 ='{"searchType":"basic","listType":"list","genericKeyword":null,"queryIndexSearchParams":[{"field":"Device Status","keyword":"","keywords":["Active"],"fieldType":"LIST","minNum":null,"maxNum":null,"minDate":null,"maxDate":null,"range":true},{"field":"scheme.name","keyword":null,"keywords":[],"fieldType":"LIST","minNum":null,"maxNum":null,"minDate":null,"maxDate":null,"range":false},{"field":"form.name","keyword":null,"keywords":["Server"],"fieldType":"LIST","minNum":null,"maxNum":null,"minDate":null,"maxDate":null,"range":false}],"pageNumber":1,"pageSize":50,"sortDirection":"asc","sortField":"asset.name","respTime":1572004822394}';
printAssets(queryToPostJson2);



Result