Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script Filter for Alfred listing all the issues and PRs assigned to or created by you
#!/Users/YOUR_USERNAME/.pyenv/versions/3.6.2/envs/alfred/bin/python
import functools
import json
import os
from datetime import datetime
from threading import Thread
import requests
access_token = os.environ['GITHUB_ACCESS_TOKEN']
username = os.environ['GITHUB_USERNAME']
def print_items(items):
print(json.dumps({'items': items}))
class RequestThread(Thread):
def __init__(self, query):
super().__init__()
self.query = query
self.response = None
def run(self):
headers = {
'Authorization': 'bearer {}'.format(access_token)
}
data = {
'query': self.query
}
self.response = requests.post(url, headers=headers, data=json.dumps(data))
q1 = """query {
search(first:50, type:ISSUE, query:"assignee:%s is:open") {
nodes {
__typename,
... on PullRequest {
id, number, title, url, createdAt, state,
repository {
name
}
},
... on Issue {
id, number, title, url, createdAt, state,
repository {
name
}
}
}
}
}
""" % username
q2 = """query {
search(first:50, type:ISSUE, query:"assignee:%s is:closed") {
nodes {
__typename,
... on PullRequest {
id, number, title, url, createdAt, state,
repository {
name
}
},
... on Issue {
id, number, title, url, createdAt, state,
repository {
name
}
}
}
}
}
""" % username
q3 = """query {
search(first:50, type:ISSUE, query:"author:%s is:open") {
nodes {
__typename,
... on PullRequest {
id, number, title, url, createdAt, state,
repository {
name
}
},
... on Issue {
id, number, title, url, createdAt, state,
repository {
name
}
}
}
}
}
""" % username
q4 = """query {
search(first:50, type:ISSUE, query:"author:%s is:closed") {
nodes {
__typename,
... on PullRequest {
id, number, title, url, createdAt, state,
repository {
name
}
},
... on Issue {
id, number, title, url, createdAt, state,
repository {
name
}
}
}
}
}
""" % username
url = 'https://api.github.com/graphql'
threads = []
for q in [q1, q2, q3, q4]:
t = RequestThread(q)
t.start()
threads.append(t)
items = []
for t in threads:
t.join()
nodes = t.response.json()['data']['search']['nodes']
for node in nodes:
gh_url = node['url']
state = node['state']
if node['__typename'] == 'PullRequest':
if state in ('MERGED', 'CLOSED'):
icon_path = 'pr_closed.png'
else:
icon_path = 'pr_open.png'
else:
if state == 'CLOSED':
icon_path = 'issue_closed.png'
else:
icon_path = 'issue_open.png'
created = datetime.strptime(node['createdAt'], '%Y-%m-%dT%H:%M:%SZ')
created_order = int((datetime(2030, 12, 31) - created).total_seconds())
title = node['title']
number = node['number']
markdown_link = '{} ([#{}]({}))'.format(title, number, gh_url)
items.append({
'title': title,
'subtitle': gh_url,
'arg': gh_url,
'quicklookurl': gh_url,
'icon': {
'path': icon_path
},
'mods': {
'ctrl': {
'arg': markdown_link,
'subtitle': markdown_link,
}
},
'created_order': created_order,
'state': state
})
def item_key(a):
if a['state'] == 'OPEN':
state_id = 1
else:
state_id = 2
return state_id, a['created_order']
items = sorted(items, key=item_key)
print_items(items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment