How to configure the Jenkins Cobertura plugin to monitor specific packages?

There are several packages in my project ("models", "controllers", etc.). I created Jenkins with the Cobertura plugin to generate coverage reports, which is great. I would like to note that the assembly is unstable if the coverage falls below a certain threshold, but only on certain packages (for example, “controllers”, but not “models”). However, I don’t see an obvious way to do this in the configuration user interface - it seems that the thresholds are global.

Is there any way to do this?

+3
source share
1 answer

(Answering my own question here)

, - . script, , , - , , . /, . .

#!/usr/bin/env python

'''
Jenkins' Cobertura plugin doesn't allow marking a build as successful or
failed based on coverage of individual packages -- only the project as a
whole. This script will parse the coverage.xml file and fail if the coverage of
specified packages doesn't meet the thresholds given

'''

import sys

from lxml import etree

PACKAGES_XPATH = etree.XPath('/coverage/packages/package')


def main(argv):
    filename = argv[0]
    package_args = argv[1:] if len(argv) > 1 else []
    # format is package_name:coverage_threshold
    package_coverage = {package: int(coverage) for
        package, coverage in [x.split(':') for x in package_args]}

    xml = open(filename, 'r').read()
    root = etree.fromstring(xml)

    packages = PACKAGES_XPATH(root)

    failed = False
    for package in packages:
        name = package.get('name')
        if name in package_coverage:
            # We care about this one
            print 'Checking package {} -- need {}% coverage'.format(
                name, package_coverage[name])
            coverage = float(package.get('line-rate', '0.0')) * 100
            if coverage < package_coverage[name]:
                print ('FAILED - Coverage for package {} is {}% -- '
                       'minimum is {}%'.format(
                        name, coverage, package_coverage[name]))
                failed = True
            else:
                print "PASS"

    if failed:
        sys.exit(1)

if __name__ == '__main__':
    main(sys.argv[1:])
+2

All Articles