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):