| # Copyright 2015 the Melange authors. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| import json |
| import re |
| import requests |
| |
| from buildbot.status.base import StatusReceiverMultiService |
| from buildbot.status.builder import SUCCESS |
| |
| |
| class GerritStatusPush(StatusReceiverMultiService): |
| """Buildbot Plugin to update Gerrit with Build results.""" |
| |
| def __init__(self): |
| StatusReceiverMultiService.__init__(self) |
| |
| def setServiceParent(self, parent): |
| StatusReceiverMultiService.setServiceParent(self, parent) |
| self.master_status = self.parent |
| self.master_status.subscribe(self) |
| self.master = self.master_status.master |
| |
| def disownServiceParent(self): |
| self.master_status.unsubscribe(self) |
| self.master_status = None |
| for watched in self.watched: |
| watched.unsubscribe(self) |
| return StatusReceiverMultiService.disownServiceParent(self) |
| |
| def builderAdded(self, name, builder): |
| return self |
| |
| def buildFinished(self, builderName, build, buildResult): |
| # figure out which branch |
| branch = None |
| for source_stamps in build.getSourceStamps(): |
| if source_stamps.branch: |
| branch = source_stamps.branch |
| |
| if branch: |
| # figure out the change |
| branch_re = re.compile(r'refs/changes/\d+/(\d+)/(\d+)$') |
| result = re.match(branch_re, branch) |
| if result: |
| (change, patch) = result.groups() |
| |
| # build the message |
| if buildResult == SUCCESS: |
| value = 1 |
| textValue = "passed" |
| else: |
| value = -1 |
| textValue = "failed" |
| |
| jsonData = json.dumps({ |
| 'labels': { |
| 'Buildbot': value, |
| }, |
| 'message': 'Tests %s!.\nDetails: %s' % ( |
| textValue, |
| self.master_status.getURLForThing(build)) |
| }) |
| |
| headers = {'content-type': 'application/json; charset=UTF-8'} |
| base_url = 'https://melange-review.googlesource.com/a' |
| request_endpoint = '/changes/%s/revisions/%s/review' % ( |
| change, patch) |
| requests.post( |
| url=base_url+request_endpoint, data=jsonData, headers=headers) |