Import users to Bitbucket from an external user directory
Platform Notice: Data Center - This article applies to Atlassian products on the Data Center platform.
Note that this knowledge base article was created for the Data Center version of the product. Data Center knowledge base articles for non-Data Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Purpose
Outages sometimes make it necessary to move users from an external user directory like JIRA or LDAP into Stash to prevent interruption.
Solutions
If you only have a few users that you need to import you can do so manually from the UI or via REST API.
Importing with the UI
To make these changes manually, please use the following steps:
- Identify the users that you need to copy and note their attributes: user name and full name.
- Disable the external user directory in Administration > User Directories.
- Go to Administration > Users and Add user at the top right.
- Manually add all the noted users in this way.
- Enable the external directory again.When you reenable the external directory Bitbucket Server should now label these users in the user list as part of the "Bitbucket Server Internal" directory.
Importing with REST API
If you have more than a few users to add, the quickest way to add them may be to write a script utilizing the REST API. The creation of such scripts can by accomplished in many ways and you can find an example of this in the our article How to add users to Bitbucket Server using Rest API.
Script and Tabulated Text File
You can create a tabulated text file from querying the Bitbucket Server Database with something similar to the below.
SELECT user_name, user_name, display_name, email_address
FROM BSERV.cwd_user
Where directory_id like '<directory_id>'
This will generate the following text file:
user_name user_name display_name email_address
bstuart bstuart bstuart bstuart@noemail.com
rgoody rgoody "ryan goody" bee_phats@yahoo.com
Pirates Pirates "Fletcher Christian" fchristi@royalnavy.mod.uk
Here is an example script to import your users and assign their user name as the password by default.
#!/bin/bash
ABORT="N"
case "$1" in
"") echo "Usage: AddUsers.sh Admin_UserName Admin_Password"
ABORT="Y";;
* ) USERNAME=$1;;
esac
if [ "$ABORT" == 'N' ]; then
case "$2" in
"") echo "Usage: AddUsers.sh Admin_UserName Admin_Password"
ABORT="Y";;
* ) PASSWORD=$2;;
esac
fi
if [ "$ABORT" == 'N' ]; then
clear
while read -r a b c d; do
echo "User: $a"
curl -u $USERNAME:$PASSWORD -X POST -H "Content-Type: application/json" "http://localhost:7990/bitbucket/rest/api/1.0/admin/users?name=$a&password=$b&displayName=$c&emailAddress=$d&addToDefualtGroup=true¬ify=false"
done < users.txt
fi
This is only meant as an example and is not supported. There is no error checking or handling if users already exist in Bitbucket Server.
There are ways of editing the database directly in order to add users as well, but this is riskier and is not recommended.