Skip to content

Instantly share code, notes, and snippets.

@YoshiRi
Last active September 7, 2022 07:20
Show Gist options
  • Save YoshiRi/ebbe5c15d5a02bc138332d0384731c2c to your computer and use it in GitHub Desktop.
Save YoshiRi/ebbe5c15d5a02bc138332d0384731c2c to your computer and use it in GitHub Desktop.
Gather discussions with Graph QL API (Need access token and gh cli)
"""Gather Discussions in Github Repo with GraphQL
Returns:
CSV output file
"""
from ast import parse
import subprocess
import json
LABEL_FILTER = ["sensing", "perception"]
def get_discussions_graphql():
command = "gh api graphql -f query="
query_body = """'
query{
repository(owner:"autowarefoundation", name:"autoware") {
discussions(first:100){
edges{
node{
title
publishedAt
lastEditedAt
updatedAt
url
answer{
author{
login
}
publishedAt
}
author {
login
}
authorAssociation
labels(first:5) {
edges {
node {
name
}
}
}
}
}
}
}
}
'
"""
cp = subprocess.run(command + query_body, shell=True, capture_output=True)
text = cp.stdout.decode()
#print(text)
return json.loads(text)
def get_discussion_list(data):
datalist = data['data']['repository']['discussions']['edges']
print("Data len is:", len(datalist))
return datalist
def parse_discussion(data):
node = data['node']
milestone=""
title = node['title']
answered = True if node['answer'] else False
state = "closed" if answered else "open"
created_date = node['publishedAt']
last_edited_date = node['lastEditedAt']
updated_date = node['updatedAt']
url = node['url']
author = node['author']['login']
labels = ""
authorAssociation = node['authorAssociation']
if 'labels' in node.keys():
label_list = node['labels']['edges']
for ll in label_list:
labels = labels + "," + ll['node']['name']
if filter_via_label(labels, LABEL_FILTER):
return [milestone, title, None , state, labels, last_edited_date, created_date, updated_date, url, author, authorAssociation]
else:
return []
def filter_via_label(labels, filter_list):
for filter in filter_list:
if filter in labels:
return True
return False
def main():
discussions = get_discussions_graphql()
d_list = get_discussion_list(discussions)
csv_list = []
column_title = ["Milestone", "Title", "Assignee", "Status", "Labels", "Last edited", "opened_at", "updated_at", "URL", "user name", "user association"]
csv_list.append(column_title)
for d in d_list:
parsed = parse_discussion(d)
if parsed:
csv_list.append(parsed)
import csv
with open("discussions.csv", "w") as f:
writer = csv.writer(f)
writer.writerows(csv_list)
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment