Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Add everyone to a group

Maricel Ronquillo January 18, 2022

Hi,

I created a new Bitbucket group and would like to add all our users there. Can't seem to find a way to add users by bulk to groups.

Someone might have tried this before.

TIA

1 answer

1 accepted

0 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 19, 2022

Hello @Maricel Ronquillo ,

Thank you for contacting Atlassian Community. My name is Patrik, I'm with the Bitbucket Cloud Support Team and I'll be helping you on this case!

Although we currently don't have the option to bulk add users into a group using the UI, we do have a groups api endpoint available, which you can use to add the users to groups programmatically.

The endpoint is the following :

PUT https://api.bitbucket.org/1.0/groups/{workspace_id}/{group_slug}/members/{uuid}/ --data '{}'

The user calling the API must authenticate with administrative rights for a workspace to access its groups.

For more details about required parameters and examples on how to call this API, you can refer to the following documentation :

You can then make a custom script to call this endpoint and add the user in bulk.

A member of our support team helped on building the below python script, that uses the groups endpoint to add members to a group in a workspace :

from requests import Session

USERNAME = ''
APP_PASSWORD = ''
WORKSPACE_ID = ''
GROUP_ID = ''

'''
This script will attempt to read a text file "users.txt" in the same working directory as itself 
where each line is a uuid or email address of the user you wish to add to the above group
'''

def read_from_text_file() -> str:
    with open('users.txt', 'r') as user_list:
        for user in user_list:
            yield user.strip() #removes any whitespace before or after

def add_user_to_group(session: Session, user: str):
    data = '{}'
    endpoint = f'https://api.bitbucket.org/1.0/groups/{WORKSPACE_ID}/{GROUP_ID}/members/{user}'
    headers = {'Accept': 'application/json', 'Content-type': 'text/plain'}
    r = session.put(endpoint, data=data, headers=headers)
    if r.status_code == 200:
        print(f'Successfully added {user} to {GROUP_ID}')
    else:
        print(f'Failed to add {user} to {GROUP_ID} with error code: {r.status_code}\n\t{r.text}')

def main():
    session = Session()
    session.auth = (USERNAME, APP_PASSWORD)
    for user in read_from_text_file():
        add_user_to_group(session, user)


if __name__ == '__main__':
    main()

 Following the steps to run the script:

1. Install python 3.6  or higher;

2. Install the request package with


$ pip3 install requests

3. Edit the script and provide your Bitbucket Username, App Password for authentication, Workspace and Group.

4. Create a file users.txt where each line is a uuid or email address of the user you wish to add to the above group

5. After following all of these steps, you should be able to run the script with the following command :


$ python3 <name of the script file>.py

Please note the above script was produced by an individual within our support team and is not directly supported by Atlassian or our Support team as a whole. This is provided on a best efforts basis and is provided as we are trying to assist you as much as possible. We recommend you have your dev team review it and ensure you understand what it is attempting to accomplish before executing it. If you would like to change or add functionality to the script, feel free to do so.

Hope that helps. Let us know in case you have any questions.

Thanks, @Maricel Ronquillo 
Patrik S

Maricel Ronquillo January 23, 2022

Hi @Patrik S 

Thank you for the above information.

As we need to have it done ASAP, I just manually added the users.

I will keep your suggested solution as I might need this in future.

Best regards,

Maricel

Like Patrik S likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events