Skip to content

Commit

Permalink
added colour and dp config options
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyarr committed Jul 3, 2017
1 parent 2370c53 commit f681c60
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 50 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Changelog
---------

1.0 (2017-06-30)
1.0.0 (2017-06-30)
******************

- Initial release.


1.1.0 (2017-07-03)
******************

- Added coverage threshold and decimal point cofiguration options
25 changes: 6 additions & 19 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
:target: https://badge.fury.io/py/jenkins-badges


`jenkins_badges` is a small flask app that provides dynamic badge images based on data from Jenkins CI.
`jenkins_badges` is a small flask app that serves dynamic badge images based on data from Jenkins CI.

Supported badges
-----------------

+---------+---------------------------------------------------------------------------------------------------------------+----------------------------------+
|coverage | .. image:: https://cdn.rawgit.com/jeremyarr/jenkins_badges/master/docs/_static/coverage_green.svg | default: 80% + |
|Badge | Examples | Default |
+=========+===============================================================================================================+==================================+
|coverage | .. image:: https://cdn.rawgit.com/jeremyarr/jenkins_badges/master/docs/_static/coverage_green.svg | 80% + |
+ +---------------------------------------------------------------------------------------------------------------+----------------------------------+
| | .. image:: https://cdn.rawgit.com/jeremyarr/jenkins_badges/master/docs/_static/coverage_yellow.svg | default: 20%-80% |
| | .. image:: https://cdn.rawgit.com/jeremyarr/jenkins_badges/master/docs/_static/coverage_yellow.svg | 20%-80% |
+ +---------------------------------------------------------------------------------------------------------------+----------------------------------+
| | .. image:: https://cdn.rawgit.com/jeremyarr/jenkins_badges/master/docs/_static/coverage_red.svg | default: < 20% |
| | .. image:: https://cdn.rawgit.com/jeremyarr/jenkins_badges/master/docs/_static/coverage_red.svg | < 20% |
+ +---------------------------------------------------------------------------------------------------------------+----------------------------------+
| | .. image:: https://cdn.rawgit.com/jeremyarr/jenkins_badges/master/docs/_static/coverage_error.svg | error getting coverage data |
+---------+---------------------------------------------------------------------------------------------------------------+----------------------------------+
Expand All @@ -36,20 +37,6 @@ Jenkins Requirements

For the coverage badge to work, your Jenkins instance must have the `Cobertura plugin <https://wiki.jenkins.io/display/JENKINS/Cobertura+Plugin>`_ installed with coverage data being supplied to it after every successful build.

You can test out whether `jenkins_badges` will be able to communicate with Jenkins by performing the following API request:

Linux:

.. code-block:: bash
$ curl http<s>://<path to your jenkins instance>/job/<job name>/lastSuccessfulBuild/cobertura/api/json/?depth=2
Sample Output:

.. code-block:: console
{"_class":"hudson.plugins.cobertura.targets.CoverageResult","results":{"children":[{"children":[{}],"elements":[{},{},{},{}],"name":"marbl"}],"elements":[{"denominator":1.0,"name":"Packages","numerator":1.0,"ratio":100.0},{"denominator":1.0,"name":"Files","numerator":1.0,"ratio":100.0},{"denominator":1.0,"name":"Classes","numerator":1.0,"ratio":100.0},{"denominator":5.0,"name":"Lines","numerator":4.0,"ratio":80.0},{"denominator":0.0,"name":"Conditionals","numerator":0.0,"ratio":100.0}],"name":"Cobertura Coverage Report"}}
Quickstart
----------

Expand Down
59 changes: 39 additions & 20 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ welcome to jenkins_badges

Current version: v\ |version|.

`jenkins_badges` is a small flask app that provides dynamic badge images based on data from Jenkins CI.
`jenkins_badges` is a small flask app that serves dynamic badge images based on data from Jenkins CI.

.. contents::
:local:
Expand All @@ -16,11 +16,13 @@ Supported badges
-----------------

