| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from buildbot.status import builder as build_results | 5 from buildbot.status import builder as build_results |
| 6 from buildbot.status.base import StatusReceiverMultiService | 6 from buildbot.status.base import StatusReceiverMultiService |
| 7 from twisted.internet.defer import inlineCallbacks, returnValue | 7 from twisted.internet.defer import inlineCallbacks, returnValue |
| 8 | 8 |
| 9 from . import common | 9 from . import common |
| 10 from .buildbot_gateway import BuildbotGateway | 10 from .buildbot_gateway import BuildbotGateway |
| 11 | 11 |
| 12 | 12 |
| 13 BUILD_STATUS_NAMES = { | 13 BUILD_STATUS_NAMES = { |
| 14 build_results.EXCEPTION: 'EXCEPTION', | 14 build_results.EXCEPTION: 'EXCEPTION', |
| 15 build_results.FAILURE: 'FAILURE', | 15 build_results.FAILURE: 'FAILURE', |
| 16 build_results.RETRY: 'RETRY', | 16 build_results.RETRY: 'RETRY', |
| 17 build_results.SKIPPED: 'SKIPPED', | 17 build_results.SKIPPED: 'SKIPPED', |
| 18 build_results.SUCCESS: 'SUCCESS', | 18 build_results.SUCCESS: 'SUCCESS', |
| 19 build_results.WARNINGS: 'SUCCESS', # Treat warnings as SUCCESS. | 19 build_results.WARNINGS: 'SUCCESS', # Treat warnings as SUCCESS. |
| 20 } | 20 } |
| 21 | 21 |
| 22 | 22 |
| 23 class BuildBucketStatus(StatusReceiverMultiService): | 23 class BuildBucketStatus(StatusReceiverMultiService): |
| 24 """Updates build status on buildbucket.""" | 24 """Updates build status on buildbucket.""" |
| 25 | 25 |
| 26 def __init__(self, integrator, buildbucket_service_factory, dry_run): | 26 def __init__(self, integrator, buildbucket_service, dry_run): |
| 27 """Creates a new BuildBucketStatus. | 27 """Creates a new BuildBucketStatus. |
| 28 | 28 |
| 29 Args: | 29 Args: |
| 30 integrator (BuildBucketIntegrator): integrator to notify about status | 30 integrator (BuildBucketIntegrator): integrator to notify about status |
| 31 changes. | 31 changes. |
| 32 buildbucket_service_factory (function): returns a DeferredResource as | 32 buildbucket_service (DeferredResource): buildbucket API client. |
| 33 Deferred that will be used to access buildbucket service API. | |
| 34 dry_run (bool): if True, do not start integrator. | 33 dry_run (bool): if True, do not start integrator. |
| 35 """ | 34 """ |
| 36 StatusReceiverMultiService.__init__(self) | 35 StatusReceiverMultiService.__init__(self) |
| 37 self.integrator = integrator | 36 self.integrator = integrator |
| 38 self.buildbucket_service_factory = buildbucket_service_factory | 37 self.buildbucket_service = buildbucket_service |
| 39 self.dry_run = dry_run | 38 self.dry_run = dry_run |
| 40 self.integrator_starting = None | 39 self.integrator_starting = None |
| 41 | 40 |
| 42 @inlineCallbacks | |
| 43 def _start_integrator(self): | |
| 44 buildbucket_service = yield self.buildbucket_service_factory() | |
| 45 buildbot = BuildbotGateway(self.parent) | |
| 46 self.integrator.start(buildbot, buildbucket_service) | |
| 47 | |
| 48 def _run_when_started(self, fn, *args): | |
| 49 assert self.integrator_starting | |
| 50 d = self.integrator_starting.addCallback(lambda _: fn(*args)) | |
| 51 common.log_on_error(d) | |
| 52 | |
| 53 def startService(self): | 41 def startService(self): |
| 54 StatusReceiverMultiService.startService(self) | 42 StatusReceiverMultiService.startService(self) |
| 55 if self.dry_run: | 43 if self.dry_run: |
| 56 return | 44 return |
| 57 self.integrator_starting = self._start_integrator() | 45 |
| 58 common.log_on_error(self.integrator_starting, 'Could not start integrator') | 46 buildbot = BuildbotGateway(self.parent) |
| 59 self._run_when_started(self.integrator.poll_builds) | 47 self.integrator.start(buildbot, self.buildbucket_service) |
| 48 self.integrator.poll_builds() |
| 60 self.parent.getStatus().subscribe(self) | 49 self.parent.getStatus().subscribe(self) |
| 61 | 50 |
| 62 def stopService(self): | 51 def stopService(self): |
| 63 self.integrator.stop() | 52 self.integrator.stop() |
| 64 StatusReceiverMultiService.stopService(self) | 53 StatusReceiverMultiService.stopService(self) |
| 65 | 54 |
| 66 # pylint: disable=W0613 | 55 # pylint: disable=W0613 |
| 67 def builderAdded(self, name, builder): | 56 def builderAdded(self, name, builder): |
| 68 # Subscribe to this builder. | 57 # Subscribe to this builder. |
| 69 return self | 58 return self |
| 70 | 59 |
| 71 def buildStarted(self, builder_name, build): | 60 def buildStarted(self, builder_name, build): |
| 72 if self.dry_run: | 61 if self.dry_run: |
| 73 return | 62 return |
| 74 self._run_when_started(self.integrator.on_build_started, build) | 63 self.integrator.on_build_started(build) |
| 75 | 64 |
| 76 def buildFinished(self, builder_name, build, result): | 65 def buildFinished(self, builder_name, build, result): |
| 77 if self.dry_run: | 66 if self.dry_run: |
| 78 return | 67 return |
| 79 assert result in BUILD_STATUS_NAMES | 68 assert result in BUILD_STATUS_NAMES |
| 80 status = BUILD_STATUS_NAMES[result] | 69 status = BUILD_STATUS_NAMES[result] |
| 81 self._run_when_started(self.integrator.on_build_finished, build, status) | 70 self.integrator.on_build_finished(build, status) |
| OLD | NEW |