Skip to content

Instantly share code, notes, and snippets.

@elbosso
Created January 12, 2019 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbosso/bd6af8e3015cda953f6c5e5afb57bd1a to your computer and use it in GitHub Desktop.
Save elbosso/bd6af8e3015cda953f6c5e5afb57bd1a to your computer and use it in GitHub Desktop.
This is another take on dynamic badges in gitlab - this time it shows ways of counting issues for specific time intervals (see also https://gist.github.com/elbosso/6637702612991bb454d205d936dd04dc and https://gist.github.com/elbosso/cbce113805735478e926c28a9079cede)
from anybadge import Badge
from urlparse import parse_qs
import datetime
import gitlab
def index(req):
#change the next two lines to accomodate your installation
url='http://<gitlab.host>'
token='xxxx-xxxx-xxxxxxxxxx'
args=req.args
lbl='n/a'
action='closedissues'
projid=None
days=None
if args != None:
params = parse_qs(req.args)
lbl=params.get('label', 'n/a')[0]
action=params.get('action', None)[0]
projid=params.get('projid', None)[0]
if params.get('weeks', None) is not None :
days=params.get('weeks', None)[0]
req.content_type = 'image/svg+xml;charset=utf-8'
filename='test.svg'
req.headers_out["Content-Disposition"] = "attachment; filename=%s" % filename
gl = gitlab.Gitlab(url, private_token=token)
vfmt="%d"
thresh={2: 'red',
4: 'orange',
8: 'yellow',
10: 'green'}
project = None
if projid is not None :
project = gl.projects.get(1)
if project is not None:
if action is None:
v='error'
vfmt="%s"
thresh={0: 'red'}
elif action == 'openedissues':
v = project.issues.list(state='opened',as_list=False).total
thresh={5: 'green',
10: 'jellowgreen',
15: 'yellow',
20: 'orange',
25: 'red'}
elif action == 'closedissues' :
v = project.issues.list(state='closed',as_list=False).total
thresh={0: 'green'}
elif action == 'newissues' :
if days is not None :
start_date = datetime.date.today() + datetime.timedelta(-(int(days)*7))
dateParam=start_date.strftime('%Y-%m-%d')
lbl=lbl+' last '+days+' weeks'
v = project.issues.list(created_after=dateParam,as_list=False).total
thresh={0: 'green'}
else :
v='error'
vfmt="%s"
thresh={0: 'red'}
elif action == 'modifiedissues' :
if days is not None :
start_date = datetime.date.today() + datetime.timedelta(-(int(days)*7))
dateParam=start_date.strftime('%Y-%m-%d')
lbl=lbl+' last '+days+' weeks'
v = project.issues.list(modified_after=dateParam,as_list=False).total
thresh={0: 'green'}
else :
v='error'
vfmt="%s"
thresh={0: 'red'}
else :
v='error'
vfmt="%s"
thresh={0: 'red'}
else :
v='error'
vfmt="%s"
thresh={0: 'red'}
badge = Badge(label=lbl, value=v, value_format=vfmt,thresholds=thresh)
return badge.badge_svg_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment