From 4583e902406676eb9f9c2457ab7b4c8792ca9d87 Mon Sep 17 00:00:00 2001
From: Mattia Verga <mattia.verga@tiscali.it>
Date: Feb 16 2023 20:32:34 +0000
Subject: Mark users with no account in pagure.io


Signed-off-by: Mattia Verga <mattia.verga@tiscali.it>

---

diff --git a/find_inactive_packagers.py b/find_inactive_packagers.py
index 2ca6dfb..bf8bc00 100644
--- a/find_inactive_packagers.py
+++ b/find_inactive_packagers.py
@@ -212,18 +212,14 @@ def _check_pagure_activity(user, base_url='https://src.fedoraproject.org'):
     if not user:
         log.error('ERROR: Cannot check an empty username!')
         return False
-    try:
-        resp_src = session.get(f'{base_url}/api/0/user/{user}/activity/stats').json()
-        if not resp_src:
-            log.info(f'No activity detected for user {user} in {base_url}')
-            return False
-        error = resp_src.get('error', None)
-        if error is not None:
-            log.info(f'Error checking user {user} in {base_url}: {error}')
-            return False
-    except Exception:
-        # May happen when the user never interacted with pagure
-        log.warning(f'Error while retrieving data for user {user}.')
+    resp_src = session.get(f'{base_url}/api/0/user/{user}/activity/stats').json()
+    if resp_src.get('error', None) is not None:
+        if resp_src.get('error_code', None) == 'ENOUSER':
+            raise AttributeError(f'User not found in {base_url}')
+        log.info(f'Error checking user {user} in {base_url}: {error}')
+        return False
+    if not bool(resp_src):
+        log.info(f'No activity detected for user {user} in {base_url}')
         return False
     log.info(f'User {user} was active in {base_url}')
     return True
@@ -352,10 +348,17 @@ def _check_user_activity(user, privacy=False, fasclient=None, bzclient=None):
             raise SystemExit('Unable to connect to fasclient, you probably forgot to obtain '
                              'a Kerberos ticket.')
     log.info(f'Checking {user} activity in Pagure...')
-    if (
-        _check_pagure_activity(user)
-        or _check_pagure_activity(user, base_url='https://pagure.io')
-    ):
+    try:
+        src_fpo = _check_pagure_activity(user)
+    except AttributeError as ex:
+        log.info(f'{ex}')
+        src_fpo = False
+    try:
+        pagure = _check_pagure_activity(user, base_url='https://pagure.io')
+    except AttributeError as ex:
+        log.info(f'{ex}')
+        pagure = False
+    if src_fpo or pagure:
         return True
     log.info(f'Checking {user} activity in Bodhi...')
     if _check_bodhi_activity(user):
@@ -446,6 +449,7 @@ def step_one(ctx, open_tickets):
     packagers = fasclient.list_group_members(groupname='packager').result
     log.info(f'### Found {len(packagers)} users in the packager group. ###')
     inactive_packagers = []
+    nouser_in_pagureio = []
 
     # Check for activity in Pagure
     log.info('Checking users activity in Pagure...')
@@ -455,10 +459,19 @@ def step_one(ctx, open_tickets):
         user = p.get('username')
         if user in EXCLUDE_USERS:
             continue
-        if (
-            not _check_pagure_activity(user)
-            and not _check_pagure_activity(user, base_url='https://pagure.io')
-        ):
+        try:
+            src_fpo = _check_pagure_activity(user)
+        except AttributeError as ex:
+            log.info(f'{ex}')
+            src_fpo = False
+        if not src_fpo:
+            try:
+                pagure = _check_pagure_activity(user, base_url='https://pagure.io')
+            except AttributeError as ex:
+                log.info(f'{ex}')
+                pagure = False
+                nouser_in_pagureio.append(user)
+        if not src_fpo and not pagure:
             inactive_packagers.append(user)
 
     log.info(f'### Found {len(inactive_packagers)} users with no activity in pagure/src.fp.org over the last year. ###')
@@ -524,8 +537,9 @@ def step_one(ctx, open_tickets):
                         ticket_id = 'ERROR'
                 # Write results to file
                 emailstring = '|'.join([mask_email(em, privacy=privacy) for em in emails])
-                log.info(f'{user} - {ticket_id} - {emailstring}')
-                fout.write(f'{user},{ticket_id},{emailstring}\n')
+                nosuchuser = 'not registered in pagure.io' if user in nouser_in_pagureio else ''
+                log.info(f'{user} - {ticket_id} - {emailstring} - {nosuchuser}')
+                fout.write(f'{user},{ticket_id},{emailstring},{nosuchuser}\n')
     else:
         log.info('### No inactive packagers detected, YHAY! Nothing to do. ###')
 
@@ -573,7 +587,7 @@ def step_two(ctx, close_tickets, from_file):
         log.debug('Using CSV file')
         pending_removal_users = []
         for line in from_file:
-            user, ticket, emails = line.split(',')
+            user, ticket, emails, pagureio = line.split(',')
             pending_removal_users.append(user)
         log.info(f'### Checking {len(pending_removal_users)} users from csv file ###')
         for i, p in enumerate(copy(pending_removal_users)):

