Created
September 17, 2017 06:35
-
-
Save artagnon/706124c040695c553b9555d3f3529561 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is part of Buildbot. Buildbot is free software: you can | |
# redistribute it and/or modify it under the terms of the GNU General Public | |
# License as published by the Free Software Foundation, version 2. | |
# | |
# This program is distributed in the hope that it will be useful, but WITHOUT | |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
# details. | |
# | |
# You should have received a copy of the GNU General Public License along with | |
# this program; if not, write to the Free Software Foundation, Inc., 51 | |
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
# | |
# Copyright Buildbot Team Members | |
from __future__ import absolute_import | |
from __future__ import print_function | |
from twisted.internet import defer | |
from twisted.python import log as twlog | |
from buildbot import config | |
from humanize import naturaltime | |
from buildbot.process.results import CANCELLED | |
from buildbot.process.results import EXCEPTION | |
from buildbot.process.results import FAILURE | |
from buildbot.process.results import SUCCESS | |
from buildbot.process.results import WARNINGS | |
from buildbot.process.results import Results | |
from buildbot.reporters.message import MessageFormatter as DefaultMessageFormatter | |
from buildbot.reporters.message import MessageFormatterMissingWorker | |
from buildbot.reporters.notifier import NotifierBase | |
from buildbot.util import httpclientservice | |
ENCODING = 'utf8' | |
VALID_PARAMS = {"sound", "callback", "timestamp", "url", | |
"url_title", "device", "retry", "expire", "html"} | |
PRIORITIES = { | |
CANCELLED: 'cancelled', | |
EXCEPTION: 'exception', | |
FAILURE: 'failing', | |
SUCCESS: 'passing', | |
WARNINGS: 'warnings' | |
} | |
class SlackPush(NotifierBase): | |
def checkConfig(self, user_key, api_token, | |
mode="all", tags=None, builders=None, | |
buildSetSummary=False, messageFormatter=None, | |
subject="Buildbot %(result)s in %(title)s on %(builder)s", | |
schedulers=None, branches=None, | |
priorities=None, otherParams=None, | |
watchedWorkers=None, messageFormatterMissingWorker=None): | |
super(SlackPush, self).checkConfig(mode, tags, builders, | |
buildSetSummary, messageFormatter, | |
subject, False, False, | |
schedulers, | |
branches, watchedWorkers) | |
httpclientservice.HTTPClientService.checkAvailable(self.__class__.__name__) | |
if otherParams is not None and set(otherParams.keys()) - VALID_PARAMS: | |
config.error("otherParams can be only 'sound', 'callback', 'timestamp', " | |
"'url', 'url_title', 'device', 'retry', 'expire', or 'html'") | |
@defer.inlineCallbacks | |
def reconfigService(self, user_key, api_token, | |
mode="all", tags=None, builders=None, | |
buildSetSummary=False, messageFormatter=None, | |
subject="Buildbot %(result)s in %(title)s on %(builder)s", | |
schedulers=None, branches=None, | |
priorities=None, otherParams=None, | |
watchedWorkers=None, messageFormatterMissingWorker=None): | |
if messageFormatter is None: | |
messageFormatter = DefaultMessageFormatter(template_type='html', | |
template_filename='default_notification.txt') | |
if messageFormatterMissingWorker is None: | |
messageFormatterMissingWorker = MessageFormatterMissingWorker( | |
template_filename='missing_notification.txt') | |
super(SlackPush, self).reconfigService(mode, tags, builders, | |
buildSetSummary, messageFormatter, | |
subject, False, False, | |
schedulers, branches, | |
watchedWorkers, messageFormatterMissingWorker) | |
# we use [:-1] to remove the trailing slash, and add it ourselves, for readability | |
self.icon = "http://buildbot.net/img/nut.png" | |
self.username = user_key | |
self.endpoint = api_token | |
self._http = yield httpclientservice.HTTPClientService.getService( | |
self.master, 'https://hooks.slack.com') | |
def sendMessage(self, body, subject=None, type=None, builderName=None, | |
results=None, builds=None, users=None, patches=None, | |
logs=None, worker=None): | |
url = "%s/#/builders/%d/builds/%d" % (self.master.config.buildbotURL[:-1], | |
builds[0]['builderid'], | |
builds[0]['buildid']) | |
humantime = naturaltime(builds[0]['complete_at'] - builds[0]['started_at']) | |
message = """ | |
Build {name} {result} | {humantime} | |
Details at {url}""".format(name=builderName, result=Results[results], | |
humantime=humantime, url=url) | |
payload = { | |
"text": message | |
} | |
payload['username'] = self.username | |
if self.icon.startswith(':'): | |
payload['icon_emoji'] = self.icon | |
else: | |
payload['icon_url'] = self.icon | |
return self._http.post(self.endpoint, json=payload) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment