Skip to content

Commit

Permalink
Initial changes for PR Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
saadmk11 committed Jun 19, 2019
1 parent 1207afb commit c74d294
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
33 changes: 33 additions & 0 deletions readthedocs/api/v2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
STABLE,
STABLE_VERBOSE_NAME,
TAG,
EXTERNAL,
)
from readthedocs.core.utils import trigger_build
from readthedocs.builds.models import Version


Expand Down Expand Up @@ -77,6 +79,15 @@ def sync_versions(project, versions, type): # pylint: disable=redefined-builtin
version_name,
version_id,
)
elif type == EXTERNAL:
created_version = Version.objects.create(
project=project,
type=type,
identifier=version_id,
verbose_name=version_name,
)
added.add(created_version.slug)

else:
# New Version
created_version = Version.objects.create(
Expand Down Expand Up @@ -135,6 +146,9 @@ def delete_versions(project, version_data):
versions_tags = [
version['verbose_name'] for version in version_data.get('tags', [])
]
external_versions = [
version['verbose_name'] for version in version_data.get('external_branches', [])
]
versions_branches = [
version['identifier'] for version in version_data.get('branches', [])
]
Expand All @@ -147,6 +161,10 @@ def delete_versions(project, version_data):
type=BRANCH,
identifier__in=versions_branches,
)
to_delete_qs = to_delete_qs.exclude(
type=EXTERNAL,
verbose_name__in=external_versions,
)
to_delete_qs = to_delete_qs.exclude(uploaded=True)
to_delete_qs = to_delete_qs.exclude(active=True)
to_delete_qs = to_delete_qs.exclude(slug__in=NON_REPOSITORY_VERSIONS)
Expand Down Expand Up @@ -176,6 +194,21 @@ def run_automation_rules(project, versions_slug):
rule.run(version)


def trigger_external_build(project, version_list):
"""
Trigger Builds for all external versions provided.
The rules are sorted by priority.
"""
for version_slug in version_list:
version = project.versions(manager=EXTERNAL).get(slug=version_slug)
version.active = True
version.save()

trigger_build(project=project, version=version)


class RemoteOrganizationPagination(PageNumberPagination):
page_size = 25

Expand Down
7 changes: 7 additions & 0 deletions readthedocs/api/v2/views/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
GITHUB_EVENT_HEADER = 'HTTP_X_GITHUB_EVENT'
GITHUB_SIGNATURE_HEADER = 'HTTP_X_HUB_SIGNATURE'
GITHUB_PUSH = 'push'
GITHUB_PULL_REQUEST = 'pull_request'
GITHUB_PULL_REQUEST_OPEN = 'open'
GITHUB_PULL_REQUEST_SYNC = 'synchronize'
GITHUB_CREATE = 'create'
GITHUB_DELETE = 'delete'
GITLAB_TOKEN_HEADER = 'HTTP_X_GITLAB_TOKEN'
Expand Down Expand Up @@ -271,6 +274,10 @@ def handle_webhook(self):
raise ParseError('Parameter "ref" is required')
if event in (GITHUB_CREATE, GITHUB_DELETE):
return self.sync_versions(self.project)

if event == GITHUB_PULL_REQUEST:
return self.sync_versions(self.project)

return None

def _normalize_ref(self, ref):
Expand Down
24 changes: 21 additions & 3 deletions readthedocs/api/v2/views/model_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rest_framework.renderers import BaseRenderer, JSONRenderer
from rest_framework.response import Response

from readthedocs.builds.constants import BRANCH, TAG, INTERNAL
from readthedocs.builds.constants import BRANCH, TAG, INTERNAL, EXTERNAL
from readthedocs.builds.models import Build, BuildCommandResult, Version
from readthedocs.core.utils import trigger_build
from readthedocs.core.utils.extend import SettingsOverrideObject
Expand Down Expand Up @@ -189,6 +189,7 @@ def sync_versions(self, request, **kwargs): # noqa: D205
# Update All Versions
data = request.data
added_versions = set()
added_external_versions = set()
if 'tags' in data:
ret_set = api_utils.sync_versions(
project=project,
Expand All @@ -203,6 +204,15 @@ def sync_versions(self, request, **kwargs): # noqa: D205
type=BRANCH,
)
added_versions.update(ret_set)

if 'external_branches' in data:
ret_set = api_utils.sync_versions(
project=project,
versions=data['external_branches'],
type=EXTERNAL,
)
added_external_versions.update(ret_set)

deleted_versions = api_utils.delete_versions(project, data)
except Exception as e:
log.exception('Sync Versions Error')
Expand All @@ -213,11 +223,13 @@ def sync_versions(self, request, **kwargs): # noqa: D205
status=status.HTTP_400_BAD_REQUEST,
)

all_added_versions = added_versions | added_external_versions

try:
# The order of added_versions isn't deterministic.
# We don't track the commit time or any other metadata.
# We usually have one version added per webhook.
api_utils.run_automation_rules(project, added_versions)
api_utils.run_automation_rules(project, all_added_versions)
except Exception:
# Don't interrupt the request if something goes wrong
# in the automation rules.
Expand All @@ -226,6 +238,12 @@ def sync_versions(self, request, **kwargs): # noqa: D205
project.slug, added_versions
)

if added_external_versions:
api_utils.trigger_external_build(
project=project,
version_list=added_external_versions
)

# TODO: move this to an automation rule
promoted_version = project.update_stable_version()
new_stable = project.get_stable_version()
Expand All @@ -250,7 +268,7 @@ def sync_versions(self, request, **kwargs): # noqa: D205
trigger_build(project=project, version=promoted_version)

return Response({
'added_versions': added_versions,
'added_versions': all_added_versions,
'deleted_versions': deleted_versions,
})

Expand Down
6 changes: 6 additions & 0 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ def sync_versions(self, version_repo):
'verbose_name': v.verbose_name,
} for v in version_repo.branches]

if version_repo.supports_external_branches:
version_post_data['external_branches'] = [{
'identifier': v.identifier,
'verbose_name': v.verbose_name,
} for v in version_repo.external_branches]

self.validate_duplicate_reserved_versions(version_post_data)

try:
Expand Down
26 changes: 23 additions & 3 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Backend(BaseVCS):

supports_tags = True
supports_branches = True
supports_external_branches = True
supports_submodules = True
fallback_branch = 'master' # default branch
repo_depth = 50
Expand Down Expand Up @@ -213,11 +214,30 @@ def branches(self):

for branch in branches:
verbose_name = branch.name
if verbose_name.startswith('origin/'):
if not verbose_name.startswith('origin/external/'):
if verbose_name.startswith('origin/'):
verbose_name = verbose_name.replace('origin/', '')
if verbose_name == 'HEAD':
continue
versions.append(VCSVersion(self, str(branch), verbose_name))
return versions

@property
def external_branches(self):
repo = git.Repo(self.working_dir)
versions = []
branches = []

# ``repo.remotes.origin.refs`` returns remote branches
if repo.remotes:
branches += repo.remotes.origin.refs

for branch in branches:
verbose_name = branch.name
if verbose_name.startswith('origin/external/'):
verbose_name = verbose_name.replace('origin/', '')
if verbose_name == 'HEAD':
versions.append(VCSVersion(self, str(branch.commit), verbose_name))
continue
versions.append(VCSVersion(self, str(branch), verbose_name))
return versions

@property
Expand Down

0 comments on commit c74d294

Please sign in to comment.