]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/bin/parsemail.py
parser: Decode the subject header
[patchwork] / apps / patchwork / bin / parsemail.py
index 68bd94cd37174a4f25851f2a2596cea2e56f34d9..0a9daf541d6b8f5937639c6292dc70b28d199d00 100755 (executable)
@@ -36,7 +36,7 @@ except ImportError:
 from patchwork.parser import parse_patch
 from patchwork.models import Patch, Project, Person, Comment
 
-list_id_headers = ['List-ID', 'X-Mailing-List']
+list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list']
 
 whitespace_re = re.compile('\s+')
 def normalise_space(str):
@@ -135,9 +135,20 @@ def mail_headers(mail):
                     continuation_ws = '\t').encode()) \
                 for (k, v) in mail.items()])
 
+def find_pull_request(content):
+    git_re = re.compile('^The following changes since commit .*' +
+                        '^are available in the git repository at:\n'
+                        '^\s*(git://[^\n]+)$',
+                           re.DOTALL | re.MULTILINE)
+    match = git_re.search(content)
+    if match:
+        return match.group(1)
+    return None
+
 def find_content(project, mail):
     patchbuf = None
     commentbuf = ''
+    pullurl = None
 
     for part in mail.walk():
         if part.get_content_maintype() != 'text':
@@ -158,10 +169,13 @@ def find_content(project, mail):
             patchbuf = payload
 
         elif subtype == 'plain':
+            c = payload
+
             if not patchbuf:
                 (patchbuf, c) = parse_patch(payload)
-            else:
-                c = payload
+
+            if not pullurl:
+                pullurl = find_pull_request(payload)
 
             if c is not None:
                 commentbuf += c.strip() + '\n'
@@ -171,10 +185,15 @@ def find_content(project, mail):
 
     if patchbuf:
         mail_headers(mail)
-       name = clean_subject(mail.get('Subject'), [project.linkname])
+        name = clean_subject(mail.get('Subject'), [project.linkname])
         patch = Patch(name = name, content = patchbuf,
                     date = mail_date(mail), headers = mail_headers(mail))
 
+    if pullurl:
+        name = clean_subject(mail.get('Subject'), [project.linkname])
+        patch = Patch(name = name, pull_url = pullurl,
+                    date = mail_date(mail), headers = mail_headers(mail))
+
     if commentbuf:
         if patch:
             cpatch = patch
@@ -287,6 +306,8 @@ def clean_subject(subject, drop_prefixes = None):
     '[bar] meep'
     """
 
+    subject = clean_header(subject)
+
     if drop_prefixes is None:
         drop_prefixes = []
     else: