Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support private repositories and cleanup #2

Merged
merged 6 commits into from
Oct 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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