From 9b117389606bbe5c398c52bf7001c0324ba83183 Mon Sep 17 00:00:00 2001 From: kongr45gpen Date: Sun, 11 May 2014 13:52:29 +0300 Subject: Handle travis hook messages --- .travis.yml | 2 +- __init__.py | 2 + local/handler/GithubHandler.py | 7 +- local/handler/TravisHandler.py | 15 ++++ local/testing/ExpectationPluginTestCase.py | 23 +++++- local/utility.py | 9 ++- samples/travis-notification.json | 58 ++++++++++++++ samples/wiki-new-page.json | 117 +++++++++++++++++++++++++++++ test.py | 15 ++++ 9 files changed, 238 insertions(+), 10 deletions(-) create mode 100644 local/handler/TravisHandler.py create mode 100644 samples/travis-notification.json create mode 100644 samples/wiki-new-page.json diff --git a/.travis.yml b/.travis.yml index 7566ee1..63050b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ install: - cd ../void/Github script: - - ../../supybot/scripts/supybot-test --plugins-dir=.. Github + - python ../../supybot/scripts/supybot-test --plugins-dir=.. Github after_success: - coverage run --branch --source plugin.py,local --omit '*init*' ../../supybot/scripts/supybot-test --plugins-dir=.. Github diff --git a/__init__.py b/__init__.py index d7be23a..137a950 100644 --- a/__init__.py +++ b/__init__.py @@ -33,6 +33,7 @@ import local.handler.PushHandler import local.handler.WikiHandler import local.handler.IssueHandler import local.handler.StatusHandler +import local.handler.TravisHandler import local.handler.IssueCommentHandler import local.utility reload(RequestHandler) @@ -40,6 +41,7 @@ reload(local.handler.PushHandler) reload(local.handler.WikiHandler) reload(local.handler.IssueHandler) reload(local.handler.StatusHandler) +reload(local.handler.TravisHandler) reload(local.handler.IssueCommentHandler) reload(local.utility) diff --git a/local/handler/GithubHandler.py b/local/handler/GithubHandler.py index 788ef94..fabe544 100644 --- a/local/handler/GithubHandler.py +++ b/local/handler/GithubHandler.py @@ -26,6 +26,7 @@ import PushHandler import WikiHandler import IssueHandler import StatusHandler +import TravisHandler import IssueCommentHandler class GithubHandler(BaseHTTPServer.BaseHTTPRequestHandler): @@ -89,7 +90,9 @@ class GithubHandler(BaseHTTPServer.BaseHTTPRequestHandler): msgs = [] - if 'pages' in data: + if 'matrix' in data: + msgs = TravisHandler.handle(data) + elif 'pages' in data: msgs = WikiHandler.handle(data) elif 'state' in data: msgs = StatusHandler.handle(data) @@ -101,7 +104,7 @@ class GithubHandler(BaseHTTPServer.BaseHTTPRequestHandler): else: msgs = IssueHandler.handle(data) else: - msgs.append( ircmsgs.privmsg(channel, "Something happened")) + msgs.append("Something happened") saveMessages(msgs) diff --git a/local/handler/TravisHandler.py b/local/handler/TravisHandler.py new file mode 100644 index 0000000..a8a8d86 --- /dev/null +++ b/local/handler/TravisHandler.py @@ -0,0 +1,15 @@ +from ..utility import * + +def handle(data): + msgs = [] + + msgs.append( "%s: The build %s * %s by %s (%s - %s)" % ( + ircutils.bold(data['repository']['name']), + colorAction(data['status_message'].lower()), + ircutils.bold(data['commit'][0:6]), + ircutils.mircColor(data['author_name'], "green"), + ircutils.mircColor(maxLen(data['message'].splitlines()[0], 50), "dark gray"), + getShortURL(data['build_url']) + )) + + return msgs diff --git a/local/testing/ExpectationPluginTestCase.py b/local/testing/ExpectationPluginTestCase.py index af6e56d..9027927 100644 --- a/local/testing/ExpectationPluginTestCase.py +++ b/local/testing/ExpectationPluginTestCase.py @@ -2,6 +2,8 @@ from supybot.log import info from supybot.test import * +from copy import deepcopy + from sys import stdout from time import sleep @@ -40,7 +42,7 @@ class ExpectationPluginTestCase(PluginTestCase): for error in errors: print "- Failed to assert that %s" % (error,) print tcolors.ENDC - self.fail() + self.fail("%i assertions failed while describing %s" % (len(errors), query)) def sendRequest(self, file): """ Opens the `samples` folder and sends a file as a request @@ -87,11 +89,22 @@ def it(): class Expectation: def __init__(self): self.error = '' + self.negation = False + self.should = self self.to = self + self.should_not = self.negate() def evaluate(self): - return self.assertion() + if self.negation is True: + return not self.assertion() + else: + return self.assertion() + + def negate(self): + other = deepcopy(self) + other.negation = not other.negation + return other def cleanReply(self): return clean(self.reply) @@ -105,7 +118,11 @@ class Expectation: def contain(self, what): self.assertion = self.contains self.assertionParameter = what - self.summary = "'%s' contains '%s'" + if self.negation: + verb = "does not contain" + else: + verb = "contains" + self.summary = "'%s' " +verb+ " '%s'" return self def contains(self, flags=re.I): diff --git a/local/utility.py b/local/utility.py index 5aa8724..fa8ca28 100644 --- a/local/utility.py +++ b/local/utility.py @@ -41,7 +41,8 @@ def maxLen(msg, maxn=400): def colorAction(action): """Give an action string (e.g. created, edited) and get a nice IRC colouring""" - if action == "created" or action == "opened" or action == "tagged" or action == "success": + if action == "created" or action == "opened" or action == "tagged" or \ + action == "success" or action == "passed": return ircutils.bold(ircutils.mircColor(action, "green")) if action == "deleted" or action == "closed" or action == "re-tagged" or \ action == "deleted tag" or action == "failed" or action == "errored" or \ @@ -73,9 +74,9 @@ def saveMessages(msgs): globals.messageList = msgs def clean(string): - """Strips IRC control characters from a string""" - regex = re.compile("(([\x02\x1f\x16\x0f])|(\x03(?:\d{1,2}(?:,\d{1,2})?)?))", re.UNICODE) - return regex.sub('', string) + """Strips IRC control characters from a string""" + regex = re.compile("(([\x02\x1f\x16\x0f])|(\x03(?:\d{1,2}(?:,\d{1,2})?)?))", re.UNICODE) + return regex.sub('', string) # Possible colours: # white, black, (light/dark) blue, (light) green, red, brown, purple, diff --git a/samples/travis-notification.json b/samples/travis-notification.json new file mode 100644 index 0000000..90475ad --- /dev/null +++ b/samples/travis-notification.json @@ -0,0 +1,58 @@ +{ + "id": 1, + "number": "1", + "status": null, + "started_at": null, + "finished_at": null, + "status_message": "Passed", + "commit": "62aae5f70ceee39123ef", + "branch": "master", + "message": "this is a long commit message, because we must learn how to lead ever-present lives in the face of turbulence. It is a sign of things to come. The vector of aspiration is now happening worldwide. It is time to take it to the next level. Today, science tells us that the essence of nature is truth. Throughout history, humans have been interacting with the quantum matrix via four-dimensional superstructures. We are in the midst of a cosmic summoning of power that will let us access the multiverse itself. Who are we? Where on the great path will we be aligned?", + "compare_url": "https://github.com/svenfuchs/minimal/compare/master...develop", + "committed_at": "2011-11-11T11: 11: 11Z", + "committer_name": "Sven Fuchs", + "committer_email": "svenfuchs@artweb-design.de", + "author_name": "Sven Fuchs", + "author_email": "svenfuchs@artweb-design.de", + "type": "push", + "build_url": "https://travis-ci.org/svenfuchs/minimal/builds/1", + "repository": { + "id": 1, + "name": "minimal", + "owner_name": "svenfuchs", + "url": "http://github.com/svenfuchs/minimal" + }, + "config": { + "notifications": { + "webhooks": ["http://evome.fr/notifications", "http://example.com/"] + } + }, + "matrix": [ + { + "id": 2, + "repository_id": 1, + "number": "1.1", + "state": "created", + "started_at": null, + "finished_at": null, + "config": { + "notifications": { + "webhooks": ["http://evome.fr/notifications", "http://example.com/"] + } + }, + "status": null, + "log": "", + "result": null, + "parent_id": 1, + "commit": "62aae5f70ceee39123ef", + "branch": "master", + "message": "the commit message", + "committed_at": "2011-11-11T11: 11: 11Z", + "committer_name": "Sven Fuchs", + "committer_email": "svenfuchs@artweb-design.de", + "author_name": "Sven Fuchs", + "author_email": "svenfuchs@artweb-design.de", + "compare_url": "https://github.com/svenfuchs/minimal/compare/master...develop" + } + ] +} diff --git a/samples/wiki-new-page.json b/samples/wiki-new-page.json new file mode 100644 index 0000000..b9ca4fa --- /dev/null +++ b/samples/wiki-new-page.json @@ -0,0 +1,117 @@ +{ + "pages": [ + { + "page_name": "Home", + "title": "Home", + "summary": null, + "action": "created", + "sha": "9941c1a1bb1b2db99ad9aabf10c8f946d808e634", + "html_url": "https://github.com/kongr45gpen/supybot-github/wiki/Home" + } + ], + "repository": { + "id": 11590921, + "name": "supybot-github", + "full_name": "kongr45gpen/supybot-github", + "owner": { + "login": "kongr45gpen", + "id": 720678, + "avatar_url": "https://avatars.githubusercontent.com/u/720678?", + "gravatar_id": "237485d64005289a16ab3dcf7e04969a", + "url": "https://api.github.com/users/kongr45gpen", + "html_url": "https://github.com/kongr45gpen", + "followers_url": "https://api.github.com/users/kongr45gpen/followers", + "following_url": "https://api.github.com/users/kongr45gpen/following{/other_user}", + "gists_url": "https://api.github.com/users/kongr45gpen/gists{/gist_id}", + "starred_url": "https://api.github.com/users/kongr45gpen/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/kongr45gpen/subscriptions", + "organizations_url": "https://api.github.com/users/kongr45gpen/orgs", + "repos_url": "https://api.github.com/users/kongr45gpen/repos", + "events_url": "https://api.github.com/users/kongr45gpen/events{/privacy}", + "received_events_url": "https://api.github.com/users/kongr45gpen/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/kongr45gpen/supybot-github", + "description": "Highly configurable, still under construction supybot/gribble plugin that uses an HTTP post-commit hook to report commits and other updates from Github repositories", + "fork": false, + "url": "https://api.github.com/repos/kongr45gpen/supybot-github", + "forks_url": "https://api.github.com/repos/kongr45gpen/supybot-github/forks", + "keys_url": "https://api.github.com/repos/kongr45gpen/supybot-github/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/kongr45gpen/supybot-github/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/kongr45gpen/supybot-github/teams", + "hooks_url": "https://api.github.com/repos/kongr45gpen/supybot-github/hooks", + "issue_events_url": "https://api.github.com/repos/kongr45gpen/supybot-github/issues/events{/number}", + "events_url": "https://api.github.com/repos/kongr45gpen/supybot-github/events", + "assignees_url": "https://api.github.com/repos/kongr45gpen/supybot-github/assignees{/user}", + "branches_url": "https://api.github.com/repos/kongr45gpen/supybot-github/branches{/branch}", + "tags_url": "https://api.github.com/repos/kongr45gpen/supybot-github/tags", + "blobs_url": "https://api.github.com/repos/kongr45gpen/supybot-github/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/kongr45gpen/supybot-github/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/kongr45gpen/supybot-github/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/kongr45gpen/supybot-github/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/kongr45gpen/supybot-github/statuses/{sha}", + "languages_url": "https://api.github.com/repos/kongr45gpen/supybot-github/languages", + "stargazers_url": "https://api.github.com/repos/kongr45gpen/supybot-github/stargazers", + "contributors_url": "https://api.github.com/repos/kongr45gpen/supybot-github/contributors", + "subscribers_url": "https://api.github.com/repos/kongr45gpen/supybot-github/subscribers", + "subscription_url": "https://api.github.com/repos/kongr45gpen/supybot-github/subscription", + "commits_url": "https://api.github.com/repos/kongr45gpen/supybot-github/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/kongr45gpen/supybot-github/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/kongr45gpen/supybot-github/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/kongr45gpen/supybot-github/issues/comments/{number}", + "contents_url": "https://api.github.com/repos/kongr45gpen/supybot-github/contents/{+path}", + "compare_url": "https://api.github.com/repos/kongr45gpen/supybot-github/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/kongr45gpen/supybot-github/merges", + "archive_url": "https://api.github.com/repos/kongr45gpen/supybot-github/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/kongr45gpen/supybot-github/downloads", + "issues_url": "https://api.github.com/repos/kongr45gpen/supybot-github/issues{/number}", + "pulls_url": "https://api.github.com/repos/kongr45gpen/supybot-github/pulls{/number}", + "milestones_url": "https://api.github.com/repos/kongr45gpen/supybot-github/milestones{/number}", + "notifications_url": "https://api.github.com/repos/kongr45gpen/supybot-github/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/kongr45gpen/supybot-github/labels{/name}", + "releases_url": "https://api.github.com/repos/kongr45gpen/supybot-github/releases{/id}", + "created_at": "2013-07-22T19:55:10Z", + "updated_at": "2014-04-28T17:34:27Z", + "pushed_at": "2014-04-28T16:07:39Z", + "git_url": "git://github.com/kongr45gpen/supybot-github.git", + "ssh_url": "git@github.com:kongr45gpen/supybot-github.git", + "clone_url": "https://github.com/kongr45gpen/supybot-github.git", + "svn_url": "https://github.com/kongr45gpen/supybot-github", + "homepage": "", + "size": 252, + "stargazers_count": 0, + "watchers_count": 0, + "language": "Python", + "has_issues": true, + "has_downloads": true, + "has_wiki": true, + "forks_count": 0, + "mirror_url": null, + "open_issues_count": 2, + "forks": 0, + "open_issues": 2, + "watchers": 0, + "default_branch": "master" + }, + "sender": { + "login": "kongr45gpen", + "id": 720678, + "avatar_url": "https://avatars.githubusercontent.com/u/720678?", + "gravatar_id": "237485d64005289a16ab3dcf7e04969a", + "url": "https://api.github.com/users/kongr45gpen", + "html_url": "https://github.com/kongr45gpen", + "followers_url": "https://api.github.com/users/kongr45gpen/followers", + "following_url": "https://api.github.com/users/kongr45gpen/following{/other_user}", + "gists_url": "https://api.github.com/users/kongr45gpen/gists{/gist_id}", + "starred_url": "https://api.github.com/users/kongr45gpen/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/kongr45gpen/subscriptions", + "organizations_url": "https://api.github.com/users/kongr45gpen/orgs", + "repos_url": "https://api.github.com/users/kongr45gpen/repos", + "events_url": "https://api.github.com/users/kongr45gpen/events{/privacy}", + "received_events_url": "https://api.github.com/users/kongr45gpen/received_events", + "type": "User", + "site_admin": false + } +} diff --git a/test.py b/test.py index e97ed91..55a9612 100644 --- a/test.py +++ b/test.py @@ -48,4 +48,19 @@ class GithubTestCase(ExpectationPluginTestCase): it().should.contain('https://github.com/kongr45gpen/supybot-github/wiki/Home') ) + def testTravisNotification(self): + self.sendRequest('travis-notification') + + self.describe('first message', + it().should.contain('minimal'), + it().should.contain('passed'), + it().should.contain('62aae'), + it().should.contain('Sven Fuchs'), + it().should.contain('this is a long commit message'), + it().should_not.contain('this is a long commit message, because we must learn how to lead ever-present lives in the face of turbulence. It is a sign of things to come. The vector of aspiration is now happening'), + it().should.contain('https://travis-ci.org/svenfuchs/minimal/builds/1') + ) + + self.assertError('get second message') + # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: -- cgit v1.2.3