Skip to content

Commit 4b3727a

Browse files
authored
API: don't rely on the version object for builds (#12395)
If a version is deleted, we still keep its builds around, so we can't rely on the version being present. Ref https://read-the-docs.sentry.io/issues/3961831199/?project=148442
1 parent 4286c03 commit 4b3727a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

readthedocs/api/v2/serializers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ class BuildCommandReadOnlySerializer(BuildCommandSerializer):
226226
command = serializers.SerializerMethodField()
227227

228228
def get_command(self, obj):
229-
return normalize_build_command(obj.command, obj.build.project.slug, obj.build.version.slug)
229+
return normalize_build_command(
230+
obj.command, obj.build.project.slug, obj.build.get_version_slug()
231+
)
230232

231233

232234
class BuildSerializer(serializers.ModelSerializer):

readthedocs/api/v2/views/model_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def retrieve(self, *args, **kwargs):
355355
buildcommand["command"] = normalize_build_command(
356356
buildcommand["command"],
357357
instance.project.slug,
358-
instance.version.slug,
358+
instance.get_version_slug(),
359359
)
360360
except Exception:
361361
log.exception(

readthedocs/rtd_tests/tests/test_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,29 @@ def test_build_filter_by_commit(self):
707707
build = resp.data
708708
self.assertEqual(len(build["results"]), 1)
709709

710+
def test_build_without_version(self):
711+
build = get(
712+
Build,
713+
project=self.project,
714+
version=None,
715+
state=BUILD_STATE_FINISHED,
716+
exit_code=0,
717+
)
718+
command = "python -m pip install --upgrade --no-cache-dir pip setuptools<58.3.0"
719+
get(
720+
BuildCommandResult,
721+
build=build,
722+
command=command,
723+
output="Running...",
724+
exit_code=0,
725+
)
726+
client = APIClient()
727+
client.force_authenticate(user=self.user)
728+
r = client.get(reverse("build-detail", args=(build.pk,)))
729+
assert r.status_code == 200
730+
assert r.data["version"] is None
731+
assert r.data["commands"][0]["command"] == command
732+
710733

711734
class APITests(TestCase):
712735
fixtures = ["eric.json", "test_data.json"]

0 commit comments

Comments
 (0)