This article discusses how you can leverage Create user (/rest/user) Rest 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):
#!/bin/bash<br>while read i ;<br>do emailAddress=$(echo $i | cut -d "," -f3);<br>name=$(echo $i | cut -d "," -f1);<br>displayName=$(echo $i | cut -d "," -f2);<br>password=$(echo $i | cut -d "," -f4);<br>data='{"emailAddress": "'${emailAddress}'","displayName": "'${displayName}'","name": "'${name}'","password": "'${password}'"}';<br>echo $data;<br>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}" ;<br>echo $command;<br>done < <PATH_TO_CSV_FILE>/test.csv# cat bulkuser.sh
|
Now, create the CSV file with user information:
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# cat test.csv
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:
sys
import csv
import os
from optparse<span> </span>import OptionParser
import requests
from requests.auth<span> </span>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 =<span> </span>"https://"+options.weburl+"/rest/api/2/user"
auth = HTTPBasicAuth(options.username, options.password)
headers = {"Accept":<span> </span>"application/json",<span> </span>"Content-Type":<span> </span>"application/json"}
with<span> </span>open(options.INPUT_FILE) as csvfile:
reader = csv.DictReader(csvfile, delimiter=options.dlimit)
for row<span> </span>in reader:
emailAddress = row['emailAddress']
displayName = row['displayName']
name = row['name']
password = row['password']
payload = json.dumps( {"emailAddress": emailAddress,<span> </span>"displayName": displayName,<span> </span>"name": name,<span> </span>"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=(",",<span> </span>": ")))import
Now,Create a CSV file as in the following example, separated with ",":
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# cat bulkuser.py
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 ,
<span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="scriptusage.png" style="width: 361px;"><img src="https://us.v-cdn.net/6038660/uploads/images/200328i6AB0476D01A7090F/scriptusage.png" width="361" height="139" role="button" title="scriptusage.png" alt="scriptusage.png"></span>