def api_v1_recent(): """Query Redis and Elasticsearch and return some basic info on recent runs. :return: JSON containing recent run info. """ result = [] # Query Redis for all the UUIDs stored in the 'recent' list # TODO: limit the output of this endpoint to the last N runs # TODO: Alternately, set a TTL on the items in the 'recent' list in Redis for run_uuid in redis.lrange('recent', 0, -1): run_uuid = run_uuid.decode('utf-8') response = es.search(q="source:{} AND id:{}".format('jenkins-hookshot', run_uuid), size=1) hook_payload = response['hits']['hits'][0]['_source'] repo = hook_payload['payload']['repository']['full_name'] result.append(dict( run_uuid=run_uuid, timestamp=hook_payload['@timestamp'], org=repo.split('/')[0], repo=repo.split('/')[1], head_sha=hook_payload['payload']['head_commit']['id'], run_number=get_run_number(repo, run_uuid), ref=hook_payload['payload']['ref'] )) return jsonify(runs=result)
def get_run_number(repo, run_uuid): """A simple search routine that, given a run's UUID, iterates over a Redis list to determine the index, and hence the number of the run. :param repo: The full name of the GitHub repo (e.g. 'puppetlabs/puppet') :param run_uuid: UUID of the given run :return: Run number as an int, or None if not found """ run_number = None for index, uuid in enumerate( redis.lrange(repo, 0, -1)): if uuid.decode('utf-8') == run_uuid: run_number = index + 1 break return run_number