Supybot-plugins/GitHub/plugin.py

404 lines
16 KiB
Python
Raw Normal View History

###
# Copyright (c) 2011-2014, Valentin Lorentz
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
2013-03-25 11:12:08 -07:00
import sys
import json
2011-02-27 00:31:54 -08:00
import time
import urllib
2011-02-27 00:31:54 -08:00
import socket
import threading
from string import Template
2011-02-27 00:31:54 -08:00
import supybot.log as log
import supybot.utils as utils
2011-02-27 00:31:54 -08:00
import supybot.world as world
from supybot.commands import *
import supybot.plugins as plugins
2011-02-27 00:31:54 -08:00
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.httpserver as httpserver
2013-03-25 11:12:08 -07:00
if sys.version_info[0] < 3:
from cStringIO import StringIO
quote_plus = urllib.quote_plus
2013-03-25 11:12:08 -07:00
else:
from io import StringIO
quote_plus = urllib.parse.quote_plus
try:
from supybot.i18n import PluginInternationalization
from supybot.i18n import internationalizeDocstring
_ = PluginInternationalization('GitHub')
except:
# This are useless functions that's allow to run the plugin on a bot
# without the i18n plugin
_ = lambda x:x
internationalizeDocstring = lambda x:x
if sys.version_info[0] >= 3:
def b(s):
return s.encode('utf-8')
2013-06-16 01:08:06 -07:00
def u(s):
return s
urlencode = urllib.parse.urlencode
else:
2013-06-16 01:08:06 -07:00
def u(s):
2013-07-21 04:22:21 -07:00
return s.decode('utf-8')
def b(s):
return s
2013-06-16 01:08:06 -07:00
urlencode = urllib.urlencode
def flatten_subdicts(dicts, flat=None):
"""Change dict of dicts into a dict of strings/integers. Useful for
using in string formatting."""
if flat is None:
# Instanciate the dictionnary when the function is run and now when it
# is declared; otherwise the same dictionnary instance will be kept and
# it will have side effects (memory exhaustion, ...)
flat = {}
if isinstance(dicts, list):
return flatten_subdicts(dict(enumerate(dicts)))
elif isinstance(dicts, dict):
for key, value in dicts.items():
if isinstance(value, dict):
value = dict(flatten_subdicts(value))
for subkey, subvalue in value.items():
flat['%s__%s' % (key, subkey)] = subvalue
else:
flat[key] = value
return flat
else:
return dicts
2011-02-27 00:31:54 -08:00
#####################
# Server stuff
#####################
class GithubCallback(httpserver.SupyHTTPServerCallback):
name = "GitHub announce callback"
defaultResponse = _("""
2013-10-19 23:59:27 -07:00
You shouldn't be here, this subfolder is not for you. Go back to the
index and try out other plugins (if any).""")
def doPost(self, handler, path, form):
if not handler.address_string().endswith('.rs.github.com') and \
not handler.address_string().endswith('.cloud-ips.com') and \
not handler.address_string() == 'localhost' and \
not handler.address_string().startswith('127.0.0.') and \
not handler.address_string().startswith('192.30.252.') and \
not handler.address_string().startswith('204.232.175.'):
log.warning("""'%s' tried to act as a web hook for Github,
but is not GitHub.""" % handler.address_string())
self.send_response(403)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b('Error: you are not a GitHub server.'))
else:
headers = dict(self.headers)
try:
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b('Thanks.'))
except socket.error:
pass
self.plugin.announce.onPayload(headers, json.loads(form['payload'].value))
2011-02-27 00:31:54 -08:00
#####################
# API access stuff
#####################
def query(caller, type_, uri_end, args):
args = dict([(x,y) for x,y in args.items() if y is not None])
url = '%s/%s/%s?%s' % (caller._url(), type_, uri_end,
2013-06-16 01:08:06 -07:00
urlencode(args))
2014-01-15 13:34:17 -08:00
if sys.version_info[0] >= 3:
return json.loads(utils.web.getUrl(url).decode('utf8'))
else:
return json.load(utils.web.getUrlFd(url))
2011-02-27 00:31:54 -08:00
#####################
# Plugin itself
#####################
instance = None
@internationalizeDocstring
class GitHub(callbacks.Plugin):
"""Add the help for "@plugin help GitHub" here
This should describe *how* to use this plugin."""
def __init__(self, irc):
global instance
self.__parent = super(GitHub, self)
callbacks.Plugin.__init__(self, irc)
instance = self
callback = GithubCallback()
callback.plugin = self
httpserver.hook('github', callback)
for cb in self.cbs:
cb.plugin = self
2011-02-27 00:31:54 -08:00
class announce(callbacks.Commands):
def _shorten_url(self, url):
try:
data = urlencode({'url': url})
if sys.version_info[0] >= 3:
data = data.encode()
f = utils.web.getUrlFd('http://git.io/', data=data)
url = list(filter(lambda x:x[0] == 'Location',
f.headers._headers))[0][1].strip()
else:
f = utils.web.getUrlFd('http://git.io/', data=data)
url = filter(lambda x:x.startswith('Location: '),
f.headers.headers)[0].split(': ', 1)[1].strip()
except Exception as e:
log.error('Cannot connect to git.io: %s' % e)
2014-03-16 15:15:26 -07:00
return None
return url
2014-03-16 15:15:26 -07:00
def _createPrivmsg(self, irc, channel, payload, event, hidden=None):
bold = ircutils.bold
format_ = self.plugin.registryValue('format.%s' % event, channel)
if not format_.strip():
return
repl = flatten_subdicts(payload)
2014-03-16 15:15:26 -07:00
try_gitio = True
for (key, value) in dict(repl).items():
if key.endswith('url'):
2014-03-16 15:15:26 -07:00
if try_gitio:
url = self._shorten_url(value)
else:
url = None
if url:
repl[key + '__tiny'] = url
else:
repl[key + '__tiny'] = value
try_gitio = False
elif key.endswith('ref'):
2014-03-16 15:15:26 -07:00
try:
repl[key + '__branch'] = value.split('/', 2)[2]
except IndexError:
pass
elif isinstance(value, str):
repl[key + '__firstline'] = value.split('\n', 1)[0]
2014-03-16 15:15:26 -07:00
repl.update({'__hidden': hidden or 0})
command = Template(format_).safe_substitute(repl)
2014-03-26 00:05:28 -07:00
#if hidden is not None:
# s += _(' (+ %i hidden commits)') % hidden
#if sys.version_info[0] < 3:
# s = s.encode('utf-8')
2014-03-16 15:15:26 -07:00
tokens = callbacks.tokenize(command)
if not tokens:
return
fake_msg = ircmsgs.IrcMsg(command='PRIVMSG',
args=(channel, 'GITHUB'))
try:
self.plugin.Proxy(irc, fake_msg, tokens)
except Exception as e:
self.plugin.log.exception('Error occured while running triggered command:')
2011-02-27 00:31:54 -08:00
def onPayload(self, headers, payload):
2014-03-16 15:15:26 -07:00
if 'full_name' in payload['repository']:
repo = payload['repository']['full_name']
elif 'name' in payload['repository']['owner']:
repo = '%s/%s' % (payload['repository']['owner']['name'],
payload['repository']['name'])
else:
repo = '%s/%s' % (payload['repository']['owner']['login'],
payload['repository']['name'])
event = headers['X-GitHub-Event']
2011-02-27 00:31:54 -08:00
announces = self._load()
if repo not in announces:
log.info('Commit for repo %s not announced anywhere' % repo)
return
for channel in announces[repo]:
for irc in world.ircs:
if channel in irc.state.channels:
break
if event == 'push':
commits = payload['commits']
if channel not in irc.state.channels:
log.info('Cannot announce commit for repo %s on %s' %
(repo, channel))
elif len(commits) == 0:
log.warning('GitHub push hook called without any commit.')
else:
hidden = None
last_commit = commits[-1]
if last_commit['message'].startswith('Merge ') and \
len(commits) > 5:
hidden = len(commits) + 1
commits = [last_commit]
payload2 = dict(payload)
for commit in commits:
payload2['__commit'] = commit
2014-03-16 15:15:26 -07:00
self._createPrivmsg(irc, channel, payload2,
'push', hidden)
2011-02-27 00:31:54 -08:00
else:
2014-03-16 15:15:26 -07:00
self._createPrivmsg(irc, channel, payload, event)
2011-02-27 00:31:54 -08:00
def _load(self):
announces = instance.registryValue('announces').split(' || ')
if announces == ['']:
return {}
announces = [x.split(' | ') for x in announces]
output = {}
for repo, chan in announces:
2012-07-30 09:09:53 -07:00
if repo not in output:
2011-02-27 00:31:54 -08:00
output[repo] = []
output[repo].append(chan)
return output
def _save(self, data):
list_ = []
for repo, chans in data.items():
list_.extend([' | '.join([repo,chan]) for chan in chans])
string = ' || '.join(list_)
instance.setRegistryValue('announces', value=string)
@internationalizeDocstring
def add(self, irc, msg, args, channel, owner, name):
"""[<channel>] <owner> <name>
Announce the commits of the GitHub repository called
<owner>/<name> in the <channel>.
<channel> defaults to the current channel."""
repo = '%s/%s' % (owner, name)
announces = self._load()
if repo not in announces:
announces[repo] = [channel]
elif channel in announces[repo]:
irc.error(_('This repository is already announced to this '
'channel.'))
return
2011-02-27 00:31:54 -08:00
else:
announces[repo].append(channel)
self._save(announces)
irc.replySuccess()
add = wrap(add, ['channel', 'something', 'something'])
@internationalizeDocstring
def remove(self, irc, msg, args, channel, owner, name):
"""[<channel>] <owner> <name>
Don't announce the commits of the GitHub repository called
<owner>/<name> in the <channel> anymore.
<channel> defaults to the current channel."""
repo = '%s/%s' % (owner, name)
announces = self._load()
if repo not in announces:
announces[repo] = []
elif channel not in announces[repo]:
2011-02-28 00:40:38 -08:00
irc.error(_('This repository is not yet announced to this '
'channel.'))
return
2011-02-27 00:31:54 -08:00
else:
announces[repo].remove(channel)
self._save(announces)
irc.replySuccess()
remove = wrap(remove, ['channel', 'something', 'something'])
class repo(callbacks.Commands):
def _url(self):
2012-09-08 03:15:18 -07:00
url = instance.registryValue('api.url')
if url == 'http://github.com/api/v2/json': # old api
url = 'https://api.github.com'
instance.setRegistryValue('api.url', value=url)
return url
@internationalizeDocstring
def search(self, irc, msg, args, search, optlist):
2011-02-26 03:33:17 -08:00
"""<searched string> [--page <id>] [--language <language>]
Searches the string in the repository names database. You can
specify the page <id> of the results, and restrict the search
to a particular programming <language>."""
args = {'page': None, 'language': None}
for name, value in optlist:
if name in args:
args[name] = value
2012-09-08 03:15:18 -07:00
results = query(self, 'legacy/repos/search',
quote_plus(search), args)
2011-02-26 03:33:17 -08:00
reply = ' & '.join('%s/%s' % (x['owner'], x['name'])
for x in results['repositories'])
if reply == '':
2012-09-08 03:15:18 -07:00
irc.error(_('No repositories matches your search.'))
else:
2013-06-16 01:08:06 -07:00
irc.reply(u(reply))
search = wrap(search, ['something',
getopts({'page': 'id',
'language': 'somethingWithoutSpaces'})])
2011-02-26 03:33:17 -08:00
@internationalizeDocstring
def info(self, irc, msg, args, owner, name, optlist):
"""<owner> <repository> [--enable <feature> <feature> ...] \
[--disable <feature> <feature>]
Displays informations about <owner>'s <repository>.
Enable or disable features (ie. displayed data) according to the
request)."""
enabled = ['watchers', 'forks', 'pushed_at', 'open_issues',
'description']
for mode, features in optlist:
features = features.split(' ')
for feature in features:
if mode == 'enable':
enabled.append(feature)
else:
try:
enabled.remove(feature)
except ValueError:
# No error is raised, because:
# 1. it wouldn't break anything
# 2. it enhances cross-compatiblity
pass
2012-09-08 03:15:18 -07:00
results = query(self, 'repos', '%s/%s' % (owner, name), {})
2011-02-26 03:33:17 -08:00
output = []
for key, value in results.items():
if key in enabled:
output.append('%s: %s' % (key, value))
2013-06-16 01:08:06 -07:00
irc.reply(u(', '.join(output)))
2011-02-26 03:33:17 -08:00
info = wrap(info, ['something', 'something',
getopts({'enable': 'anything',
'disable': 'anything'})])
2011-02-27 00:31:54 -08:00
def die(self):
self.__parent.die()
httpserver.unhook('github')
Class = GitHub
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: