From dfa08b9b4ef776f7f651ec4c61db19b6bb4d1ae9 Mon Sep 17 00:00:00 2001 From: kongr45gpen Date: Thu, 1 Jan 2015 00:47:42 +0200 Subject: Show changed issue labels --- local/handler/IssueHandler.py | 10 +++++++++- local/theme/DefaultTheme.py | 10 ++++++++-- local/utility.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/local/handler/IssueHandler.py b/local/handler/IssueHandler.py index df07a9c..1265745 100644 --- a/local/handler/IssueHandler.py +++ b/local/handler/IssueHandler.py @@ -9,6 +9,12 @@ def handle(data, theme): if 'assignee' in data['issue'] and data['issue']['assignee']: assignee = data['issue']['assignee']['login'] + labelName = None + labelColor = None + if 'label' in data and data['label']: + labelName = data['label']['name'] + labelColor = data['label']['color'] + theme.issue( repo = data['repository']['name'], actor = data['sender']['login'], @@ -18,5 +24,7 @@ def handle(data, theme): creator = data['issue']['user']['login'], milestone = milestone, url = getShortURL(data['issue']['url']), - assignee = assignee + assignee = assignee, + labelName = labelName, + labelColor = labelColor ) diff --git a/local/theme/DefaultTheme.py b/local/theme/DefaultTheme.py index 18982de..00a1171 100644 --- a/local/theme/DefaultTheme.py +++ b/local/theme/DefaultTheme.py @@ -77,12 +77,18 @@ class DefaultTheme(Theme): ' (%s)' % url if url else '' )) - def issue(self, repo, actor, action, issueNo, issueTitle, creator, milestone, url, assignee = None, comment = None): + def issue(self, repo, actor, action, issueNo, issueTitle, creator, milestone, url, assignee = None, comment = None, labelName = None, labelColor = None): formattedActor = ircutils.mircColor(actor, "green") if actor == assignee: formattedActor = ircutils.bold(formattedActor) + extra = '' + if action == 'assigned': + extra = " to %s" % ircutils.bold(ircutils.mircColor(assignee, "green")) + elif action == 'labeled': + extra = " as %s" % ircutils.mircColor(labelName, hexToMirc(labelColor)) + self.msgs.append( "%s: %s %s issue #%s \"%s\"%s%s%s %s%s)%s" % ( ircutils.bold(repo), formattedActor, @@ -90,7 +96,7 @@ class DefaultTheme(Theme): issueNo, ircutils.bold(issueTitle), " by %s" % ircutils.mircColor(creator,"green") if creator != actor else '', - " to %s" % ircutils.bold(ircutils.mircColor(assignee, "green")) if action == 'assigned' else '', + extra, " (%s" % ircutils.mircColor(milestone, "brown") if milestone else '', '- ' if milestone else '(', url, diff --git a/local/utility.py b/local/utility.py index b7d99ee..d33722c 100644 --- a/local/utility.py +++ b/local/utility.py @@ -1,4 +1,5 @@ import re +import math import urllib2 import supybot.conf as conf @@ -123,6 +124,42 @@ def isStatusVisible(repo, status): globals.travisStatuses[repo] = status return changed +def hexToMirc(hash): + colors = { + 'white': (255, 255, 255), + 'black': (0, 0, 0), + 'blue': (0, 0, 127), + 'green': (0, 147, 0), + 'red': (255,0 ,0), + 'brown': (127, 0, 0), + 'purple': (156, 0, 156), + 'orange': (252, 127, 0), + 'yellow': (255, 255, 0), + 'light green': (0, 252, 0), + 'teal': (0, 147, 147), + 'light blue': (0, 255, 255), + 'dark blue': (0, 0, 252), + 'pink': (255, 0, 255), + 'dark grey': (127, 127, 127), + 'light grey': (210, 210, 210) + } + + rgb = _hex_to_rgb(hash) + + return min(colors, key = lambda x:_colourDistance(colors[x], rgb)) + +def _hex_to_rgb(value): + value = value.lstrip('#') + lv = len(value) + return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3)) + +def _colourDistance(a, b): + rmean = (a[0] + b[0]) /2 + r = a[0] - b[0] + g = a[1] - b[1] + b = a[2] - b[2] + + return math.sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8)) # Possible colours: # white, black, (light/dark) blue, (light) green, red, brown, purple, # orange, yellow, teal, pink, light/dark gray/grey -- cgit v1.2.3