[PATCH v2 03/10] parser: Extract and save labels

Stephen Finucane stephen at that.guru
Sun Oct 14 23:45:34 AEDT 2018


Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/parser.py            | 17 +++++++++++++++++
 patchwork/tests/test_parser.py | 16 ++++++++++++++++
 patchwork/tests/utils.py       | 15 +++++++++++++++
 3 files changed, 48 insertions(+)

diff --git a/patchwork/parser.py b/patchwork/parser.py
index 2ba1db74..8c5106c9 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -16,12 +16,14 @@ import re
 
 from django.contrib.auth.models import User
 from django.db.utils import IntegrityError
+from django.db.models import Q
 from django.utils import six
 
 from patchwork.models import Comment
 from patchwork.models import CoverLetter
 from patchwork.models import DelegationRule
 from patchwork.models import get_default_initial_patch_state
+from patchwork.models import Label
 from patchwork.models import Patch
 from patchwork.models import Person
 from patchwork.models import Project
@@ -490,6 +492,16 @@ def parse_version(subject, subject_prefixes):
     return 1
 
 
+def parse_labels(subject_prefixes, project):
+    """Extract labels from subject.
+
+    Args:
+        subject_prefixes: List of subject prefixes to extract tags from
+    """
+    return Label.objects.filter(Q(project=project) | Q(project=None),
+                                name__in=subject_prefixes)
+
+
 def _find_content(mail):
     """Extract the payload(s) from a mail.
 
@@ -1004,6 +1016,8 @@ def parse_mail(mail, list_id=None):
             filenames = find_filenames(diff)
             delegate = find_delegate_by_filename(project, filenames)
 
+        labels = parse_labels(prefixes, project)
+
         try:
             patch = Patch.objects.create(
                 msgid=msgid,
@@ -1022,6 +1036,9 @@ def parse_mail(mail, list_id=None):
         except IntegrityError:
             raise DuplicateMailError(msgid=msgid)
 
+        if labels:
+            patch.labels.set(labels)
+
         # if we don't have a series marker, we will never have an existing
         # series to match against.
         series = None
diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py
index a9df5e35..9a4066fe 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -25,6 +25,7 @@ from patchwork.parser import find_patch_content as find_content
 from patchwork.parser import find_comment_content
 from patchwork.parser import find_project
 from patchwork.parser import find_series
+from patchwork.parser import parse_labels
 from patchwork.parser import parse_mail as _parse_mail
 from patchwork.parser import parse_pull_request
 from patchwork.parser import parse_series_marker
@@ -33,6 +34,7 @@ from patchwork.parser import split_prefixes
 from patchwork.parser import subject_check
 from patchwork.tests import TEST_MAIL_DIR
 from patchwork.tests import TEST_FUZZ_DIR
+from patchwork.tests.utils import create_label
 from patchwork.tests.utils import create_project
 from patchwork.tests.utils import create_series
 from patchwork.tests.utils import create_series_reference
@@ -900,6 +902,20 @@ class SubjectTest(TestCase):
         self.assertEqual(parse_version('Hello, world (v2)', []), 2)
         self.assertEqual(parse_version('Hello, world (V6)', []), 6)
 
+    def test_labels(self):
+        label = create_label(name='RFC')
+
+        self.assertEqual(list(parse_labels(['RFC'], label.project)), [label])
+        self.assertEqual(list(parse_labels(['rfcx'], label.project)), [])
+
+        project = create_project()
+
+        self.assertEqual(list(parse_labels(['RFC'], project)), [])
+
+        label = create_label(name='stuff', project=None)
+
+        self.assertEqual(list(parse_labels(['stuff'], project)), [label])
+
 
 class SubjectMatchTest(TestCase):
 
diff --git a/patchwork/tests/utils.py b/patchwork/tests/utils.py
index d280fd69..38452f00 100644
--- a/patchwork/tests/utils.py
+++ b/patchwork/tests/utils.py
@@ -15,6 +15,7 @@ from patchwork.models import Bundle
 from patchwork.models import Check
 from patchwork.models import Comment
 from patchwork.models import CoverLetter
+from patchwork.models import Label
 from patchwork.models import Patch
 from patchwork.models import Person
 from patchwork.models import Project
@@ -252,6 +253,20 @@ def create_series_reference(**kwargs):
     return SeriesReference.objects.create(**values)
 
 
+def create_label(**kwargs):
+    """Create a 'Label' object."""
+    num = Label.objects.count()
+
+    values = {
+        'name': 'label%d' % num,
+        'project': create_project() if 'project' not in kwargs else None,
+        'color': '#fff',
+    }
+    values.update(**kwargs)
+
+    return Label.objects.create(**values)
+
+
 def _create_submissions(create_func, count=1, **kwargs):
     """Create 'count' Submission-based objects.
 
-- 
2.17.1



More information about the Patchwork mailing list