Commit 160ce5ef authored by Brennen Bearnes's avatar Brennen Bearnes
Browse files

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

We used this some time ago to rewrite usernames per https://phabricator.wikimedia.org/T288392

Self-merging as this is a handy thing to keep around, although the code requires tweaking for actual use.

Bug: T288392
parent f568f6af
#!/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()
Supports Markdown
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