class PostReceiveDispatcher(object): def __init__(self, config, bot): self.config = config self.bot = bot self.shortener = UrlShortener() def _dispatch_commit(self, repository, branch, commit): d = self.shortener.make_short_url(commit['url']) def onUrlShortened(short_url): self.bot.send_message(repository.channel, repository.format % { 'repository': repository.name, 'branch': branch, 'commit_id': commit['id'][:7], 'url': short_url, 'author': _get_commit_author(commit), 'summary': commit['message'].splitlines()[0] }) d.addCallback(onUrlShortened) def _dispatch_bundle(self, info, repository, branch, commits): authors = set() for commit in commits: authors.add(_get_commit_author(commit)) before = info['before'] after = info['after'] commit_range = before[:7] + '..' + after[:7] url = "http://github.com/%s/compare/%s...%s" % (repository.name, before, after) d = self.shortener.make_short_url(url) def onUrlShortened(short_url): self.bot.send_message(repository.channel, repository.bundled_format % { 'repository': repository.name, 'branch': branch, 'authors': ', '.join(authors), 'commit_count': len(commits), 'commit_range': commit_range, 'url': short_url, }) d.addCallback(onUrlShortened) def dispatch(self, payload): parsed = json.loads(payload) repository_name = (parsed['repository']['owner']['name'] + '/' + parsed['repository']['name']) repository = self.config.repositories_by_name[repository_name] branch = parsed['ref'].split('/')[-1] commits = parsed['commits'] if not repository.branches or branch in repository.branches: if len(commits) <= repository.max_commit_count: for commit in commits: self._dispatch_commit(repository, branch, commit) else: self._dispatch_bundle(parsed, repository, branch, commits)
def __init__(self, config, bot): self.config = config self.bot = bot self.shortener = UrlShortener()