Skip to content

Commit

Permalink
Create a python script to triage issues
Browse files Browse the repository at this point in the history
* An issue is considered triaged if certain criterion such as labels are set.

See: kubeflow/community#280
  • Loading branch information
jlewi committed Jul 25, 2019
1 parent 982000a commit e978921
Show file tree
Hide file tree
Showing 6 changed files with 475 additions and 5 deletions.
3 changes: 3 additions & 0 deletions notifications/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
TOKEN_NAME = "GITHUB_TOKEN"
PULL_REQUEST_TYPE = "PullRequest"

# TODO(jlewi): Rewrite this code to use:
# i) graphql.unpack_and_split_nodes
# ii) graphql.shard_writer
def process_notification(n):
# Mark as read anything that isn't an explicit mention.
# For PRs there doesn't seem like a simple way to detect if the notice
Expand Down
70 changes: 65 additions & 5 deletions py/code_intelligence/graphql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""This module contains utilities for working with GitHub's graphql API"""

from code_intelligence import util
import logging
import os
import requests

Expand All @@ -12,12 +13,71 @@ def __init__(self):
self._headers = {"Authorization":
"Bearer {0}".format(os.getenv("GITHUB_TOKEN"))}

def run_query(self, query):
"""Issue the GraphQL query and return the results."""
def run_query(self, query, variables=None):
"""Issue the GraphQL query and return the results.
Args:
query: String containing the query
variables: Dictionary of variables
"""
payload = {'query': query}

if variables:
payload["variables"] = variables

request = requests.post('https://api.github.com/graphql',
json={'query': query}, headers=self._headers)
json=payload, headers=self._headers)
if request.status_code == 200:
return request.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(
request.status_code, query))
request.status_code, query))

def unpack_and_split_nodes(data, path):
"""Unpack a list of results
Args:
data: A dictionary containing the results
path: A list of fields indicating the fields to select.
final one should be a list of nodes
Returns:
issues: A list of dicts; each dict is the data for some of
the results
"""

children = [data]

for i, f in enumerate(path):
last_child = children[-1]
if not f in last_child:
# If there are no edges then the field will not exist
return []
children.append(last_child.get(f))


child = children[-1]

items = []
for i in child:
items.append(i["node"])

return items

class ShardWriter(object):
"""Write items as a set of file shards"""

def __init__(self, total_shards, output_dir, prefix="items"):
self.output_dir = output_dir
self.total_shards = total_shards
self.shard = 0
self.prefix = prefix

def write_shard(self, items):
"""Write the shard"""
shard_file = os.path.join(
self.output_dir,
self.prefix + "-{0:03d}-of-{1:03d}.json".format(
self.shard, self.total_shards))
util.write_items_to_json(shard_file, items)
self.shard += 1
3 changes: 3 additions & 0 deletions py/code_intelligence/test_data/issues_for_triage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"author": {"__typename": "User", "login": "jlewi"}, "title": "Doesn't need triage", "body": "", "url": "https://github.com/kubeflow/kubeflow/issues/365", "labels": {"totalCount": 2, "edges": [{"node": {"name": "kind/bug"}}, {"node": {"name": "area/jupyter"}}, {"node": {"name": "priority/p1"}}]}, "projectCards": {"totalCount": 1, "edges": [{"node": {"project": {"name": "0.2 Release", "number": 2}}}]}}
{"author": {"__typename": "User", "login": "jlewi"}, "title": "Needs triage; missing everything", "body": "", "url": "https://github.com/kubeflow/kubeflow/issues/365", "labels": {"totalCount": 2, "edges": []}, "projectCards": {"totalCount": 1, "edges": []}}
{"author": {"__typename": "User", "login": "jlewi"}, "title": "Needs triage; missing projectCards", "body": "", "url": "https://github.com/kubeflow/kubeflow/issues/365", "labels": {"totalCount": 2, "edges": []}}
Loading

0 comments on commit e978921

Please sign in to comment.