我查看了 Bitbucket API 文档 和这篇文章 BitBucket 获取所有贡献者的列表。第二个链接询问属于存储库的用户,但就我而言,我只想要所有许可用户的列表。真的没有办法做到这一点还是我在文档中错过了它?
执行这样的事情:
curl -s --user USER:PASS --request GET https://BITBUCKET-SERVER/rest/api/1.0/admin/users?limit=1000 | jq --raw-output '.values[] | .name + " => " + .displayName'
您将获得“用户名 => 名称”的用户列表。
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)
<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.
脚本:
#!/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."