Skip to content

Commit 42a82ca

Browse files
authored
Build: use tag name for checkout (#10879)
* Build: use tag name for checkout * Comment
1 parent 996a5b2 commit 42a82ca

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

readthedocs/doc_builder/director.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,12 @@ def checkout(self):
225225
log.info("Cloning and fetching.")
226226
self.vcs_repository.update()
227227

228-
identifier = self.data.build_commit or self.data.version.identifier
228+
# NOTE: we use `commit_name` instead of `identifier`,
229+
# since identifier can be a ref name or a commit hash,
230+
# and we want to use the ref name when doing the checkout when possible
231+
# (e.g. `main` instead of `a1b2c3d4`, or `v1.0` instead of `a1b2c3d4`).
232+
# See https://github.com/readthedocs/readthedocs.org/issues/10838.
233+
identifier = self.data.build_commit or self.data.version.commit_name
229234
log.info("Checking out.", identifier=identifier)
230235
self.vcs_repository.checkout(identifier)
231236

readthedocs/projects/tests/test_build_tasks.py

+73-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
BUILD_STATUS_FAILURE,
1212
BUILD_STATUS_SUCCESS,
1313
EXTERNAL,
14+
TAG,
1415
)
1516
from readthedocs.builds.models import Build
1617
from readthedocs.config import ALL, ConfigError
@@ -21,6 +22,7 @@
2122
from readthedocs.projects.models import EnvironmentVariable, Project, WebHookEvent
2223
from readthedocs.projects.tasks.builds import sync_repository_task, update_docs_task
2324
from readthedocs.telemetry.models import BuildData
25+
from readthedocs.vcs_support.backends.git import Backend
2426

2527
from .mockers import BuildEnvironmentMocker
2628

@@ -60,17 +62,19 @@ def _get_project(self):
6062
return fixture.get(
6163
Project,
6264
slug="project",
65+
repo="https://github.com/readthedocs/readthedocs.org",
6366
enable_epub_build=True,
6467
enable_pdf_build=True,
6568
)
6669

67-
def _trigger_update_docs_task(self):
70+
def _trigger_update_docs_task(self, **kwargs):
6871
# NOTE: is it possible to replace calling this directly by `trigger_build` instead? :)
72+
kwargs.setdefault("build_api_key", "1234")
73+
kwargs.setdefault("build_commit", self.build.commit)
6974
return update_docs_task.delay(
7075
self.version.pk,
7176
self.build.pk,
72-
build_api_key="1234",
73-
build_commit=self.build.commit,
77+
**kwargs,
7478
)
7579

7680
class TestCustomConfigFile(BuildEnvironmentBase):
@@ -690,6 +694,72 @@ def test_failed_build(
690694
assert revoke_key_request._request.method == "POST"
691695
assert revoke_key_request.path == "/api/v2/revoke/"
692696

697+
@mock.patch.object(Backend, "ref_exists")
698+
@mock.patch("readthedocs.doc_builder.director.load_yaml_config")
699+
def test_checkout_tag_by_name(self, load_yaml_config, ref_exists):
700+
ref_exists.return_value = False
701+
self.version.type = TAG
702+
self.version.identifier = "abc123"
703+
self.version.verbose_name = "v1.0"
704+
self.version.slug = "v1.0"
705+
self.machine = False
706+
self.version.save()
707+
load_yaml_config.return_value = get_build_config({})
708+
709+
self._trigger_update_docs_task(build_commit=None)
710+
711+
self.mocker.mocks["git.Backend.run"].assert_has_calls(
712+
[
713+
mock.call("git", "clone", "--depth", "1", mock.ANY, "."),
714+
mock.call(
715+
"git",
716+
"fetch",
717+
"origin",
718+
"--force",
719+
"--prune",
720+
"--prune-tags",
721+
"--depth",
722+
"50",
723+
"refs/tags/v1.0:refs/tags/v1.0",
724+
),
725+
mock.call("git", "checkout", "--force", "v1.0"),
726+
mock.call("git", "clean", "-d", "-f", "-f"),
727+
]
728+
)
729+
730+
@mock.patch.object(Backend, "ref_exists")
731+
@mock.patch("readthedocs.doc_builder.director.load_yaml_config")
732+
def test_checkout_external_version_by_commit(self, load_yaml_config, ref_exists):
733+
ref_exists.return_value = False
734+
self.version.type = EXTERNAL
735+
self.version.identifier = "abc123"
736+
self.version.verbose_name = "22"
737+
self.version.slug = "22"
738+
self.machine = False
739+
self.version.save()
740+
load_yaml_config.return_value = get_build_config({})
741+
742+
self._trigger_update_docs_task(build_commit=None)
743+
744+
self.mocker.mocks["git.Backend.run"].assert_has_calls(
745+
[
746+
mock.call("git", "clone", "--depth", "1", mock.ANY, "."),
747+
mock.call(
748+
"git",
749+
"fetch",
750+
"origin",
751+
"--force",
752+
"--prune",
753+
"--prune-tags",
754+
"--depth",
755+
"50",
756+
"pull/22/head:external-22",
757+
),
758+
mock.call("git", "checkout", "--force", "abc123"),
759+
mock.call("git", "clean", "-d", "-f", "-f"),
760+
]
761+
)
762+
693763
@mock.patch("readthedocs.doc_builder.director.load_yaml_config")
694764
def test_build_commands_executed(
695765
self,

0 commit comments

Comments
 (0)