-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
github-review-requests.5m.py
executable file
·133 lines (106 loc) · 3.9 KB
/
github-review-requests.5m.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# <xbar.title>Github review requests</xbar.title>
# <xbar.desc>Shows a list of PRs that need to be reviewed</xbar.desc>
# <xbar.version>v0.1</xbar.version>
# <xbar.author>Adam Bogdał</xbar.author>
# <xbar.author.github>bogdal</xbar.author.github>
# <xbar.image>https://github-bogdal.s3.amazonaws.com/bitbar-plugins/review-requests.png</xbar.image>
# <xbar.dependencies>python</xbar.dependencies>
# ----------------------
# --- BEGIN CONFIG ---
# ----------------------
# Create your Personal Access Token here https://github.com/settings/tokens
# The token needs the following permissions:
# - repo - Full control of private repositories
# (See also https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/)
ACCESS_TOKEN = ''
# (required) Your GitHub login https://github.com/<login>
GITHUB_LOGIN = ''
# (optional) PRs with this label (e.g 'in progress') will be grayed out on the list
WIP_LABEL = ''
# (optional) Filter the PRs by an organization, labels, etc. E.g 'org:YourOrg -label:dropped draft:false'
FILTERS = 'draft:false'
# --------------------
# --- END CONFIG ---
# --------------------
import datetime
import json
import os
import sys
try:
# For Python 3.x
from urllib.request import Request, urlopen
except ImportError:
# For Python 2.x
from urllib2 import Request, urlopen
DARK_MODE = os.environ.get('BitBarDarkMode')
query = '''{
search(query: "%(search_query)s", type: ISSUE, first: 100) {
issueCount
edges {
node {
... on PullRequest {
repository {
nameWithOwner
}
author {
login
}
createdAt
number
url
title
labels(first:100) {
nodes {
name
}
}
}
}
}
}
}'''
colors = {
'inactive': '#b4b4b4',
'title': '#ffffff' if DARK_MODE else '#000000',
'subtitle': '#586069'}
def execute_query(query):
headers = {
'Authorization': 'bearer ' + ACCESS_TOKEN,
'Content-Type': 'application/json'}
data = json.dumps({'query': query}).encode('utf-8')
req = Request(
'https://api.github.com/graphql', data=data, headers=headers)
body = urlopen(req).read()
return json.loads(body)
def search_pull_requests(login, filters=''):
search_query = 'type:pr state:open review-requested:%(login)s %(filters)s' % {
'login': login, 'filters': filters}
response = execute_query(query % {'search_query': search_query})
return response['data']['search']
def parse_date(text):
date_obj = datetime.datetime.strptime(text, '%Y-%m-%dT%H:%M:%SZ')
return date_obj.strftime('%B %d, %Y')
def print_line(text, **kwargs):
params = ' '.join(['%s=%s' % (key, value) for key, value in kwargs.items()])
print('%s | %s' % (text, params) if kwargs.items() else text)
if __name__ == '__main__':
if not all([ACCESS_TOKEN, GITHUB_LOGIN]):
print_line('⚠ Github review requests', color='red')
print_line('---')
print_line('ACCESS_TOKEN and GITHUB_LOGIN cannot be empty')
sys.exit(0)
response = search_pull_requests(GITHUB_LOGIN, FILTERS)
print_line('#%s' % response['issueCount'])
print_line('---')
for pr in [r['node'] for r in response['edges']]:
labels = [l['name'] for l in pr['labels']['nodes']]
title = '%s - %s' % (pr['repository']['nameWithOwner'], pr['title'].replace('|', '-'))
title_color = colors.get('inactive' if WIP_LABEL in labels else 'title')
subtitle = '#%s opened on %s by @%s' % (
pr['number'], parse_date(pr['createdAt']), pr['author']['login'])
subtitle_color = colors.get('inactive' if WIP_LABEL in labels else 'subtitle')
print_line(title, size=16, color=title_color, href=pr['url'])
print_line(subtitle, size=12, color=subtitle_color)
print_line('---')