+---------+----------------------------------------+----------------------------------+
|coverage | .. image:: _static/coverage_green.svg | default: 80% + |
|Badge | Examples | Default |
+=========+========================================+==================================+
|coverage | .. image:: _static/coverage_green.svg | 80% + |
+ +----------------------------------------+----------------------------------+
| | .. image:: _static/coverage_yellow.svg | default: 20%-80% |
| | .. image:: _static/coverage_yellow.svg | 20%-80% |
+ +----------------------------------------+----------------------------------+
| | .. image:: _static/coverage_red.svg | default: < 20% |
| | .. image:: _static/coverage_red.svg | < 20% |
+ +----------------------------------------+----------------------------------+
| | .. image:: _static/coverage_error.svg | error getting coverage data |
+---------+----------------------------------------+----------------------------------+
Expand All @@ -42,20 +44,6 @@ Jenkins Requirements

For the coverage badge to work, your Jenkins instance must have the `Cobertura plugin <https://wiki.jenkins.io/display/JENKINS/Cobertura+Plugin>`_ installed with coverage data being supplied to it after every successful build.

You can test out whether `jenkins_badges` will be able to communicate with Jenkins by performing the following API request:

Linux:

.. code-block:: bash
$ curl http<s>://<path to your jenkins instance>/job/<job name>/lastSuccessfulBuild/cobertura/api/json/?depth=2
Sample Output:

.. code-block:: console
{"_class":"hudson.plugins.cobertura.targets.CoverageResult","results":{"children":[{"children":[{}],"elements":[{},{},{},{}],"name":"marbl"}],"elements":[{"denominator":1.0,"name":"Packages","numerator":1.0,"ratio":100.0},{"denominator":1.0,"name":"Files","numerator":1.0,"ratio":100.0},{"denominator":1.0,"name":"Classes","numerator":1.0,"ratio":100.0},{"denominator":5.0,"name":"Lines","numerator":4.0,"ratio":80.0},{"denominator":0.0,"name":"Conditionals","numerator":0.0,"ratio":100.0}],"name":"Cobertura Coverage Report"}}
Quickstart
----------

Expand Down Expand Up @@ -120,7 +108,7 @@ Linux:
import jenkins_badges
app = jenkins_badges.create_app()
app = jenkins_badges.create_app(from_envvar=True)
app.run()
Output:
Expand Down Expand Up @@ -168,14 +156,45 @@ Just like any Flask app, a `jenkins_badges` app can be placed on a server with W
#name of app must be "application"
application = create_app()
Comparison with Shields.io API
--------------------------------------------------------
`shields.io <https://shields.io>`_ has a simple API for accessing Jenkins coverage data, **providing your Jenkins anonymous user is granted read access**:

`https://img.shields.io/jenkins/c/<scheme>/<jenkins host>/job/<Jenkins Job>.svg`

`shields.io` also hard codes badge colours and the number of decimal points. `jenkins_badges` is more suited if you want finer control of how your coverage badge is displayed or if you only allow authenticated users to access your jenkins instance.


Responsiveness
---------------
`jenkins_badges` serves badge images with a "maxAge" `cache-control` header value of 30 seconds. As long as the server hosting your documentation respects `cache-control` headers, your badge should update on a page refresh after a jenkins build.
`jenkins_badges` serves badge images with a "maxAge" `cache-control` header value of 30 seconds. It does not perform redirects to `shields.io` due to GitHub's well known `badge caching problems <https://github.com/sbts/github-badge-cache-buster/blob/master/README.md>`_. As long as the server hosting your documentation respects `cache-control` headers, your badge should update on a page refresh after a jenkins build.

The responsiveness of images served by `jenkins_badges` has been successfully tested on readme pages hosted by GitHub.



Troubleshooting:
-----------------

Coverage error badge displayed
*******************************

Problem communicating with Jenkins.

You can test out whether `jenkins_badges` will be able to communicate with Jenkins by performing the following API request:

Linux:

