How to use Coverity in groovy pipeline?

734 views
Skip to first unread message

Gardell, Steven

unread,
Nov 19, 2016, 7:50:47 AM11/19/16
to jenkins...@googlegroups.com

Posting a question and the answer.

 

The existing Coverity pluggin does not have any pipeline support.

 

Background: A Coverity scan involves 3 activities: “processing source code” , “analyzing the results”, “committing” results to the Coverity Connect server. The first if these is often in the context of a wrapper around a ‘normal’ build. The first and second steps are both time consuming – suggesting a pipeline.

 

The following is an outline of how to structure this in the pipeline and then, most importantly, how to recover the results in a useful fashion. The Coverity Connect server is used to create a ‘view’ that presents ‘issues of interest’ for this build. The rules for the build & analysis are in ‘build’ scripts following the ‘use pipelines for orchestration mantra.’

 

I am new to both Groovy and Jenkins so any suggestions are welcome – but it does work, so I don’t need any help with that.

 

 

 

Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6')

import static groovyx.net.http.ContentType.TEXT

import groovyx.net.http.HTTPBuilder

import groovy.json.JsonSlurper

import groovy.json.JsonParserType

 

class uriHelper implements java.io.Serializable {

    private String ccUrl;

    private String project;

    public uriHelper(String ccUrl, String project) {

        this.ccUrl = ccUrl;

        this.project = project.replaceAll(" ","%20");

    }

    public String GetReqUrl(String view) {

       return String.format("http://%s/api/viewContents/issues/v1/%s?projectId=%s&rowCount=-1",

           ccUrl, view.replaceAll(" ","%20"),  project);

        }

    public String GetCidUrl(Integer cid) {

        return String.format("http://%s/query/defects.htm?project=%s&cid=%d",

            ccUrl, project, cid);

 

    }

}

 

def ShortenPath(String path) {

    // customize…

    path=path.replaceFirst(".*/foobar/","foobar/")

    return path

}

 

def covProject = "NameOfCoverityProject"

def covView="NameOfCovertyView"

def ccUrl = "Coverity-Connect-URL:8080"

 

def helper = new uriHelper(ccUrl, covProject)

 

stage("Stage 1 - Build with cov-build wrapper") {

    //

    // Build activities to create the coverity intermediate directory

    //

    //node("somenode1") { 'invoke build with cov-build wrapper' }

}

 

stage("Stage 2 - Coverity Analyze & commit") {

    parallel(

        'Coverity' : {

            //

            // Run cov-analyze on the intermediate directory and then cov-commit

            //

            //node("somenode2") { 'invoke cov-analyze/commit' }

            node {

            def s = "admin:password"

            String encoded = s.bytes.encodeBase64().toString()

            def request = helper.GetReqUrl(covView)

            def client = new HTTPBuilder(request)

 

            def headers = [ Authorization: " Basic " + encoded,

                    Accept: "application/json" ]

            client.setHeaders(headers)

 

            def json = client.get(contentType: TEXT)

            def slurper = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY)

            def parsed = slurper.parse( json )

            //

            // The JSON output will consist of a single top-level map.

            // Use the outer loop to step into it. Note that we use 'for'

            // loops instead of 'each' since 'each' is broken in Jenkins pipeline

            //

            for (values in parsed) {

                def rows = values.getValue().get('rows')

                if (rows.size() > 0) {

                    def summary = manager.createSummary("warning.gif")

                    summary.appendText("<b>Coverity Issues:</b><table>",false)

                    for (row in rows) {

                        def cidUrl = helper.GetCidUrl(row.cid)

                        def path = ShortenPath(row.displayFile)

                        summary.appendText("<tr><td><a><a href=\"${cidUrl}\"> ${row.cid}</a></td><td>$row.displayType</td><td>$path</td></tr>", false)

                    }

                    summary.appendText("</table>", false)

                }

            }

        }})

}

Reply all
Reply to author
Forward
0 new messages