In Bitbucket API, how can I get a list of ALL licensed users?
Asked Answered
G

4

5

I've looked at the Bitbucket API docs and this post BitBucket get list of all contributors. The second link asks about users belonging to a repo, but in my case I just want a list of ALL licensed users. Is there really NO way to do this or did I miss it in the docs?

Gefell answered 2/4, 2018 at 18:45 Comment(2)
Are you asking about Bitbucket Server? Both links you've provided are for Bitbucket Cloud (bitbucket.org), which has a different API structure.Defalcation
Sorry yes, Bitbucket serverGefell
S
4

Execute something like this:

curl -s --user USER:PASS --request GET https://BITBUCKET-SERVER/rest/api/1.0/admin/users?limit=1000 | jq --raw-output '.values[] | .name + " => " + .displayName'

And you'll get the "username => name" list of users.

Stpeter answered 2/4, 2018 at 19:33 Comment(3)
Thanks, I tried this and get "parse error: Invalid numeric literal at line 1, column 10". I do have jq installedGefell
Uhm... to check if it's a concatenation issue, run only this: curl -s --user USER:PASS --request GET BITBUCKET-SERVER/rest/api/1.0/admin/users?limit=1000 | jq --raw-output '.values[] | .name'Swinson
Forgot the api version. So this works: curl -s --user USER:PASS --request GET BITBUCKET-SERVER/rest/api/1.0/admin/users?limit=1000 | jq --raw-output '.values[] | .name'Gefell
M
4

Just for anyone else looking for this: The currently accepted answer provides a way to list all users, but this will include unlicensed users, i.e. user records that are not currently consuming a seat.

If you want to get a list of all licensed users, follow the steps described in How do I find which users count against my Bitbucket Server license? to install an add-on which will give you exactly this.

Margetmargette answered 4/4, 2018 at 10:17 Comment(0)
S
1

Update: Bitbucket 8.12.x and later have an easy way to do this from the GUI: https://confluence.atlassian.com/bitbucketserverkb/how-do-i-find-which-users-count-against-my-bitbucket-server-license-779171685.html

Legacy answer

Here is some python code that did the trick for me. I had many users (10k+) but <1000 users with permissions. I also have basic authentication (username/passoword) enabled for my bitbucket server. By querying both "users" and "permissions" and merging the results using pandas, I got a pretty good result.

import pandas as pd
import numpy as np
import json
import requests
import datetime
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

bitbucket_url = "https://my-bitbucket-server" 
users_url = bitbucket_url + "/rest/api/1.0/admin/users?limit=1000&start="
permissions_url = bitbucket_url + "/rest/api/1.0/admin/permissions/users?limit=1000&start="
out_filename = r"C:\mypath\authenticatedUsers.csv"

username = "myusername"
password = "mypassword"

def get_responses(url,username,password):
    json_responses = []
    with requests.Session() as s:
        s.auth = (username,password)
        start = 0
        while(True):
            response = s.get(url + str(start),verify=False)
            json_response = json.loads(response.text)
            if "values" not in json_response:
                print("Error:" + response.text)
            json_responses += json_response["values"]
            if(len(json_response["values"]) < 1000):
                break
            start += 1000
    return json_responses

users = pd.DataFrame(get_responses(users_url,username=username,password=password))
users["DaysSinceAuth"] = np.floor((datetime.datetime.now().timestamp() - users.lastAuthenticationTimestamp/1000)/3600/24)
permissions = get_responses(permissions_url,username=username,password=password)
permissions = pd.DataFrame([{"id":p["user"]["id"],"permission":p["permission"]} for p in permissions])
usersmerged = users.merge(permissions,how="inner",on="id")
usersmerged.to_csv(out_filename)
Sharronsharyl answered 3/10, 2022 at 16:50 Comment(0)
L
1

Hope this bash script helps everyone who is looking for solution. It retrieves all the users from the groups that have LICENCES_USER permissions.

Script:

#!/bin/bash
 
# Define variables
BITBUCKET_URL="https://my-bitbucket-server"
USERNAME="myusername"
PASSWORD="mypassword"  
OUTPUT_FILE="licensed_users.csv"
 
# Create or clear the output file
echo "Name,Email" > "$OUTPUT_FILE"
 
# Get groups with specific permission
groups_response=$(curl -u "$USERNAME:$PASSWORD" -X GET -k "$BITBUCKET_URL/rest/api/1.0/admin/permissions/groups?limit=1000")
 
# Extract group names with LICENSED_USER permission and SYS_ADMIN
echo "Fetching groups with LICENSED_USER or SYS_ADMIN permissions..."
group_names=$(echo "$groups_response" | jq -r '.values[] | select(.permission == "LICENSED_USER" or .permission == "SYS_ADMIN") | .group.name')
 
# Check if there are any groups with specified permissions
if [ -z "$group_names" ]; then
    echo "No groups found with LICENSED_USER or SYS_ADMIN permission."
    exit 1
fi
 
# Print the list of groups being processed
echo "Groups with LICENSED_USER or SYS_ADMIN permissions:"
echo "$group_names"
 
# Loop through each group to get the members
for group in $group_names; do
    echo "Processing group: $group"
    # Fetch group members
    members_response=$(curl -u "$USERNAME:$PASSWORD" -X GET -k "$BITBUCKET_URL/rest/api/latest/admin/groups/more-members?context=$group")
 
    # Extract member details
    echo "$members_response" | jq -r '.values[] | "\(.displayName),\(.emailAddress)"' >> "$OUTPUT_FILE"
done
 
# Remove duplicates
awk '!seen[$0]++' "$OUTPUT_FILE" > "temp_$OUTPUT_FILE" && mv "temp_$OUTPUT_FILE" "$OUTPUT_FILE"
 
echo "Finished processing. The list of licensed users is in $OUTPUT_FILE."
Lan answered 29/10 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.