Jenkins GSoC: Code Coverage API plugin A new code coverage

Jenkins GSoC: Code Coverage API plugin A new code coverage

说明:由于微信不允许外部链接,请点击文末地址,访问文中链接。

Overview

The Code Coverage API plugin is a sub-project of Jenkins in GSoC 2018. GSoC is an activity organized by Google to help school students enter the open source community and contribute code to open source organizations.

In this project, my mentors are Steven Christou, Supun Wanniarachchi, Jeff Pearce and Oleg Nenashev.

Currently in Jenkins, there are many plug-ins that implement the access of code coverage tools, such as Cobertura Plugin, Jacoco Plugin, Clover Plugin... But the configuration items of these plug-ins, the charts displayed on the result page, and the displayed content are similar.

Therefore, compared to writing a new plug-in for each code coverage tool from scratch, we can provide an API plug-in that will greatly reduce the workload of developers. This API plug-in will handle the most repetitive work, encapsulate it into different abstraction layers, and provide easy-to-use API interfaces for other plug-ins to implement.

Supported code coverage tools

  • Built-in

    • JaCoCo
  • Other plugins that implement the Code Coverage API plugin

    • Coverage (Plugin Coverage)

    • llvm-cov (llvm-cov Plugin)

Features

  • Modern chart

  • Code coverage change trend graph

  • Support source code browsing

  • Support Pipeline and Parallel Pipeline

  • Support Report combining

  • Provide REST API

  • Flexible Failed Conditions

Modern chart

In the summary table we can see an overview of the code coverage at the current location.

Jenkins GSoC: Code Coverage API plugin A new code coverage
In the sub-summary table, you can see the code coverage of each sub-item. At the same time, use the range handler in the upper right corner to filter out the items we want to see to reduce the size of the table. You can enter the details page of the sub-item by clicking the name of the node to see more information about the code coverage of the sub-item.

Jenkins GSoC: Code Coverage API plugin A new code coverage

Code coverage change trend graph

We also support code coverage trend graphs to show the changing trend of code coverage between builds.

Jenkins GSoC: Code Coverage API plugin A new code coverage

Source code browsing

Enable source code browsing by setting the Source File Storing Level to save last build source files (the source code will be displayed in the current and last build result pages) or save all build source files (the source code is displayed in the result pages of all builds) .
Jenkins GSoC: Code Coverage API plugin A new code coverage
Then we can see the source code and the code coverage information associated with it in the node of the File element.

Jenkins GSoC: Code Coverage API plugin A new code coverage

Pipeline 和 Parallel Pipeline

The API plug-in provides support for Pipeline and Parallel Pipeline. You can call the plug-in in different branches:

node {
    parallel firstBranch: {
        publishCoverage adapters: [jacocoAdapter('target/site/jacoco/jacoco.xml')]
}, secondBranch: {
        publishCoverage adapters: [jacocoAdapter('jacoco.xml')]
    }
}

Reports Combining

By setting tags for publishCoverage, the reports with the same tag are combined into one report.

node {
    parallel firstBranch: {
        publishCoverage adapters: [jacocoAdapter('target/site/jacoco/jacoco.xml')], tag: ‘t’
}, secondBranch: {
        publishCoverage adapters: [jacocoAdapter('jacoco.xml')], tag: ‘t’
    }
}

REST API

We provide REST API for other applications to obtain coverage information.

  • Coverage: …/{buildNumber}/coverage/…/result/api/{json|xml}

  • Coverage changes: …/{buildNumber}/coverage/…/trend/api/{json|xml}

  • Coverage of the last build: …/{buildNumber}/coverage/…/last/result/api/{json|xml}

  • Coverage change of the last build: …/{buildNumber}/coverage/…/last/trend/api/{json|xml}

Flexible Failed Conditions

We can set failure conditions for different elements at the Global and Adapter levels to control the result of the Build.

Jenkins GSoC: Code Coverage API plugin A new code coverage

If the code coverage meets the failure conditions, the plug-in will fail the current Build.

Jenkins GSoC: Code Coverage API plugin A new code coverage

Other functions

We also support other functions such as automatic detection report and filter coverage. You can find more information in the plug-in documentation.

Architecture

The plug-in will mainly do the following things during its operation:

  • Find the code coverage report file according to the user's configuration

  • Use the Adapter to convert the report file into a unified standard format

  • Parse standard format report files and merge them

  • Display the parsed result

Therefore, we can simply write an Adapter to implement a new code coverage tool. This Adapter only needs to do one thing to convert code coverage reports in other formats into the standard format of our plug-in. The implementation of Adapter is based on ExtensionPoint, so we can separate the implementation of adapter into different plug-ins, and the plug-ins will automatically discover them. At the same time, in order to simplify the transformation process, we also provide a series of abstraction layers.

The below diagram show the architecture of Code Coverage API plugin

Jenkins GSoC: Code Coverage API plugin A new code coverage

Implement a new code coverage plugin

We implement a new plug-in by implementing the extension point of CoverageReportAdapter. By using the abstraction layer provided by our plugin, we can implement JaCoCo as simple as the following:

public final class JacocoReportAdapter extends JavaXMLCoverageReportAdapter {

    @DataBoundConstructor
    public JacocoReportAdapter(String path) {
        super(path);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getXSL() {
        return "jacoco-to-standard.xsl";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getXSD() {
        return null;
    }

    @Symbol("jacoco")
    @Extension
    public static final class JacocoReportAdapterDescriptor extends JavaCoverageReportAdapterDescriptor {

        public JacocoReportAdapterDescriptor() {
            super(JacocoReportAdapter.class);
        }

        @Nonnull
        @Override
        public String getDisplayName() {
            return Messages.JacocoReportAdapter_displayName();
        }
    }
}

Here we only did two things, implemented an abstraction layer for writing Java XML reports, and provided an XSL file that converts JaCoCo reports into our standard format.

If you want to implement a code coverage tool that we do not provide an abstraction layer, you also need to register CoverageElement and implement a simple Parser. You can refer to llvm-cov Plugin. For more information, please refer to the plugin's GitHub wiki page.

附文中链接:

 Code Coverage API plugin:https://jenkins.io/projects/gsoc/

 Steven Christou:https://github.com/christ66

 Supun Wanniarachchi:https://github.com/Supun94

 Jeff Pearce:https://github.com/jeffpearce

 Oleg Nenashev:https://github.com/oleg-nenashev

 Cobertura Plugin:https://github.com/jenkinsci/cobertura-plugin

 llvm-cov Plugin:https://github.com/jenkinsci/llvm-cov-plugin

 repo:https://github.com/jenkinsci/code-coverage-api-plugin

Guess you like

Origin blog.51cto.com/15127503/2657649