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
c61faeeb
Commit
c61faeeb
authored
2 years ago
by
Florent Gluck
Browse files
Options
Downloads
Patches
Plain Diff
Dirty hack which fixes the issue in which a user id cannot be retrieved based on an email.
parent
1f977907
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#19536
passed
2 years ago
Stage: build
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pwm
+53
-12
53 additions, 12 deletions
pwm
with
53 additions
and
12 deletions
pwm
+
53
−
12
View file @
c61faeeb
...
...
@@ -59,15 +59,30 @@ def emails_to_ids(emails: List[str], headers: Dict[str, str]) -> List[int]:
Get students ids from their emails
"""
user_ids
=
[]
for
email
in
emails
:
user_requested
=
requests
.
get
(
BASE_API_URL
+
'
/users
'
,
params
=
{
'
search
'
:
email
},
headers
=
headers
).
json
()
if
len
(
user_requested
)
==
0
:
print
(
'
No user %s found, operation aborted
'
%
email
)
exit
(
1
)
user_ids
.
append
(
user_requested
[
0
][
'
id
'
])
return
user_ids
# Dirty and hackish way that attempts to extract the username from the email.
# It's inefficient, but currently there is now way to reliably obtain
# the username from the email.
username
=
email
.
split
(
"
@
"
)[
0
]
#print("Email: ",email)
while
len
(
username
)
>
1
:
#print("Guessed username: ",username)
user_requested
=
requests
.
get
(
BASE_API_URL
+
'
/users
'
,
params
=
{
'
search
'
:
username
},
headers
=
headers
).
json
()
if
len
(
user_requested
)
==
0
:
#print('No user %s found, another try...' % email)
lastchar
=
username
[
-
1
]
username
=
username
.
rstrip
(
lastchar
)
continue
#print(json.dumps(user_requested, indent=4))
user_ids
.
append
(
user_requested
[
0
][
'
id
'
])
return
user_ids
print
(
'
User %s not found, aborting.
'
%
email
)
exit
(
1
)
def
create_repository
(
token
:
str
,
group_id
:
str
,
emails
:
List
[
str
],
name
:
str
,
import_url
:
Optional
[
str
],
expires_at
:
Optional
[
str
]):
"""
...
...
@@ -229,11 +244,35 @@ def clone_all(token: str, id: str, directory: str, until_date: Optional[str], so
print
()
def
validate_yaml
(
args
):
"""
Verify that the yaml file is valid by checking:
- the yaml syntax
- that a user id can be retrieved from the email (through some hackish inference)
This function stops the program if a check fails.
"""
with
open
(
args
.
repos_file
)
as
f
:
repos
=
yaml
.
full_load
(
f
)
for
repo
in
repos
:
if
'
name
'
in
repo
:
name
=
repo
[
'
name
'
]
elif
'
emails
'
in
repo
:
name
=
repo
[
'
emails
'
][
0
].
split
(
'
@
'
)[
0
]
else
:
print
(
'
Syntax error in YAML file.
\n
Aborted.
'
)
exit
(
1
)
headers
=
{
'
PRIVATE-TOKEN
'
:
args
.
token
}
# check the user id can sucessfully be retrieved from the email address
emails_to_ids
(
repo
[
'
emails
'
],
headers
)
def
command_create_group_repos
(
args
):
"""
Combine create_group and create_repository. For each repository listed in
given file, create a repo in group.
"""
validate_yaml
(
args
)
if
args
.
visibility
:
group_id
=
create_group
(
args
.
token
,
args
.
group_name
,
args
.
visibility
)
else
:
...
...
@@ -248,11 +287,11 @@ def command_create_group_repos(args):
elif
'
emails
'
in
repo
:
name
=
repo
[
'
emails
'
][
0
].
split
(
'
@
'
)[
0
]
else
:
print
(
'
YAML file not correct, exit and delete group
'
)
print
(
'
Syntax error in YAML file.
'
)
delete_group
(
args
.
token
,
group_id
)
print
(
'
Deleted group.
\n
Aborted.
'
)
exit
(
1
)
create_repository
(
args
.
token
,
group_id
,
repo
[
'
emails
'
],
name
,
args
.
import_url
,
args
.
expires_at
)
create_repository
(
args
.
token
,
group_id
,
repo
[
'
emails
'
],
name
,
args
.
import_url
,
args
.
expires_at
)
print
()
...
...
@@ -261,6 +300,8 @@ def command_create_repos(args):
Create a set of repositories inside the specified (existing) group. For each repository listed
in the given file, create a repository.
"""
validate_yaml
(
args
)
group_id
=
args
.
group_id
with
open
(
args
.
repos_file
)
as
f
:
...
...
@@ -274,8 +315,8 @@ def command_create_repos(args):
print
(
'
YAML file not correct, exit and delete group
'
)
delete_group
(
args
.
token
,
group_id
)
exit
(
1
)
create_repository
(
args
.
token
,
group_id
,
repo
[
'
emails
'
],
name
,
args
.
import_url
,
args
.
expires_at
)
create_repository
(
args
.
token
,
group_id
,
repo
[
'
emails
'
],
name
,
args
.
import_url
,
args
.
expires_at
)
print
(
'
created repo:
'
,
repo
[
'
emails
'
],
name
)
print
()
...
...
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