Skip to content

Commit

Permalink
Improve discussion time display
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow committed Jan 26, 2020
1 parent a291787 commit 9486d40
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
21 changes: 15 additions & 6 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def __init__(self, build):

@property
def name(self):
return self._build.description
return self._build.context

@property
def url(self):
Expand All @@ -262,8 +262,7 @@ def author(self):

@property
def created_at(self):
return self._comment.created_at.astimezone(tzlocal())

return self._comment.created_at.replace(tzinfo=tzutc()).astimezone(tzlocal())
@property
def body(self):
return self._comment.body
Expand Down Expand Up @@ -312,10 +311,8 @@ def print_mr(self, mr):
sub_text += f"--{job.name} | color=red href={job.url}\n"

if len(mr.reviews) > 0:
diff = datetime.now().astimezone(tzlocal()) - mr.reviews[0].created_at

sub_text += '-----\n'
sub_text += f"--Discussions ({diff})\n"
sub_text += f"--Discussions ({self.time_diff(mr.reviews[0].created_at)})\n"

for review in mr.reviews:
firstname = review.author.split(' ')[0]
Expand Down Expand Up @@ -364,11 +361,23 @@ def print_title(self, mrs):
print(title)
print('---\n')

def time_diff(self, before):
diff = (datetime.now().astimezone(tzlocal()) - before).seconds

hours = int(diff/3600)
hours_text = ''
if hours > 0:
hours_text = f"{hours} hours "
minutes = int(diff%3600/60)

return f"{hours_text}{minutes} minutes ago"

bp = BitbarPrinter()

try:
from dateutil import parser
from dateutil.tz import tzlocal
from dateutil.tz import tzutc
except:
bp.print_error('Missing dependencies', 'You need to install python-dateutil | href=https://dateutil.readthedocs.io/en/stable/#installation')

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_bitbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from datetime import datetime
from datetime import timedelta

from dateutil.tz import tzlocal

from noti import BitbarPrinter

class TestBitbarPrinter:

def test_time_diff(self):
b = BitbarPrinter()
before = datetime.now().astimezone(tzlocal()) - timedelta(minutes=30)
assert b.time_diff(before) == "30 minutes ago"

before = datetime.now().astimezone(tzlocal()) - timedelta(minutes=130)
assert b.time_diff(before) == "2 hours 10 minutes ago"

0 comments on commit 9486d40

Please sign in to comment.