Created
December 8, 2015 12:48
-
-
Save marnitto/7eadc74de4fd96d67287 to your computer and use it in GitHub Desktop.
Simple Github Issue to IRC notification bot
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import datetime | |
import os | |
import time | |
import github | |
import irc.bot | |
import schedule | |
last_issue_time = datetime.datetime.now() - datetime.timedelta(days=1) | |
class GiniBot(irc.bot.SingleServerIRCBot): | |
def __init__(self, channel, nickname, server, port, messages): | |
irc.bot.SingleServerIRCBot.__init__( | |
self, [(server, port)], nickname, nickname) | |
self.channel = channel | |
self.messages = messages | |
def on_nicknameinuse(self, c, e): | |
c.nick(c.get_nickname() + "_") | |
def on_welcome(self, c, e): | |
c.join(self.channel) | |
for msg in self.messages: | |
c.privmsg(self.channel, msg) | |
time.sleep(1) | |
time.sleep(1) | |
c.disconnect() | |
def job(): | |
global last_issue_time | |
# Get github issue | |
g = github.Github(os.environ['GITHUB_ACCESS_TOKEN']) | |
user = g.get_user() | |
repo = None | |
for r in user.get_repos(): | |
owner = r.organization.login if r.organization else user.name | |
reponame = r.name | |
if '/'.join([owner, reponame]) == os.environ['GITHUB_REPO']: | |
repo = r | |
break | |
assert repo | |
messages = [] | |
for issue in repo.get_issues(since=last_issue_time): | |
if issue.comments: | |
continue | |
message = '[%s] %s opened new issue: %s (%s)' % ( | |
repo.name, issue.user.name, issue.title, issue.url) | |
messages.append(message) | |
print message | |
# IRC notification | |
c = GiniBot( | |
channel=os.environ['IRC_CHANNEL'], | |
nickname='Gini', | |
server='irc.ozinger.org', | |
port=6664, | |
messages=messages | |
) | |
c.start() | |
if messages: | |
last_issue_time = datetime.datetime.now() | |
def main(): | |
schedule.every(5).minutes.do(job) | |
job() | |
while True: | |
schedule.run_pending() | |
time.sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment