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

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('', private_token=token)
# server = gitlab.Gitlab('', 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():
# Uncomment below and run to migrate usernames from a CSV formatted like:
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 in new_usernames:
new_username = new_usernames[]
print("mapping ",, " ", user.username, " ", new_username)
# user.username = new_username
# user.provider = "cas3"
# user.extern_uid = new_username
print("Did not find a mapping for ",
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