Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Henrik Skov Midtiby
gitlab-assignment-handler
Commits
14831929
Commit
14831929
authored
Aug 10, 2020
by
Henrik Skov Midtiby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated comments.
parent
e7fd3f2b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
7 deletions
+14
-7
gitlab_class_helper/gitlab_class_helper.py
gitlab_class_helper/gitlab_class_helper.py
+14
-7
No files found.
gitlab_class_helper/gitlab_class_helper.py
View file @
14831929
...
...
@@ -7,12 +7,19 @@ from typing import List
class
GitlabClassHelper
:
def
__init__
(
self
,
url
:
str
,
access_token
:
str
):
"""
Used to initialize the connection to the gitlab server.
"""
self
.
path
=
url
self
.
headers
=
{
'Authorization'
:
'Bearer {}'
.
format
(
access_token
)}
self
.
check_connection
()
self
.
group_id
=
None
def
check_connection
(
self
):
"""
The method is used to verify that an authentificated connection
is established to the gitlab server.
"""
url
=
self
.
path
+
'/projects'
resp
=
requests
.
get
(
url
,
self
.
headers
)
if
resp
.
status_code
==
401
:
...
...
@@ -30,7 +37,7 @@ class GitlabClassHelper:
def
create_class
(
self
,
group_name
:
str
,
id_of_parent_group
:
int
)
->
int
:
"""
Create a new group with group_name as a subgroup of that parent with id id_of_parent_group
returns the new groups id
returns the new groups id
.
"""
group_path
=
''
.
join
(
e
if
e
.
isalnum
()
else
'-'
for
e
in
group_name
)
url
=
self
.
path
+
'/groups?name=%s&path=%s&parent_id=%d&visibility=private'
%
(
group_name
,
group_path
,
id_of_parent_group
)
...
...
@@ -49,7 +56,7 @@ class GitlabClassHelper:
def
create_assignment_repositories
(
self
,
repositories
:
List
[
str
],
group_id
:
int
=
None
):
"""
Create repositories in working group or in the group with group_id if supplied.
Create
remote
repositories in working group or in the group with group_id if supplied.
"""
use_group_id
,
group_name
=
self
.
check_group_id
(
group_id
)
for
repo
in
set
(
repositories
):
...
...
@@ -58,7 +65,7 @@ class GitlabClassHelper:
def
create_repo
(
self
,
repo_name
:
str
,
group_id
:
int
,
group_name
:
str
):
"""
Create a repository in the group with id
Create a
remote
repository in the group with id
"""
url
=
self
.
path
+
'/projects?name=%s&namespace_id=%d'
%
(
repo_name
,
group_id
)
resp
=
requests
.
post
(
url
,
headers
=
self
.
headers
)
...
...
@@ -82,7 +89,7 @@ class GitlabClassHelper:
def
add_students_to_repositories
(
self
,
repositories
:
List
[
str
],
user_ids
:
List
[
int
],
group_id
:
int
=
None
):
"""
Give developer access rights to user in repository.
Give developer access rights to user in
remote
repository.
"""
use_group_id
,
_
=
self
.
check_group_id
(
group_id
)
for
repo
,
user_id
in
zip
(
repositories
,
user_ids
):
...
...
@@ -195,7 +202,7 @@ class GitlabClassHelper:
def
clone_repositories
(
self
,
repositories
:
List
[
str
],
local_path_to_fetch_to
:
str
,
group_id
:
int
=
None
):
"""
Clone all repositories in "repositories" to the local path "local_path_to_fetch_to"
Clone all
remote
repositories in "repositories" to the local path "local_path_to_fetch_to"
"""
use_group_id
,
_
=
self
.
check_group_id
(
group_id
)
for
repo
in
set
(
repositories
):
...
...
@@ -207,14 +214,14 @@ class GitlabClassHelper:
def
clone_repo
(
self
,
local_folder
:
str
,
project_id
:
int
):
"""
Clone the project into the local folder
Clone the
remote
project into the local folder
"""
ssh_url_to_git_repo
=
self
.
get_project_ssh_url
(
project_id
)
git
.
Repo
.
clone_from
(
ssh_url_to_git_repo
,
local_folder
)
def
push_repositories
(
self
,
repositories
:
List
[
str
],
local_path
:
str
,
group_id
:
int
=
None
):
"""
Push repositories to remotes.
Push
local
repositories to remote
repositorie
s.
"""
use_group_id
,
_
=
self
.
check_group_id
(
group_id
)
for
repo
in
set
(
repositories
):
...
...
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