[PATCH 03/11] parser: Extract and save labels
Stephen Finucane
stephen at that.guru
Mon Apr 16 08:53:57 AEST 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 805037c7..7ac527a2 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -29,12 +29,14 @@ import logging
import re
from django.contrib.auth.models import User
+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
@@ -497,6 +499,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.
@@ -1001,6 +1013,8 @@ def parse_mail(mail, list_id=None):
filenames = find_filenames(diff)
delegate = find_delegate_by_filename(project, filenames)
+ labels = parse_labels(prefixes, project)
+
patch = Patch.objects.create(
msgid=msgid,
project=project,
@@ -1016,6 +1030,9 @@ def parse_mail(mail, list_id=None):
state=find_state(mail))
logger.debug('Patch saved')
+ 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 5ba06c0f..50a07523 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -38,6 +38,7 @@ from patchwork.parser import get_or_create_author
from patchwork.parser import find_patch_content as find_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
@@ -46,6 +47,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
@@ -888,6 +890,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 00eb6c2a..e51862fc 100644
--- a/patchwork/tests/utils.py
+++ b/patchwork/tests/utils.py
@@ -29,6 +29,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
@@ -266,6 +267,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.14.3
More information about the Patchwork
mailing list