-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
26 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "1.1.1" | ||
__version__ = "1.1.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |