-
Notifications
You must be signed in to change notification settings - Fork 2
/
atdumpmemex.py
executable file
·139 lines (115 loc) · 4.69 KB
/
atdumpmemex.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
#!/usr/bin/env python3
import base64
import json
import os
import re
import sys
import requests
import dotenv
# Color constants
# Reference: https://gist.github.com/chrisopedia/8754917
COLERR="\033[0;31m"
COLINFO="\033[0;35m"
COLRESET="\033[m"
graphqlurl = 'https://api.github.com/graphql'
DUMP_CARDS_PATH = os.getcwd()
DOTENV_PATH = f"{DUMP_CARDS_PATH}/.env"
dotenv.load_dotenv(DOTENV_PATH)
try:
token = os.environ['GITHUB_API_TOKEN']
except:
print("Make sure GITHUB_API_TOKEN env variable is set")
sys.exit()
headers = {"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer " + token,
"GraphQL-Features": "projects_next_graphql" }
def list_memex_projects(org):
"""List the Projects(beta) available in a given org.
Args:
org (str): GitHub Organization
Returns:
dictionary:
"""
query = ('query{ organization(login: \\"' + org + '\\") '
'{ projectsNext(first: 20) { nodes { id number title closed } } } }')
response = requests.post(graphqlurl,
headers=headers,
data='{"query": '+'\"' + query + '\"}')
if response.status_code != 200:
# An error occured
print(COLERR + "Error getting project list : "
+ str(response.status_code) + " " + response.text + COLRESET)
json_projects = json.loads(response.text)
return json_projects
def list_memex_columns(project_id):
"""List the columns in a given Projects(beta).
Args:
project_id (str): Projects(beta) ID
Returns:
dictionary:
"""
#b64id = base64.b64encode(project_id.encode("ascii")).decode("utf-8")
query = ('query{ node(id: \\"' + project_id + '\\") '
'{ ... on ProjectNext { fields(first: 20) '
'{ nodes { id name settings } } } } }')
response = requests.post(graphqlurl,
headers=headers,
data='{"query": '+'\"' + query + '\"}')
if response.status_code != 200:
# An error occured
print(COLERR + "Error getting project columns : "
+ str(response.status_code) + " " + response.text + COLRESET)
columns = []
json_nodes = json.loads(response.text)
for node in json_nodes["data"]["node"]["fields"]["nodes"]:
if node["name"] == "Status":
json_status = json.loads(node["settings"])
for options in json_status["options"]:
columns.append(str(options["id"])+' '+options["name"])
return columns
def list_memex_cards(column_id, project_id):
"""Export the cards in a Projects(beta) column to .csv.
Args:
column_id (str): Column ID
project_id (str): Projects(beta) ID
"""
cards_file = column_id + ".csv"
#b64id = base64.b64encode(project_id.encode("ascii")).decode("utf-8")
getPage = True
cursor = ""
f = open(cards_file, 'w')
f.write("Issue Key,Summary,Description,Acceptance Criteria,Story Points\n")
while getPage:
query = ('query{ node(id: \\"' + project_id + '\\") '
'{ ... on ProjectNext { items(first: 100 ' + cursor + ') '
'{ pageInfo { hasNextPage endCursor}'
' nodes{ title fieldValues(first: 8) { nodes{ value } } '
'content{ ...on Issue { number labels(first: 50) '
'{ nodes{ name } } } } } } } } }')
response = requests.post(graphqlurl,
headers=headers,
data='{"query": '+'\"' + query + '\"}')
if response.status_code != 200:
# An error occured
print(COLERR + "Error getting project column cards : "
+ str(response.status_code) + " " + response.text + COLRESET)
json_cards = json.loads(response.text)
for card in json_cards["data"]["node"]["items"]["nodes"]:
for status in card["fieldValues"]["nodes"]:
if status["value"] == column_id:
# Remove special chars and limit length to 80 chars
title = re.sub('[^A-Za-z0-9.@ ]+', '', card["title"])[:80]
if card["content"]==None:
f.write(f'Draft,{title},,,\n')
break
f.write(f'{card["content"]["number"]},{title},,,')
for label in card["content"]["labels"]["nodes"]:
if (label["name"][-2:]=="SP"):
f.write (f'{label["name"].partition(" ")[0]}')
# break loop in case there are multiple SP labels
break
f.write ('\n')
getPage = json_cards["data"]["node"]["items"]["pageInfo"]["hasNextPage"]
cursor = 'after:\\"' + json_cards["data"]["node"]["items"]["pageInfo"]["endCursor"] + '\\"'
f.close