Skip to content

Commit eb86bf1

Browse files
author
Gonchik Tsymzhitov
committed
Bitbucket: add user usage report example and related method
1 parent 4a6a595 commit eb86bf1

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

atlassian/bitbucket.py

+20
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,26 @@ def fork_repository(self, project, repository, new_repository):
17361736
body['project'] = {'key': project}
17371737
return self.post(url, data=body)
17381738

1739+
def get_users_info(self, user_filter=None, start=0, limit=25):
1740+
"""
1741+
The authenticated user must have the LICENSED_USER permission to call this resource.
1742+
:param user_filter: if specified only users with usernames, display name or email addresses
1743+
containing the supplied string will be returned
1744+
:param limit:
1745+
:param start:
1746+
:return:
1747+
"""
1748+
params = {}
1749+
if limit:
1750+
params['limit'] = limit
1751+
if start:
1752+
params['start'] = start
1753+
if user_filter:
1754+
params['filter'] = user_filter
1755+
1756+
url = "rest/api/1.0/admin/users"
1757+
return self.get(url, params=params)
1758+
17391759
def get_current_license(self):
17401760
"""
17411761
Retrieves details about the current license, as well as the current status of the system with
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from atlassian import Bitbucket
2+
from pprint import pprint
3+
from datetime import datetime
4+
import argparse
5+
import logging
6+
7+
"""
8+
That example shows how to make a report of bitbucket usage
9+
"""
10+
11+
stash = Bitbucket(
12+
url="https://stash.example.com",
13+
username="admin",
14+
password="*********",
15+
timeout=60)
16+
17+
18+
def report(all=False, non_auth=False, limit=20):
19+
response = stash.get_users_info(stash, limit=limit)
20+
users = []
21+
if response:
22+
users = response.get("values") or []
23+
for user in users:
24+
print(user)
25+
auth_date = user.get('lastAuthenticationTimestamp') or None
26+
if auth_date:
27+
auth_date = int(auth_date / 1000)
28+
full_date = datetime.utcfromtimestamp(auth_date).strftime('%Y-%m-%d %H:%M:%S')
29+
else:
30+
full_date = None
31+
if full_date:
32+
output = f"{user.get('displayName')} ({user.get('emailAddress')}) authenticated on {full_date}"
33+
if all:
34+
print(output)
35+
else:
36+
output = f"{user.get('displayName')} ({user.get('emailAddress')}) not authenticated yet"
37+
if non_auth or all:
38+
print(output)
39+
40+
41+
if __name__ == '__main__':
42+
"""
43+
This part of code only executes if we run this module directly.
44+
You can still import the execute_build function and use it separately in the different module.
45+
"""
46+
# Setting the logging level. INFO|ERROR|DEBUG are the most common.
47+
logging.basicConfig(level=logging.ERROR)
48+
# Initialize argparse module with some program name and additional information
49+
parser = argparse.ArgumentParser(
50+
prog="bitbucker_auth_reviewer",
51+
usage="%(prog)s",
52+
description="Simple script to make a report of authenticated or non authenticated users"
53+
)
54+
# Adding the build key as the first argument
55+
parser.add_argument("--non-auth", help="Find non-auth users", dest='non_auth', action='store_true')
56+
parser.add_argument("--all", help="Review all users", dest='all', action='store_true')
57+
# Adding key=value parameters after the --arguments key
58+
# Getting arguments
59+
args = parser.parse_args()
60+
if args.all:
61+
report(all=True)
62+
if args.non_auth:
63+
report(non_auth=True)
64+
else:
65+
report(non_auth=False)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from atlassian import Bitbucket
2+
from pprint import pprint
3+
from datetime import datetime
4+
import argparse
5+
import logging
6+
7+
"""
8+
That example shows how to make a report of bitbucket usage
9+
"""
10+
11+
stash = Bitbucket(
12+
url="https://stash.example.com",
13+
username="admin",
14+
password="*************",
15+
timeout=60)
16+
17+
18+
def report(limit=200, include_in_active=False):
19+
response = stash.get_users_info(stash, limit=limit)
20+
users = []
21+
if response:
22+
users = response.get("values") or []
23+
print("|Status|Display name| Email |Last Auth DateTime|")
24+
for user in users:
25+
auth_date = user.get('lastAuthenticationTimestamp') or None
26+
if auth_date:
27+
auth_date = int(auth_date / 1000)
28+
full_date = datetime.utcfromtimestamp(auth_date).strftime('%Y-%m-%d %H:%M:%S')
29+
else:
30+
full_date = None
31+
if include_in_active or user.get('active'):
32+
output = f"|{user.get('active')}|{user.get('displayName')}|{user.get('emailAddress')}|{full_date}|"
33+
print(output)
34+
35+
36+
if __name__ == '__main__':
37+
"""
38+
This part of code only executes if we run this module directly.
39+
You can still import the execute_build function and use it separately in the different module.
40+
"""
41+
# Setting the logging level. INFO|ERROR|DEBUG are the most common.
42+
logging.basicConfig(level=logging.ERROR)
43+
report(limit=1000)

0 commit comments

Comments
 (0)