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

Refine PR Builder Code #5933

Merged
6 changes: 3 additions & 3 deletions readthedocs/api/v2/views/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def sync_versions(self, project):

def get_external_version_response(self, project):
"""
Build External version on pull/merge request events and return API response.
Trigger builds for External versions on pull/merge request events and return API response.

Return a JSON response with the following::

Expand All @@ -211,7 +211,7 @@ def get_external_version_response(self, project):
}

:param project: Project instance
:type project: Project
:type project: readthedocs.projects.models.Project
"""
identifier, verbose_name = self.get_external_version_data()
# create or get external version object using `verbose_name`.
Expand Down Expand Up @@ -369,7 +369,7 @@ def handle_webhook(self):
return self.sync_versions(self.project)

if (
self.project.has_feature(Feature.ENABLE_EXTERNAL_VERSION_BUILD) and
self.project.has_feature(Feature.EXTERNAL_VERSION_BUILD) and
event == GITHUB_PULL_REQUEST and action
):
if (
Expand Down
2 changes: 2 additions & 0 deletions readthedocs/builds/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,5 @@

GITHUB_EXTERNAL_VERSION_NAME = 'Pull Request'
GENERIC_EXTERNAL_VERSION_NAME = 'External Version'

RTD_BUILD_STATUS_API_NAME = 'continuous-documentation/read-the-docs'
10 changes: 7 additions & 3 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def config(self):
"""
last_build = (
self.builds(manager=INTERNAL).filter(
state='finished',
state=BUILD_STATE_FINISHED,
success=True,
).order_by('-date')
.only('_config')
Expand Down Expand Up @@ -742,7 +742,11 @@ def get_absolute_url(self):
return reverse('builds_detail', args=[self.project.slug, self.pk])

def get_full_url(self):
"""Get full url including domain."""
"""
Get full url of the build including domain.

Example: https://readthedocs.org/projects/pip/builds/99999999/
"""
scheme = 'http' if settings.DEBUG else 'https'
full_url = '{scheme}://{domain}{absolute_url}'.format(
scheme=scheme,
Expand Down Expand Up @@ -803,7 +807,7 @@ def get_commit_url(self):
commit=self.commit
)

return ''
return None

@property
def finished(self):
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/builds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BuildBase:
def get_queryset(self):
self.project_slug = self.kwargs.get('project_slug', None)
self.project = get_object_or_404(
Project.objects.protected(self.request.user),
Project.objects.public(self.request.user),
slug=self.project_slug,
)
queryset = Build.objects.public(
Expand Down
38 changes: 17 additions & 21 deletions readthedocs/core/views/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,27 @@ def get_or_create_external_version(project, identifier, verbose_name):
:returns: External version.
:rtype: Version
"""
external_version = project.versions(
manager=EXTERNAL
).filter(verbose_name=verbose_name).first()
external_version, created = project.versions.get_or_create(
verbose_name=verbose_name,
type=EXTERNAL,
defaults={'identifier': identifier, 'active': True},
)

if external_version:
if created:
log.info(
'(Create External Version) Added Version: [%s] ',
external_version.slug
)
else:
# identifier will change if there is a new commit to the Pull/Merge Request
if external_version.identifier != identifier:
external_version.identifier = identifier
external_version.save()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably log in this case too (eg. updated external version)

else:
# create an external version if the version does not exist.
created_external_version = Version.objects.create(
project=project,
type=EXTERNAL,
identifier=identifier,
verbose_name=verbose_name,
active=True
)
log.info(
'(Create External Version) Added Version: [%s] ', ' '.join(
created_external_version.slug

log.info(
'(Update External Version) Updated Version: [%s] ',
external_version.slug
)
)
return created_external_version
return external_version


Expand All @@ -152,9 +149,8 @@ def delete_external_version(project, identifier, verbose_name):
# Delete External Version
external_version.delete()
log.info(
'(Delete External Version) Deleted Version: [%s]', ' '.join(
external_version.slug
)
'(Delete External Version) Deleted Version: [%s]',
external_version.slug
)

return external_version.verbose_name
Expand Down
10 changes: 7 additions & 3 deletions readthedocs/oauth/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

from readthedocs.api.v2.client import api
from readthedocs.builds import utils as build_utils
from readthedocs.builds.constants import SELECT_BUILD_STATUS
from readthedocs.builds.constants import (
SELECT_BUILD_STATUS,
RTD_BUILD_STATUS_API_NAME
)
from readthedocs.integrations.models import Integration

from ..models import RemoteOrganization, RemoteRepository
Expand Down Expand Up @@ -336,7 +339,7 @@ def send_build_status(self, build, state):
'state': github_build_state,
'target_url': build.get_full_url(),
'description': description,
'context': 'continuous-documentation/read-the-docs'
'context': RTD_BUILD_STATUS_API_NAME
}

resp = None
Expand All @@ -349,8 +352,9 @@ def send_build_status(self, build, state):
)
if resp.status_code == 201:
log.info(
'GitHub commit status for project: %s',
"GitHub commit status created for project: %s, commit status: %s",
project,
github_build_state,
)
return True

