Syncing users from Perforce

Top  Previous  Next

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

Outline

The script runs the Perforce p4 users command, parses the output, and then calls the Code 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 Code Collaborator Command Line Client, so that needs to be installed as well.  Note you must be a Code 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 Code Collaborator

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

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

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

 

Finished

That's it!  Here's the script in finished form: syncusers-p4.pl.  You can run this script periodically (e.g. with a cron job) and it will keep your Perforce users and Code Collaborator in sync.