-
Notifications
You must be signed in to change notification settings - Fork 0
/
issues.py
172 lines (135 loc) · 4.49 KB
/
issues.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import json
import os
import requests
from datetime import datetime
from dotenv import load_dotenv
from alive_progress import alive_bar
load_dotenv()
label_v9 = "Fluent UI react-components (v9)"
label_epic = "Type: Epic"
label_feature = "Type: Feature"
label_bug = "Type: Bug :bug:"
label_needs_backlog_grooming = "Needs: Backlog review"
label_a11y = "Area: Accessibility"
label_soft_close = "Resolution: Soft Close"
label_parter_ask = "Partner Ask"
label_needs_triage = "Needs: Triage :mag:"
token = os.environ["GITHUB_TOKEN"]
repo = "microsoft/fluentui"
def run_query(query):
url = "https://api.github.com/graphql"
headers = {"Authorization": f"bearer {token}"}
response = requests.post(url, json={"query": query}, headers=headers)
response.raise_for_status()
return response.json()
def generate_graphql_query(repo, after_query):
return f"""
{{
search(query: "repo:{repo} is:issue label:\\"{label_v9}\\"", type: ISSUE, first: 100, {after_query}) {{
pageInfo {{
endCursor
hasNextPage
}}
edges {{
node {{
... on Issue {{
title
number
createdAt
closedAt
state
labels(first: 100) {{
nodes {{
name
}}
}}
timelineItems(first: 100, itemTypes: [LABELED_EVENT, UNLABELED_EVENT]) {{
nodes {{
__typename
... on LabeledEvent {{
createdAt
label {{
name
}}
}}
... on UnlabeledEvent {{
createdAt
label {{
name
}}
}}
}}
}}
}}
}}
}}
}}
}}
"""
def fetch_all_issues():
issues = []
has_next_page = True
end_cursor = None
after_query = ""
with alive_bar(0, title="Fetching issues", unit=" pages") as bar:
while has_next_page:
query = generate_graphql_query(repo, after_query)
result = run_query(query)
issues.extend([edge["node"]
for edge in result["data"]["search"]["edges"]])
page_info = result["data"]["search"]["pageInfo"]
has_next_page = page_info["hasNextPage"]
if has_next_page:
end_cursor = f'"{page_info["endCursor"]}"'
after_query = "after: " + end_cursor
bar()
return issues
def get_issues(from_file=False):
issues = []
data_folder = "data"
data_file = os.path.join(data_folder, "issues.json")
if not os.path.exists(data_folder):
os.makedirs(data_folder)
if from_file:
with open(data_file, "r") as json_file:
issues = json.load(json_file)
else:
issues = fetch_all_issues()
with open(data_file, "w") as json_file:
json.dump(issues, json_file, indent=2)
return issues
def normalize_issues(issues):
normalized_issues = []
for issue in issues:
labels = set([label["name"] for label in issue["labels"]["nodes"]])
timeline_items = []
if "timelineItems" in issue:
for timeline_item in issue["timelineItems"]["nodes"]:
formatted_item = {
"type": timeline_item["__typename"],
"createdAt": datetime.strptime(
timeline_item["createdAt"], "%Y-%m-%dT%H:%M:%SZ"
),
}
if "label" in timeline_item:
formatted_item["label"] = timeline_item["label"]["name"]
timeline_items.append(formatted_item)
normalized_issue = {
"title": issue["title"],
"number": issue["number"],
"createdAt": datetime.strptime(issue["createdAt"], "%Y-%m-%dT%H:%M:%SZ"),
"closedAt": (
datetime.strptime(issue["closedAt"], "%Y-%m-%dT%H:%M:%SZ")
if issue["closedAt"]
else None
),
"state": issue["state"],
"labels": labels,
"timelineItems": timeline_items,
}
normalized_issues.append(normalized_issue)
return normalized_issues
if __name__ == "__main__":
issues = get_issues(True)
normalized_issues = normalize_issues(issues)
print(normalized_issues[-1])