Skip to content

Instantly share code, notes, and snippets.

@tim-jansen
Last active December 11, 2015 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-jansen/4598663 to your computer and use it in GitHub Desktop.
Save tim-jansen/4598663 to your computer and use it in GitHub Desktop.
Pushes buildbot start and stop builds to a Flowdock room.
# Buildbot main.cfg configuration for Flowdock Alerts
#
# Copyright © 2013 SIMB Tecnnologia
# Licensed under the terms of the MIT License
# See http://www.opensource.org/licenses/mit-license.php for details
#
from buildbot.status.base import StatusReceiverMultiService
from buildbot.status.results import SUCCESS, WARNINGS, FAILURE, EXCEPTION, RETRY
from twisted.web import client
import urllib
class FlowdockPush(StatusReceiverMultiService):
baseurl = "https://api.flowdock.com/v1/messages/team_inbox/"
results_desc = {
SUCCESS: "Success",
WARNINGS: "Warnings",
FAILURE: "Failure",
EXCEPTION: "Exception",
RETRY: "Retry",
}
def __init__(self, token, source, from_address, tags=None, from_name=None, extra_post_params=None):
StatusReceiverMultiService.__init__(self)
# Parameters.
self.serverUrl = self.baseurl + token
self.extra_post_params = extra_post_params or {}
self.source = source
self.from_address = from_address
self.tags = tags or []
self.from_name = from_name or ""
def startService(self):
"""Starting up."""
StatusReceiverMultiService.startService(self)
self.status = self.parent.getStatus()
self.status.subscribe(self)
def buildStarted(self, builderName, build):
builder = build.getBuilder()
url = builder.status.getURLForThing(build)
subject = "build #%d of %s started for %s" % \
(build.getNumber(), builder.getName(), ", ".join([c.split('<')[0] for c in build.getResponsibleUsers()]),
)
content = """
<hr />
<br />
<b>Blamelist</b>: %(blamelist)s <br />
<b>Revisions</b>: %(revisions)s <br />
<br />
Changes follows: <br />
<hr />
<code>
%(changes)s
""" % { 'blamelist': ", ".join([c.split('<')[0] for c in build.getResponsibleUsers()]),
'changes': "</code><hr /><code>".join(["<br />".join(c.asText().splitlines()) for c in build.getChanges()]),
'revisions': ", ".join([str(c.revision) for c in build.getChanges()]),
}
self.pushHttp(subject=subject, content=content,tags=['buildstarted'], link=url)
def buildFinished(self, builderName, build, results):
builder = build.getBuilder()
url = builder.status.getURLForThing(build)
subject = "build #%d of %s is complete: %s" % \
(build.getNumber(), builder.getName(), self.results_desc.get(results,"Unknown"))
content = """
<hr />
<br />
<b>Blamelist</b>: %(blamelist)s <br />
<b>Revisions</b>: %(revisions)s <br />
<br />
<hr />
<b>Results</b>: %(results)s <br />
<hr />
Changes follows: <br />
<hr />
<code>
%(changes)s
""" % { 'blamelist': ", ".join([c.split('<')[0] for c in build.getResponsibleUsers()]),
'changes': "</code><hr /><code>".join(["<br />".join(c.asText().splitlines()) for c in build.getChanges()]),
'revisions': ", ".join([str(c.revision) for c in build.getChanges()]),
'results': " ".join(build.getText()),
}
self.pushHttp(subject=subject, content=content,tags=['buildfinished'], link=url)
def builderAdded(self, name, builder):
log.msg('Builder %s added' % (builder))
builder.subscribe(self)
def pushHttp(self, subject, content, link=None, tags=None, project=None):
log.msg('pushHTTP called')
data = {
'source': self.source,
'from_address': self.from_address,
'subject': subject,
'content': content,
'from_name': self.from_name or "",
'project': project or "",
'tags': ",".join(self.tags or [] + tags or []),
'link': link or "",
}
def Success(result):
"""Queue up next push."""
log.msg('Sent event to %s' % self.serverUrl)
def Failure(result):
"""Insert back items not sent and queue up next push."""
# Server is now down.
log.msg('Failed to push event to %s: %s' %
(self.serverUrl, str(result)))
# Trigger the HTTP POST request.
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
connection = client.getPage(self.serverUrl,
method='POST',
postdata=urllib.urlencode(data),
headers=headers,
agent='buildbot')
connection.addCallbacks(Success, Failure)
return connection
c['status'].append(FlowdockPush(token="ROOM_TOKEN",source="SOURCE_NAME",from_address="FROM@ADDRESS",tags=['TAG1','TAG2']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment