-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrictions.py
160 lines (148 loc) · 8.04 KB
/
restrictions.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
import re
import bitbucket
from runpy import run_path
import os
from environment import items
import argparse
from datetime import datetime as dt
#-------------------!!Init Bitbucket!!----------------------------#
def load_module(dir):
bitbucket_py = "bitbucket.py"
bitbucket_module = run_path(os.path.join(dir,bitbucket_py))
BitBucket = bitbucket_module['BitBucket']
return BitBucket
def connect_bitbucket(user, password):
url = items['auth']['url']
try:
nb_dir = os.path.split(os.getcwd())[0]
BitBucket = load_module(nb_dir)
except Exception:
nb_dir = os.getcwd()
BitBucket = load_module(nb_dir)
try:
bitbucket_ci = BitBucket(url, user, password, gitPath='tmp', is_windows=True)
except Exception:
print('Invalid login or password, please, check and try again')
exit()
return bitbucket_ci
#-------------------!!Init Bitbucket!!----------------------------#
def get_bb_ci_projects(bitbucket_ci):
res, projects = bitbucket_ci.get_projects()
bb_ci_projects_key = [x['key'].lower() for x in projects['values']]
return bb_ci_projects_key
def main(user, password, new_user):
#-------------------!!Main Variable!!----------------------------#
bitbucket_ci = connect_bitbucket(user, password)
project_repos_restrict = {}
all_project_key = get_bb_ci_projects(bitbucket_ci)
count_project_permission = 0
count_repos_permission = 0
count_project = len(all_project_key)
count_repos = 0
try:
bitbucket_ci.get_user_info(new_user)
except Exception:
print('Invalid new user, please, check and try again')
exit()
#-------------------!!Main!!----------------------------#
print('Всего проектов: {}'.format(str(count_project)))
for project in all_project_key:
all_repos = bitbucket_ci.get_repos(project)
count_repos += len(all_repos)
project_restrict_list = bitbucket_ci.get_project_restrictions(project)
for project_restrict in project_restrict_list[1]['values']:
count_project_permission += 1
if project_restrict['type'] != 'fast-forward-only':
current_project_keys = []
for current in project_restrict['accessKeys']:
current_project_keys.append(current['key']['id'])
current_project_groups = []
for current in project_restrict['groups']:
current_project_groups.append(current)
new_project_users = []
for current in project_restrict['users']:
if current['name'] != 'simplelogin':
try:
bitbucket_ci.get_user_info(current['name'])
new_project_users.append(current['name'])
except Exception:
with open('error_restriction.log', 'a') as log_file:
log_file.write('WARNING: Cannot get {} - reason: inactive\n'.format(current['name']))
new_project_users.append(new_user)
new_project_users.remove(new_user)
new_project_restrict = {"types": project_restrict['type'], "matcher": {"id": project_restrict['matcher']['id'], "displayId": project_restrict['matcher']['displayId'], "type": project_restrict['matcher']['type'], "active": project_restrict['matcher']['active']}, "users": new_project_users, "groups": current_project_groups, "accessKeys": current_project_keys}
try:
project_change = bitbucket_ci.set_project_restrictions(project, restrict = new_project_restrict)
except Exception as e:
with open('error_restriction.log', 'a') as log_file:
log_file.write(str(e) + '\n')
for repos in all_repos[1]['values']:
try:
repos_restrict_list = bitbucket_ci.get_restrictions(project, repos['name'])
if repos_restrict_list[0]['status_code'] == 200 and repos_restrict_list[1]['size'] > 0:
for repos_restrict in repos_restrict_list[1]['values']:
count_repos_permission += 1
if (repos_restrict['scope']['type'] != 'PROJECT') and (repos_restrict['type'] != 'fast-forward-only'):
current_keys = []
for current in repos_restrict['accessKeys']:
current_keys.append(current['key']['id'])
current_groups = []
for current in repos_restrict['groups']:
current_groups.append(current)
new_users = []
for current in repos_restrict['users']:
if current['name'] != 'Metlyakov1-AA':
try:
bitbucket_ci.get_user_info(current['name'])
new_users.append(current['name'])
except Exception:
with open('error_restriction.log', 'a') as log_file:
log_file.write('WARNING: Cannot get {} - reason: inactive\n'.format(current['name']))
new_users.append(new_user)
new_users.remove(new_user)
new_restrict = {"types": repos_restrict['type'], "matcher": {"id": repos_restrict['matcher']['id'], "displayId": repos_restrict['matcher']['displayId'], "type": repos_restrict['matcher']['type'], "active": repos_restrict['matcher']['active']}, "users": new_users, "groups": current_groups, "accessKeys": current_keys}
try:
repos_change = bitbucket_ci.set_restrictions(project, repos['name'], restrict = new_restrict)
except Exception as e:
with open('error_restriction.log', 'a') as log_file:
log_file.write(str(e) + '\n')
except Exception as e:
with open('error_restriction.log', 'a') as log_file:
log_file.write(str(e) + '\n')
return count_project_permission, count_repos_permission, count_project, count_repos
def check_input(param, type):
escape_char = ('\n', '\t', ' ', '\r', '\a', '\f', '\v', '\b')
for char in escape_char:
param = param.replace(char, '')
if len(param) == 0:
print("Your {type} doesn't correct".format(type = type))
exit()
else:
return param
parser = argparse.ArgumentParser()
parser.add_argument(
'login', type=str, help='Stash admin username')
parser.add_argument(
'password', type=str, help='Stash admin password')
parser.add_argument(
'-n', '--new', type=str, help='User login what you need to push into all branch permission', required = True)
args = parser.parse_args()
user = args.login
user = check_input(user, 'login')
password = args.password
password = check_input(password, 'password')
new_user = args.new
new_user = check_input(new_user, 'new user')
start_time = dt.now()
print('Время начала: {}'.format(str(dt.now())))
count_all = main(user, password, new_user)
finish_time = dt.now()
print('Всего ограничений на уровне проекта: {}'.format(str(count_all[0])))
print('Всего репозиториев: {}'.format(str(count_all[3])))
print('Всего ограничений на уровне репозитория: {}'.format(str(count_all[1])))
print('Время окончания: {}'.format(str(dt.now())))
delta_time = finish_time - start_time
print('Всего затрачено: {}'.format(str(delta_time)))
print('В среднем на проект: {}'. format(str(delta_time/count_all[2])))
print('В среднем на репозиторий: {}'. format(str(delta_time/count_all[3])))
print('В среднем на один Branch Permission: {}'. format(str(delta_time/(count_all[0] + count_all[1]))))