Skip to content
Snippets Groups Projects
Commit 69e3f78f authored by Nick Lee's avatar Nick Lee
Browse files

Fix userid lookup and allow _gitlab_session to be specified from a file

parent 3e5af62a
No related branches found
No related tags found
No related merge requests found
......@@ -239,12 +239,13 @@ creates projects for each student group according to an input CSV file, and adds
This script must be run on the UW campus network. If you're off-campus, you can use the
[VPN client](https://uwaterloo.ca/information-systems-technology/services/virtual-private-network-vpn) or ssh into a UW server.
When running the script, you will be prompted for your `_gitlab_session` cookie. The script uses the cookie to interface with the Gitlab
web page directly when there's no appropriate API calls available. Most browsers can show you the cookie value in the privacy settings.
The script will not print what you type for security.
The script requires you to enter your `_gitlab_session` cookie value, either through the keyboard (stdin) or from a file.
The script uses the cookie to interface with the Gitlab web page directly when there's no appropriate API calls available.
Most browsers can show you the cookie value in the privacy settings. If you type in the `_gitlab_session` cookie value,
the script will not print what you type for security.
$ ./stqam-create-repos.py --help
usage: stqam-create-repos.py [-h] [--token-file TOKEN_FILE]
[--gitlab-session-file GITLAB_SESSION_FILE]
[--current-membership] [--check-membership]
group_name membership_file
......@@ -262,6 +263,9 @@ The script will not print what you type for security.
--token-file TOKEN_FILE
Path to file containing your Gitlab private token.
Default is to read from standard input.
--gitlab-session-file GITLAB_SESSION_FILE
Path to file containing your _gitlab_session cookie
value. Default is to read from standard input.
--current-membership Prints the current group memberships according to
git.uwaterloo.ca and quit.
--check-membership Checks the membership_file against the current group
......@@ -280,6 +284,8 @@ The script will not print what you type for security.
* The last field is a number or blank. If there's a number, the script will not create the project. If it's blank, the project will be created.
The number can be arbitrary.
* `--token-file TOKEN_FILE`: Same usage as in `clone.py`.
* `--gitlab-session-file GITLAB_SESSION_FILE`: Similar to `--token-file`, put your `_gitlab_session` cookie value in the first line of
`GITLAB_SESSION_FILE`. Note that your `_gitlab_session` cookie value changes when you logout and log back in.
* `--current-membership`: Prints the current group memberships according to git.uwaterloo.ca and quit. The memberships are printed by project
and by student ID. Also prints the groups from the CSV file where at least one student doesn't have a group on git.uwaterloo.ca.
* `--check-membership`: Checks the CSV file and the groups that are already on git.uwaterloo.ca. Checks that:
......
......@@ -12,5 +12,17 @@ def get_student_email(userid):
else:
return "%s@uwaterloo.ca" % userid
# Returns the user id given an @uwaterloo.ca email
def get_userid(email):
conn = Connection('uwldap.uwaterloo.ca', auto_bind=True)
conn.search('dc=uwaterloo,dc=ca', '(mailLocalAddress=%s)' % email, attributes=['mail', 'mailLocalAddress', 'cn', 'uid'])
if conn.entries and conn.entries[0].uid[0]:
return conn.entries[0].uid[0][0:8]
else:
return email[:-len("@uwaterloo.ca")]
# For testing
#print(get_student_email('yc2lee'))
#print(get_userid('yc2lee@uwaterloo.ca'))
......@@ -62,6 +62,8 @@ parser.add_argument('group_name', help="The name of the Gitlab group to create p
parser.add_argument('membership_file', help="Path to a CSV containing group memberships, with fields: Timestamp, WatIAM user id for member 1, WatIAM user id for member 2 (optional), WatIAM user id for member 3 (optional), Group number (optional)")
parser.add_argument('--token-file', default="/dev/stdin",
help="Path to file containing your Gitlab private token. Default is to read from standard input.")
parser.add_argument('--gitlab-session-file', default="/dev/stdin",
help="Path to file containing your _gitlab_session cookie value. Default is to read from standard input.")
parser.add_argument('--current-membership', action='store_true',
help="Prints the current group memberships according to git.uwaterloo.ca and quit.")
parser.add_argument('--check-membership', action='store_true',
......@@ -75,6 +77,7 @@ args = parser.parse_args()
group_name = args.group_name
membership_file = args.membership_file
token_file = args.token_file
gitlab_session_file = args.gitlab_session_file
print_current_membership = args.current_membership
check_membership = args.check_membership
......@@ -140,11 +143,25 @@ max_project_name = max(list(map(int, project_names_numbers))) if project_names_n
#
# Read gitlab session cookie
#
print()
print("This script adds projects and get projects' info by interfacing with the git.uwaterloo.ca website directly.")
print("Please login to https://git.uwaterloo.ca and enter your _gitlab_session cookie from git.uwaterloo.ca below.")
gitlab_session_cookie = getpass.getpass("git.uwaterloo.ca _gitlab_session cookie value:")
print()
gitlab_session_cookie = None
if gitlab_session_file == "/dev/stdin":
# Read the _gitlab_session cookie from standard input (keyboard)
print()
print("This script adds projects and get projects' info by interfacing with the git.uwaterloo.ca website directly.")
print("Please login to https://git.uwaterloo.ca and enter your _gitlab_session cookie from git.uwaterloo.ca below.")
gitlab_session_cookie = getpass.getpass("git.uwaterloo.ca _gitlab_session cookie value:")
print()
else:
# Read the _gitlab_session cookie from file
print("Reading _gitlab_session cookie value from file %s" % gitlab_session_file)
try:
gitlab_session_handle = open(gitlab_session_file, 'r')
gitlab_session_cookie = gitlab_session_handle.readline().strip()
gitlab_session_handle.close()
except Exception as e:
print("Error occurred trying to read _gitlab_session value from file %s" % gitlab_session_file)
print("Error message: %s" % str(e))
sys.exit(1)
#
......@@ -162,13 +179,14 @@ if print_current_membership or check_membership:
# Get members from API call. This doesn't return students who have been invited, but
# haven't accepted their invitation yet.
members_raw_data = gitlab.request("/projects/%d/members" % project['id'])
members_list = list(map(lambda x : x['username'], members_raw_data))
members_list = list(map(lambda x : ldap.get_userid(x['username'] + "@uwaterloo.ca"), members_raw_data))
# Get members from the web page directly.
req = urllib.request.Request("https://git.uwaterloo.ca/%s/%s/project_members" % (group_name, project['name']),
headers={'Cookie': "_gitlab_session=%s"%gitlab_session_cookie})
with urllib.request.urlopen(req) as f:
project_members_html = f.read().decode('utf-8')
email_members = re.findall(r"([a-zA-Z0-9_.+-]+)@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+", project_members_html)
email_members = re.findall(r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", project_members_html)
email_members = list(map(lambda email: ldap.get_userid(email), email_members))
# Combine the members from API call and from web page into a frozenset and save it in hash
existing_memberships[project['name']] = frozenset(members_list + email_members)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment