Skip to content

Add ability to rebuild a specific build #8227

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

Merged
merged 19 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
13a9e7c
Add ability to rebuild a specific build
ericholscher Apr 29, 2020
d0e715c
Update readthedocs/core/utils/__init__.py
ericholscher Apr 30, 2020
1f74b9a
Update readthedocs/builds/views.py
ericholscher May 18, 2020
3a6d1e6
Fix rebuild link display
ericholscher May 18, 2020
a2c731a
Add test for building by commit
ericholscher May 18, 2020
423c3d8
Add a BuildCommandResult for retriggering ot make the UI nicer
ericholscher May 18, 2020
5d51f58
Merge branch 'master' of github.com:readthedocs/readthedocs.org into …
ericholscher Jun 23, 2020
0a0221e
Use new build status to convey retriggered state
ericholscher Jun 23, 2020
57ee3dc
Merge branch 'master' of github.com:readthedocs/readthedocs.org into …
ericholscher Aug 8, 2020
53cc567
Merge remote-tracking branch 'origin/master' into rebuild-builds
humitos Jun 2, 2021
2fe7612
"Rebuild this build" by creating a new `Build` object
humitos Jun 2, 2021
8ed2e44
Show rebuild button only on latest build for external versions
humitos Jun 3, 2021
5498605
Check the build is the latest for that version
humitos Jun 3, 2021
1183f99
Count builds properly on test
humitos Jun 3, 2021
1e349ca
Show a user's notification if the build can't be retriggered
humitos Jun 7, 2021
5f663d4
Move "build_to_retrigger" block behind the "if build_pk" conditional
humitos Jun 7, 2021
2f9244e
Small refactor to reuse the build variable and reduce 1 db query
humitos Jun 8, 2021
0db1c8f
Fix tests when triggering an invalid build
humitos Jun 8, 2021
4db6b34
Lint
humitos Jun 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions readthedocs/builds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import (
HttpResponseBadRequest,
HttpResponseForbidden,
HttpResponseRedirect,
)
Expand Down Expand Up @@ -54,14 +55,49 @@ def post(self, request, project_slug):
return HttpResponseForbidden()

version_slug = request.POST.get('version_slug')
version = get_object_or_404(
self._get_versions(project),
slug=version_slug,
)
build_pk = request.POST.get('build_pk')

if build_pk:
# Filter over external versions only when re-triggering a specific build
version = get_object_or_404(
Version.external.public(self.request.user),
slug=version_slug,
project=project,
)

build = get_object_or_404(
Build.objects.all(),
pk=build_pk,
version=version,
)
if build != Build.objects.filter(version=version).first():
# Return bad request if the build re-triggered is not the
# latest for that version
return HttpResponseBadRequest()
else:
# Use generic query when triggering a normal build
version = get_object_or_404(
self._get_versions(project),
slug=version_slug,
)

# Set either the build to re-trigger it or None
build_to_retrigger = version.builds.filter(pk=build_pk).first()
commit_to_retrigger = None
if build_to_retrigger:
commit_to_retrigger = build_to_retrigger.commit
log.info(
'Re-triggering build. project=%s version=%s commit=%s build=%s',
project.slug,
version.slug,
build_to_retrigger.commit,
build_to_retrigger.pk
)

update_docs_task, build = trigger_build(
project=project,
version=version,
commit=commit_to_retrigger,
)
if (update_docs_task, build) == (None, None):
# Build was skipped
Expand Down Expand Up @@ -111,6 +147,13 @@ def get_context_data(self, **kwargs):
context['project'] = self.project

build = self.get_object()
context['is_latest_build'] = (
build == Build.objects.filter(
project=build.project,
version=build.version,
).first()
)

if build.error != BuildEnvironmentError.GENERIC_WITH_BUILD_ID.format(build_id=build.pk):
# Do not suggest to open an issue if the error is not generic
return context
Expand Down
13 changes: 6 additions & 7 deletions readthedocs/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from django.utils.text import slugify as slugify_base

from readthedocs.builds.constants import (
BUILD_STATE_FINISHED,
BUILD_STATE_TRIGGERED,
BUILD_STATE_FINISHED,
BUILD_STATUS_PENDING,
EXTERNAL,
)
Expand Down Expand Up @@ -65,7 +65,6 @@ def prepare_build(
)

build = None

if not Project.objects.is_active(project):
log.warning(
'Build not triggered because Project is not active: project=%s',
Expand Down Expand Up @@ -234,11 +233,11 @@ def trigger_build(project, version=None, commit=None, record=True, force=False):
commit,
)
update_docs_task, build = prepare_build(
project,
version,
commit,
record,
force,
project=project,
version=version,
commit=commit,
record=record,
force=force,
immutable=True,
)

Expand Down
65 changes: 65 additions & 0 deletions readthedocs/rtd_tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,71 @@ def test_build_list_includes_external_versions(self):
self.assertEqual(response.status_code, 200)
self.assertIn(external_version_build, response.context['build_qs'])

@mock.patch('readthedocs.projects.tasks.update_docs_task')
def test_rebuild_specific_commit(self, mock):
builds_count = Build.objects.count()

version = self.pip.versions.first()
version.type = 'external'
version.save()

build = get(
Build,
version=version,
project=self.pip,
commit='a1b2c3',
)

r = self.client.post(
'/projects/pip/builds/',
{
'version_slug': version.slug,
'build_pk': build.pk,
}
)

self.assertEqual(r.status_code, 302)
self.assertEqual(Build.objects.count(), builds_count + 2)

newbuild = Build.objects.first()
self.assertEqual(
r._headers['location'][1],
f'/projects/pip/builds/{newbuild.pk}/',
)
self.assertEqual(newbuild.commit, 'a1b2c3')


@mock.patch('readthedocs.projects.tasks.update_docs_task')
def test_rebuild_invalid_specific_commit(self, mock):
version = self.pip.versions.first()
version.type = 'external'
version.save()

for i in range(3):
get(
Build,
version=version,
project=self.pip,
commit=f'a1b2c3-{i}',
)

build = Build.objects.filter(
version=version,
project=self.pip,
).last()

r = self.client.post(
'/projects/pip/builds/',
{
'version_slug': version.slug,
'build_pk': build.pk,
}
)

# It should return 400 because we are re-triggering a build of a
# non-lastest build for that version
self.assertEqual(r.status_code, 400)


class TestSearchAnalyticsView(TestCase):

Expand Down
17 changes: 17 additions & 0 deletions readthedocs/templates/builds/build_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@
</a>
</li>
</div>


{# Show rebuild button only if the version is external and it's the latest build for this version #}
{# see https://github.com/readthedocs/readthedocs.org/pull/6995#issuecomment-852918969 #}
{% if request.user|is_admin:project and build.version.type == "external" and is_latest_build %}
Copy link
Member

Choose a reason for hiding this comment

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

Not related to this PR, but I remember is_admin: project being expensive, we are using the check 3 times now. We can move it to the context instead.

context['is_project_admin'] = AdminPermission.is_admin(
self.request.user,
project,
)

<div data-bind="visible: finished()">
<form method="post" name="rebuild_commit" action="{% url "builds_project_list" project.slug %}">
Copy link
Member

Choose a reason for hiding this comment

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

Feels weird having this on the same list view, I think we could have another view /builds/{pk}/trigger/ or similar.

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 think this is a common pattern: POST to a list view to create a new object. We are using this in the same way when triggering a build for a particular version on Build List page at

<form method="post" action="{% url "builds_project_list" project.slug %}">

Seems fine to me to re-use the view and just pass build_pk to trigger a build for a particular version at a specific point in time.

{% csrf_token %}
<a href="#" onclick="document.forms['rebuild_commit'].submit();">
Rebuild this build
</a>
<input type="hidden" name="version_slug" value="{{ build.version.slug }}">
<input type="hidden" name="build_pk" value="{{ build.pk }}">
</form>
</div>
{% endif %}

</ul>

<div class="build-id">
Expand Down