Skip to content

Commit ba3f7e2

Browse files
committed
Be explicit when using setup_env
Currently when `sync_repository_task` gets triggered we use a LocalEnvironment as environment. We want to be explicit about respecting the docker setting in all places where we do a clone. Also, it's really confusing depending on the order of calls to have self.setup_env set. Wasn't able to remove it completely, we still use it outside the function that creates the environment.
1 parent f20832c commit ba3f7e2

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

readthedocs/projects/tasks.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,21 @@ def get_version(version_pk):
104104
version_data = api_v2.version(version_pk).get()
105105
return APIVersion(**version_data)
106106

107-
def get_vcs_repo(self):
108-
"""Get the VCS object of the current project."""
107+
def get_vcs_repo(self, environment):
108+
"""
109+
Get the VCS object of the current project.
110+
111+
All VCS commands will be executed using `environment`.
112+
"""
109113
version_repo = self.project.vcs_repo(
110-
self.version.slug,
111-
# When called from ``SyncRepositoryTask.run`` we don't have
112-
# a ``setup_env`` so we use just ``None`` and commands won't
113-
# be recorded
114-
getattr(self, 'setup_env', None),
114+
version=self.version.slug,
115+
environment=environment,
115116
verbose_name=self.version.verbose_name,
116117
version_type=self.version.type
117118
)
118119
return version_repo
119120

120-
def sync_repo(self):
121+
def sync_repo(self, environment):
121122
"""Update the project's repository and hit ``sync_versions`` API."""
122123
# Make Dirs
123124
if not os.path.exists(self.project.doc_path):
@@ -143,7 +144,7 @@ def sync_repo(self):
143144
'msg': msg,
144145
}
145146
)
146-
version_repo = self.get_vcs_repo()
147+
version_repo = self.get_vcs_repo(environment)
147148
version_repo.update()
148149
self.sync_versions(version_repo)
149150
identifier = getattr(self, 'commit', None) or self.version.identifier
@@ -238,9 +239,18 @@ def run(self, version_pk): # pylint: disable=arguments-differ
238239
try:
239240
self.version = self.get_version(version_pk)
240241
self.project = self.version.project
242+
243+
environment = LocalBuildEnvironment(
244+
project=self.project,
245+
version=self.version,
246+
build=self.build,
247+
record=False,
248+
update_on_success=False,
249+
)
250+
241251
before_vcs.send(sender=self.version)
242252
with self.project.repo_nonblockinglock(version=self.version):
243-
self.sync_repo()
253+
self.sync_repo(environment)
244254
return True
245255
except RepositoryError:
246256
# Do not log as ERROR handled exceptions
@@ -343,6 +353,7 @@ def __init__(
343353
if config is not None:
344354
self.config = config
345355
self.task = task
356+
# TODO: remove this
346357
self.setup_env = None
347358

348359
# pylint: disable=arguments-differ
@@ -437,23 +448,28 @@ def run_setup(self, record=True):
437448
438449
Return True if successful.
439450
"""
440-
self.setup_env = LocalBuildEnvironment(
451+
environment = LocalBuildEnvironment(
441452
project=self.project,
442453
version=self.version,
443454
build=self.build,
444455
record=record,
445456
update_on_success=False,
446457
)
447458

459+
# TODO: Remove.
460+
# There is code that still depends of this attribute
461+
# outside this function. Don't use self.setup_env for new code.
462+
self.setup_env = environment
463+
448464
# Environment used for code checkout & initial configuration reading
449-
with self.setup_env:
465+
with environment:
450466
try:
451467
before_vcs.send(sender=self.version)
452468
if self.project.skip:
453469
raise ProjectBuildsSkippedError
454470
try:
455471
with self.project.repo_nonblockinglock(version=self.version):
456-
self.setup_vcs()
472+
self.setup_vcs(environment)
457473
except vcs_support_utils.LockTimeout as e:
458474
self.task.retry(exc=e, throw=False)
459475
raise VersionLockedError
@@ -467,13 +483,13 @@ def run_setup(self, record=True):
467483
)
468484

469485
self.save_build_config()
470-
self.additional_vcs_operations()
486+
self.additional_vcs_operations(environment)
471487
finally:
472488
after_vcs.send(sender=self.version)
473489

474-
if self.setup_env.failure or self.config is None:
490+
if environment.failure or self.config is None:
475491
msg = 'Failing build because of setup failure: {}'.format(
476-
self.setup_env.failure,
492+
environment.failure,
477493
)
478494
log.info(
479495
LOG_TEMPLATE,
@@ -488,23 +504,23 @@ def run_setup(self, record=True):
488504
# of VersionLockedError: this exception occurs when a build is
489505
# triggered before the previous one has finished (e.g. two webhooks,
490506
# one after the other)
491-
if not isinstance(self.setup_env.failure, VersionLockedError):
507+
if not isinstance(environment.failure, VersionLockedError):
492508
self.send_notifications(self.version.pk, self.build['id'], email=True)
493509

494510
return False
495511

496-
if self.setup_env.successful and not self.project.has_valid_clone:
512+
if environment.successful and not self.project.has_valid_clone:
497513
self.set_valid_clone()
498514

499515
return True
500516

501-
def additional_vcs_operations(self):
517+
def additional_vcs_operations(self, environment):
502518
"""
503519
Execution of tasks that involve the project's VCS.
504520
505521
All this tasks have access to the configuration object.
506522
"""
507-
version_repo = self.get_vcs_repo()
523+
version_repo = self.get_vcs_repo(environment)
508524
if version_repo.supports_submodules:
509525
version_repo.update_submodules(self.config)
510526

@@ -660,13 +676,13 @@ def get_build(build_pk):
660676
for key, val in build.items() if key not in private_keys
661677
}
662678

663-
def setup_vcs(self):
679+
def setup_vcs(self, environment):
664680
"""
665681
Update the checkout of the repo to make sure it's the latest.
666682
667683
This also syncs versions in the DB.
668684
"""
669-
self.setup_env.update_build(state=BUILD_STATE_CLONING)
685+
environment.update_build(state=BUILD_STATE_CLONING)
670686

671687
log.info(
672688
LOG_TEMPLATE,
@@ -677,7 +693,7 @@ def setup_vcs(self):
677693
}
678694
)
679695
try:
680-
self.sync_repo()
696+
self.sync_repo(environment)
681697
except RepositoryError:
682698
log.warning('There was an error with the repository', exc_info=True)
683699
# Re raise the exception to stop the build at this point

0 commit comments

Comments
 (0)