UNCLASSIFIED
Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Ironbank Tools
project-template
Commits
93bfab52
Commit
93bfab52
authored
Mar 04, 2021
by
Joshua Eason
Browse files
Adding support for individual branch protections
parent
648140b7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
12 deletions
+56
-12
modules/branches.py
modules/branches.py
+56
-12
No files found.
modules/branches.py
View file @
93bfab52
import
requests
import
logging
import
json
from
collections
import
defaultdict
##### Supporting methods
...
...
@@ -95,14 +96,38 @@ def __createProtectedBranch(url, token, id, access):
PARAMS
=
{}
for
i
in
access
:
PARAMS
[
i
]
=
access
[
i
]
if
i
!=
'allowed_to_push'
and
i
!=
'allowed_to_merge'
:
PARAMS
[
i
]
=
access
[
i
]
if
'allowed_to_push'
in
access
.
keys
():
if
len
(
access
[
'allowed_to_push'
])
==
1
:
if
access
[
'allowed_to_push'
][
0
]
==
None
:
PARAMS
[
'push_access_level'
]
=
0
elif
access
[
'allowed_to_push'
][
0
].
isnumeric
():
PARAMS
[
'allowed_to_push'
]
=
access
[
'allowed_to_push'
][
0
]
else
:
PARAMS
[
'allowed_to_push[][user_id]'
]
=
[]
for
i
in
access
[
'allowed_to_push'
]:
if
i
!=
None
:
PARAMS
[
'allowed_to_push[][user_id]'
].
append
(
i
)
if
'allowed_to_merge'
in
access
.
keys
():
if
len
(
access
[
'allowed_to_merge'
])
==
1
:
if
access
[
'allowed_to_merge'
][
0
]
==
None
:
PARAMS
[
'merge_access_level'
]
=
0
elif
access
[
'allowed_to_merge'
].
isnumeric
():
PARAMS
[
'merge_access_level'
]
=
access
[
'allowed_to_merge'
][
0
]
else
:
PARAMS
[
'allowed_to_merge[][user_id]'
]
=
[]
for
i
in
access
[
'allowed_to_merge'
]:
if
i
!=
None
:
PARAMS
[
'allowed_to_merge[][user_id]'
].
append
(
i
)
# Delete the protected branch first, otherwise this will fail. However, we don't know for sure
# that this is protected so we ignore the response
response
=
requests
.
delete
(
url
+
"/"
+
access
[
'name'
],
headers
=
HEADER
)
response
=
requests
.
post
(
url
,
params
=
PARAMS
,
headers
=
HEADER
)
if
response
.
status_code
!=
201
:
logging
.
critical
(
"Failed to create protected branch: "
+
str
(
response
.
status_code
)
+
" "
+
response
.
reason
)
...
...
@@ -114,7 +139,7 @@ def __listEqual(a, b):
for
i
in
a
:
match
=
False
for
j
in
b
:
if
i
==
j
and
a
[
i
]
==
b
[
j
]
:
if
i
==
j
:
match
=
True
if
match
==
False
:
...
...
@@ -167,13 +192,25 @@ def checkProtected(tprotected, protected):
if
a
[
'name'
]
==
b
[
'name'
]:
found
=
True
if
len
(
a
[
'push_access_levels'
])
>
0
and
len
(
b
[
'push_access_levels'
])
>
0
:
if
__listEqual
(
a
[
'push_access_levels'
]
[
0
]
,
b
[
'push_access_levels'
]
[
0
]
)
==
False
:
if
__listEqual
(
a
[
'push_access_levels'
],
b
[
'push_access_levels'
])
==
False
:
misprotectedBranches
.
append
(
a
)
if
len
(
a
[
'merge_access_levels'
])
>
0
and
len
(
b
[
'merge_access_levels'
])
>
0
:
if
__listEqual
(
a
[
'merge_access_levels'
]
[
0
]
,
b
[
'merge_access_levels'
]
[
0
]
)
==
False
:
if
__listEqual
(
a
[
'merge_access_levels'
],
b
[
'merge_access_levels'
])
==
False
:
misprotectedBranches
.
append
(
a
)
# if len(a['allowed_to_push']) > 0 and len(b['allowed_to_push']) > 0:
# if __listEqual(a['allowed_to_push'], b['allowed_to_push']) == False:
# misprotectedBranches.append(a)
# if len(a['allowed_to_merge']) > 0 and len(b['allowed_to_merge']) > 0:
# if __listEqual(a['allowed_to_merge'], b['allowed_to_merge']) == False:
# misprotectedBranches.append(a)
# if len(a['allowed_to_unprotect']) > 0 and len(b['allowed_to_unprotect']) > 0:
# if __listEqual(a['allowed_to_unprotect'], b['allowed_to_unprotect']) == False:
# misprotectedBranches.append(a)
# If not specified from CLI, GitLab automatically adds this but doesn't match
# when creating from the UI. Doesn't seem to impact anything, so commenting out.
# elif a['unprotect_access_levels'] != b['unprotect_access_levels']:
...
...
@@ -215,34 +252,41 @@ def fixProtected(url, token, id, misprotected):
if
'push_access_levels'
in
i
.
keys
():
if
len
(
i
[
'push_access_levels'
])
>
0
:
access
[
'push_access_level'
]
=
i
[
'push_access_levels'
][
0
][
'access_level'
]
access
[
'allowed_to_push'
]
=
[]
for
level
in
i
[
'push_access_levels'
]:
access
[
'allowed_to_push'
].
append
(
level
[
'user_id'
])
if
'merge_access_levels'
in
i
.
keys
():
if
len
(
i
[
'merge_access_levels'
])
>
0
:
access
[
'merge_access_level'
]
=
i
[
'merge_access_levels'
][
0
][
'access_level'
]
access
[
'allowed_to_merge'
]
=
[]
for
level
in
i
[
'merge_access_levels'
]:
access
[
'allowed_to_merge'
].
append
(
level
[
'user_id'
])
# access['allowed_to_merge'] = i['merge_access_levels']
if
'unprotect_access_levels'
in
i
.
keys
():
if
len
(
i
[
'unprotect_access_levels'
])
>
0
:
access
[
'
unprotect_access_level
'
]
=
i
[
'unprotect_access_levels'
]
[
0
][
'access_level'
]
access
[
'
allowed_to_unprotect
'
]
=
i
[
'unprotect_access_levels'
]
if
'code_owner_approval_required'
in
i
.
keys
():
access
[
'code_owner_approval_required'
]
=
i
[
'code_owner_approval_required'
]
if
'allowed_to_push'
in
i
.
keys
():
if
len
(
i
[
'allowed_to_push'
])
>
0
:
access
[
'allowed_to_push'
]
=
i
[
'allowed_to_push'
]
[
0
][
'access_level'
]
access
[
'allowed_to_push'
]
=
i
[
'allowed_to_push'
]
if
'allowed_to_merge'
in
i
.
keys
():
if
len
(
i
[
'allowed_to_merge'
])
>
0
:
access
[
'allowed_to_merge'
]
=
i
[
'allowed_to_merge'
]
[
0
][
'access_level'
]
access
[
'allowed_to_merge'
]
=
i
[
'allowed_to_merge'
]
if
'allowed_to_unprotect'
in
i
.
keys
():
if
len
(
i
[
'allowed_to_unprotect'
]
>
0
):
access
[
'allowed_to_unprotect'
]
=
i
[
'allowed_to_unprotect'
]
[
0
][
'access_level'
]
access
[
'allowed_to_unprotect'
]
=
i
[
'allowed_to_unprotect'
]
__createProtectedBranch
(
url
,
token
,
id
,
access
)
\ No newline at end of file
)
# [{'access_level': 40, 'access_level_description': 'Al Fontaine', 'user_id': 1243, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Andy Maksymowicz', 'user_id': 1242, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Hunter Stevens', 'user_id': 3801, 'group_id': None}, {'access_level': 40, 'access_level_description': 'thomas.shepherd', 'user_id': 2280, 'group_id': None}, {'access_level': 0, 'access_level_description': 'No one', 'user_id': None, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Joshua Eason', 'user_id': 514, 'group_id': None}, {'access_level': 40, 'access_level_description': 'alexander.klepal', 'user_id': 2237, 'group_id': None}, {'access_level': 40, 'access_level_description': 'balexand', 'user_id': 2971, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Jeffrey Weatherford', 'user_id': 3827, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Chris Byrd', 'user_id': 7184, 'group_id': None}, {'access_level': 40, 'access_level_description': 'sean.melissari', 'user_id': 1880, 'group_id': None}, {'access_level': 40, 'access_level_description': 'kwami.delali', 'user_id': 2588, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Michael Simmons', 'user_id': 2570, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Olga Ojjeh', 'user_id': 5562, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Fred Melendez', 'user_id': 1572, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Kelley Trujillo', 'user_id': 5480, 'group_id': None}, {'access_level': 40, 'access_level_description': 'bhearn', 'user_id': 2620, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Zachary Sanders', 'user_id': 5938, 'group_id': None}, {'access_level': 40, 'access_level_description': 'shen_vickie@bah.com', 'user_id': 2469, 'group_id': None}, {'access_level': 40, 'access_level_description': 'Christopher Vernooy', 'user_id': 2242, 'group_id': None}]
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment