You have a lot of customers in a CSV file and you don't want to add them manually to Jira Service Management as customers.
Copy & paste the script to a file called create_customers.php and modify the "define" values to your needs. You will require an API-Token.
<?php
define('JIRA_URL', 'https://mydomain.atlassian.net');
define('EMAIL', 'admin@mydomain.com');
define('APITOKEN', 'rahHWQ61rElyFHmsz9H6ABv');
define('CSVFILE', 'customers.csv');
define('FILELENGTH', 2048);
define('SENDER', 'no-reply@mydomain.com');
define('PORTAL', '/servicedesk/customer/portal/1');
define('SUBJECT', 'New account for Jira Service Management Portal');
define('BODY', 'we have just registered you at our JSM portal. Please follow the link below, login with your email and press the link "Forgot your password?" to get a reset email mail sent from the cloud.' . PHP_EOL . PHP_EOL);
function create_customer($json) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => JIRA_URL . '/rest/servicedeskapi/customer',
CURLOPT_USERPWD => EMAIL . ':' . APITOKEN,
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);
}
$header = 'From: '. SENDER . "\r\n" . 'X-Mailer: PHP';
$handle = fopen(CSVFILE, "r");
if ($handle) {
while (($data = fgetcsv($handle, FILELENGTH, ";")) !== FALSE) {
$uemail = utf8_decode($data[0]);
$udname = utf8_decode($data[1]);
$request = '{"displayName": "' . $udname . '", "email": "'. $uemail . '"}';
echo "New customer " . $uemail ;
create_customer($request);
echo " created.";
if (strlen(SENDER) > 1) {
mail($uemail, SUBJECT, 'Dear ' . $udname . ',' . PHP_EOL . PHP_EOL . BODY . JIRA_URL . PORTAL, $header);
echo " info-mail sent.";
}
echo PHP_EOL;
}
fclose($handle);
} else
echo "The file " . CSVFILE . " could not be opened!\n";
?>
php -f /path/to/create_customers.php
.\php.exe -f \path\to\create_customers.php
(tested with PHP 8.0.5 on OpenSUSE Tumbleweed)
Follow this link for How-to create multiple users in Jira with one batch run (Server).
Thomas Deiler
Senior Agile Coach
none
none
228 accepted answers
1 comment