Skip to content

Commit 20533d4

Browse files
authored
Build details page: normalize/trim command paths (#9815)
* Build details page: normalize/trim command paths Small hack to trim/normalize command paths when displaying them to the user under the build detail's page. This is a simple hack and there are more we can do here. Probably, there are some edge cases this is not catching. However, the worst it can happens is the path not being trimmed. * Make regex to work locally and in production * pre-commit
1 parent b43e8d6 commit 20533d4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

readthedocs/api/v2/serializers.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Defines serializers for each of our models."""
22

3+
import re
4+
35
from allauth.socialaccount.models import SocialAccount
6+
from django.conf import settings
47
from rest_framework import serializers
58

69
from readthedocs.builds.models import Build, BuildCommandResult, Version
@@ -127,11 +130,41 @@ class VersionAdminSerializer(VersionSerializer):
127130
class BuildCommandSerializer(serializers.ModelSerializer):
128131

129132
run_time = serializers.ReadOnlyField()
133+
command = serializers.SerializerMethodField()
130134

131135
class Meta:
132136
model = BuildCommandResult
133137
exclude = []
134138

139+
def get_command(self, obj):
140+
# HACK: remove unreadable paths from the command outputs when returning it from the API.
141+
# We could make this change at build level, but we want to avoid undoable issues from now
142+
# and hack a small solution to fix the immediate problem.
143+
#
144+
# This converts:
145+
# $ /usr/src/app/checkouts/readthedocs.org/user_builds/
146+
# <container_hash>/<project_slug>/envs/<version_slug>/bin/python
147+
# $ /home/docs/checkouts/readthedocs.org/user_builds/
148+
# <project_slug>/envs/<version_slug>/bin/python
149+
# into
150+
# $ python
151+
project_slug = obj.build.version.project.slug
152+
version_slug = obj.build.version.slug
153+
docroot = settings.DOCROOT.rstrip("/") # remove trailing '/'
154+
155+
# Remove Docker hash from DOCROOT when running it locally
156+
# DOCROOT contains the Docker container hash (e.g. b7703d1b5854).
157+
# We have to remove it from the DOCROOT it self since it changes each time
158+
# we spin up a new Docker instance locally.
159+
container_hash = "/"
160+
if settings.RTD_DOCKER_COMPOSE:
161+
docroot = re.sub("/[0-9a-z]+/?$", "", settings.DOCROOT, count=1)
162+
container_hash = "/[0-9a-z]+/"
163+
164+
regex = f"{docroot}{container_hash}{project_slug}/envs/{version_slug}(/bin/)?"
165+
command = re.sub(regex, "", obj.command, count=1)
166+
return command
167+
135168

136169
class BuildSerializer(serializers.ModelSerializer):
137170

0 commit comments

Comments
 (0)