diff mbox series

[v2,04/10] parser: Remove matching label from subject prefixes

Message ID 20181014124541.13393-5-stephen@that.guru
State Changes Requested
Headers show
Series Add labels support | expand

Commit Message

Stephen Finucane Oct. 14, 2018, 12:45 p.m. UTC
Extend the parser to strip the label from the list of prefixes. This
means we don't duplicate things twice in the UI.

Signed-off-by: Stephen Finucane <stephen@that.guru>
---
I'm not 100% sure about this which is why I've kept it separate. On one
hand, it doesn't make sense to duplicate things like this. On another,
not every label is something we'd want to parse from the subject line
and it makes prefixes in the UI kind of useless. Perhaps we should add a
'Label.parseable' flag?
---
 patchwork/parser.py            |  9 +++++++--
 patchwork/tests/test_parser.py | 19 +++++++++++++------
 2 files changed, 20 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/patchwork/parser.py b/patchwork/parser.py
index 8c5106c9..9393a062 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -498,8 +498,13 @@  def parse_labels(subject_prefixes, project):
     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)
+    labels = Label.objects.filter(Q(project=project) | Q(project=None),
+                                  name__in=subject_prefixes)
+    for label in labels:
+        if label.name in subject_prefixes:
+            subject_prefixes.remove(label.name)
+
+    return labels
 
 
 def _find_content(mail):
diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py
index 9a4066fe..39f40d56 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -905,16 +905,23 @@  class SubjectTest(TestCase):
     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)), [])
+        prefixes = ['RFC']
+        self.assertEqual(list(parse_labels(prefixes, label.project)), [label])
+        self.assertEqual(prefixes, [])
 
-        project = create_project()
+        prefixes = ['rfcx']
+        self.assertEqual(list(parse_labels(prefixes, label.project)), [])
+        self.assertEqual(prefixes, ['rfcx'])
 
-        self.assertEqual(list(parse_labels(['RFC'], project)), [])
+        project = create_project()
+        prefixes = ['RFC']
+        self.assertEqual(list(parse_labels(prefixes, project)), [])
+        self.assertEqual(prefixes, ['RFC'])
 
         label = create_label(name='stuff', project=None)
-
-        self.assertEqual(list(parse_labels(['stuff'], project)), [label])
+        prefixes = ['stuff']
+        self.assertEqual(list(parse_labels(prefixes, project)), [label])
+        self.assertEqual(prefixes, []])
 
 
 class SubjectMatchTest(TestCase):