diff --git a/harold/plugins/github.py b/harold/plugins/github.py index 53ad134..3291662 100644 --- a/harold/plugins/github.py +++ b/harold/plugins/github.py @@ -6,7 +6,6 @@ from twisted.internet.defer import inlineCallbacks, returnValue from harold.plugins.http import ProtectedResource -from harold.shorturl import UrlShortener from harold.utils import dehilight @@ -39,14 +38,13 @@ def _get_commit_author(commit): class PushDispatcher(object): - def __init__(self, bot, shortener, salons): + def __init__(self, bot, salons): self.bot = bot - self.shortener = shortener self.salons = salons @inlineCallbacks def _dispatch_commit(self, sender_username, repository, branch, commit): - short_url = yield self.shortener.make_short_url(commit['url']) + short_url = commit['url'] sender = yield self.salons.get_nick_for_user(sender_username) author = yield self.salons.get_nick_for_user(_get_commit_author(commit)) @@ -85,7 +83,7 @@ def _dispatch_bundle(self, sender_username, info, repository, branch, commits): else: format = "%(sender)s pushed %(commit_count)d commits by %(authors)s (%(commit_range)s - %(url)s) to %(repository)s/%(branch)s" - short_url = yield self.shortener.make_short_url(url) + short_url = url self.bot.send_message(repository.channel, format % { 'sender': sender, 'repository': repository.name, @@ -383,9 +381,8 @@ class Salon(object): "reviewer.", } - def __init__(self, bot, shortener, salons, database): + def __init__(self, bot, salons, database): self.bot = bot - self.shortener = shortener self.salons = salons self.database = SalonDatabase(database) @@ -418,8 +415,7 @@ def dispatch_pullrequest(self, parsed): yield self.database.process_pullrequest(sender_username, pull_request, repository_name) - html_link = pull_request["_links"]["html"]["href"] - short_url = yield self.shortener.make_short_url(html_link) + short_url = html_link = pull_request["_links"]["html"]["href"] if parsed["action"] == "review_requested": if "requested_reviewer" not in parsed: @@ -545,8 +541,7 @@ def dispatch_comment(self, parsed): repository_name = parsed["repository"]["full_name"] repository = yield self.salons.get_repository(repository_name) - html_link = parsed["issue"]["pull_request"]["html_url"] - short_url = yield self.shortener.make_short_url(html_link) + short_url = html_link = parsed["issue"]["pull_request"]["html_url"] pr_id = int(parsed["issue"]["number"]) timestamp = _parse_timestamp(parsed["comment"]["created_at"]) @@ -591,8 +586,7 @@ def dispatch_review(self, parsed): repository_name = parsed["repository"]["full_name"] repository = yield self.salons.get_repository(repository_name) - html_link = parsed["pull_request"]["html_url"] - short_url = yield self.shortener.make_short_url(html_link) + short_url = html_link = parsed["pull_request"]["html_url"] pr_id = int(parsed["pull_request"]["number"]) timestamp = _parse_timestamp(parsed["review"]["submitted_at"]) sender = parsed["sender"]["login"] @@ -639,12 +633,11 @@ class GitHubListener(ProtectedResource): def __init__(self, http, bot, salons, database): ProtectedResource.__init__(self, http) - shortener = UrlShortener() self.salons = salons - push_dispatcher = PushDispatcher(bot, shortener, salons) - salon = Salon(bot, shortener, salons, database) + push_dispatcher = PushDispatcher(bot, salons) + salon = Salon(bot, salons, database) self.dispatchers = { "ping": push_dispatcher.dispatch_ping, diff --git a/harold/shorturl.py b/harold/shorturl.py deleted file mode 100644 index 1b7f2d6..0000000 --- a/harold/shorturl.py +++ /dev/null @@ -1,73 +0,0 @@ -import urllib -from collections import deque - -from twisted.internet import reactor -from twisted.internet.defer import Deferred, succeed -from twisted.web.client import Agent - - -class StringProducer(object): - def __init__(self, body): - self.body = body - self.length = len(body) - - def startProducing(self, consumer): - consumer.write(self.body) - return succeed(None) - - def pauseProducing(self): - pass - - def stopProducing(self): - pass - - -class UrlShortener(object): - def __init__(self): - self.request_in_flight = False - self.pending_requests = deque() - - def _onRequestComplete(self): - self.request_in_flight = False - - if self.pending_requests: - d = self.pending_requests.popleft() - d.callback(None) - - def _make_short_url(self, long_url): - self.request_in_flight = True - - api_uri = "https://git.io/" - encoded = urllib.urlencode({"url": long_url}) - body_producer = StringProducer(encoded) - - agent = Agent(reactor) - d = agent.request('POST', api_uri, bodyProducer=body_producer) - - def onRequestComplete(data): - self._onRequestComplete() - return data - - def onResponse(response): - if response.code != 201: - onRequestComplete(None) - return long_url - - self._onRequestComplete() - return response.headers.getRawHeaders("Location")[-1] - d.addCallback(onResponse) - - def onError(failure): - return long_url - d.addErrback(onError) - d.addErrback(onRequestComplete) - - return d - - def _start_another_request(self, ignored, long_url): - return self._make_short_url(long_url) - - def make_short_url(self, long_url): - d = Deferred() - d.callback(long_url) - return d