Skip to content

Commit

Permalink
Allow import Gitlab repo manually and set a webhook automatically
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
humitos committed Apr 10, 2018
1 parent 4dc9e94 commit 93c0aae
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 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,22 @@ 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)
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 @@ -255,10 +273,7 @@ def setup_webhook(self, 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)
data = self.get_webhook_data(repo_id, integration, project)
resp = None
try:
Expand Down Expand Up @@ -307,10 +322,7 @@ 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)
data = self.get_webhook_data(repo_id, project, integration)
hook_id = integration.provider_data.get('id')
resp = None
Expand Down

0 comments on commit 93c0aae

Please sign in to comment.