Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gitlab-assignments
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Sneh Koul
gitlab-assignments
Commits
16268205
Commit
16268205
authored
8 years ago
by
Nick Lee
Browse files
Options
Downloads
Patches
Plain Diff
Use student's preferred UW email when inviting and auto-retry failed HTTP requests
parent
0c0194e0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
create-repos.py
+5
-3
5 additions, 3 deletions
create-repos.py
gitlab.py
+34
-30
34 additions, 30 deletions
gitlab.py
ldap.py
+16
-0
16 additions, 0 deletions
ldap.py
with
55 additions
and
33 deletions
create-repos.py
+
5
−
3
View file @
16268205
...
...
@@ -5,6 +5,7 @@ import argparse,getpass,re
import
sys
,
subprocess
,
os
import
json
,
urllib
.
request
import
gitlab
import
ldap
# Parse command-line arguments.
parser
=
argparse
.
ArgumentParser
(
description
=
"
This script is used to create student repositories.
"
)
...
...
@@ -84,11 +85,11 @@ for student in students:
master_branch_exists
=
True
if
not
master_branch_exists
:
print
(
"
> master branch doesn
'
t exist for %s. Creating it.
"
%
student
)
time
.
sleep
(
3
)
for
assn
in
[
'
A0
'
,
'
A1
'
,
'
A2
'
,
'
A3
'
,
'
A4
'
]:
print
(
"
> Doing work for assignment %s
"
%
assn
)
gitlab
.
request
(
'
projects/%d/repository/files
'
%
project_ids
[
student
],
post_hash
=
{
'
file_path
'
:(
"
%s/.gitignore
"
%
assn
),
'
branch_name
'
:
"
master
"
,
'
content
'
:
"
*.class
\n
"
,
'
commit_message
'
:(
"
Creating %s folder
"
%
assn
)})
time
.
sleep
(
5
)
# Wait for master branch to become protected. Gitlab seems to have a delay on protecting the
# master branch when it's created.
...
...
@@ -129,7 +130,8 @@ for student in students:
# Step 2: Make the post request to invite by email
if
authenticity_token
:
student_email
=
"
%s@uwaterloo.ca
"
%
student
#student_email = "%s@uwaterloo.ca" % student
student_email
=
ldap
.
get_student_email
(
student
)
print
(
"
> Got authenticity token.
"
)
print
(
"
> Sending invitation email to %s
"
%
student_email
)
post_data
=
urllib
.
parse
.
urlencode
({
'
authenticity_token
'
:
authenticity_token
,
'
user_ids
'
:
student_email
,
'
access_level
'
:
30
}).
encode
(
'
ascii
'
)
...
...
@@ -141,4 +143,4 @@ for student in students:
print
(
"
> Could not add student %s to repo!
"
%
student
)
print
(
"
> Done processing %s.
"
%
student
)
time
.
sleep
(
10
)
# Put in a bit of a delay so that git.uwaterloo.ca isn't hammered
time
.
sleep
(
5
)
# Put in a bit of a delay so that git.uwaterloo.ca isn't hammered
This diff is collapsed.
Click to expand it.
gitlab.py
+
34
−
30
View file @
16268205
...
...
@@ -12,37 +12,41 @@ private_token = ''
# post_hash: A dictionary of data to send in a POST request
# query_headers: Any headers you want to send as part of the request
# quit_on_error: If True, will quit program on error. If False, will
#
return False on error instead
#
try 2 more times, and finially return false
# Returns: A python object
def
request
(
query
,
post_hash
=
{},
query_headers
=
{},
http_method
=
None
,
quit_on_error
=
True
):
try
:
if
'
PRIVATE-TOKEN
'
not
in
query_headers
:
query_headers
[
'
PRIVATE-TOKEN
'
]
=
private_token
post_data
=
urllib
.
parse
.
urlencode
(
post_hash
).
encode
(
'
ascii
'
)
if
post_hash
else
None
req
=
urllib
.
request
.
Request
(
url
=
"
https://git.uwaterloo.ca/api/v3/
"
+
query
,
data
=
post_data
,
headers
=
query_headers
,
method
=
http_method
)
with
urllib
.
request
.
urlopen
(
req
)
as
f
:
json_string
=
f
.
read
().
decode
(
'
utf-8
'
)
try
:
python_object
=
json
.
loads
(
json_string
)
except
Exception
as
e
:
print
(
json_string
)
print
(
"
Error occurred trying to interpret above data as JSON.
"
)
print
(
"
Error message: %s
"
%
str
(
e
))
if
quit_on_error
:
sys
.
exit
(
1
)
else
:
return
False
return
python_object
except
Exception
as
e
:
print
(
"
Error occurred trying to access https://git.uwaterloo.ca/api/v3/
"
+
query
)
print
(
"
Error %s message: %s
"
%
(
type
(
e
).
__name__
,
str
(
e
)))
if
quit_on_error
:
sys
.
exit
(
1
)
else
:
return
False
def
request
(
query
,
post_hash
=
{},
query_headers
=
{},
http_method
=
None
,
quit_on_error
=
False
,
max_attempts
=
3
):
max_tries
=
3
for
request_attempt
in
list
(
range
(
1
,
max_tries
+
1
)):
try
:
if
'
PRIVATE-TOKEN
'
not
in
query_headers
:
query_headers
[
'
PRIVATE-TOKEN
'
]
=
private_token
post_data
=
urllib
.
parse
.
urlencode
(
post_hash
).
encode
(
'
ascii
'
)
if
post_hash
else
None
req
=
urllib
.
request
.
Request
(
url
=
"
https://git.uwaterloo.ca/api/v3/
"
+
query
,
data
=
post_data
,
headers
=
query_headers
,
method
=
http_method
)
with
urllib
.
request
.
urlopen
(
req
)
as
f
:
json_string
=
f
.
read
().
decode
(
'
utf-8
'
)
try
:
python_object
=
json
.
loads
(
json_string
)
except
Exception
as
e
:
print
(
json_string
)
print
(
"
Error occurred trying to interpret above data as JSON.
"
)
print
(
"
Error message: %s
"
%
str
(
e
))
if
quit_on_error
:
sys
.
exit
(
1
)
else
:
return
False
return
python_object
except
Exception
as
e
:
print
(
"
Error occurred trying to access https://git.uwaterloo.ca/api/v3/
"
+
query
)
print
(
"
Error %s message: %s
"
%
(
type
(
e
).
__name__
,
str
(
e
)))
if
quit_on_error
:
sys
.
exit
(
1
)
elif
request_attempt
<
max_tries
:
print
(
"
Retrying... (re-try number %d)
"
%
request_attempt
)
print
(
"
Request failed after %d attempts
"
%
max_tries
)
return
False
# Read private token from token_file. Mutates the global private_token
# above and returns it too.
...
...
This diff is collapsed.
Click to expand it.
ldap.py
0 → 100644
+
16
−
0
View file @
16268205
#!/usr/bin/python3
from
ldap3
import
Server
,
Connection
,
ALL
# Returns the preferred email of the given student
def
get_student_email
(
userid
):
userid
=
userid
[
0
:
8
]
conn
=
Connection
(
'
uwldap.uwaterloo.ca
'
,
auto_bind
=
True
)
conn
.
search
(
'
dc=uwaterloo,dc=ca
'
,
'
(uid=%s)
'
%
userid
,
attributes
=
[
'
mail
'
,
'
mailLocalAddress
'
,
'
cn
'
])
if
conn
.
entries
:
return
conn
.
entries
[
0
].
mail
else
:
return
"
%s@uwaterloo.ca
"
%
userid
# For testing
#print(get_student_email('jsmio'))
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