[pypy-svn] buildbot default: refactoring: move the loop over the commits outside the IRC/mail code; now, every handler has to deal with only one commit at a time

antocuni commits-noreply at bitbucket.org
Thu Apr 21 11:40:41 CEST 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r488:5e1d218032f4
Date: 2011-04-21 10:35 +0200
http://bitbucket.org/pypy/buildbot/changeset/5e1d218032f4/

Log:	refactoring: move the loop over the commits outside the IRC/mail
	code; now, every handler has to deal with only one commit at a time

diff --git a/bitbucket_hook/hook.py b/bitbucket_hook/hook.py
--- a/bitbucket_hook/hook.py
+++ b/bitbucket_hook/hook.py
@@ -17,7 +17,6 @@
 def check_for_local_repo(local_repo):
     return local_repo.check(dir=True)
 
-
 def get_commits(service, payload):
     #XXX: service is evil, get rid
     import operator
@@ -40,5 +39,6 @@
         print >> sys.stderr, 'Ignoring unknown repo', path
         return
     scm.hg('pull', '-R', local_repo)
-    irc.handle_message(payload, test)
-    mail.handle_diff_email(payload, test)
+    for commit in get_commits('hook', payload):
+        irc.handle_commit(payload, commit)
+        mail.handle_commit(payload, commit)

diff --git a/bitbucket_hook/irc.py b/bitbucket_hook/irc.py
--- a/bitbucket_hook/irc.py
+++ b/bitbucket_hook/irc.py
@@ -6,7 +6,6 @@
 import time
 import subprocess
 
-
 def getpaths(files, listfiles=False):
 
     # Handle empty input
@@ -53,40 +52,33 @@
         ])
 
 
-def handle_message(payload, test=False):
-    #XXX
-    from .hook import get_commits
+def handle_commit(payload, commit):
     from .main import app
-    commits = get_commits('irc', payload)
-    if test:
-        print "#" * 20
-        print "IRC messages:"
 
-    for commit in commits:
-        author = commit['author']
-        branch = commit['branch']
-        node = commit['node']
-        timestamp = commit.get('timestamp')
-        print time.strftime('[%Y-%m-%d %H:%M]'), node, timestamp, author
+    author = commit['author']
+    branch = commit['branch']
+    node = commit['node']
+    timestamp = commit.get('timestamp')
+    print time.strftime('[%Y-%m-%d %H:%M]'), node, timestamp, author
 
-        files = commit.get('files', [])
-        common_prefix, filenames = getpaths(files, app.config['LISTFILES'])
-        pathlen = len(common_prefix) + len(filenames) + 2
-        common_prefix = '/' + common_prefix
+    files = commit.get('files', [])
+    common_prefix, filenames = getpaths(files, app.config['LISTFILES'])
+    pathlen = len(common_prefix) + len(filenames) + 2
+    common_prefix = '/' + common_prefix
 
-        if app.config['USE_COLOR_CODES']:
-            author = '\x0312%s\x0F' % author   # in blue
-            branch = '\x02%s\x0F' % branch   # in bold
-            node = '\x0311%s\x0F' % node     # in azure
-            common_prefix = '\x0315%s\x0F' % common_prefix  # in gray
+    if app.config['USE_COLOR_CODES']:
+        author = '\x0312%s\x0F' % author   # in blue
+        branch = '\x02%s\x0F' % branch   # in bold
+        node = '\x0311%s\x0F' % node     # in azure
+        common_prefix = '\x0315%s\x0F' % common_prefix  # in gray
 
-        message = commit['message'].replace('\n', ' ')
-        fields = (author, branch, node, common_prefix, filenames)
-        part1 = '%s %s %s %s%s: ' % fields
-        totallen = 160 + pathlen
-        if len(message) + len(part1) <= totallen:
-            irc_msg = part1 + message
-        else:
-            maxlen = totallen - (len(part1) + 3)
-            irc_msg = part1 + message[:maxlen] + '...'
-        send_message(irc_msg, test)
+    message = commit['message'].replace('\n', ' ')
+    fields = (author, branch, node, common_prefix, filenames)
+    part1 = '%s %s %s %s%s: ' % fields
+    totallen = 160 + pathlen
+    if len(message) + len(part1) <= totallen:
+        irc_msg = part1 + message
+    else:
+        maxlen = totallen - (len(part1) + 3)
+        irc_msg = part1 + message[:maxlen] + '...'
+    send_message(irc_msg)

diff --git a/bitbucket_hook/test/test_hook.py b/bitbucket_hook/test/test_hook.py
--- a/bitbucket_hook/test/test_hook.py
+++ b/bitbucket_hook/test/test_hook.py
@@ -89,7 +89,9 @@
     }
 
     payload, expected = irc_cases(payload)
-    irc.handle_message(payload)
+    commits = payload['commits']
+    irc.handle_commit(payload, commits[0])
+    irc.handle_commit(payload, commits[1])
 
     msg1, msg2 = messages[:2]
 

diff --git a/bitbucket_hook/mail.py b/bitbucket_hook/mail.py
--- a/bitbucket_hook/mail.py
+++ b/bitbucket_hook/mail.py
@@ -13,6 +13,9 @@
 
 """
 
+def handle_commit(payload, commit):
+    return send_diff_for_commit(payload, commit)
+
 
 def send_diff_for_commit(payload, commit, test=False):
     from .main import app
@@ -56,9 +59,3 @@
         smtp = SMTP(app.config['SMTP_SERVER'], app.config['SMTP_PORT'])
         smtp.sendmail(from_, [to], msg.as_string())
 
-
-def handle_diff_email(payload, test=False):
-    from . import hook
-    commits = hook.get_commits('email', payload)
-    for commit in commits:
-        send_diff_for_commit(payload, commit, test)

diff --git a/bitbucket_hook/test/test_irc.py b/bitbucket_hook/test/test_irc.py
--- a/bitbucket_hook/test/test_irc.py
+++ b/bitbucket_hook/test/test_irc.py
@@ -91,5 +91,3 @@
     # doesnt get called in test mode
     monkeypatch.setattr(subprocess, 'call', lambda: None)
     irc.send_message('test', test=True)
-
-


More information about the Pypy-commit mailing list