Skip to content

Commit

Permalink
Merge pull request #2 from galileo-press/sync-gitlab-repos
Browse files Browse the repository at this point in the history
Support private repositories and cleanup
  • Loading branch information
saily authored Oct 5, 2016
2 parents 9152bb6 + 50285fe commit e288bf3
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions readthedocs/oauth/services/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
import json
import re

try:
from urlparse import urljoin, urlparse
except ImportError:
from urllib.parse import urljoin, urlparse # noqa

from django.conf import settings
from requests.exceptions import RequestException
from allauth.socialaccount.models import SocialToken
from allauth.socialaccount.providers.gitlab.views import GitLabOAuth2Adapter
from urlparse import urljoin

from readthedocs.restapi.client import api

Expand All @@ -23,7 +27,9 @@ class GitLabService(Service):
"""Provider service for GitLab"""

adapter = GitLabOAuth2Adapter
url_pattern = re.compile(re.escape(adapter.provider_base_url))
# Just use the network location to determine if it's a GitLab project
# because private repos have another base url, eg. [email protected]
url_pattern = re.compile(re.escape(urlparse(adapter.provider_base_url).netloc))
default_avatar = {
'repo': urljoin(settings.MEDIA_URL, 'images/fa-bookmark.svg'),
'org': urljoin(settings.MEDIA_URL, 'images/fa-users.svg'),
Expand Down Expand Up @@ -172,24 +178,26 @@ def setup_webhook(self, project):
:rtype: bool
"""
session = self.get_session()
resp = None
repositories = RemoteRepository.objects.filter(clone_url=project.vcs_repo().repo_url)

if not repositories.exists():
log.error('GitLab remote repository not found')
return False, resp

repo_id = repositories[0].get_serialized()['id']
# See: http://doc.gitlab.com/ce/api/projects.html#add-project-hook
data = json.dumps({
'id': 'readthedocs',
'id': repo_id,
'push_events': True,
'issues_events': False,
'merge_requests_events': False,
'note_events': False,
'tag_push_events': True,
'url': u'https://{0}/gitlab'.format(settings.PRODUCTION_DOMAIN),
})
resp = None

try:
repositories = RemoteRepository.objects.filter(
clone_url=project.vcs_repo().repo_url
)
assert repositories
repo_id = repositories[0].get_serialized()['id']
resp = session.post(
u'{url}/api/v3/projects/{repo_id}/hooks'.format(
url=self.adapter.provider_base_url,
Expand All @@ -199,19 +207,13 @@ def setup_webhook(self, project):
headers={'content-type': 'application/json'}
)
if resp.status_code == 201:
log.info('GitLab webhook creation successful for project: %s', # noqa
project)
return (True, resp)
except (AssertionError, RemoteRepository.DoesNotExist) as ex:
log.error('GitLab remote repository not found', exc_info=ex)
except RequestException as ex:
pass
log.info('GitLab webhook creation successful for project: %s', project)
return True, resp
except RequestException:
log.error('GitLab webhook creation failed for project: %s', project, exc_info=True)
else:
ex = False

log.error('GitLab webhook creation failed for project: %s', # noqa
project, exc_info=ex)
return (False, resp)
log.error('GitLab webhook creation failed for project: %s', project)
return False, resp

@classmethod
def get_token_for_project(cls, project, force_local=False):
Expand Down

0 comments on commit e288bf3

Please sign in to comment.