From f7b1429a8fcda3a9b8a0a6b75c47a9f4d00e8c0b Mon Sep 17 00:00:00 2001 From: Mattia Verga Date: Aug 20 2022 08:33:17 +0000 Subject: Store ticket id in output file Signed-off-by: Mattia Verga --- diff --git a/find_inactive_packagers.py b/find_inactive_packagers.py index 04dd487..ff2a955 100644 --- a/find_inactive_packagers.py +++ b/find_inactive_packagers.py @@ -252,40 +252,50 @@ def cli(ctx, privacy): logging.info(f'### Found {len(packager_email_map)} users which also show no activity in Bugzilla over the last year. ###') ctx.obj['packager_email_map'] = packager_email_map - - if packager_email_map: - with open('inactive_packagers.csv', 'w') as fout: - for user, emails in packager_email_map.items(): - emailstring = '|'.join(emails) - logging.info(f'{user} - {emailstring if not privacy else "***"}') - fout.write(f'{user},{emailstring}\n') + ctx.obj['privacy'] = privacy @cli.command() +@click.option('--open-tickets', default=False, help='File tickets in Pagure.') @click.pass_context -def step_one(ctx): +def step_one(ctx, open_tickets): """Open Pagure tickets against inactive packagers.""" packager_email_map = ctx.obj['packager_email_map'] + if open_tickets: + if not PAGURE_API_KEY or not PAGURE_NEW_TICKET_URL: + logging.error('You need to provide PAGURE_API_KEY and PAGURE_NEW_TICKET_URL, ' + 'queue processing will stop immediately.') + sys.exit('You need to provide PAGURE_API_KEY and PAGURE_NEW_TICKET_URL, ' + 'queue processing will stop immediately.') + if packager_email_map: - # Open Pagure tickets - if PAGURE_API_KEY and PAGURE_NEW_TICKET_URL: - headers = {'Authorization': f'token {PAGURE_API_KEY}'} + with open('inactive_packagers.csv', 'w') as fout: for user, emails in packager_email_map.items(): - data = {'title': f'Inactive packager detected for user {user}', - 'issue_content': PING_INACTIVE_TEXT.format(username = user, email = emails[0]), - 'tag': PAGURE_NEW_TICKET_TAGS.split(','), - 'assignee': PAGURE_NEW_TICKET_ASSIGNEE} - try: - resp = requests.post(PAGURE_NEW_TICKET_URL, data=data, headers=headers) - if resp.status_code == 401: - logging.error(f'Invalid or expired Pagure token, queue processing will stop immediately.') - break - if resp.status_code != 200: + # Open Pagure tickets + if open_tickets: + headers = {'Authorization': f'token {PAGURE_API_KEY}'} + data = {'title': f'Inactive packager detected for user {user}', + 'issue_content': PING_INACTIVE_TEXT.format(username = user, email = emails[0]), + 'tag': PAGURE_NEW_TICKET_TAGS.split(','), + 'assignee': PAGURE_NEW_TICKET_ASSIGNEE} + try: + resp = requests.post(PAGURE_NEW_TICKET_URL, data=data, headers=headers) + if resp.status_code == 401: + logging.error('Invalid or expired Pagure token, queue processing will stop immediately.') + sys.exit('Invalid or expired Pagure token, queue processing will stop immediately.') + if resp.status_code != 200: + logging.error(f'Error opening Pagure ticket for user {user}') + ticket_id = 'ERROR' + else: + ticket_id = resp.json().get('issue', dict()).get('id', 'ERROR') + except Exception: logging.error(f'Error opening Pagure ticket for user {user}') - except Exception: - logging.error(f'Error opening Pagure ticket for user {user}') - continue + ticket_id = 'ERROR' + # Write results to file + emailstring = '|'.join(emails) + logging.info(f'{user} - {ticket_id} - {emailstring if not ctx.obj['privacy'] else "***"}') + fout.write(f'{user},{ticket_id},{emailstring}\n') @cli.command()