@@ -104,20 +104,21 @@ def get_version(version_pk):
104
104
version_data = api_v2 .version (version_pk ).get ()
105
105
return APIVersion (** version_data )
106
106
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
+ """
109
113
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 ,
115
116
verbose_name = self .version .verbose_name ,
116
117
version_type = self .version .type
117
118
)
118
119
return version_repo
119
120
120
- def sync_repo (self ):
121
+ def sync_repo (self , environment ):
121
122
"""Update the project's repository and hit ``sync_versions`` API."""
122
123
# Make Dirs
123
124
if not os .path .exists (self .project .doc_path ):
@@ -143,7 +144,7 @@ def sync_repo(self):
143
144
'msg' : msg ,
144
145
}
145
146
)
146
- version_repo = self .get_vcs_repo ()
147
+ version_repo = self .get_vcs_repo (environment )
147
148
version_repo .update ()
148
149
self .sync_versions (version_repo )
149
150
identifier = getattr (self , 'commit' , None ) or self .version .identifier
@@ -238,9 +239,18 @@ def run(self, version_pk): # pylint: disable=arguments-differ
238
239
try :
239
240
self .version = self .get_version (version_pk )
240
241
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
+
241
251
before_vcs .send (sender = self .version )
242
252
with self .project .repo_nonblockinglock (version = self .version ):
243
- self .sync_repo ()
253
+ self .sync_repo (environment )
244
254
return True
245
255
except RepositoryError :
246
256
# Do not log as ERROR handled exceptions
@@ -343,6 +353,7 @@ def __init__(
343
353
if config is not None :
344
354
self .config = config
345
355
self .task = task
356
+ # TODO: remove this
346
357
self .setup_env = None
347
358
348
359
# pylint: disable=arguments-differ
@@ -437,23 +448,28 @@ def run_setup(self, record=True):
437
448
438
449
Return True if successful.
439
450
"""
440
- self . setup_env = LocalBuildEnvironment (
451
+ environment = LocalBuildEnvironment (
441
452
project = self .project ,
442
453
version = self .version ,
443
454
build = self .build ,
444
455
record = record ,
445
456
update_on_success = False ,
446
457
)
447
458
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
+
448
464
# Environment used for code checkout & initial configuration reading
449
- with self . setup_env :
465
+ with environment :
450
466
try :
451
467
before_vcs .send (sender = self .version )
452
468
if self .project .skip :
453
469
raise ProjectBuildsSkippedError
454
470
try :
455
471
with self .project .repo_nonblockinglock (version = self .version ):
456
- self .setup_vcs ()
472
+ self .setup_vcs (environment )
457
473
except vcs_support_utils .LockTimeout as e :
458
474
self .task .retry (exc = e , throw = False )
459
475
raise VersionLockedError
@@ -467,13 +483,13 @@ def run_setup(self, record=True):
467
483
)
468
484
469
485
self .save_build_config ()
470
- self .additional_vcs_operations ()
486
+ self .additional_vcs_operations (environment )
471
487
finally :
472
488
after_vcs .send (sender = self .version )
473
489
474
- if self . setup_env .failure or self .config is None :
490
+ if environment .failure or self .config is None :
475
491
msg = 'Failing build because of setup failure: {}' .format (
476
- self . setup_env .failure ,
492
+ environment .failure ,
477
493
)
478
494
log .info (
479
495
LOG_TEMPLATE ,
@@ -488,23 +504,23 @@ def run_setup(self, record=True):
488
504
# of VersionLockedError: this exception occurs when a build is
489
505
# triggered before the previous one has finished (e.g. two webhooks,
490
506
# one after the other)
491
- if not isinstance (self . setup_env .failure , VersionLockedError ):
507
+ if not isinstance (environment .failure , VersionLockedError ):
492
508
self .send_notifications (self .version .pk , self .build ['id' ], email = True )
493
509
494
510
return False
495
511
496
- if self . setup_env .successful and not self .project .has_valid_clone :
512
+ if environment .successful and not self .project .has_valid_clone :
497
513
self .set_valid_clone ()
498
514
499
515
return True
500
516
501
- def additional_vcs_operations (self ):
517
+ def additional_vcs_operations (self , environment ):
502
518
"""
503
519
Execution of tasks that involve the project's VCS.
504
520
505
521
All this tasks have access to the configuration object.
506
522
"""
507
- version_repo = self .get_vcs_repo ()
523
+ version_repo = self .get_vcs_repo (environment )
508
524
if version_repo .supports_submodules :
509
525
version_repo .update_submodules (self .config )
510
526
@@ -660,13 +676,13 @@ def get_build(build_pk):
660
676
for key , val in build .items () if key not in private_keys
661
677
}
662
678
663
- def setup_vcs (self ):
679
+ def setup_vcs (self , environment ):
664
680
"""
665
681
Update the checkout of the repo to make sure it's the latest.
666
682
667
683
This also syncs versions in the DB.
668
684
"""
669
- self . setup_env .update_build (state = BUILD_STATE_CLONING )
685
+ environment .update_build (state = BUILD_STATE_CLONING )
670
686
671
687
log .info (
672
688
LOG_TEMPLATE ,
@@ -677,7 +693,7 @@ def setup_vcs(self):
677
693
}
678
694
)
679
695
try :
680
- self .sync_repo ()
696
+ self .sync_repo (environment )
681
697
except RepositoryError :
682
698
log .warning ('There was an error with the repository' , exc_info = True )
683
699
# Re raise the exception to stop the build at this point
0 commit comments