Tarek Ziadé avatar Tarek Ziadé committed 49bafbc

initial version

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+from email.mime.text import MIMEText
+from email.header import Header
+import smtplib
+import socket
+
+from mozillapulse import consumers
+
+
+def send_email(sender, rcpt, subject, body, smtp_host='localhost',
+               smtp_port=25, smtp_user=None, smtp_password=None):
+    # preparing the message
+    msg = MIMEText(body.encode('utf8'), 'plain', 'utf8')
+    msg['From'] = Header(sender, 'utf8')
+    msg['To'] = Header(rcpt, 'utf8')
+    msg['Subject'] = Header(subject, 'utf8')
+
+    try:
+        server = smtplib.SMTP(smtp_host, smtp_port, timeout=5)
+    except (smtplib.SMTPConnectError, socket.error), e:
+        return False, str(e)
+
+    # auth
+    if smtp_user is not None and smtp_password is not None:
+        try:
+            server.login(smtp_user, smtp_password)
+        except (smtplib.SMTPHeloError,
+                smtplib.SMTPAuthenticationError,
+                smtplib.SMTPException), e:
+            return False, str(e)
+
+    # the actual sending
+    try:
+        server.sendmail(sender, [rcpt], msg.as_string())
+    finally:
+        server.quit()
+
+    return True, None
+
+
+MSG = """\
+Hi there,
+
+%(author)s has commited in "%(repo)s"
+
+The code was reviewed by %(reviewers)s.
+
+Added:
+%(added)s
+
+Modified:
+%(modified)s
+
+Removed:
+%(removed)s
+
+Sincerly,
+
+--
+Mozilla Pulse
+"""
+
+
+def got_message(data, message):
+    # Do something with the message data
+    print 'got an event.'
+
+    # Ack the message to tell pulse we processed it
+    message.ack()
+
+    payload = data['payload']
+
+    repo = payload['repository']
+
+    if not repo.startswith('services'):
+        return
+
+    # hey that's us ! let's send an e-mail with the diff
+    if 'commits' in payload:
+        commits = payload['commits']
+    elif 'commits' in payload:
+        commits = payload['changesets']
+    else:
+        commits = [payload]
+
+    for changeset in commits:
+        info = {}
+        info['repo'] = repo
+        info['msg'] = changeset['message']
+        info['author'] = changeset['author']['name']
+        info['files'] = changeset['files']
+        reviewers = changeset.get('reviewers', [])
+        if len(reviewers) == 0:
+            reviewers = ['None']
+        info['reviewers'] = ', '.join(reviewers)
+
+        for kind in ('added', 'modified', 'removed'):
+            files_ = changeset['files'][kind]
+            if files_ == []:
+                files_ = ['None']
+            info[kind] = '\n'.join(['- %s' % f for f in files_])
+
+        # need to build the diff...
+        send_email('tarek@mozilla.com', 'tarek@mozilla.com',
+                   '[checkins] %s' % changeset['message'], MSG % info)
+
+
+def main():
+
+    # Let's generate a unique label for the script
+    try:
+        import uuid
+        unique_label = 'quickstart-%s' % uuid.uuid4()
+    except:
+        from datetime import datetime
+        unique_label = 'quickstart-%s' % datetime.now()
+
+    # Connect to pulse
+    pulse = consumers.CodeConsumer(applabel=unique_label)
+
+    # Tell pulse that you want to listen for all messages ('#' is everything)
+    # and give a function to call every time there is a message
+    # Print a helpful message
+    pulse.configure(topic='#', durable=True, callback=got_message)
+
+    # Print a helpful message
+    print 'Listening...\n'
+
+    # Block and call the callback function when a message comes in
+    pulse.listen()
+
+
+if __name__ == "__main__":
+    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.