Synchronize Users From Perforce

Applies to Collaborator 14.5, last modified on April 18, 2024

In this tutorial we will write a script that will create a corresponding Collaborator user for every Perforce user. The script can be run periodically to pick up any new Perforce users.

This script performs the same actions as the ccollab admin syncusers command.

Outline

The script runs the Perforce p4 users command, parses the output, and then calls the Collaborator Command-Line Client ccollab admin user create command for every user. If the user already exists then the ccollab admin user create command fails and the script goes on to the next user.

Prerequisites

The script in this example is written in Perl, so you must have the Perl runtime installed. The script invokes the Collaborator Command-Line Client, so that needs to be installed as well. Note you must be a Collaborator Administrator in order for this script to work.

Script Step 1: Get Users From Perforce

The script runs the p4 users command to get the a list of all user records from Perforce.

# Get users from Perforce

@p4Users = `p4 users`;

Script Step 2: Parse Fields From Perforce User Record

The script extracts the user name, email, and full name from the Perforce user record.

#parse fields from Perforce user record

$p4User =~ /(\S+)\s*<([^>]*)>\s*\((.*?)\)\s*accessed.*/;

 

$user = $1;

$email = $2;

$fullName = $3;

Script Step 3: Create User in Collaborator

The script runs the Command-Line Client ccollab admin user create command to create the user in Collaborator. If a user with that name already exists, the command fails and the script goes on to the next user.

#create user in Collaborator - does nothing if user already exists

system("ccollab admin user create \"$user\" --email \"$email\" --full-name \"$fullName\"");

Finished

That is it! Here is the script in finished form: syncusers-p4.pl (CTRL + click, or CMD + click to open in new window). You can run this script periodically (for example, with a cron job) and it will keep your Perforce users and Collaborator in sync.

See Also

Scripting

Highlight search results