Last active
July 1, 2016 05:27
-
-
Save platan/8723c837c5322172c983 to your computer and use it in GitHub Desktop.
Script for creating Hound config based on public Stash repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2015 Marcin Mielnicki | |
# Licensed under the MIT License | |
# | |
# Requirements: | |
# stashy (https://github.com/RisingOak/stashy): pip install stashy | |
# | |
# Creates Hound config (https://github.com/etsy/Hound) with public Stash | |
# repositories | |
# | |
# Code formatting: | |
# autopep8 --in-place --aggressive --aggressive --max-line-length 120 prepare_hound_config.py | |
import argparse | |
import itertools | |
import json | |
import re | |
import stashy | |
def project_repos_iter(stash, project_key): | |
repos = stash.projects[project_key].repos.list() | |
for repo in repos: | |
if repo['project']['public'] or repo['public']: | |
slug = repo['slug'] | |
browse_url = repo['links']['self'][0]['href'] | |
project = repo['project']['key'] | |
clone_url = repo['cloneUrl'] if 'cloneUrl' in repo else [ | |
x for x in repo['links']['clone'] if x['name'] == 'http'][0]['href'] | |
# remove username from URL | |
clone_url = re.sub('://.*?@', '://', clone_url) | |
yield (project, slug, browse_url, clone_url) | |
def all_repos_iter(stash, excluded_projects): | |
projects = stash.projects.list() | |
for project in projects: | |
project_key = project['key'] | |
repos = project_repos_iter(stash, project_key) | |
for repo in repos: | |
if project_key not in excluded_projects: | |
yield repo | |
def hound_repos_iter(repos, interval=0): | |
for project_key, slug, browse_url, clone_url in repos: | |
repo_name = "{}-{}".format(project_key, slug) | |
repo_config = {"url": clone_url} | |
if interval > 0: | |
repo_config["ms-between-poll"] = interval | |
base_url = "{}/{{path}}{{anchor}}".format(browse_url) | |
repo_config["url-pattern"] = {"base-url": base_url, "anchor": "#{line}"} | |
hound_repo = {repo_name: repo_config} | |
yield hound_repo | |
def prepare_parser(): | |
parser = argparse.ArgumentParser(description='Creates config for Hound based on public Stash repositories.') | |
parser.add_argument('stash_url', type=str, help='Stash URL') | |
parser.add_argument('stash_username', type=str, help='Stash username') | |
parser.add_argument('stash_password', type=str, help='Stash password') | |
parser.add_argument('--interval', type=int, help='interval between poll in ms') | |
parser.add_argument('--exclude-projects', type=str, nargs='*', default=[], | |
help='list of projects to exclude') | |
parser.add_argument('--limit', type=int, help='maximum number of repositories to configure') | |
parser.add_argument('--max-concurrent-indexers', type=int, help='max concurrent indexers') | |
return parser | |
def main(): | |
parser = prepare_parser() | |
args = parser.parse_args() | |
stash = stashy.connect(args.stash_url, args.stash_username, args.stash_password) | |
limit = args.limit | |
interval = args.interval | |
excluded_projects = args.exclude_projects | |
hound_repos = hound_repos_iter(all_repos_iter(stash, excluded_projects), interval) | |
if limit > 0: | |
hound_repos = itertools.islice(hound_repos, limit) | |
hound_repos_dict = {} | |
for repo in hound_repos: | |
hound_repos_dict = dict(hound_repos_dict.items() + repo.items()) | |
config = {'dbpath': 'data', 'repos': hound_repos_dict} | |
if args.max_concurrent_indexers > 0: | |
config['max-concurrent-indexers'] = args.max_concurrent_indexers | |
print json.dumps(config, indent=4) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A rough shell script for doing it without python (just requires curl and jq): https://gist.github.com/l8nite/654567309434e576cdd3