def api_v1_org(org): """Organization overview. Returns a list of repositories that have been run through the system, with their most recent created date and run number. :param org: The GitHub username or organization (e.g. 'puppetlabs') :return: JSON containing an organization's repositories known to the system. """ response = es.search(q="source:{} AND payload.repository.owner.name:{}".format('jenkins-hookshot', org)) repos = [] for hit in response['hits']['hits']: repos.append(hit['_source']['payload']['repository']['full_name']) result = [] for repo in set(repos): last_run_uuid = redis.lindex(repo, -1).decode('utf-8') last_run_num = get_run_number(repo, last_run_uuid) resp = es.search(q="source:{} AND id:{}".format('jenkins-hookshot', last_run_uuid)) hit = resp['hits']['hits'][0]['_source'] repo = hit['repo'] last_run_timestamp = hit['@timestamp'] result.append(dict( name=repo, last_run_uuid=last_run_uuid, last_run_number=last_run_num, last_run_timestamp=last_run_timestamp, )) return jsonify(dict(org=org, repos=sorted(result, key=itemgetter('name'))))
def api_v1_repo(org, repo): """Repo overview. For the given repository (e.g. 'puppetlabs/puppet'), return a list of runs. :param org: GitHub username or organization (e.g. 'puppetlabs') :param repo: GitHub repository name (e.g. 'puppet') :return: JSON containing runs known to the system. """ # Note: here we're using the fullname of the repo, e.g. 'puppetlabs/puppet', # as is GitHub's convention. However, Flask sees the '/' character as a path # separator, so it has to be two individual arguments that we later combine. repo_full_name = '/'.join([org, repo]) result = [] response = es.search(q="source:{} AND payload.repository.full_name:\"{}\"".format('jenkins-hookshot', repo_full_name)) # TODO: limit the output of this endpoint to the last N runs for hit in response['hits']['hits']: hit = hit['_source'] uuid = hit['id'] number = get_run_number(repo_full_name, uuid) timestamp = hit['@timestamp'] head_sha = hit['payload']['head_commit']['id'] ref = hit['payload']['ref'] result.append(dict( run_uuid=uuid, run_number=number, timestamp=timestamp, head_sha=head_sha, ref=ref, )) return jsonify(repo=repo_full_name, runs=sorted(result, key=itemgetter('run_number'), reverse=True))
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)