Skip to content

Commit

Permalink
Allow import Gitlab repo manually and set a webhook automatically (#3934
Browse files Browse the repository at this point in the history
)

* Allow import Gitlab repo manually and set a webhook automatically

When we import a Project manually we don't have a RemoteRepository
associated with that Project, so we use the old technique of getting
the username and repo names from the URL using
`get_gitlab_username_repo` function.

This case (Manual Import) won't support custom Gitlab installations in
the future with this code.

* Handle username,repo not found

* Test case
  • Loading branch information
humitos authored and agjohnson committed May 3, 2018
1 parent c3bbf76 commit 5300c90
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
35 changes: 28 additions & 7 deletions readthedocs/oauth/services/gitlab.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)

0 comments on commit 5300c90

Please sign in to comment.