-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
128 lines (117 loc) · 5.04 KB
/
main.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
import csv
import http.client
import json
# SET UP
# CircleCI API Token https://app.circleci.com/settings/user/tokens
CIRCLECI_API_TOKEN = 'API-TOKEN-HERE'
# Output folder for csv files
OUTPUT_DIR = '/tmp'
# Organization name
ORG_NAME = 'org-name'
# VCS provider name (E.g. 'github', 'bitbucket')
VCS_TYPE = 'github'
conn = http.client.HTTPSConnection('circleci.com')
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Circle-Token': CIRCLECI_API_TOKEN
}
# Create project name list
# ref: No api documentation
project_names = []
for i in range(1, 100):
conn.request(
'GET', f'/api/v1.1/user/repos/{VCS_TYPE}?page={i}&per-page=100', headers=headers)
res = conn.getresponse()
res_data = json.loads(res.read().decode('utf-8'))
names = [repo['name']
for repo in res_data if repo['username'] == ORG_NAME]
if not len(names):
break
project_names += names
max = len(project_names)
# Export: Project environment variables
# ref: https://circleci.com/docs/api/v2/index.html#operation/listEnvVars
with open(f'{OUTPUT_DIR}/project_envvars.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(['project_name', 'name', 'value'])
for i, prj in enumerate(project_names, 1):
print(f'[{f.name}] {i}/{max}: {prj}')
conn.request(
'GET', f'/api/v2/project/{VCS_TYPE}/{ORG_NAME}/{prj}/envvar', headers=headers)
res = conn.getresponse()
body = json.loads(res.read().decode('utf-8'))
if res.status != 200:
print(f'Status code: {res.status}, {body}')
continue
rows = [[prj, item['name'], item['value']] for item in body['items']]
writer.writerows(rows)
# Export: Project Checkout SSH keys
# ref: https://circleci.com/docs/api/v2/index.html#operation/listCheckoutKeys
with open(f'{OUTPUT_DIR}/project_checkout-ssh-keys.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(['project_name', 'type', 'preferred',
'created_at', 'public_key', 'fingerprint'])
for i, prj in enumerate(project_names, 1):
print(f'[{f.name}] {i}/{max}: {prj}')
conn.request(
'GET', f'/api/v2/project/{VCS_TYPE}/{ORG_NAME}/{prj}/checkout-key', headers=headers)
res = conn.getresponse()
body = json.loads(res.read().decode('utf-8'))
if res.status != 200:
print(f'Status code: {res.status}, {body}')
continue
rows = [[prj, item['type'], item['preferred'], item['created_at'],
item['public_key'], item['fingerprint']] for item in body['items']]
writer.writerows(rows)
# Export: Project Additional SSH keys
# ref: No api documentation
with open(f'{OUTPUT_DIR}/project_additional-ssh-keys.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(['project_name', 'hostname', 'public_key', 'fingerprint'])
for i, prj in enumerate(project_names, 1):
print(f'[{f.name}] {i}/{max}: {prj}')
conn.request(
'GET', f'/api/v1.1/project/{VCS_TYPE}/{ORG_NAME}/{prj}/settings', headers=headers)
res = conn.getresponse()
body = json.loads(res.read().decode('utf-8'))
if res.status != 200:
print(f'Status code: {res.status}, {body}')
continue
rows = [[prj, item['hostname'], item['public_key'],
item['fingerprint']] for item in body['ssh_keys']]
writer.writerows(rows)
# Export: Project API tokens
# ref: No api documentation
with open(f'{OUTPUT_DIR}/project_api-tokens.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(['project_name', 'label', 'scope', 'time', 'id'])
for i, prj in enumerate(project_names, 1):
print(f'[{f.name}] {i}/{max}: {prj}')
conn.request(
'GET', f'/api/v1.1/project/{VCS_TYPE}/{ORG_NAME}/{prj}/token', headers=headers)
res = conn.getresponse()
body = json.loads(res.read().decode('utf-8'))
if res.status != 200:
print(f'Status code: {res.status}, {body}')
continue
rows = [[prj, item['label'], item['scope'], item['time'], item['id']]
for item in body]
writer.writerows(rows)
# Create context list
# ref: https://circleci.com/docs/api/v2/index.html#operation/listContexts
conn.request('GET', f'/api/v2/context?owner-slug={VCS_TYPE}/{ORG_NAME}', headers=headers)
res = conn.getresponse()
body = json.loads(res.read().decode('utf-8'))
context_ids = {c['name']: c['id'] for c in body['items']}
# Export: Context variables
# ref: https://circleci.com/docs/api/v2/index.html#operation/listEnvironmentVariablesFromContext
with open(f'{OUTPUT_DIR}/context_envvars.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(['context_name', 'variable', 'created_at'])
for name, id in context_ids.items():
conn.request("GET", f"/api/v2/context/{id}/environment-variable", headers=headers)
res = conn.getresponse()
body = json.loads(res.read().decode('utf-8'))
rows = [[name, item['variable'], item['created_at']] for item in body['items']]
writer.writerows(rows)