|
| 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) |
0 commit comments