Expand Down
2 changes: 1 addition & 1 deletion readthedocs/projects/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,4 @@
'commit/{commit}'
)

GITHUB_GIT_PATTERN = 'pull/{id}/head:external-{id}'
GITHUB_PR_PULL_PATTERN = 'pull/{id}/head:external-{id}'
4 changes: 2 additions & 2 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ def add_features(sender, **kwargs):
SHARE_SPHINX_DOCTREE = 'share_sphinx_doctree'
DEFAULT_TO_MKDOCS_0_17_3 = 'default_to_mkdocs_0_17_3'
CLEAN_AFTER_BUILD = 'clean_after_build'
ENABLE_EXTERNAL_VERSION_BUILD = 'enable_external_version_build'
EXTERNAL_VERSION_BUILD = 'enable_external_version_build'
UPDATE_CONDA_STARTUP = 'update_conda_startup'

FEATURES = (
Expand Down Expand Up @@ -1450,7 +1450,7 @@ def add_features(sender, **kwargs):
_('Clean all files used in the build process'),
),
(
ENABLE_EXTERNAL_VERSION_BUILD,
EXTERNAL_VERSION_BUILD,
_('Enable project to build on pull/merge requests'),
),
(
Expand Down
5 changes: 3 additions & 2 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,17 +578,18 @@ def run_build(self, docker, record):

if self.build_env.failed:
self.send_notifications(self.version.pk, self.build['id'])
# send build failure status to git Status API
send_external_build_status(
version=self.version, build_pk=self.build['id'], status=BUILD_STATUS_FAILURE
)
elif self.build_env.successful:
# send build successful status to git Status API
send_external_build_status(
version=self.version, build_pk=self.build['id'], status=BUILD_STATUS_SUCCESS
)
else:
msg = 'Unhandled Build Status'
send_external_build_status(
version=self.version, build_pk=self.build['id'], status=BUILD_STATUS_FAILURE
)
log.warning(
LOG_TEMPLATE,
{
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ def setUp(self):
self.feature_flag = get(
Feature,
projects=[self.project],
feature_id=Feature.ENABLE_EXTERNAL_VERSION_BUILD,
feature_id=Feature.EXTERNAL_VERSION_BUILD,
)
self.version = get(
Version, slug='master', verbose_name='master',
Expand Down
3 changes: 2 additions & 1 deletion readthedocs/rtd_tests/tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def test_send_build_status_successful(self, session, mock_logger):

self.assertTrue(success)
mock_logger.info.assert_called_with(
'GitHub commit status for project: %s',
"GitHub '%s' commit status created for project: %s",
BUILD_STATUS_SUCCESS,
self.project
)

Expand Down
4 changes: 4 additions & 0 deletions readthedocs/templates/builds/build_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@
{% endif %}
</span>
<span class="build-commit" data-bind="visible: commit">
{% if build.get_commit_url %}
(<a data-bind="attr: {href: commit_url}"><span data-bind="text: commit">{{ build.commit }}</span></a>)
{% else %}
(<span data-bind="text: commit">{{ build.commit }}</span>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this break the JS code that tries to update commit_url if it doesn't exist? I don't know how knockout.js handles this, but we might also need to check for it on that side.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll check this :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked. there is no error on the browsers console window. and commit_url does always exist. but set to None when conditions are not met.

{% endif %}
</span>
</div>

Expand Down
11 changes: 5 additions & 6 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from readthedocs.builds.constants import EXTERNAL
from readthedocs.config import ALL
from readthedocs.projects.constants import GITHUB_GIT_PATTERN
from readthedocs.projects.constants import GITHUB_PR_PULL_PATTERN
from readthedocs.projects.exceptions import RepositoryError
from readthedocs.projects.validators import validate_submodule_url
from readthedocs.vcs_support.base import BaseVCS, VCSVersion
Expand Down Expand Up @@ -57,7 +57,6 @@ def update(self):
super().update()
if self.repo_exists():
self.set_remote_url(self.repo_url)
# A fetch is always required to get external versions properly
return self.fetch()
self.make_clean_working_dir()
# A fetch is always required to get external versions properly
Expand Down Expand Up @@ -154,18 +153,18 @@ def fetch(self):
cmd = ['git', 'fetch', 'origin',
'--tags', '--prune', '--prune-tags']

if self.use_shallow_clone():
cmd.extend(['--depth', str(self.repo_depth)])

if (
self.verbose_name and
self.version_type == EXTERNAL and
'github.com' in self.repo_url
):
cmd.append(
GITHUB_GIT_PATTERN.format(id=self.verbose_name)
GITHUB_PR_PULL_PATTERN.format(id=self.verbose_name)
)

if self.use_shallow_clone():
cmd.extend(['--depth', str(self.repo_depth)])

code, stdout, stderr = self.run(*cmd)
if code != 0:
raise RepositoryError
Expand Down