From 42278f88f90717a3e7a5b0a076953fb8ee47bfb7 Mon Sep 17 00:00:00 2001 From: Eugene Aseev Date: Mon, 11 Feb 2019 22:10:54 +0800 Subject: [PATCH 1/2] Bumped requests/github3 versions --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 238e8f2..c8aba3b 100644 --- a/setup.py +++ b/setup.py @@ -14,8 +14,8 @@ py_modules=['slack_pull_reminder'], license='MIT', install_requires=[ - 'requests==2.20.1', - 'github3.py==1.0.0a4' + 'requests==2.21.0', + 'github3.py==1.2.0' ], entry_points=''' [console_scripts] From ab2ed45507d21a0da4102eae3f185347f331e2d3 Mon Sep 17 00:00:00 2001 From: Eugene Aseev Date: Mon, 11 Feb 2019 22:17:18 +0800 Subject: [PATCH 2/2] Added additional PR info: age, review statuses, mergeability --- slack_pull_reminder.py | 47 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/slack_pull_reminder.py b/slack_pull_reminder.py index 7ec32df..447e880 100644 --- a/slack_pull_reminder.py +++ b/slack_pull_reminder.py @@ -1,5 +1,7 @@ import os import sys +from collections import defaultdict +from datetime import datetime, timezone, timedelta import requests from github3 import login @@ -34,7 +36,8 @@ def fetch_repository_pulls(repository): pulls = [] - for pull in repository.pull_requests(): + for short_pull in repository.pull_requests(): + pull = repository.pull_request(short_pull.number) if pull.state == 'open' and (not USERNAMES or pull.user.login.lower() in USERNAMES): pulls.append(pull) return pulls @@ -49,14 +52,52 @@ def is_valid_title(title): return True +def get_age(pull): + td = datetime.now(timezone.utc) - pull.updated_at + hours, _ = divmod(int(td.total_seconds()), 3600) + return hours + + +def get_review_statuses(pull): + dict = defaultdict(set) + + for review in pull.reviews(): + if review.state == 'APPROVED': + state = ':white_check_mark:' + elif review.state == 'CHANGES_REQUESTED': + state = ':o:' + else: + continue + + dict[state].add('@{0}'.format(review.user.login)) + + if dict: + line = 'Reviews: ' + ' '.join(['{0} by {1}'.format(key, ', '.join(value)) for (key, value) in dict.items()]) + else: + line = 'No reviews :warning:' + + return line + + +def is_mergeable(pull): + line = ':-1:' + if pull.mergeable: + line = ':+1:' + + return line + + def format_pull_requests(pull_requests, owner, repository): lines = [] for pull in pull_requests: if is_valid_title(pull.title): creator = pull.user.login - line = '*[{0}/{1}]* <{2}|{3} - by {4}>'.format( - owner, repository, pull.html_url, pull.title, creator) + review_statuses = get_review_statuses(pull) + mergeable = is_mergeable(pull) + age = get_age(pull) + line = '*[{0}/{1}]* <{2}|{3}> by @{4} • Updated {5}h ago • {6} • Mergeable: {7}'.format( + owner, repository, pull.html_url, pull.title, creator, age, review_statuses, mergeable) lines.append(line) return lines