X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=apps%2Fpatchwork%2Fbin%2Fparsemail.py;h=52c85fe362bbe27fea1542061f899060b85c9623;hb=f6e0464d987dc00fa5e9e31972c98196be4fb6b9;hp=68bd94cd37174a4f25851f2a2596cea2e56f34d9;hpb=7d9334e879857f8a380bc9509b6cbf9972cecc25;p=patchwork diff --git a/apps/patchwork/bin/parsemail.py b/apps/patchwork/bin/parsemail.py index 68bd94c..52c85fe 100755 --- a/apps/patchwork/bin/parsemail.py +++ b/apps/patchwork/bin/parsemail.py @@ -34,9 +34,11 @@ except ImportError: from email.Utils import parsedate_tz, mktime_tz from patchwork.parser import parse_patch -from patchwork.models import Patch, Project, Person, Comment +from patchwork.models import Patch, Project, Person, Comment, State +from django.contrib.auth.models import User -list_id_headers = ['List-ID', 'X-Mailing-List'] +default_patch_state = 'New' +list_id_headers = ['List-ID', 'X-Mailing-List', 'X-list'] whitespace_re = re.compile('\s+') def normalise_space(str): @@ -135,9 +137,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*([\S]+://[^\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 +171,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' @@ -169,10 +185,9 @@ def find_content(project, mail): patch = None comment = None - if patchbuf: - mail_headers(mail) - name = clean_subject(mail.get('Subject'), [project.linkname]) - patch = Patch(name = name, content = patchbuf, + if pullurl or patchbuf: + name = clean_subject(mail.get('Subject'), [project.linkname]) + patch = Patch(name = name, pull_url = pullurl, content = patchbuf, date = mail_date(mail), headers = mail_headers(mail)) if commentbuf: @@ -287,6 +302,8 @@ def clean_subject(subject, drop_prefixes = None): '[bar] meep' """ + subject = clean_header(subject) + if drop_prefixes is None: drop_prefixes = [] else: @@ -325,6 +342,24 @@ def clean_content(str): str = sig_re.sub('', str) return str.strip() +def get_state(state_name): + """ Return the state with the given name or the default State """ + if state_name: + try: + return State.objects.get(name__iexact=state_name) + except State.DoesNotExist: + pass + return State.objects.get(name=default_patch_state) + +def get_delegate(delegate_email): + """ Return the delegate with the given email or None """ + if delegate_email: + try: + return User.objects.get(email__iexact=delegate_email) + except User.DoesNotExist: + pass + return None + def parse_mail(mail): # some basic sanity checks @@ -360,6 +395,9 @@ def parse_mail(mail): patch.submitter = author patch.msgid = msgid patch.project = project + patch.state = get_state(mail.get('X-Patchwork-State', '').strip()) + patch.delegate = get_delegate( + mail.get('X-Patchwork-Delegate', '').strip()) try: patch.save() except Exception, ex: