-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbcli.py
355 lines (252 loc) · 13 KB
/
bbcli.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Inspired by:
# article: https://dev.to/ayushsharma/exporting-bitucket-repositories-and-pipelines-with-python-3h5o
# code from: https://github.com/ayush-sharma/infra_helpers/blob/master/bitbucket/report_repos_pipelines.py
# BB API 1.0 reference: https://confluence.atlassian.com/bitbucket/version-1-423626337.html
#
# list repos example: BB_ACCOUNT_ID=id BB_OAUTH_ID=key BB_OAUTH_SECRET=secret python bbcli.py --operation listrepos --filereport
#
# set repo permission example:
# BB_ACCOUNT_ID=id BB_OAUTH_ID=key BB_OAUTH_SECRET=secret python bbcli.py --operation permissions --group foo --grant read
#
# set repos permission example:
# BB_ACCOUNT_ID=id BB_OAUTH_ID=key BB_OAUTH_SECRET=secret python bbcli.py --operation permissions --group foo --repo bar --grant read
# SOME QUERIES
# list users of a group: https://bitbucket.org/api/2.0/groups/{teamname}
# repos info for a group : https://bitbucket.org/api/1.0/group-privileges/{teamname}/{groupowner}/{groupnameslug}
#response = client.BBClient.get("https://bitbucket.org/api/1.0/group-privileges/moldiscovery/moldiscovery/test")
# get repo https://bitbucket.org/api/1.0/group-privileges/{teamname}/{reposlug}/{groupowner}/{groupnameslug}
#response = client.BBClient.get("https://bitbucket.org/api/1.0/group-privileges/moldiscovery/testrepo/moldiscovery/test")
from urllib.parse import urlencode
from urllib.parse import parse_qs, urlsplit, urlunsplit
from requests.exceptions import HTTPError
import csv
import json
import sys
from os.path import join, exists
from os import environ
from datetime import datetime
import click
from BBclient import AuthClient
# TODO user click groups to configure subcommands menu
@click.command()
@click.option('--repoall/--no-repoall', default=False)
@click.option("--filereport/--no-filereport", help="Report to file", default=False, required=False)
@click.option("--operation", help="Operation choose: list_repos, ", type=click.Choice(['listrepos', 'permissions', 'groupinfo', 'userinfo', 'restoregroupsgrant'], case_sensitive=False))
@click.option("--repo", help="apply to a single repo", type=str, required=False)
@click.option("--group", help="grant permissions for this group", type=str, required=False)
@click.option("--user", help="user id", type=str, required=False)
@click.option("--grant", help="type of permission to grant", default='read', type=click.Choice(['read', 'write'], case_sensitive=False))
@click.option("--backupfilepath", help="file containing the backed up user permission", type=str, required=False)
def run(operation, filereport, repo, user, group, grant, backupfilepath, repoall):
click.echo(operation)
ac = AuthClient()
#ac.connect()
if operation == "listrepos":
print("Options: group, grant, repo skipped")
ac.connect()
list_team_repos(ac, filereport)
if operation == "permissions":
if group:
if repoall:
if group and grant:
print("update/create group permission on all repos")
ac.connect()
repos = list_team_repos(ac,None)
for repo in repos:
if repo[0] != "PAInS filter":
print("set/create group: %s permission: %s on repo: %s" % (group, grant, repo[0]))
setRepoGroupPermissions(ac, group.lower(), repo[0], grant)
else:
if repo:
ac.connect()
answer = input("This command will change/create the group '{}' with permission '{}' for repo '{}', are you sure? yes/no ".format(group, grant,repo))
if 'yes' in answer:
print("run on repo {}".format(repo))
setRepoGroupPermissions(ac, group.lower(), repo, grant)
else:
# Single request doesn't not work as expected so I need to iterate over the group repos
ac.connect()
answer = input("This command will change/create the group '{}' with permission '{}', are you sure? yes/no ".format(group, grant))
if 'yes' in answer:
repos = listgroup_repos(ac, group.lower())
for repo in repos:
print("run on repo {}".format(repo))
setRepoGroupPermissions(ac, group.lower(), repo, grant)
if operation == 'groupinfo':
ac.connect()
group_info(ac, group.lower(), filereport)
if user and operation == 'userinfo':
ac.connect()
user_info(ac, user, filereport)
if operation == 'restoregroupsgrant':
if not group:
error("missing group name")
if backupfilepath and exists(backupfilepath):
ac.connect()
with open(backupfilepath, 'r') as backup:
reader = csv.DictReader(backup)
for item in reader:
s_repo = item['repo'].strip()
s_grant = item['permission'].strip()
print("set {1} on repo {0}".format(s_repo, s_grant))
setRepoGroupPermissions(ac, group.lower(), s_repo, s_grant)
else:
error("wrong backup file")
def error(msg):
click.ClickException(msg).show()
sys.exit(1)
def list_team_repos(client, filereport):
repo_list = get_repo_page(
client.BBClient, next_page_url=client.server_base_uri + '2.0/repositories/' + client.account_id)
repo_data_map = {}
for repo in repo_list:
# I only consider a shortdate for 'updated_on' of format YYYY-MM-dd
repo_data_map[repo['name']] = repo['updated_on'][:10]
# sort by date
ordered_repos = sorted(repo_data_map.items(), key = lambda item:datetime.strptime(item[1], '%Y-%m-%d'), reverse=True)
if filereport:
with open('repos.csv', 'w') as csv_file:
print('> Saving repos report to file...')
csv_file.write('git_remote , updated_on\n')
for rname,rtime in ordered_repos:
csv_file.write(join("git@bitbucket.org:"+client.account_id,rname) + " , " + rtime + '\n')
print('> Repo report saved. (repos.csv)')
#else:
# print(ordered_repos)
return ordered_repos
# only works for the caller user id
def user_info(client, user, filereport):
out = []
response = client.BBClient.get("https://api.bitbucket.org/2.0/users/{0}/repositories".format(user))
if response.status_code != 200:
error("API Request error, code {}".format(response.status_code))
body = json.loads(response.content)
if filereport:
targetfile = "group_{0}_info.csv".format(group)
with open(targetfile, 'w') as csv_file:
print('> Saving groupinfo report to file...')
csv_file.write('group , permission\n')
for item in body:
csv_file.write(item['repo'].split('/')[-1] + ", " + item['privilege'] + '\n')
print("> Repo report saved. (%s)" % targetfile)
else:
print(body)
return out
def group_info(client, group, filereport):
try:
out = []
response = client.BBClient.get(join("https://bitbucket.org/api/1.0/group-privileges",client.account_id,client.account_id,group))
if response.status_code != 200:
error("API Request error, code {}".format(response.status_code))
body = json.loads(response.content)
if filereport:
targetfile = "group_{0}_info.csv".format(group)
with open(targetfile, 'w') as csv_file:
print('> Saving groupinfo report to file...')
csv_file.write('repo,permission\n')
for item in body:
csv_file.write(item['repo'].split('/')[-1] + "," + item['privilege'] + '\n')
print("> Repo report saved. (%s)" % targetfile)
else:
for item in body:
out.append(item['repo'].split('/')[-1] + ", " + item['privilege'])
print(out)
return out
except HTTPError:
error("BB Endpoint request error")
def listgroup_repos(client, group):
try:
out = []
response = client.BBClient.get(join("https://bitbucket.org/api/1.0/group-privileges",client.account_id,client.account_id,group))
if response.status_code != 200:
error("API Request error, code {}".format(response.status_code))
body = json.loads(response.content)
for item in body:
out.append(item['repo'].split('/')[-1])
return out
except HTTPError:
error("BB Endpoint request error")
# use API v1.0 cause this feature is deprecated on 2.0
# https://developer.atlassian.com/cloud/bitbucket/deprecation-notice-v1-apis/?_ga=2.232592733.337263193.1581505695-823020582.1566895316
# I'm starting from this page: https://confluence.atlassian.com/bitbucket/group-privileges-endpoint-296093137.html
def setRepoGroupPermissions(client, group, repo, grant):
try:
# change repo perm: PUT https://api.bitbucket.org/1.0/group-privileges/{workspace_id}/{repo_slug}/{group_owner}/{group_slug} data=['read'|'write'|admin']
response = client.BBClient.put(join("https://bitbucket.org/api/1.0/group-privileges/",client.account_id,repo,client.account_id,group), data=grant)
if response.status_code != 200:
error("API Request error, code {}".format(response.status_code))
print("Completed")
except HTTPError:
error("BB Endpoint request error")
# TODO FIX , this set permissions on all repos, expected behaviour is to apply to the group repos
# def setGroupPermissions(client, group, grant):
# try:
# # change repo perm: PUT https://api.bitbucket.org/1.0/group-privileges/{workspace_id}/{repo_slug}/{group_owner}/{group_slug} data=['read'|'write'|admin']
# response = client.BBClient.put(join("https://api.bitbucket.org/1.0/group-privileges",TEAM,TEAM,group), data=grant)
# if response.status_code != 200:
# error("API Request error, code {}".format(response.status_code))
# print(response.content)
# print("Completed")
# except HTTPError:
# error("BB Endpoint request error")
def get_repo_page(BBClient, next_page_url):
data = []
response = BBClient.get(next_page_url)
try:
response_dict = json.loads(response.content)
except Exception as e:
print(str(e))
if 'values' in response_dict:
for repo in response_dict['values']:
data.append({
'uuid': repo['uuid'],
'name': repo['name'],
'pr_url': repo['links']['pullrequests']['href'],
'lang': repo['language'],
'updated_on': repo['updated_on'],
'size': repo['size'],
'slug': repo['slug']
})
if 'next' in response_dict:
data += get_repo_page(BBClient=BBClient,
next_page_url=response_dict['next'])
return data
def get_all_pipelines(BBClient, next_page_url):
data = []
response = BBClient.get(next_page_url)
try:
response_dict = json.loads(response.content)
except Exception as e:
print(str(e))
if 'values' in response_dict:
for repo in response_dict['values']:
data.append({
'uuid': repo['uuid'],
'repo': repo['repository']['name'],
'state': repo['state']['result']['name'],
'build_number': repo['build_number'],
'creator': repo['creator']['display_name'] + '/' + repo['creator']['username'],
'target_type': repo['target']['ref_type'] if 'ref_type' in repo['target'] else '',
'target_name': repo['target']['ref_name'] if 'ref_name' in repo['target'] else '',
'trigger': str(repo['trigger']['name']),
'duration': repo['duration_in_seconds'],
'created_on': repo['created_on'],
'completed_on': repo['completed_on']
})
if 'next' in response_dict:
data += get_all_pipelines(BBClient=BBClient,
next_page_url=response_dict['next'])
elif 'page' in response_dict and 'pagelen' in response_dict and response_dict['page'] < response_dict['pagelen']:
""" If next page URL does not exist, assemble next page URL manually.
"""
scheme, netloc, path, query_string, fragment = urlsplit(next_page_url)
query_params = parse_qs(query_string)
query_params['page'] = [response_dict['page'] + 1]
new_query_string = urlencode(query_params, doseq=True)
next_page_url = urlunsplit(
(scheme, netloc, path, new_query_string, fragment))
data += get_all_pipelines(BBClient=BBClient,
next_page_url=next_page_url)
return data
if __name__ == '__main__':
run()