3
我的項目有一些包(「模型」,「控制器」等)。我使用Cobertura插件創建了Jenkins以生成覆蓋率報告,這非常棒。如果覆蓋率下降到某個閾值以下,但是僅限於某些包(例如「控制器」,而不是「模型」),我希望將構建標記爲不穩定。但是,在配置界面中我看不到一個明顯的方法 - 它看起來像閾值是全局的。如何配置Jenkins Cobertura插件來監視特定的包?
有沒有辦法做到這一點?
我的項目有一些包(「模型」,「控制器」等)。我使用Cobertura插件創建了Jenkins以生成覆蓋率報告,這非常棒。如果覆蓋率下降到某個閾值以下,但是僅限於某些包(例如「控制器」,而不是「模型」),我希望將構建標記爲不穩定。但是,在配置界面中我看不到一個明顯的方法 - 它看起來像閾值是全局的。如何配置Jenkins Cobertura插件來監視特定的包?
有沒有辦法做到這一點?
(這裏回答我的問題)
據我所知,這是不可能的 - 我的後找了幾天都沒有見過。我編寫了一個簡單的腳本,它可以做我想做的事情 - 獲取覆蓋率輸出,解析它,並在特定軟件包的覆蓋率未達到特定閾值時構建失敗。它很髒,可以清理/擴展,但基本的想法在這裏。歡迎評論。
#!/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:])