From 5d00ec46560c603f698bcc0fd6ec836346f217bc Mon Sep 17 00:00:00 2001 From: Mattia Verga Date: Feb 17 2023 17:06:33 +0000 Subject: Remove BZ_API_KEY and use ~/.bugzillarc Signed-off-by: Mattia Verga --- diff --git a/find_inactive_packagers.py b/find_inactive_packagers.py index bf8bc00..148f5fb 100644 --- a/find_inactive_packagers.py +++ b/find_inactive_packagers.py @@ -52,7 +52,7 @@ To run the script you'll need: - fasjson_client to be installed - an active kerberos ticket to login to fasjson.fedoraproject.org -- a bugzilla API key stored as OS env variable 'BZ_API_KEY' +- a bugzilla API key for bugzilla.redhat.com in your ~/.bugzillarc config file Optionally, providing a Pagure repository URL and a token the script can automatically open and manage an issue ticket for each detected inactive packager. @@ -73,7 +73,6 @@ from urllib3.util.retry import Retry from bugzilla import Bugzilla from fasjson_client import Client -BZ_API_KEY = getenv('BZ_API_KEY', None) PAGURE_API_KEY = getenv('PAGURE_API_KEY', None) PAGURE_API_BASE_URL = getenv('PAGURE_API_BASE_URL', 'https://pagure.io/api/0/find-inactive-packagers') # A tag for marking "inactive packager" ticket or empty string @@ -367,10 +366,8 @@ def _check_user_activity(user, privacy=False, fasclient=None, bzclient=None): maillist_result = _check_maillists_activity(fasclient, user) if maillist_result[0]: return True - if BZ_API_KEY: + if bzclient: log.info(f'Checking {user} activity in bugzilla...') - if not bzclient: - bzclient = Bugzilla(url='https://bugzilla.redhat.com/xmlrpc.cgi', api_key=BZ_API_KEY) packager = (user, maillist_result[1]) if _check_bugzilla_activity(bzclient, packager, privacy=privacy): return True @@ -419,8 +416,10 @@ def cli(ctx, debug, privacy): @cli.command() @click.option('--open-tickets', is_flag=True, default=False, help='File tickets in Pagure.') +@click.option('--with-bz-check/--without-bz-check', default=True, + help='Run (or not) activity checks on RedHat Bugzilla.') @click.pass_context -def step_one(ctx, open_tickets): +def step_one(ctx, with_bz_check, open_tickets): """Find inactive packagers. If open-tickets flag is activated, the script will file tickets in Pagure, otherwise @@ -436,16 +435,24 @@ def step_one(ctx, open_tickets): if not PAGURE_API_KEY or not PAGURE_API_BASE_URL: log.error('ERROR: You need to provide PAGURE_API_KEY and PAGURE_API_BASE_URL, ' 'queue processing will stop immediately.') - raise SystemExit('You need to provide PAGURE_API_KEY and PAGURE_API_BASE_URL, ' - 'queue processing will stop immediately.') + raise SystemExit() + + if with_bz_check: + bzclient = Bugzilla(url='https://bugzilla.redhat.com/xmlrpc.cgi') + try: + assert bzclient.logged_in + except Exception: + log.error('ERROR: An authenticated session to bugzilla.redhat.com is required, ' + 'maybe you forgot to provide an API key for logging in? ' + 'Queue processing will stop immediately.') + raise SystemExit() try: fasclient = Client(FASCLIENT_URL) except Exception: log.error('ERROR: Unable to connect to fasclient, you probably forgot to obtain ' 'a Kerberos ticket.') - raise SystemExit('Unable to connect to fasclient, you probably forgot to obtain ' - 'a Kerberos ticket.') + raise SystemExit() packagers = fasclient.list_group_members(groupname='packager').result log.info(f'### Found {len(packagers)} users in the packager group. ###') inactive_packagers = [] @@ -499,16 +506,14 @@ def step_one(ctx, open_tickets): log.info(f'### Found {len(packager_email_map)} users which also show no activity in mailing lists over the last year. ###') # Check for activity in Bugzilla - if BZ_API_KEY: + if with_bz_check: log.info('Checking users activity in bugzilla...') - bzclient = Bugzilla(url='https://bugzilla.redhat.com/xmlrpc.cgi', api_key=BZ_API_KEY) for i, p in enumerate(copy(packager_email_map).items()): if i > 0 and i % 100 == 0: log.info(f'Done {i}.') if _check_bugzilla_activity(bzclient, p, privacy=privacy): packager_email_map.pop(p[0]) - - log.info(f'### Found {len(packager_email_map)} users which also show no activity in Bugzilla over the last year. ###') + log.info(f'### Found {len(packager_email_map)} users which also show no activity in Bugzilla over the last year. ###') if packager_email_map: with open('inactive_packagers.csv', 'w') as fout: @@ -547,8 +552,10 @@ def step_one(ctx, open_tickets): @cli.command() @click.option('--from-file', type=click.File('r'), help='Use local csv files instead of Pagure.') @click.option('--close-tickets', is_flag=True, default=False, help='Close tickets in Pagure.') +@click.option('--with-bz-check/--without-bz-check', default=True, + help='Run (or not) activity checks on RedHat Bugzilla.') @click.pass_context -def step_two(ctx, close_tickets, from_file): +def step_two(ctx, with_bz_check, close_tickets, from_file): """Check any user still inactive. If a csv file is provided, the script will use that as input, rather than fetching tickets from @@ -570,18 +577,26 @@ def step_two(ctx, close_tickets, from_file): if not PAGURE_API_KEY or not PAGURE_API_BASE_URL: log.error('ERROR: You need to provide PAGURE_API_KEY and PAGURE_API_BASE_URL, ' 'queue processing will stop immediately.') - raise SystemExit('You need to provide PAGURE_API_KEY and PAGURE_API_BASE_URL, ' - 'queue processing will stop immediately.') + raise SystemExit() + + if with_bz_check: + bzclient = Bugzilla(url='https://bugzilla.redhat.com/xmlrpc.cgi') + try: + assert bzclient.logged_in + except Exception: + log.error('ERROR: An authenticated session to bugzilla.redhat.com is required, ' + 'maybe you forgot to provide an API key for logging in? ' + 'Queue processing will stop immediately.') + raise SystemExit() + else: + bzclient = None + try: fasclient = Client(FASCLIENT_URL) except Exception: log.error('ERROR: Unable to connect to fasclient, you probably forgot to obtain ' 'a Kerberos ticket.') - raise SystemExit('Unable to connect to fasclient, you probably forgot to obtain ' - 'a Kerberos ticket.') - - if BZ_API_KEY: - bzclient = Bugzilla(url='https://bugzilla.redhat.com/xmlrpc.cgi', api_key=BZ_API_KEY) + raise SystemExit() if from_file: log.debug('Using CSV file') @@ -717,14 +732,26 @@ def check_impact(ctx): @cli.command() @click.argument('username') +@click.option('--with-bz-check/--without-bz-check', default=True, + help='Run (or not) activity checks on RedHat Bugzilla.') @click.pass_context -def check_user(ctx, username): +def check_user(ctx, with_bz_check, username): """Check if any activity is detected for a specific username.""" privacy = ctx.obj['privacy'] if ctx.obj['debug']: log.setLevel(logging.DEBUG) - if _check_user_activity(username, privacy=privacy): + if with_bz_check: + bzclient = Bugzilla(url='https://bugzilla.redhat.com/xmlrpc.cgi') + try: + assert bzclient.logged_in + except Exception: + log.error('ERROR: An authenticated session to bugzilla.redhat.com is required, ' + 'maybe you forgot to provide an API key for logging in? ' + 'Queue processing will stop immediately.') + raise SystemExit() + + if _check_user_activity(username, privacy=privacy, bzclient=bzclient): click.echo(f"Activity detected for user {username}.") else: click.echo(f"User {username} appear to be inactive.")