How to add users to Bitbucket Server using Rest API

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

Because every environment is different there will be a few assumptions made in this example. If the assumptions are not true in the environment that the API will be run then you may need to make modifications or you will get errors. The script example is provided as is and is not guaranteed meet all possible situations. The script provided is meant only as an example and is not formally supported.

目的

Adding many users to the Internal Bitbucket Server Directory is a task that is easier to accomplish with the REST API rather than trying to manually add each user one at a time via the UI

The purpose of this KB is to provide a working example of how to import a large number of users. 

仮定

  1. Bitbucket Server is running on localhost
  2. Bitbucket Server is running on HTTP using the default port
  3. Bitbucket Server is running with a context path called Bitbucket
  4. The Sys Admin user is defined as Admin
  5. The Sys Admin user password is defined as Admin

This would then be the URL for accessing Bitbucket Server: http://localhost:7990/bitbucket

Add A User

  • curl to add a user to Bitbucket Server
    curl -u Admin:Admin -X POST -H "Content-Type: application/json" "http://localhost:7990/bitbucket/rest/api/1.0/admin/users?name=<USERNAME_TO_ADD>&password=<PASSWORD_FOR_USER>&displayName=<DISPLAY_NAME_FOR_USER>&emailAddress=<EMAIL_ADDRESS_FOR_USER>&addToDefualtGroup=true&notify=false"

    <USERNAME_TO_ADD> - is replaced with the user name that you want to add
    <PASSWORD_FOR_USER> - is replaced with the password the new user will use. The user can edit this password when they login but they will not be forced to change the password
    <DISPLAY_NAME_FOR_USER> - is replaced with the full name of the user that you are adding
    <EMAIL_ADDRESS_FOR_USER> - is replaced with the email address of the user that you are adding

    Working example
    curl -u Admin:Admin -X POST -H "Content-Type: application/json" "http://localhost:7990/bitbucket/rest/api/1.0/admin/users?name=Marty&password=P-ssw0rd&displayName=Marty%20Mouse&emailAddress=martym@yourdomain.com&addToDefualtGroup=true&notify=false"
  • Below is an example data file and bash script for adding any number of users from a file with the name of users.txt

    users.txt
    marty P-ssw0rd Marty%20Mouse martym@yourdomain.com
    pluto P-ssw0rd1 Plutto%20Dog plutod@yourdomain.com
    goofy P-ssw0rd2 Goofy%20Dog goofyd@yourdomain.com

    (Make sure there is no blank lines at the end of the file)

    AddUsers.sh
    #!/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&notify=false"
      done < users.txt
    fi

 

 

最終更新日: 2016 年 2 月 19 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.