From 89eba2b8555db6eca48698fc9ad98e1c9a2fcfa1 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Thu, 2 Nov 2023 11:28:47 -0500 Subject: [PATCH] Version: don't use default_branch when getting the commit name The name of the branch used is already in the version itself, we don't need to depend on the project. This doesn't work on the builders, because we use the APIProject object, which doesn't have access to the DB nor has the remote_repository relationship in it. https://github.com/readthedocs/readthedocs.org/blob/fe7e976dbf989f1eee04368df3427086edfb89c9/readthedocs/projects/models.py#L1262-L1263 --- readthedocs/builds/models.py | 8 ++--- .../projects/tests/test_build_tasks.py | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/readthedocs/builds/models.py b/readthedocs/builds/models.py index e782f52599f..b6e166c38a5 100644 --- a/readthedocs/builds/models.py +++ b/readthedocs/builds/models.py @@ -341,10 +341,10 @@ def commit_name(self): """ # LATEST is special as it is usually a branch but does not contain the # name in verbose_name. - if self.slug == LATEST: - return self.project.get_default_branch() + if self.slug == LATEST and self.machine: + return self.identifier - if self.slug == STABLE: + if self.slug == STABLE and self.machine: if self.type == BRANCH: # Special case, as we do not store the original branch name # that the stable version works on. We can only interpolate the @@ -356,7 +356,7 @@ def commit_name(self): return self.identifier # By now we must have handled all special versions. - if self.slug in NON_REPOSITORY_VERSIONS: + if self.machine and self.slug in NON_REPOSITORY_VERSIONS: # pylint: disable=broad-exception-raised raise Exception('All special versions must be handled by now.') diff --git a/readthedocs/projects/tests/test_build_tasks.py b/readthedocs/projects/tests/test_build_tasks.py index 0bd577827df..5ec565bc03c 100644 --- a/readthedocs/projects/tests/test_build_tasks.py +++ b/readthedocs/projects/tests/test_build_tasks.py @@ -694,6 +694,35 @@ def test_failed_build( assert revoke_key_request._request.method == "POST" assert revoke_key_request.path == "/api/v2/revoke/" + @mock.patch.object(Backend, "ref_exists") + @mock.patch("readthedocs.doc_builder.director.load_yaml_config") + def test_checkout_latest(self, load_yaml_config, ref_exists): + ref_exists.return_value = False + self.version.identifier = "main" + self.version.save() + load_yaml_config.return_value = get_build_config({}) + + self._trigger_update_docs_task(build_commit=None) + + self.mocker.mocks["git.Backend.run"].assert_has_calls( + [ + mock.call("git", "clone", "--depth", "1", mock.ANY, "."), + mock.call( + "git", + "fetch", + "origin", + "--force", + "--prune", + "--prune-tags", + "--depth", + "50", + "refs/heads/main:refs/remotes/origin/main", + ), + mock.call("git", "checkout", "--force", "main"), + mock.call("git", "clean", "-d", "-f", "-f"), + ] + ) + @mock.patch.object(Backend, "ref_exists") @mock.patch("readthedocs.doc_builder.director.load_yaml_config") def test_checkout_tag_by_name(self, load_yaml_config, ref_exists):