How to bulk create Users in Jira

This article discusses how you can leverage Create user (/rest/userRest API endpoint to create bulk users in Jira. This approach is targeted for all the Jira versions, where the /rest/api/2/user endpoint is supported.

Prior to the script execution, the CSV file with the user information or supported payload(emailAddress, displayName, name, password) is to be prepared.

Option 1: Using Bash command line

Prepare and create the script as below(by replacing Jira base URL, API username, password and path to CSV file):

# cat bulkuser.sh

#!/bin/bash
while read i ;
do emailAddress=$(echo $i | cut -d "," -f3);
name=$(echo $i | cut -d "," -f1);
displayName=$(echo $i | cut -d "," -f2);
password=$(echo $i | cut -d "," -f4);
data='{"emailAddress": "'${emailAddress}'","displayName": "'${displayName}'","name": "'${name}'","password": "'${password}'"}';
echo $data;
curl -X POST --url '<JIRA_BASE_URL>/rest/api/2/user' --user '<username:password>' --header 'Accept: application/json' --header 'Content-Type: application/json' --data "${data}" ;
echo $command;
done < <PATH_TO_CSV_FILE>/test.csv

Now, create the CSV file with user information:

# cat test.csv
test1,Test User 1,test1@sample.com,Passw0rd1
test2,Test User 2,test2@sample.com,Passw0rd2
test3,Test User 3,test3@sample.com,Passw0rd3
test4,Test User 4,test4@sample.com,Passw0rd4
test5,Test User 5,test5@sample.com,Passw0rd5
Then, execute the bash script (sh bulkuser.sh) to create users in bulk.
Option 2 Using Python script
Prepare the script as in the following example:
import sys
import csv
import os
from optparse import OptionParser
import requests
from requests.auth import HTTPBasicAuth
import json
parser = OptionParser()
parser.add_option("--username", dest="username", help="API username")
parser.add_option("--password", dest="password", help="API password")
parser.add_option("--url", dest="weburl", help="Endpoint hostname")
parser.add_option("--input-file", dest="INPUT_FILE", help="CSV values of emailAddress name displayName")
parser.add_option("--csv-delimiter", dest="dlimit", help="CSV seperator used")
(options, args) = parser.parse_args()

if not options.INPUT_FILE:
        parser.error("INPUT_FILE must be specified")
if not options.username or not options.weburl or not options.password or not options.dlimit :
        parser.error("--username <username> --password <password> --url <endpoint uri> --input-file <input csv file with header emailAddress name displayName> --csv-delimiter <; or ,>")

url = "https://"+options.weburl+"/rest/api/2/user"
auth = HTTPBasicAuth(options.username, options.password)
headers = {"Accept": "application/json", "Content-Type": "application/json"}
with open(options.INPUT_FILE) as csvfile:
    reader = csv.DictReader(csvfile, delimiter=options.dlimit)
    for row in reader:
        emailAddress = row['emailAddress']
        displayName = row['displayName']
        name = row['name']
        password = row['password']
        payload = json.dumps( {"emailAddress": emailAddress, "displayName": displayName, "name": name, "password": password } )
        response = requests.request("POST", url, data=payload, headers=headers, auth=auth)
        print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
Now,Create a CSV file as in the following example, separated with ",":
# cat bulkuser.py
name,displayName,emailAddress,password
test31,Test User 31,123@sample.com,Passw0rd1
test32,Test User 32,123@sample.com,Passw0rd2
test33,Test User 33,123@sample.com,Passw0rd3
test34,Test User 34,123@sample.com,Passw0rd4
test35,Test User 35,123@sample.com,Passw0rd5
Then, execute the script as below to create users in bulk(--url should be just hostname and without https):
python bulkuser.py --username admin --password admin --url jira.atlassian.com --input-file ./test.csv --csv-delimiter ,
scriptusage.png

1 comment

Shilpa Sanil October 31, 2022

While using bash script am getting the error:

 


<div class="aui-message aui-message-warning warning">
<p>Encountered a <code>&quot;401 - Unauthorized&quot;</code> error while loading this page.</p>
<p>Basic Authentication Failure - Reason : AUTHENTICATED_FAILED</p>
<p><a href="/secure/MyJiraHome.jspa">Go to Jira home</a></p>
</div>
</section><!-- .aui-page-panel-content -->
</div><!-- .aui-page-panel-inner --></div><!-- .aui-page-panel -->
</body>

 

Can anybody please help me with this?

 

Thanks in advance

Shilpa

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events