-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (69 loc) · 2.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging
from datetime import timedelta, datetime
import click
from ghe import client
from statsdata import connection, schemas, organizations, repos, contributions
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('cmd')
conn_option = click.option('--db', type=str, help='The database connection string', prompt=True, envvar='DB_CONN')
token_option = click.option('--token', type=str, help='The Github Or Github Enterprise Token', prompt=True,
envvar='GITHUB_TOKEN')
github_option = click.option('--github-url', type=str, help='Github Enterprise URL', default='https://api.github.com',
envvar='GITHUB_URL')
@click.group()
def database():
pass
@database.command()
@conn_option
def create_schemas(db):
conn = connection.get_connection(db)
logger.debug('Connection %s', conn)
schemas.create_schemas(conn)
@click.group()
def ghe():
pass
@ghe.command()
@token_option
@github_option
def whoami(token, github_url):
g = client.client(token, github_url)
user = g.get_user()
click.echo('User: {}'.format(user.login))
@ghe.command()
@conn_option
@token_option
@github_option
def add_orgs(db, token, github_url):
g = client.client(token, github_url)
conn = connection.get_connection(db)
organizations.truncate_organizations(conn)
organs = g.get_organizations()
organizations.add_orgs(conn, organs)
@ghe.command()
@conn_option
@token_option
@github_option
def add_repos(db, token, github_url):
g = client.client(token, github_url)
conn = connection.get_connection(db)
for org in organizations.get_unsynced(conn):
logger.debug("Adding Organization: %s", str(org))
repos.truncate(conn, org[0])
repos.get_repos(conn, g, org[0], org[1])
logger.debug("Finished processing orgs")
@ghe.command()
@conn_option
@token_option
@github_option
@click.option("--days", type=int, help="Number of days of contributions for syncing", default=30)
def get_contributions(db, token, github_url, days):
six_months_ago = datetime.now() + timedelta(days=-days)
g = client.client(token, github_url)
conn = connection.get_connection(db)
for r in repos.get_unsynced(conn):
logger.info('Syncing Repository: %s', str(r))
contributions.del_repo_contributions(conn, r)
repos.get_contributions(g, conn, r, six_months_ago)
cli = click.CommandCollection(sources=[database, ghe])
if __name__ == '__main__':
cli()