forked from sourcerer-io/hall-of-fame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (129 loc) · 5.6 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Google Cloud Functions for Hall of Fame."""
__copyright__ = '2018 Sourcerer, Inc.'
__author__ = 'Sergey Surkov'
import os
import flask
from google.cloud import pubsub
import fame.ssl_hack
import fame.storage
from fame.github_tracker import RepoTracker
from fame.glory import Glory
class CloudError(Exception):
def __init__(self, message):
super().__init__(message)
class Manage:
"""Management commands."""
ADD = 'add' # Add a repo to tracking.
REMOVE = 'remove' # Remove a repo from tracking.
LIST = 'list' # List repos for a given user.
@staticmethod
def is_valid(command):
return command in [Manage.ADD, Manage.REMOVE, Manage.LIST]
class Refresh:
"""Refresh commands."""
REFRESH = 'refresh' # Update repo stats and make badges (update+glorify).
REFRESH_ALL = 'refresh-all' # Refresh all repos.
@staticmethod
def is_valid(command):
return command in [Refresh.REFRESH, Refresh.REFRESH_ALL]
def error(message):
raise CloudError(message)
def error_if_false(expression, message):
if not expression:
error(message)
def make_error_response(error_code, message):
response = flask.jsonify({'status': 'error', 'message': message})
response.status_code = error_code
return response
def configure_storage():
bucket = os.environ.get('bucket', None)
error_if_false(bucket, 'Google cloud bucket is required')
fame.storage.configure_for_google_cloud(os.environ['bucket'])
def get_fame_pubsub_topic():
project = os.environ.get('project', None)
error_if_false(project, 'Google pubsub project is required')
topic = os.environ.get('topic', None)
error_if_false(topic, 'Google pubsub topic is required')
return 'projects/%s/topics/%s' % (project, topic)
def fame_manage(request):
"""HTTP Google cloud function. Adds/Removes/Lists repos."""
try:
data = request.get_json()
error_if_false(data, 'No payload')
print('i %s' % str(data))
command = data.get('command', None)
error_if_false(Manage.is_valid(command),
'Invalid command %s' % command)
user = data.get('user', None)
error_if_false(user, 'User is required')
configure_storage()
topic = get_fame_pubsub_topic() # Let it fail early.
if command in [Manage.ADD, Manage.REMOVE]:
owner = data.get('owner', None)
error_if_false(owner, 'Repo owner is required')
repo = data.get('repo', None)
error_if_false(repo, 'Repo is required')
tracker = RepoTracker()
result = {'status': 'ok'}
if command == Manage.ADD:
tracker.configure(user, owner, repo)
tracker.add()
client = pubsub.PublisherClient()
client.publish(topic, b'', command=Refresh.REFRESH,
user=user, owner=owner, repo=repo)
elif command == Manage.REMOVE:
tracker.configure(user, owner, repo)
tracker.remove()
elif command == Manage.LIST:
directory = []
for item in RepoTracker.list(user):
modified = item.last_modified.strftime('%Y-%m-%dT%H:%M:%SZ')
directory.append({
'user': item.user,
'owner': item.owner,
'repo': item.repo,
'status': item.status,
'last_modified': modified,
'message': item.error_message})
result['data'] = directory
return flask.jsonify(result)
except Exception as e:
print('e %s' % str(e))
return make_error_response(400, str(e))
def fame_refresh(data, context):
"""Pubsub Google cloud function. Refreshes repos."""
try:
attrs = data['attributes']
command = attrs.get('command', None)
error_if_false(Refresh.is_valid(command),
'Invalid command %s' % command)
sourcerer_origin = os.environ.get('sourcerer_origin', None)
error_if_false(sourcerer_origin, 'Sourcerer origin is required')
sourcerer_api_origin = os.environ.get('sourcerer_api_origin', None)
sourcerer_api_secret = os.environ.get('sourcerer_api_secret', None)
if os.environ.get('no_ssl_host_check', None) == '1':
fame.ssl_hack.disable_ssl_host_check()
configure_storage()
topic = get_fame_pubsub_topic() # Let it fail early.
if command == Refresh.REFRESH_ALL: # Enqueue refresh for all repos.
client = pubsub.PublisherClient()
for user, owner, repo, _, _, _ in RepoTracker.list():
client.publish(topic, b'', command=Refresh.REFRESH,
user=user, owner=owner, repo=repo)
print('i Enqueued for refresh %s:%s/%s' % (user, owner, repo))
elif command == Refresh.REFRESH: # Do an actual refresh for a repo.
user = attrs.get('user', None)
error_if_false(user, 'User is required')
owner = attrs.get('owner', None)
error_if_false(owner, 'Repo owner is required')
repo = attrs.get('repo', None)
error_if_false(repo, 'Repo is required')
tracker = RepoTracker()
tracker.configure(user, owner, repo,
sourcerer_api_origin=sourcerer_api_origin,
sourcerer_api_secret=sourcerer_api_secret)
tracker.update()
glory = Glory(sourcerer_origin, sourcerer_api_origin)
glory.make(tracker.load())
except Exception as e:
print('e %s' % str(e))