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

Allow import Gitlab repo manually and set a webhook automatically #3934

Merged
merged 3 commits into from May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 28 additions & 7 deletions readthedocs/oauth/services/gitlab.py
Expand Up @@ -12,7 +12,9 @@
from django.core.urlresolvers import reverse
from requests.exceptions import RequestException

from readthedocs.builds.utils import get_gitlab_username_repo
from readthedocs.integrations.models import Integration
from readthedocs.projects.models import Project

from ..models import RemoteOrganization, RemoteRepository
from .base import Service
Expand Down Expand Up @@ -41,6 +43,25 @@ class GitLabService(Service):
url_pattern = re.compile(
re.escape(urlparse(adapter.provider_base_url).netloc))

def _get_repo_id(self, project):
# The ID or URL-encoded path of the project
# https://docs.gitlab.com/ce/api/README.html#namespaced-path-encoding
try:
repo_id = json.loads(project.remote_repository.json).get('id')
except Project.remote_repository.RelatedObjectDoesNotExist:
# Handle "Manual Import" when there is no RemoteRepository
# associated with the project. It only works with gitlab.com at the
# moment (doesn't support custom gitlab installations)
username, repo = get_gitlab_username_repo(project.repo)
if (username, repo) == (None, None):
return None

repo_id = '{username}%2F{repo}'.format(
username=username,
repo=repo,
)
return repo_id

def get_next_url_to_paginate(self, response):
return response.links.get('next', {}).get('url')

Expand Down Expand Up @@ -249,17 +270,17 @@ def setup_webhook(self, project):
:returns: boolean based on webhook set up success
:rtype: bool
"""
session = self.get_session()
integration, _ = Integration.objects.get_or_create(
project=project,
integration_type=Integration.GITLAB_WEBHOOK,
)

# The ID or URL-encoded path of the project
# https://docs.gitlab.com/ce/api/README.html#namespaced-path-encoding
repo_id = json.loads(project.remote_repository.json).get('id')
repo_id = self._get_repo_id(project)
if repo_id is None:
return (False, None)

data = self.get_webhook_data(repo_id, project, integration)
session = self.get_session()
resp = None
try:
resp = session.post(
Expand Down Expand Up @@ -307,9 +328,9 @@ def update_webhook(self, project, integration):
"""
session = self.get_session()

# The ID or URL-encoded path of the project
# https://docs.gitlab.com/ce/api/README.html#namespaced-path-encoding
repo_id = json.loads(project.remote_repository.json).get('id')
repo_id = self._get_repo_id(project)
if repo_id is None:
return (False, None)

data = self.get_webhook_data(repo_id, project, integration)
hook_id = integration.provider_data.get('id')
Expand Down
5 changes: 5 additions & 0 deletions readthedocs/rtd_tests/tests/test_oauth.py
Expand Up @@ -485,3 +485,8 @@ def test_make_private_project(self):
m.return_value = True
repo = self.service.create_repository(data, organization=self.org)
self.assertIsNotNone(repo)

def test_setup_webhook(self):
success, response = self.service.setup_webhook(self.project)
self.assertFalse(success)
self.assertIsNone(response)