Skip to content

Instantly share code, notes, and snippets.

@olaurendeau
Last active May 7, 2025 08:50
Show Gist options
  • Save olaurendeau/bdcf09730586e98465fe81b7b8e52ad0 to your computer and use it in GitHub Desktop.
Save olaurendeau/bdcf09730586e98465fe81b7b8e52ad0 to your computer and use it in GitHub Desktop.

Mattermost Import Username Updater

This script updates usernames in a Mattermost JSONL import file to match existing users in a Mattermost instance. It's particularly useful when migrating from Slack to Mattermost and you need to ensure usernames in the import file match those of existing Mattermost users.

Requirements

  • Bash environment (macOS or Linux)
  • mmctl installed and configured with access to your Mattermost instance
  • The JSONL import file from Slack export

Features

  • Updates usernames in user records based on email matching
  • Updates usernames in post records
  • Updates @mentions in message content
  • Preserves the original file and creates a new one with updates
  • Provides detailed status for each processed user
  • Shows a summary of all modifications

Usage

./update_users.sh <input_file> <output_file>

Example:

./update_users.sh mattermost_import.jsonl updated_import.jsonl

Output

The script provides real-time feedback with color-coded status:

  • 🟡 [MODIFIED]: Username was updated
  • 🟢 [UNCHANGED]: Username already matches
  • 🔵 [NOT FOUND]: User doesn't exist in Mattermost

At the end, it displays a summary with:

  • Total number of user records processed
  • Number of modified records
  • Number of unchanged records
  • Output file location

Note for macOS users

The script uses the macOS version of sed (sed -i ''). For Linux systems, you'll need to modify the sed commands to remove the empty quotes.

Example output

Processing user records...
[MODIFIED] Email: user@example.com, Username: oldname -> newname
[UNCHANGED] Email: user2@example.com, Username: user2
[NOT FOUND] Email: unknown@example.com, Username: unknown
Summary:
Total user records processed: 3
Modified records: 1
Unchanged records: 2
Output written to: updated_import.jsonl
#!/bin/bash
# Check if arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <input_file> <output_file>"
echo "Example: $0 mattermost_import.jsonl updated_import.jsonl"
exit 1
fi
input_file="$1"
output_file="$2"
# Check if input file exists
if [ ! -f "$input_file" ]; then
echo "Error: Input file '$input_file' not found"
exit 1
fi
# 1. Copy original file
echo "Creating copy of original file..."
cp "$input_file" "$output_file"
# Counters for final report
total_users=0
modified_users=0
echo "Processing user records..."
# 2. Process user lines
while IFS= read -r line; do
((total_users++))
# Extract email and current username
email=$(echo "$line" | grep -o '"email":"[^"]*"' | cut -d'"' -f4)
current_username=$(echo "$line" | grep -o '"username":"[^"]*"' | cut -d'"' -f4)
# Search user in mattermost
mmctl_output=$(mmctl user search "$email" 2>&1)
# Check if user exists
if ! echo "$mmctl_output" | grep -q "error occurred"; then
# Extract new username from mmctl output
new_username=$(echo "$mmctl_output" | grep "username:" | cut -d' ' -f2)
if [ "$current_username" != "$new_username" ]; then
((modified_users++))
echo -e "\033[33m[MODIFIED]\033[0m Email: $email, Username: $current_username -> $new_username"
# Remove user line as they already exist in Mattermost
sed -i '' "/\"email\":\"$email\"/d" "$output_file"
# Update username references in posts
sed -i '' "s/\"user\":\"$current_username\"/\"user\":\"$new_username\"/g" "$output_file"
# Update @mentions in messages
sed -i '' "s/@$current_username/@$new_username/g" "$output_file"
else
echo -e "\033[32m[UNCHANGED]\033[0m Email: $email, Username: $current_username"
fi
else
echo -e "\033[34m[NOT FOUND]\033[0m Email: $email, Username: $current_username"
fi
done < <(grep '"type":"user"' "$input_file")
# Display final report
echo -e "\n\033[1mSummary:\033[0m"
echo "Total user records processed: $total_users"
echo "Modified records: $modified_users"
echo "Unchanged records: $((total_users - modified_users))"
echo "Output written to: $output_file"
@jonathan-reisdorf
Copy link

Awesome, thank you! 🤩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment