-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
github.py
304 lines (280 loc) · 11.2 KB
/
github.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""OAuth utility functions."""
from __future__ import absolute_import
from builtins import str
import logging
import json
import re
from django.conf import settings
from django.core.urlresolvers import reverse
from requests.exceptions import RequestException
from allauth.socialaccount.models import SocialToken
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from readthedocs.builds import utils as build_utils
from readthedocs.integrations.models import Integration
from readthedocs.restapi.client import api
from ..models import RemoteOrganization, RemoteRepository
from .base import Service
log = logging.getLogger(__name__)
class GitHubService(Service):
"""Provider service for GitHub."""
adapter = GitHubOAuth2Adapter
# TODO replace this with a less naive check
url_pattern = re.compile(r'github\.com')
def sync(self):
"""Sync repositories and organizations."""
self.sync_repositories()
self.sync_organizations()
def sync_repositories(self):
"""Sync repositories from GitHub API."""
repos = self.paginate('https://api.github.com/user/repos?per_page=100')
try:
for repo in repos:
self.create_repository(repo)
except (TypeError, ValueError) as e:
log.exception('Error syncing GitHub repositories')
raise Exception('Could not sync your GitHub repositories, '
'try reconnecting your account')
def sync_organizations(self):
"""Sync organizations from GitHub API."""
try:
orgs = self.paginate('https://api.github.com/user/orgs')
for org in orgs:
org_resp = self.get_session().get(org['url'])
org_obj = self.create_organization(org_resp.json())
# Add repos
# TODO ?per_page=100
org_repos = self.paginate(
'{org_url}/repos'.format(org_url=org['url'])
)
for repo in org_repos:
self.create_repository(repo, organization=org_obj)
except (TypeError, ValueError) as e:
log.exception('Error syncing GitHub organizations')
raise Exception('Could not sync your GitHub organizations, '
'try reconnecting your account')
def create_repository(self, fields, privacy=None, organization=None):
"""
Update or create a repository from GitHub API response.
:param fields: dictionary of response data from API
:param privacy: privacy level to support
:param organization: remote organization to associate with
:type organization: RemoteOrganization
:rtype: RemoteRepository
"""
privacy = privacy or settings.DEFAULT_PRIVACY_LEVEL
if (
(privacy == 'private') or
(fields['private'] is False and privacy == 'public')
):
try:
repo = RemoteRepository.objects.get(
full_name=fields['full_name'],
users=self.user,
account=self.account,
)
except RemoteRepository.DoesNotExist:
repo = RemoteRepository.objects.create(
full_name=fields['full_name'],
account=self.account,
)
repo.users.add(self.user)
if repo.organization and repo.organization != organization:
log.debug('Not importing %s because mismatched orgs',
fields['name'])
return None
else:
repo.organization = organization
repo.name = fields['name']
repo.description = fields['description']
repo.ssh_url = fields['ssh_url']
repo.html_url = fields['html_url']
repo.private = fields['private']
if repo.private:
repo.clone_url = fields['ssh_url']
else:
repo.clone_url = fields['clone_url']
repo.admin = fields.get('permissions', {}).get('admin', False)
repo.vcs = 'git'
repo.account = self.account
repo.avatar_url = fields.get('owner', {}).get('avatar_url')
if not repo.avatar_url:
repo.avatar_url = self.default_user_avatar_url
repo.json = json.dumps(fields)
repo.save()
return repo
else:
log.debug('Not importing %s because mismatched type',
fields['name'])
def create_organization(self, fields):
"""
Update or create remote organization from GitHub API response.
:param fields: dictionary response of data from API
:rtype: RemoteOrganization
"""
try:
organization = RemoteOrganization.objects.get(
slug=fields.get('login'),
users=self.user,
account=self.account,
)
except RemoteOrganization.DoesNotExist:
organization = RemoteOrganization.objects.create(
slug=fields.get('login'),
account=self.account,
)
organization.users.add(self.user)
organization.url = fields.get('html_url')
organization.name = fields.get('name')
organization.email = fields.get('email')
organization.avatar_url = fields.get('avatar_url')
if not organization.avatar_url:
organization.avatar_url = self.default_org_avatar_url
organization.json = json.dumps(fields)
organization.account = self.account
organization.save()
return organization
def get_next_url_to_paginate(self, response):
return response.links.get('next', {}).get('url')
def get_paginated_results(self, response):
return response.json()
def get_webhook_data(self, project, integration):
"""Get webhook JSON data to post to the API."""
return json.dumps({
'name': 'web',
'active': True,
'config': {
'url': 'https://{domain}{path}'.format(
domain=settings.PRODUCTION_DOMAIN,
path=reverse(
'api_webhook',
kwargs={'project_slug': project.slug,
'integration_pk': integration.pk}
)
),
'content_type': 'json',
},
'events': ['push', 'pull_request'],
})
def setup_webhook(self, project):
"""
Set up GitHub project webhook for project.
:param project: project to set up webhook for
:type project: Project
:returns: boolean based on webhook set up success, and requests Response object
:rtype: (Bool, Response)
"""
session = self.get_session()
owner, repo = build_utils.get_github_username_repo(url=project.repo)
integration, _ = Integration.objects.get_or_create(
project=project,
integration_type=Integration.GITHUB_WEBHOOK,
)
data = self.get_webhook_data(project, integration)
resp = None
try:
resp = session.post(
('https://api.github.com/repos/{owner}/{repo}/hooks'
.format(owner=owner, repo=repo)),
data=data,
headers={'content-type': 'application/json'}
)
# GitHub will return 200 if already synced
if resp.status_code in [200, 201]:
recv_data = resp.json()
integration.provider_data = recv_data
integration.save()
log.info('GitHub webhook creation successful for project: %s',
project)
return (True, resp)
# Catch exceptions with request or deserializing JSON
except (RequestException, ValueError):
log.exception(
'GitHub webhook creation failed for project: %s',
project,
)
else:
log.exception(
'GitHub webhook creation failed for project: %s',
project,
)
# Response data should always be JSON, still try to log if not
# though
try:
debug_data = resp.json()
except ValueError:
debug_data = resp.content
log.debug(
'GitHub webhook creation failure response: %s',
debug_data,
)
return (False, resp)
def update_webhook(self, project, integration):
"""
Update webhook integration.
:param project: project to set up webhook for
:type project: Project
:param integration: Webhook integration to update
:type integration: Integration
:returns: boolean based on webhook update success, and requests Response object
:rtype: (Bool, Response)
"""
session = self.get_session()
data = self.get_webhook_data(project, integration)
url = integration.provider_data.get('url')
resp = None
try:
resp = session.patch(
url,
data=data,
headers={'content-type': 'application/json'}
)
# GitHub will return 200 if already synced
if resp.status_code in [200, 201]:
recv_data = resp.json()
integration.provider_data = recv_data
integration.save()
log.info(
'GitHub webhook creation successful for project: %s',
project,
)
return (True, resp)
# Catch exceptions with request or deserializing JSON
except (RequestException, ValueError):
log.exception(
'GitHub webhook update failed for project: %s',
project,
)
else:
log.exception(
'GitHub webhook update failed for project: %s',
project,
)
try:
debug_data = resp.json()
except ValueError:
debug_data = resp.content
log.debug(
'GitHub webhook creation failure response: %s',
debug_data,
)
return (False, resp)
@classmethod
def get_token_for_project(cls, project, force_local=False):
"""Get access token for project by iterating over project users."""
# TODO why does this only target GitHub?
if not getattr(settings, 'ALLOW_PRIVATE_REPOS', False):
return None
token = None
try:
if getattr(settings, 'DONT_HIT_DB', True) and not force_local:
token = api.project(project.pk).token().get()['token']
else:
for user in project.users.all():
tokens = SocialToken.objects.filter(
account__user=user,
app__provider=cls.adapter.provider_id)
if tokens.exists():
token = tokens[0].token
except Exception:
log.exception('Failed to get token for project')
return token