🚧 This instance is under construction; expect occasional downtime. Runners available in /repos. Questions? Ask in #wikimedia-gitlab on libera.chat, or under GitLab on Phabricator.

Commit 6a5d542f authored by Brennen Bearnes's avatar Brennen Bearnes
Browse files

set-usernames - a quick and dirty utility for changing usernames in bulk

Takes a CSV specifying user account by e-mail and the new target username.
parent ee2c9d1b
#!/usr/bin/env python3
"""
set-usernames - set username and extern_uid for a list of users
"""
import os
import sys
import yaml
import argparse
import gitlab
import time
import csv
from gitlab_settings.util import get_token
def get_gitlab():
"""Get a GitLab instance."""
token = get_token()
server = gitlab.Gitlab('https://gitlab-test.wmcloud.org/', private_token=token)
# server = gitlab.Gitlab('https://gitlab.wikimedia.org/', private_token=token)
return server
def get_users():
"""Get all users on the server as a generator."""
server = get_gitlab()
return server.users.list(all=True, as_list=False)
# Use this to grab all mails:
def list_users():
for user in get_users():
print(user.email)
# Uncomment below and run to migrate usernames from a CSV formatted like:
# foo@example.com,foo
def rename_users():
new_usernames = {}
with open("user-mapping.csv") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
new_usernames[row[0]] = row[1]
for user in get_users():
if user.email in new_usernames:
new_username = new_usernames[user.email]
print("mapping ", user.email, " ", user.username, " ", new_username)
# user.username = new_username
# user.provider = "cas3"
# user.extern_uid = new_username
# user.save()
else:
print("Did not find a mapping for ", user.email)
time.sleep(.5)
rename_users()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment