Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gitlab-getlists
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WP9-T2-SWD-Tools
gitlab-getlists
Commits
fe2ad705
Commit
fe2ad705
authored
2 years ago
by
kazix
Browse files
Options
Downloads
Patches
Plain Diff
add scripts to manage users
parent
1c14cb09
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
delete_users.py
+44
-0
44 additions, 0 deletions
delete_users.py
get-users.py
+124
-0
124 additions, 0 deletions
get-users.py
main.py
+2
-0
2 additions, 0 deletions
main.py
put-users.py
+55
-0
55 additions, 0 deletions
put-users.py
with
225 additions
and
0 deletions
delete_users.py
0 → 100644
+
44
−
0
View file @
fe2ad705
import
gitlab
import
json
import
argparse
import
os
from
dotenv
import
load_dotenv
parser
=
argparse
.
ArgumentParser
(
description
=
"
Achtung! Achtung! Delete users form Gitlab
"
)
parser
.
add_argument
(
"
--production
"
,
action
=
'
store_true
'
,
help
=
"
Use for Production
"
)
parser
.
add_argument
(
"
--developer
"
,
action
=
'
store_true
'
,
help
=
"
Use for Developer
"
)
args
=
parser
.
parse_args
()
load_dotenv
()
try
:
if
args
.
production
:
token
=
os
.
getenv
(
'
PRODUCTION_TOKEN
'
)
print
(
'
Production Mode, connect to server
'
)
# gl = gitlab.Gitlab(url='https://gitlab.geant.org', private_token=token)
elif
args
.
developer
:
token
=
os
.
getenv
(
'
CE_DEVELOPER_TOKEN
'
)
print
(
'
Developer Mode, connect to server
'
)
gl
=
gitlab
.
Gitlab
(
url
=
'
https://ribes-89.man.poznan.pl
'
,
private_token
=
token
)
else
:
print
(
"
--production or --developer, please, now exit
"
)
exit
()
except
:
print
(
'
Something wrong or Cant connect to GitLab server, check credential
'
)
exit
()
with
open
(
'
ultimate_users.json
'
,
encoding
=
'
utf8
'
)
as
ultimate_users_file
:
ultimate_users
=
json
.
load
(
ultimate_users_file
)
all_users
=
ultimate_users
[
'
all_users
'
]
# delete users
for
user
in
all_users
:
# u = all_users[user]
d
=
all_users
[
user
][
'
username
'
]
# new_user = gl.users.create(u)
d_user
=
gl
.
users
.
list
(
username
=
d
)[
0
]
d_user
.
delete
()
This diff is collapsed.
Click to expand it.
get-users.py
0 → 100644
+
124
−
0
View file @
fe2ad705
import
gitlab
import
requests
import
json
import
argparse
import
os
from
dotenv
import
load_dotenv
import
secrets
import
string
def
randompass
():
"""
funkcja zwraca losowe hasło
"""
# define the alphabet
letters
=
string
.
ascii_letters
digits
=
string
.
digits
# special_chars = string.punctuation
alphabet
=
letters
+
digits
# + special_chars
# fix password length
pwd_length
=
16
# generate a password string
pwd
=
''
for
i
in
range
(
pwd_length
):
pwd
+=
''
.
join
(
secrets
.
choice
(
alphabet
))
return
pwd
parser
=
argparse
.
ArgumentParser
(
description
=
"
Export users and Owners from gitlab
"
)
parser
.
add_argument
(
"
--production
"
,
action
=
'
store_true
'
,
help
=
"
Use for Production
"
)
parser
.
add_argument
(
"
--developer
"
,
action
=
'
store_true
'
,
help
=
"
Use for Developer
"
)
args
=
parser
.
parse_args
()
load_dotenv
()
try
:
if
args
.
production
:
token
=
os
.
getenv
(
'
PRODUCTION_TOKEN
'
)
print
(
'
Production Mode, connect to server
'
)
headers
=
{
'
PRIVATE-TOKEN
'
:
token
}
gl
=
gitlab
.
Gitlab
(
url
=
'
https://gitlab.geant.org
'
,
private_token
=
token
)
gl_proj
=
'
https://gitlab.geant.org/api/v4/projects
'
projects
=
gl
.
projects
.
list
(
all
=
True
)
elif
args
.
developer
:
token
=
os
.
getenv
(
'
CE_DEVELOPER_TOKEN
'
)
print
(
'
Developer Mode, connect to server
'
)
headers
=
{
'
PRIVATE-TOKEN
'
:
token
}
gl
=
gitlab
.
Gitlab
(
url
=
'
https://ribes-89.man.poznan.pl
'
,
private_token
=
token
)
projects
=
gl
.
projects
.
list
()
else
:
print
(
"
--production or --developer, please, now exit
"
)
exit
()
exclude_users
=
[
'
root
'
,
'
ghost
'
]
ultimate_users
=
{}
ultimate_users
[
'
all_users
'
]
=
{}
users
=
gl
.
users
.
list
(
all
=
True
)
# print(users)
for
user
in
users
:
u
=
gl
.
users
.
get
(
user
.
id
)
if
u
.
username
not
in
exclude_users
:
print
(
u
.
id
)
ultimate_users
[
'
all_users
'
][
u
.
id
]
=
{}
tmp
=
{
'
username
'
:
u
.
username
,
'
name
'
:
u
.
name
,
'
state
'
:
u
.
state
,
'
avatar_url
'
:
u
.
avatar_url
,
'
web_url
'
:
u
.
web_url
,
'
bio
'
:
u
.
bio
,
'
location
'
:
u
.
location
,
# 'public_email': u.public_email,
'
skype
'
:
u
.
skype
,
'
linkedin
'
:
u
.
linkedin
,
'
twitter
'
:
u
.
twitter
,
'
discord
'
:
u
.
discord
,
'
website_url
'
:
u
.
website_url
,
'
organization
'
:
u
.
organization
,
'
job_title
'
:
u
.
job_title
,
'
email
'
:
u
.
email
,
'
commit_email
'
:
u
.
commit_email
,
'
password
'
:
randompass
()
}
ultimate_users
[
'
all_users
'
][
u
.
id
]
=
tmp
# print(ultimate_users)
ultimate_users
[
'
owners
'
]
=
{}
# owners_tmp = {}
for
project
in
projects
:
link
=
(
project
.
attributes
[
'
_links
'
][
'
members
'
]
+
'
/all
'
)
response
=
requests
.
get
(
link
,
headers
=
headers
)
data
=
response
.
text
owners
=
json
.
loads
(
data
)
for
owner
in
owners
:
if
owner
[
'
username
'
]
not
in
exclude_users
:
if
owner
[
'
access_level
'
]
==
50
:
if
owner
[
'
id
'
]
not
in
ultimate_users
[
'
owners
'
]:
ultimate_users
[
'
owners
'
][
owner
[
'
id
'
]]
=
{
'
username
'
:
owner
[
'
username
'
]
}
try
:
json_users
=
json
.
dumps
(
ultimate_users
,
indent
=
4
,
ensure_ascii
=
False
)
with
open
(
"
ultimate_users.json
"
,
"
w
"
,
encoding
=
'
utf8
'
)
as
outfile
:
outfile
.
write
(
json_users
)
print
(
"
save json file...
"
)
except
:
print
(
'
cant save file
'
)
exit
()
except
:
print
(
'
Something wrong or Cant connect to GitLab server, check credential
'
)
exit
()
This diff is collapsed.
Click to expand it.
main.py
+
2
−
0
View file @
fe2ad705
...
...
@@ -52,6 +52,8 @@ try:
check_b_response
=
requests
.
get
(
check_b
,
headers
=
headers
)
check_b_data
=
check_b_response
.
text
check_b_answer
=
json
.
loads
(
check_b_data
)
# check approvals
print
(
'
Approval Rules:
'
,
project
.
approvalrules
.
list
())
if
"
default_branch
"
in
check_b_answer
:
branch
=
project
.
attributes
[
'
default_branch
'
]
# x = project.attributes['id']
...
...
This diff is collapsed.
Click to expand it.
put-users.py
0 → 100644
+
55
−
0
View file @
fe2ad705
import
gitlab
import
json
import
argparse
import
os
from
dotenv
import
load_dotenv
parser
=
argparse
.
ArgumentParser
(
description
=
"
Get Project, License and Owners from gitlab
"
)
parser
.
add_argument
(
"
--production
"
,
action
=
'
store_true
'
,
help
=
"
Use for Production
"
)
parser
.
add_argument
(
"
--developer
"
,
action
=
'
store_true
'
,
help
=
"
Use for Developer
"
)
args
=
parser
.
parse_args
()
load_dotenv
()
try
:
if
args
.
production
:
token
=
os
.
getenv
(
'
PRODUCTION_TOKEN
'
)
print
(
'
Production Mode, connect to server
'
)
# gl = gitlab.Gitlab(url='https://gitlab.geant.org', private_token=token)
elif
args
.
developer
:
token
=
os
.
getenv
(
'
CE_DEVELOPER_TOKEN
'
)
print
(
'
Developer Mode, connect to server
'
)
gl
=
gitlab
.
Gitlab
(
url
=
'
https://ribes-89.man.poznan.pl
'
,
private_token
=
token
)
else
:
print
(
"
--production or --developer, please, now exit
"
)
exit
()
except
:
print
(
'
Something wrong or Cant connect to GitLab server, check credential
'
)
exit
()
with
open
(
'
ultimate_users.json
'
,
encoding
=
'
utf8
'
)
as
ultimate_users_file
:
ultimate_users
=
json
.
load
(
ultimate_users_file
)
all_users
=
ultimate_users
[
'
all_users
'
]
# tworzenie usera
for
user
in
all_users
:
u
=
all_users
[
user
]
print
(
"
create user:
"
,
u
[
'
username
'
])
new_user
=
gl
.
users
.
create
(
u
)
# zmiana uprawnień
owners
=
ultimate_users
[
'
owners
'
]
for
owner
in
owners
:
o
=
owners
[
owner
][
'
username
'
]
new_owner
=
gl
.
users
.
list
(
username
=
o
)[
0
]
new_owner
.
can_create_group
=
'
true
'
new_owner
.
projects_limit
=
'
10
'
new_owner
.
save
()
print
(
'
add permission for user:
'
,
o
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment