Exporting last activity information for workspace users
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
This article provides a comprehensive guide on how to export the last activity report for a user within a workspace, particularly for unmanaged accounts.
Cause
We are aware that we have an option to export user report from "Workspace Settings" > "User Directory".
The "Last Login" field you get from the CSV in the User Directory export is not a Last Activity field. This field refers to the time when the sign-in was last invoked by the user.
If a user signs in to Bitbucket using SSO, username-password combo etc. The time is recorded and will be the last login
The next time the user accesses Bitbucket with the same session cookie and does not explicitly does a login, this time does not overwrite the last login
If the user performs git activities without opening the Bitbucket UI such as push, pull from the terminal or using external tools, this is also not recorded and does not overwrite the last login.
The last login is explicitly stored only when the sign-in workflow is invoked and the session cookie is refreshed. This is why it gets updated when the user to log off and on.
Also, we have a KB article that explains better kindly review through: Last login information from Bitbucket cloud user directory
Solution
To get last accessed details for the users on workspace, we do have an API to retrieve this data:
1
2
3
4
curl --request GET \
--url 'https://api.bitbucket.org/2.0/workspaces/{workspace}/permissions' \
--header 'Authorization: Bearer <access_token>' \
--header 'Accept: application/json'
Source:API to get workspace permissions
Output:
1
2
3
4
5
6
7
}
},
"added_on": "2024-02-09T16:18:37.523382+00:00",
"permission": "member",
"last_accessed": "2024-09-18T10:13:52.159725+00:00"
},
{
The output provides last accessed field for every user, which means the time when user did their last activity such as push/add comments etc, on the workspace.
Additionally, below is a sample script that provides details such as user name, UUID and last accessed in a CSV file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# Replace with your actual Bitbucket username and app password
USERNAME="YOUR_USER_NAME"
APP_PASSWORD="ADD_YOUR_APP_PASSWORD"
WORKSPACE="WORKSPACE_NAME"
# Initial API URL for fetching workspace permissions
API_URL="https://api.bitbucket.org/2.0/workspaces/$WORKSPACE/permissions"
# Output CSV file
OUTPUT_FILE="bitbucket_permissions.csv"
# Check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq could not be found, please install it."
exit 1
fi
# Initialize CSV with headers
echo "nickname,uuid,last_accessed" > "$OUTPUT_FILE"
# Function to process each page of results
process_page() {
local response="$1"
# Extract nickname, uuid, and last_accessed, and append to the CSV file
echo "$response" | jq -r '.values[] | [.user.nickname, .user.uuid, .last_accessed] | @csv' >> "$OUTPUT_FILE"
}
# Loop through all pages
while [ -n "$API_URL" ]; do
# Fetch the API response for the current page
response=$(curl -s -u "$USERNAME:$APP_PASSWORD" -X GET "$API_URL")
# Process the current page of results
process_page "$response"
# Check if there's a next page and update API_URL, otherwise set it to empty to end the loop
API_URL=$(echo "$response" | jq -r '.next // empty')
done
echo "CSV file has been created: $OUTPUT_FILE"
(i) Please note this script is not officially supported by Atlassian. Hence we recommend you to review this with your development team before executing it
Was this helpful?