.. code-block:: bash
$ curl http<s>://<path to your jenkins instance>/job/<job name>/lastSuccessfulBuild/cobertura/api/json/?depth=2
Sample Output:

.. code-block:: console
{"_class":"hudson.plugins.cobertura.targets.CoverageResult","results":{"children":[{"children":[{}],"elements":[{},{},{},{}],"name":"marbl"}],"elements":[{"denominator":1.0,"name":"Packages","numerator":1.0,"ratio":100.0},{"denominator":1.0,"name":"Files","numerator":1.0,"ratio":100.0},{"denominator":1.0,"name":"Classes","numerator":1.0,"ratio":100.0},{"denominator":5.0,"name":"Lines","numerator":4.0,"ratio":80.0},{"denominator":0.0,"name":"Conditionals","numerator":0.0,"ratio":100.0}],"name":"Cobertura Coverage Report"}}
Project info
------------

Expand Down
29 changes: 23 additions & 6 deletions jenkins_badges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,43 @@
'''
from flask import Flask

def create_app(base_url=None,username=None,token=None):

def create_app(from_envvar=False,base_url=None,username=None,token=None,
coverage_yellow=None, coverage_red=None,
coverage_decimal_points=None):
'''
creates the flask application object
username: jenkins username
token: username token
base url: jenkins base url
'''
app = Flask(__name__)
if not base_url:
app.config['JENKINS_USERNAME'] = None
app.config['JENKINS_TOKEN'] = None
app.config.from_object('jenkins_badges.default_settings')

if from_envvar:
app.config.from_envvar('JENKINS_BADGES_SETTINGS')
assert "JENKINS_BASE_URL" in app.config
else:
if base_url is None:
raise ValueError("must supply base_url if from_envvar is False")

app.config['JENKINS_BASE_URL'] = base_url
app.config['JENKINS_USERNAME'] = username
app.config['JENKINS_TOKEN'] = token

if username is not None:
app.config['JENKINS_USERNAME'] = username
if token is not None:
app.config['JENKINS_TOKEN'] = token
if coverage_yellow is not None:
app.config['COVERAGE_YELLOW'] = coverage_yellow
if coverage_red is not None:
app.config['COVERAGE_RED'] = coverage_red
if coverage_decimal_points is not None:
app.config['COVERAGE_DECIMAL_POINTS'] = coverage_decimal_points


from jenkins_badges.coverage_badge import coverage_badge
app.register_blueprint(coverage_badge)

return app


10 changes: 7 additions & 3 deletions jenkins_badges/coverage_badge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ def extract_coverage(jresp):
if d['name'] == "Lines":
cov_raw = d['ratio']
colour = get_colour(cov_raw)
formatted = "{:.2f}%".format(cov_raw)
formatted = "{:.{}f}%".format(cov_raw,current_app.config["COVERAGE_DECIMAL_POINTS"])
print("cov_raw={}, formatted={}, dp = {}".format(cov_raw,formatted,current_app.config["COVERAGE_DECIMAL_POINTS"]))


# formatted = "{:.2f}%".format(cov_raw)
return Coverage(formatted=formatted,colour=colour)

def generate_shields_url(c):
return ("https://img.shields.io/badge/coverage-{}25-{}.svg"
"?maxAge=2".format(c.formatted,c.colour))

def get_colour(cov_raw):
if cov_raw < 20:
if cov_raw < current_app.config["COVERAGE_RED"]:
return "red"
elif cov_raw < 80:
elif cov_raw < current_app.config["COVERAGE_YELLOW"]:
return "yellow"
else:
return "brightgreen"
2 changes: 1 addition & 1 deletion jenkins_badges/coverage_badge/static/error_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions jenkins_badges/default_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
JENKINS_BASE_URL = None
JENKINS_USERNAME = None
JENKINS_TOKEN = None

COVERAGE_RED = 20
COVERAGE_YELLOW = 80
COVERAGE_DECIMAL_POINTS = 2

0 comments on commit f681c60

Please sign in to comment.