Skip to content

Commit

Permalink
pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyarr committed Jul 3, 2017
1 parent 5123413 commit 8c5b871
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
9 changes: 3 additions & 6 deletions jenkins_badges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from flask import Flask

def create_app(from_envvar=False,base_url=None,username=None,token=None,

def create_app(from_envvar=False, base_url=None, username=None, token=None,
coverage_yellow=80, coverage_red=20,
coverage_decimal_points=2):
'''
Expand All @@ -15,7 +16,7 @@ def create_app(from_envvar=False,base_url=None,username=None,token=None,
:param from_envvar: if True, configuration parameters are sourced from file referenced by the local JENKINS_BADGES_SETTINGS environmental variable. if False, configuration parameters are sourced from the arguments.
:type from_envvar: bool
:param base_url: url of Jenkins instance. Must be supplied if from_envvar=False.
:param base_url: url of Jenkins instance. Must be supplied if from_envvar=False.
:type base_url: str
:param username: username of Jenkins user
Expand Down Expand Up @@ -55,7 +56,3 @@ def create_app(from_envvar=False,base_url=None,username=None,token=None,
app.register_blueprint(coverage_badge)

return app




2 changes: 1 addition & 1 deletion jenkins_badges/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.1.1"
__version__ = "1.1.1"
38 changes: 22 additions & 16 deletions jenkins_badges/coverage_badge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
from flask import send_file, Blueprint,current_app
from flask import send_file, Blueprint, current_app
import requests
import io
from collections import namedtuple

from six.moves.urllib_parse import urljoin

Coverage = namedtuple("Coverage",["formatted","colour"])
Coverage = namedtuple("Coverage", ["formatted", "colour"])

coverage_badge = Blueprint('coverage_badge',__name__)
coverage_badge = Blueprint('coverage_badge', __name__)

@coverage_badge.route("/coverage/<job_name>",methods=['GET'])

@coverage_badge.route("/coverage/<job_name>", methods=['GET'])
def send_coverage_badge(job_name):
if job_name == "favicon.ico":
return "",200
return "", 200

jurl = generate_jenkins_api_url(job_name)
auth = (current_app.config['JENKINS_USERNAME'],
current_app.config['JENKINS_TOKEN'])
auth = None if auth==(None,None) else auth
jresp = requests.get(jurl,auth=auth)
auth = None if auth == (None, None) else auth
jresp = requests.get(jurl, auth=auth)
print("GET {} {}".format(jresp.status_code, jurl))
if jresp.status_code != 200:
return send_error_badge()

cov = extract_coverage(jresp)
surl = generate_shields_url(cov)
sresp = requests.get(surl, stream=True)
print("GET {} {}".format(sresp.status_code,surl))
print("GET {} {}".format(sresp.status_code, surl))
if sresp.status_code != 200:
return send_error_badge()

path = io.BytesIO(sresp.content)
print("SENDING coverage badge of {}".format(cov.formatted))
return send_file(path, mimetype="image/svg+xml",cache_timeout=30), 200
return send_file(path, mimetype="image/svg+xml", cache_timeout=30), 200


def send_error_badge():
path = "coverage_badge/static/error_badge.svg"
print("SENDING access fail badge")
return send_file(path, mimetype="image/svg+xml",cache_timeout=30), 200
return send_file(path, mimetype="image/svg+xml", cache_timeout=30), 200


def generate_jenkins_api_url(job_name):
api_endpoint = ("job/{}/lastSuccessfulBuild/cobertura/api/json/?depth=2"
"").format(job_name)

return urljoin(current_app.config["JENKINS_BASE_URL"]+"/",api_endpoint)
return urljoin(current_app.config["JENKINS_BASE_URL"] + "/", api_endpoint)


def extract_coverage(jresp):
coverage_dict = jresp.json()
for d in coverage_dict['results']['elements']:
for d in coverage_dict['results']['elements']:
if d['name'] == "Lines":
cov_raw = d['ratio']
colour = get_colour(cov_raw)
formatted = "{:.{}f}%".format(cov_raw,current_app.config["COVERAGE_DECIMAL_POINTS"])
return Coverage(formatted=formatted,colour=colour)
formatted = "{:.{}f}%".format(cov_raw, current_app.config["COVERAGE_DECIMAL_POINTS"])
return Coverage(formatted=formatted, colour=colour)


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


def get_colour(cov_raw):
if cov_raw < current_app.config["COVERAGE_RED"]:
return "red"
elif cov_raw < current_app.config["COVERAGE_YELLOW"]:
return "yellow"
else:
return "brightgreen"
return "brightgreen"

0 comments on commit 8c5b871

Please sign in to comment.