Skip to content
Snippets Groups Projects
Commit 99923d9d authored by Adrien Lescourt's avatar Adrien Lescourt
Browse files

Split repo creation and user managment for the repo

parent d15c753b
Branches
No related tags found
No related merge requests found
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# WIP REFACTOR STATUS # WIP REFACTOR STATUS
- [ ] Create group repo - [x] Create group repo
- [ ] Clone repo - [ ] Clone repo
- [x] List groups - [x] List groups
- [x] List projects - [x] List projects
......
...@@ -23,7 +23,7 @@ TOKEN_URL: str = BASE_URL + "/-/user_settings/personal_access_tokens" ...@@ -23,7 +23,7 @@ TOKEN_URL: str = BASE_URL + "/-/user_settings/personal_access_tokens"
@dataclass @dataclass
class User: class User:
id: str user_login_aai: str
@dataclass @dataclass
...@@ -104,43 +104,25 @@ class Gitlab: ...@@ -104,43 +104,25 @@ class Gitlab:
def get_user_id(self, user: User) -> str | None: def get_user_id(self, user: User) -> str | None:
headers = {"PRIVATE-TOKEN": self.token} headers = {"PRIVATE-TOKEN": self.token}
res = requests.get( 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: if res.status_code != 200:
print(f"Error searching user {user.id}") print(f"Error searching user {user.user_login_aai}")
print(res.text) print(res.text)
else: else:
return res.json()[0]["id"] return str(res.json()[0]["id"])
def create_repository( def add_users_to_repository(
self, self,
group_id: str, project_id: str,
project_name: str,
users: list[User], users: list[User],
name: str,
import_url: str | None,
expires_at: str | None, expires_at: str | None,
): ):
""" """Add users to a repo, and set their permissions"""
Create repository in group_id, with members from user_ids, a name, and
optional import_url and expiration date.
"""
headers = {"PRIVATE-TOKEN": self.token} 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 # Allow users with developer access level to push and merge on master
access_level = 40 access_level = 40
params = { params = {
...@@ -148,42 +130,58 @@ class Gitlab: ...@@ -148,42 +130,58 @@ class Gitlab:
"push_access_level": str(access_level), "push_access_level": str(access_level),
"merge_access_level": str(access_level), "merge_access_level": str(access_level),
} }
requests.post( res = requests.post(
BASE_API_URL + "/projects/" + str(project["id"]) + "/protected_branches", BASE_API_URL + "/projects/" + str(project_id) + "/protected_branches",
params=params, params=params,
headers=headers, 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: for user in users:
user_id = self.get_user_id(user) user_id = self.get_user_id(user)
if user_id is None: 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} params = {"user_id": user_id, "access_level": access_level}
if expires_at: if expires_at:
params["expires_at"] = expires_at params["expires_at"] = expires_at
res = requests.post( res = requests.post(
BASE_API_URL + "/projects/" + str(project["id"]) + "/members", BASE_API_URL + "/projects/" + project_id + "/members",
params=params, params=params,
headers=headers, headers=headers,
) )
if res.status_code != 201: if res.status_code != 201:
print(res.text) print(
else: f"ERROR: User {user.user_login_aai} will not be added to project: {project_name}"
new_user = res.json()
out = (
"Adding '"
+ new_user["name"]
+ "' ("
+ new_user["username"]
+ ") in '"
+ project["name"]
+ "' with access level: "
+ str(new_user["access_level"])
) )
if expires_at: print(res.text)
out += ", expires at: " + new_user["expires_at"]
print(out) 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 @staticmethod
def _paginate_responses( def _paginate_responses(
...@@ -325,15 +323,17 @@ def command_create_group_repos(args): ...@@ -325,15 +323,17 @@ def command_create_group_repos(args):
ds = DataSource.from_yaml(args.repos_file) ds = DataSource.from_yaml(args.repos_file)
group_id = gl.create_group(args.group_name, args.visibility) group_id = gl.create_group(args.group_name, args.visibility)
for group in ds.groups: for group in ds.groups:
gl.create_repository( project_id = gl.create_repository(
group_id, group_id,
group.user_ids,
group.name, group.name,
args.import_url, args.import_url,
args.expires_at,
) )
print(f"Created repo: {group.name} with the users: {group.user_ids}") gl.add_users_to_repository(
print() 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): def command_clone_all(args):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment