| 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 """ChangeStore maps buildbucket changes to Buildbot changes.""" | 5 """ChangeStore maps buildbucket changes to Buildbot changes.""" |
| 6 | 6 |
| 7 from master.buildbucket import common | 7 from master.buildbucket import common |
| 8 from twisted.internet import defer | 8 from twisted.internet import defer |
| 9 | 9 |
| 10 | 10 |
| 11 CHANGE_CACHE_NAME = 'buildbucket_changes' | |
| 12 | |
| 13 | |
| 14 class ChangeStore(object): | 11 class ChangeStore(object): |
| 15 """Maps buildbucket changes to Buildbot changes.""" | 12 """Maps buildbucket changes to Buildbot changes.""" |
| 16 | 13 |
| 17 def __init__(self, buildbot_gateway): | 14 def __init__(self, buildbot_gateway, unique_urls=False): |
| 15 """Initializes a ChangeStore. |
| 16 |
| 17 If unique_urls is True, treats change's non-empty url as change identifier. |
| 18 It should be False on Rietveld-based tryservers because url may mean base |
| 19 revision where a Rietveld patchset must be applied to. |
| 20 """ |
| 18 assert buildbot_gateway | 21 assert buildbot_gateway |
| 19 self.buildbot = buildbot_gateway | 22 self.buildbot = buildbot_gateway |
| 20 self._find_change_cache = self.buildbot.get_cache( | 23 self.unique_urls = unique_urls |
| 21 CHANGE_CACHE_NAME, | 24 self._find_change_cache_by_id = self.buildbot.get_cache( |
| 22 self._find_change_in_db | 25 'buildbucket_changes', |
| 26 self._find_change_in_db_by_id, |
| 27 ) |
| 28 self._find_change_cache_by_revlink = self.buildbot.get_cache( |
| 29 'buildbucket_changes_revlink', |
| 30 self._find_change_in_db_by_revlink, |
| 23 ) | 31 ) |
| 24 | 32 |
| 25 def _insert_change(self, buildbucket_change): | 33 def _insert_change(self, buildbucket_change): |
| 26 """Inserts a new Change object to the buildbot. | 34 """Inserts a new Change object to the buildbot. |
| 27 | 35 |
| 28 Args: | 36 Args: |
| 29 buildbucket_change (dict): a change described in build parameters. | 37 buildbucket_change (dict): a change described in build parameters. |
| 30 | 38 |
| 31 Returns: | 39 Returns: |
| 32 Buildbot change id as Deferred. | 40 Buildbot change id as Deferred. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 50 category=common.CHANGE_CATEGORY, | 58 category=common.CHANGE_CATEGORY, |
| 51 revlink=buildbucket_change.get('url') or '', | 59 revlink=buildbucket_change.get('url') or '', |
| 52 properties={ | 60 properties={ |
| 53 common.INFO_PROPERTY: (info, 'Change'), | 61 common.INFO_PROPERTY: (info, 'Change'), |
| 54 }, | 62 }, |
| 55 repository=buildbucket_change.get('repo_url') or '', | 63 repository=buildbucket_change.get('repo_url') or '', |
| 56 project=buildbucket_change.get('project') or '', | 64 project=buildbucket_change.get('project') or '', |
| 57 ) | 65 ) |
| 58 | 66 |
| 59 @defer.inlineCallbacks | 67 @defer.inlineCallbacks |
| 60 def _find_change_in_db(self, revision_and_id): | 68 def _find_change_in_db_by_id(self, revision_and_id): |
| 61 """Searches for an existing Change object in the database. | 69 """Searches for an existing Change object in the database. |
| 62 | 70 |
| 63 Args: | 71 Args: |
| 64 revision_and_id: a tuple of change revision and buildbucket-level change | 72 revision_and_id: a tuple of change revision and buildbucket-level change |
| 65 id. | 73 id. |
| 66 | 74 |
| 67 Every Change object has a revision attribute. The buildbucket-level change | 75 Every Change object has a revision attribute. The buildbucket-level change |
| 68 id is stored as common.INFO_PROPERTY/common.BUILDBUCKET_CHANGE_ID_PROPERTY | 76 id is stored as common.INFO_PROPERTY/common.BUILDBUCKET_CHANGE_ID_PROPERTY |
| 69 property. | 77 property. |
| 70 | 78 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 81 for buildbot_change_id in buildbot_change_ids: | 89 for buildbot_change_id in buildbot_change_ids: |
| 82 change = yield self.buildbot.get_change_by_id(buildbot_change_id) | 90 change = yield self.buildbot.get_change_by_id(buildbot_change_id) |
| 83 info = change.properties.getProperty(common.INFO_PROPERTY) | 91 info = change.properties.getProperty(common.INFO_PROPERTY) |
| 84 if info is None: | 92 if info is None: |
| 85 continue | 93 continue |
| 86 prop_value = info.get(common.BUILDBUCKET_CHANGE_ID_PROPERTY) | 94 prop_value = info.get(common.BUILDBUCKET_CHANGE_ID_PROPERTY) |
| 87 if str(prop_value) == str(buildbucket_change_id): | 95 if str(prop_value) == str(buildbucket_change_id): |
| 88 defer.returnValue(change) | 96 defer.returnValue(change) |
| 89 return | 97 return |
| 90 | 98 |
| 91 def _find_change(self, revision, buildbucket_change_id): | 99 @defer.inlineCallbacks |
| 92 """Searches for a Change by revision and change_key. Uses cache.""" | 100 def _find_change_in_db_by_revlink(self, revlink): |
| 93 assert revision | 101 """Searches for one Change object by revlink in the database. |
| 94 assert buildbucket_change_id | 102 |
| 95 # Use (revision, buildbucket_change_id) tuple as cache key. | 103 Returns: |
| 96 return self._find_change_cache.get((revision, buildbucket_change_id)) | 104 buildbot.changes.change.Change object as Deferred. |
| 105 """ |
| 106 buildbot_change_ids = yield self.buildbot.find_changes_by_revlink(revlink) |
| 107 if buildbot_change_ids: |
| 108 change = yield self.buildbot.get_change_by_id(buildbot_change_ids[0]) |
| 109 defer.returnValue(change) |
| 97 | 110 |
| 98 @defer.inlineCallbacks | 111 @defer.inlineCallbacks |
| 99 def get_change(self, buildbucket_change): | 112 def get_change(self, buildbucket_change): |
| 100 """Returns an existing or new Buildbot Change for a buildbucket change. | 113 """Returns an existing or new Buildbot Change for a buildbucket change. |
| 101 | 114 |
| 102 Args: | 115 Args: |
| 103 buildbucket_change (dict): a change found in build parameters. | 116 buildbucket_change (dict): a change found in build parameters. |
| 104 | 117 |
| 105 Returns: | 118 Returns: |
| 106 buildbot.changes.change.Change object as Deferred. | 119 buildbot.changes.change.Change object as Deferred. |
| 107 """ | 120 """ |
| 108 buildbot_change = None | 121 buildbot_change = None |
| 109 | 122 |
| 110 if buildbucket_change.get('revision') and buildbucket_change.get('id'): | 123 if self.unique_urls and buildbucket_change.get('url'): |
| 111 buildbot_change = yield self._find_change( | 124 buildbot_change = yield self._find_change_cache_by_revlink.get( |
| 112 buildbucket_change['revision'], buildbucket_change['id']) | 125 buildbucket_change.get('url')) |
| 126 elif buildbucket_change.get('revision') and buildbucket_change.get('id'): |
| 127 buildbot_change = yield self._find_change_cache_by_id.get( |
| 128 (buildbucket_change['revision'], buildbucket_change['id'])) |
| 113 | 129 |
| 114 if buildbot_change is None: | 130 if buildbot_change is None: |
| 115 change_id = yield self._insert_change(buildbucket_change) | 131 change_id = yield self._insert_change(buildbucket_change) |
| 116 buildbot_change = yield self.buildbot.get_change_by_id(change_id) | 132 buildbot_change = yield self.buildbot.get_change_by_id(change_id) |
| 117 defer.returnValue(buildbot_change) | 133 defer.returnValue(buildbot_change) |
| 118 | 134 |
| 119 @defer.inlineCallbacks | 135 @defer.inlineCallbacks |
| 120 def _insert_source_stamp(self, buildbucket_changes): | 136 def _insert_source_stamp(self, buildbucket_changes): |
| 121 """Inserts a new SourceStamp for the list of changes and returns ssid.""" | 137 """Inserts a new SourceStamp for the list of changes and returns ssid.""" |
| 122 assert isinstance(buildbucket_changes, list) | 138 assert isinstance(buildbucket_changes, list) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 if cache is not None and all('id' in c for c in buildbucket_changes): | 171 if cache is not None and all('id' in c for c in buildbucket_changes): |
| 156 cache_key = tuple(sorted([c['id'] for c in buildbucket_changes])) | 172 cache_key = tuple(sorted([c['id'] for c in buildbucket_changes])) |
| 157 ssid = cache.get(cache_key) | 173 ssid = cache.get(cache_key) |
| 158 if ssid: | 174 if ssid: |
| 159 defer.returnValue(ssid) | 175 defer.returnValue(ssid) |
| 160 return | 176 return |
| 161 ssid = yield self._insert_source_stamp(buildbucket_changes) | 177 ssid = yield self._insert_source_stamp(buildbucket_changes) |
| 162 if cache is not None: | 178 if cache is not None: |
| 163 cache[cache_key] = ssid | 179 cache[cache_key] = ssid |
| 164 defer.returnValue(ssid) | 180 defer.returnValue(ssid) |
| OLD | NEW |