How-to create multiple users in Jira with one batch run

Requirement

You have a lot of users in a CSV file and you don't want to add them manually to Jira.

Your skills

  • you know how to create and edit CSV files
  • you can edit configuration files
  • you have create user rights on Jira (Administrator)
  • you have PHP installed
    • for Linux: there a packages for most distributions available (Ubuntu: apt-get install php php-curl php-xml)
    • for Win: get a package here https://windows.php.net/download
      • enable curl in php.ini: extension=C:\<path to>\php\ext\php_curl.dll
      • enable ssl in php.ini, if your site is HTTPS: extension=C:\<path to>\php\ext\php_openssl.dll
  • (optional) PHP coding xp if you would like to extend the script.

Before you run the script

  • Atlassians REST API function to create users is experimental.
  • This function doesn't allow to select a specific user directory. Make sure the one where you would like to create users is the primary (in the UI).
  • The created users will grant access to Jira-Software.
  • The script has just the minimum required lines of code - you can spend a couple of hours to make it more comfortable / dynamic.
  • The script supports sites with self-signed SSL certs.

Prepare the CSV file

  • one line per new user
  • delimiter is ";"
  • column 1 = username
  • column 2 = password
  • column 3 = email
  • column 4 = displayed user name
  • save the file to users.csv

Adapt the script to your needs

Copy & paste the script to a file called createusers.php and modify the 5 "define" values to your needs. Note that adding a password with special characters (like '$') will not work without escaping.

<?php
define('JIRA_URL', 'https://mydomain.com');
define('USER', 'user');
define('PASS', 'pass');
define('CSVFILE', 'users.csv');
define('FILELENGTH', 2048);


function create_user($json) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => JIRA_URL . '/rest/api/latest/user',
CURLOPT_USERPWD => USER . ':' . PASS,
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array('Content-type: application/json','User-Agent: x'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => 0
));
$result = curl_exec($ch);

if(!$result) {
trigger_error(curl_error($ch));
exit(1);
}

if (!curl_errno($ch)) {
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 201:
break;
default:
echo "Request failed with HTTP-Code: ", $http_code, "\n";
var_dump($result);
exit(1);
}
}

curl_close($ch);
}

$handle = fopen(CSVFILE, "r");
if ($handle) {
while (($data = fgetcsv($handle, FILELENGTH, ";")) !== FALSE) {

$uname = utf8_decode($data[0]);
$upass = utf8_decode($data[1]);
$uemail = utf8_decode($data[2]);
$udname = utf8_decode($data[3]);

$request = '{"name":"'.$uname.'","password":"'.$upass.'","emailAddress":"'.$uemail.'","displayName":"'.$udname.'","applicationKeys":["jira-software"]}';

echo "New user " . $uname ;
create_user($request);
echo " created.\n";
}
fclose($handle);
} else
echo "The file " . CSVFILE . " could not be opened!\n";

?>

Run the script in Linux

php -f /path/to/createusers.php

Run the script on Win PowerShell

.\php.exe -f \path\to\createusers.php

 (developed and tested with PHP 7.0.33-0ubuntu0.16.04.1 an Jira Server 7.12.3)

5 comments

M Amine
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 26, 2020

a suggestion : if you can adapt the script for shell. Thank you for this article.

Benjamin Yu March 26, 2021

This code works in jira 8.13 with php 7.2. But it is not working in jira 7.13 with php 7.0. Why? The error is New user ?username PHP Notice: in /cmtools/atlassian/createusers.php on line 24

Please help!

Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 10, 2021

Dear @Benjamin Yu ,

can you please post what's in line 24? Can you get a more specific error message? (more verbose?).

Most probably the rest api call differs from 8.13 to 7.13. 

PHP Version should not be a problem.

So long

Thomas

Benjamin Yu April 12, 2021

I can't run it again since it is in production. What should be the syntax for REST API 7.13?

Thomas Deiler
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 13, 2021

Dear @Benjamin Yu ,

by comparing both API documentations, I cannot see any difference. Further investiation is required, but I have no 7.13 anymore at hand. I also recommend to upgrade - 7.13.x has reached EOL.

So long

Thomas

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events