def scanTicket()

in src/plugins/scanners/bugzilla.py [0:0]


def scanTicket(bug, KibbleBit, source, openTickets, u, dom):
    try:
        key = bug['id']
        dhash = hashlib.sha224( ("%s-%s-%s" % (source['organisation'], source['sourceURL'], key) ).encode('ascii', errors='replace')).hexdigest()
        found = KibbleBit.exists('issue', dhash)
        parseIt = False
        if not found:
            parseIt = True
        else:
            ticket = KibbleBit.get('issue', dhash)
            if ticket['status'] == 'closed' and key in openTickets:
                KibbleBit.pprint("Ticket was reopened, reparsing")
                parseIt = True
            elif ticket['status'] == 'open' and not key in openTickets:
                KibbleBit.pprint("Ticket was recently closed, parsing it")
                parseIt = True
            else:
                pass
                #print("Ticket hasn't changed, ignoring...")
            
        if parseIt:
            KibbleBit.pprint("Parsing data from BugZilla for #%s" % key)
            
            params = {
                'ids':  [int(key)],
                'limit': 0
                }
            if source['creds'] and 'username' in source['creds'] and source['creds']['username'] and len(source['creds']['username']) > 0:
                params['Bugzilla_login'] = source['creds']['username']
                params['Bugzilla_password'] = source['creds']['password']
            ticketsURL = "%s?method=Bug.get&params=[%s]" % (u, urllib.parse.quote(json.dumps(params)))
            
            js = plugins.utils.jsonapi.get(ticketsURL)
            js= js['result']['bugs'][0]
            creator = {
                'name': bug['creator'],
                'email': js['creator']
            }
            closer = {}
            cd = getTime(js['creation_time'])
            rd = None
            status = 'open'
            if js['status'] in ["CLOSED", "RESOLVED"]:
                status = 'closed'
                KibbleBit.pprint("%s was closed, finding out who did that" % key)
                ticketsURL = "%s?method=Bug.history&params=[%s]" % (u, urllib.parse.quote(json.dumps(params)))
                hjs = plugins.utils.jsonapi.get(ticketsURL)
                history = hjs['result']['bugs'][0]['history']
                for item in history:
                    for change in item['changes']:
                        if change['field_name'] == 'status' and 'added' in change and change['added'] in ['CLOSED', 'RESOLVED']:
                            rd = getTime(item['when'])
                            closer = {
                                'name': item['who'],
                                'email': item['who']
                            }
                            break
            KibbleBit.pprint("Counting comments for %s..." % key)
            ticketsURL = "%s?method=Bug.comments&params=[%s]" % (u, urllib.parse.quote(json.dumps(params)))
            hjs = plugins.utils.jsonapi.get(ticketsURL)
            comments = len(hjs['result']['bugs'][str(key)]['comments'])
            
            title = bug['summary']
            del params['ids']
            if closer:
                
                pid = hashlib.sha1( ("%s%s" % (source['organisation'], closer['email'])).encode('ascii', errors='replace')).hexdigest()
                found = KibbleBit.exists('person', pid)
                if not found:
                    params['names'] = [closer['email']]
                    ticketsURL = "%s?method=User.get&params=[%s]" % (u, urllib.parse.quote(json.dumps(params)))
                    
                    try:
                        ujs = plugins.utils.jsonapi.get(ticketsURL)
                        displayName = ujs['result']['users'][0]['real_name']
                    except:
                        displayName = closer['email']
                    if displayName and len(displayName) > 0:
                        # Add to people db
                        
                        jsp = {
                            'name': displayName,
                            'email': closer['email'],
                            'organisation': source['organisation'],
                            'id' :pid
                        }
                        #print("Updating person DB for closer: %s (%s)" % (displayName, closerEmail))
                        KibbleBit.index('person', pid, jsp)
                    
            if creator:
                pid = hashlib.sha1( ("%s%s" % (source['organisation'], creator['email'])).encode('ascii', errors='replace')).hexdigest()
                found = KibbleBit.exists('person', pid)
                if not found:
                    if not creator['name']:
                        params['names'] = [creator['email']]
                        ticketsURL = "%s?method=User.get&params=[%s]" % (u, urllib.parse.quote(json.dumps(params)))
                        try:
                            ujs = plugins.utils.jsonapi.get(ticketsURL)
                            creator['name'] = ujs['result']['users'][0]['real_name']
                        except:
                            creator['name'] = creator['email']
                    if creator['name'] and len(creator['name']) > 0:
                        # Add to people db
                        
                        jsp = {
                            'name': creator['name'],
                            'email': creator['email'],
                            'organisation': source['organisation'],
                            'id' :pid
                        }
                        KibbleBit.index('person', pid, jsp)
                
            jso = {
                'id': dhash,
                'key': key,
                'organisation': source['organisation'],
                'sourceID': source['sourceID'],
                'url': "%s/show_bug.cgi?id=%s" % (dom, key),
                'status': status,
                'created': cd,
                'closed': rd,
                'issuetype': 'issue',
                'issueCloser': closer['email'] if 'email' in closer else None, 
                'createdDate': time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(cd)),
                'closedDate': time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(rd)) if rd else None,
                'changeDate': time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(rd if rd else cd)),
                'assignee': None,
                'issueCreator': creator['email'],
                'comments': comments,
                'title': title
            }
            KibbleBit.append('issue', jso)
            time.sleep(0.5) # BugZilla is notoriously slow. Maybe remove this later
        return True
    except Exception as err:
        KibbleBit.pprint(err)
        return False