def scan()

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


def scan(KibbleBit, source):
    # Simple URL check
    jenkins = re.match(r"(https?://.+)", source['sourceURL'])
    if jenkins:
        
        source['steps']['jenkins'] = {
            'time': time.time(),
            'status': 'Parsing Jenkins job changes...',
            'running': True,
            'good': True
        }
        KibbleBit.updateSource(source)
        
        badOnes = 0
        pendingJobs = []
        KibbleBit.pprint("Parsing Jenkins activity at %s" % source['sourceURL'])
        source['steps']['issues'] = {
            'time': time.time(),
            'status': 'Downloading changeset',
            'running': True,
            'good': True
        }
        KibbleBit.updateSource(source)
        
        # Jenkins may neeed credentials
        creds = None
        if source['creds'] and 'username' in source['creds'] and source['creds']['username'] and len(source['creds']['username']) > 0:
            creds = "%s:%s" % (source['creds']['username'], source['creds']['password'])
            
        # Get the job list
        sURL = source['sourceURL']
        KibbleBit.pprint("Getting job list...")
        jobsjs = plugins.utils.jsonapi.get("%s/api/json?tree=jobs[name,color]&depth=1" % sURL , auth = creds)
        
        # Get the current queue
        KibbleBit.pprint("Getting job queue...")
        queuejs = plugins.utils.jsonapi.get("%s/queue/api/json?depth=1" % sURL , auth = creds)
        
        # Save queue snapshot
        NOW = int(datetime.datetime.utcnow().timestamp())
        queuehash = hashlib.sha224( ("%s-%s-queue-%s" % (source['organisation'], source['sourceURL'], int(time.time())) ).encode('ascii', errors='replace')).hexdigest()
        
        
        # Scan queue items
        blocked = 0
        stuck = 0
        totalqueuetime = 0
        items = queuejs.get('items', [])
        
        for item in items:
            if item['blocked']:
                blocked += 1
            if item['stuck']:
                stuck += 1
            if 'inQueueSince' in item:
                totalqueuetime += (NOW - int(item['inQueueSince']/1000))
        
        avgqueuetime = totalqueuetime / max(1, len(items))
        
        # Count how many jobs are building, find any folders...
        actual_jobs, building = get_all_jobs(KibbleBit, source, jobsjs.get('jobs', []), creds)

        # Write up a queue doc
        queuedoc = {
            'id': queuehash,
            'date': time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(NOW)),
            'time': NOW,
            'building': building,
            'size': len(items),
            'blocked': blocked,
            'stuck': stuck,
            'avgwait': avgqueuetime,
            'ci': 'jenkins',
            
            # Standard docs values
            'sourceID': source['sourceID'],
            'organisation': source['organisation'],
            'upsert': True,
        }
        KibbleBit.append('ci_queue', queuedoc)
        
        
        pendingJobs = actual_jobs
        KibbleBit.pprint("Found %u jobs in Jenkins" % len(pendingJobs))
        
        threads = []
        block = threading.Lock()
        KibbleBit.pprint("Scanning jobs using 4 sub-threads")
        for i in range(0,4):
            t = jenkinsThread(block, KibbleBit, source, creds, pendingJobs)
            threads.append(t)
            t.start()
        
        for t in threads:
            t.join()

        # We're all done, yaay        
        KibbleBit.pprint("Done scanning %s" % source['sourceURL'])

        source['steps']['issues'] = {
            'time': time.time(),
            'status': 'Jenkins successfully scanned at ' + time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(time.time())),
            'running': False,
            'good': True
        }
        KibbleBit.updateSource(source)