Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
practical-work-manager
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
Model registry
Operate
Environments
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
ISC
projects
practical-work-manager
Commits
99923d9d
Commit
99923d9d
authored
1 month ago
by
Adrien Lescourt
Browse files
Options
Downloads
Patches
Plain Diff
Split repo creation and user managment for the repo
parent
d15c753b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+1
-1
1 addition, 1 deletion
README.md
pwm/pwm.py
+54
-54
54 additions, 54 deletions
pwm/pwm.py
with
55 additions
and
55 deletions
README.md
+
1
−
1
View file @
99923d9d
...
...
@@ -3,7 +3,7 @@
# WIP REFACTOR STATUS
-
[
] Create group repo
-
[
x
] Create group repo
-
[ ] Clone repo
-
[x] List groups
-
[x] List projects
...
...
This diff is collapsed.
Click to expand it.
pwm/pwm.py
+
54
−
54
View file @
99923d9d
...
...
@@ -23,7 +23,7 @@ TOKEN_URL: str = BASE_URL + "/-/user_settings/personal_access_tokens"
@dataclass
class
User
:
i
d
:
str
user_login_aa
i
:
str
@dataclass
...
...
@@ -104,43 +104,25 @@ class Gitlab:
def
get_user_id
(
self
,
user
:
User
)
->
str
|
None
:
headers
=
{
"
PRIVATE-TOKEN
"
:
self
.
token
}
res
=
requests
.
get
(
BASE_API_URL
+
"
/users
"
,
params
=
{
"
search
"
:
user
.
id
},
headers
=
headers
BASE_API_URL
+
"
/users
"
,
params
=
{
"
search
"
:
user
.
user_login_aai
},
headers
=
headers
,
)
if
res
.
status_code
!=
200
:
print
(
f
"
Error searching user
{
user
.
i
d
}
"
)
print
(
f
"
Error searching user
{
user
.
user_login_aa
i
}
"
)
print
(
res
.
text
)
else
:
return
res
.
json
()[
0
][
"
id
"
]
return
str
(
res
.
json
()[
0
][
"
id
"
]
)
def
create
_repository
(
def
add_users_to
_repository
(
self
,
group_id
:
str
,
project_id
:
str
,
project_name
:
str
,
users
:
list
[
User
],
name
:
str
,
import_url
:
str
|
None
,
expires_at
:
str
|
None
,
):
"""
Create repository in group_id, with members from user_ids, a name, and
optional import_url and expiration date.
"""
"""
Add users to a repo, and set their permissions
"""
headers
=
{
"
PRIVATE-TOKEN
"
:
self
.
token
}
# Create project from name, import_url (if given) and group_id
params
=
{
"
name
"
:
name
,
"
namespace_id
"
:
group_id
,
"
visibility
"
:
"
private
"
}
if
import_url
:
params
[
"
import_url
"
]
=
import_url
project
=
requests
.
post
(
BASE_API_URL
+
"
/projects
"
,
params
=
params
,
headers
=
headers
).
json
()
if
"
message
"
in
project
:
print
(
"
Error in creating project: %s
"
%
project
)
exit
(
1
)
print
(
"
Project
'"
+
project
[
"
name
"
]
+
"'
at
'"
+
project
[
"
web_url
"
]
+
"'
created
"
)
# Allow users with developer access level to push and merge on master
access_level
=
40
params
=
{
...
...
@@ -148,42 +130,58 @@ class Gitlab:
"
push_access_level
"
:
str
(
access_level
),
"
merge_access_level
"
:
str
(
access_level
),
}
requests
.
post
(
BASE_API_URL
+
"
/projects/
"
+
str
(
project
[
"
id
"
]
)
+
"
/protected_branches
"
,
res
=
requests
.
post
(
BASE_API_URL
+
"
/projects/
"
+
str
(
project
_id
)
+
"
/protected_branches
"
,
params
=
params
,
headers
=
headers
,
).
json
()
)
if
res
.
status_code
!=
201
:
raise
Exception
(
f
"
Error setting project (
{
project_name
}
) premissions
"
,
res
.
text
)
# Add each student as maintainer (level 40)
for
user
in
users
:
user_id
=
self
.
get_user_id
(
user
)
if
user_id
is
None
:
print
(
f
"
ERROR: User
{
user
.
id
}
will not be added to project:
{
name
}
"
)
print
(
f
"
ERROR: User
{
user
.
user_login_aai
}
will not be added to project:
{
project_name
}
"
)
params
=
{
"
user_id
"
:
user_id
,
"
access_level
"
:
access_level
}
if
expires_at
:
params
[
"
expires_at
"
]
=
expires_at
res
=
requests
.
post
(
BASE_API_URL
+
"
/projects/
"
+
str
(
project
[
"
id
"
])
+
"
/members
"
,
BASE_API_URL
+
"
/projects/
"
+
project
_id
+
"
/members
"
,
params
=
params
,
headers
=
headers
,
)
if
res
.
status_code
!=
201
:
print
(
res
.
text
)
else
:
new_user
=
res
.
json
()
out
=
(
"
Adding
'"
+
new_user
[
"
name
"
]
+
"'
(
"
+
new_user
[
"
username
"
]
+
"
) in
'"
+
project
[
"
name
"
]
+
"'
with access level:
"
+
str
(
new_user
[
"
access_level
"
])
print
(
f
"
ERROR: User
{
user
.
user_login_aai
}
will not be added to project:
{
project_name
}
"
)
if
expires_at
:
out
+=
"
, expires at:
"
+
new_user
[
"
expires_at
"
]
print
(
out
)
print
(
res
.
text
)
def
create_repository
(
self
,
group_id
:
str
,
name
:
str
,
import_url
:
str
|
None
,
)
->
str
:
"""
Create repository in group_id, with a name and optional import_url
"""
headers
=
{
"
PRIVATE-TOKEN
"
:
self
.
token
}
# Create project from name, import_url (if given) and group_id
params
=
{
"
name
"
:
name
,
"
namespace_id
"
:
group_id
,
"
visibility
"
:
"
private
"
}
if
import_url
:
params
[
"
import_url
"
]
=
import_url
res
=
requests
.
post
(
BASE_API_URL
+
"
/projects
"
,
params
=
params
,
headers
=
headers
)
if
res
.
status_code
!=
201
:
raise
Exception
(
f
"
Error creating project
{
name
}
"
,
res
.
text
)
return
str
(
res
.
json
()[
"
id
"
])
@staticmethod
def
_paginate_responses
(
...
...
@@ -325,15 +323,17 @@ def command_create_group_repos(args):
ds
=
DataSource
.
from_yaml
(
args
.
repos_file
)
group_id
=
gl
.
create_group
(
args
.
group_name
,
args
.
visibility
)
for
group
in
ds
.
groups
:
gl
.
create_repository
(
project_id
=
gl
.
create_repository
(
group_id
,
group
.
user_ids
,
group
.
name
,
args
.
import_url
,
args
.
expires_at
,
)
print
(
f
"
Created repo:
{
group
.
name
}
with the users:
{
group
.
user_ids
}
"
)
print
()
gl
.
add_users_to_repository
(
project_id
,
group
.
name
,
group
.
user_ids
,
args
.
expires_at
)
print
(
f
"
Created repo:
{
group
.
name
}
with the users:
{
[
u
.
user_login_aai
for
u
in
group
.
user_ids
]
}
"
)
def
command_clone_all
(
args
):
...
...
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