From patchwork Mon Jul 9 16:10:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Veronika Kabatova X-Patchwork-Id: 941411 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41PVgY21pfz9ryt for ; Tue, 10 Jul 2018 02:11:05 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 41PVgY0kCHzDr55 for ; Tue, 10 Jul 2018 02:11:05 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=redhat.com (client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=vkabatov@redhat.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 41PVg068jlzDr1S for ; Tue, 10 Jul 2018 02:10:35 +1000 (AEST) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 13C7C4074442 for ; Mon, 9 Jul 2018 16:10:33 +0000 (UTC) Received: from vkabatova.usersys.redhat.com (unknown [10.43.17.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7A0EE2156891; Mon, 9 Jul 2018 16:10:32 +0000 (UTC) From: vkabatov@redhat.com To: patchwork@lists.ozlabs.org Subject: [PATCH RESEND 1/2] Rework tagging infrastructure Date: Mon, 9 Jul 2018 18:10:21 +0200 Message-Id: <20180709161022.32622-2-vkabatov@redhat.com> In-Reply-To: <20180709161022.32622-1-vkabatov@redhat.com> References: <20180709161022.32622-1-vkabatov@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 09 Jul 2018 16:10:33 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 09 Jul 2018 16:10:33 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'vkabatov@redhat.com' RCPT:'' X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" From: Veronika Kabatova Solve #113 and #57 GitHub issues, fix up returning tags in the API, keep track of tag origin to be able to add tags to comments in the API. Use relations Tag-Patch and Tag-CoverLetter to avoid duplication of tags for each patch in series, and use `series` attribute of SubmissionTag as a notion of a tag which is related to each patch in the series (because it comes from cover letter or it's comments) Signed-off-by: Veronika Kabatova --- patchwork/api/comment.py | 18 ++++- patchwork/api/cover.py | 19 ++++- patchwork/api/filters.py | 68 +++++++++++++++- patchwork/api/patch.py | 18 ++++- patchwork/models.py | 173 ++++++++++++++++------------------------ patchwork/templatetags/patch.py | 3 +- patchwork/views/__init__.py | 3 - patchwork/views/utils.py | 10 ++- 8 files changed, 191 insertions(+), 121 deletions(-) diff --git a/patchwork/api/comment.py b/patchwork/api/comment.py index 5a5adb1..e03207e 100644 --- a/patchwork/api/comment.py +++ b/patchwork/api/comment.py @@ -26,6 +26,7 @@ from patchwork.api.base import BaseHyperlinkedModelSerializer from patchwork.api.base import PatchworkPermission from patchwork.api.embedded import PersonSerializer from patchwork.models import Comment +from patchwork.models import SubmissionTag class CommentListSerializer(BaseHyperlinkedModelSerializer): @@ -34,6 +35,7 @@ class CommentListSerializer(BaseHyperlinkedModelSerializer): subject = SerializerMethodField() headers = SerializerMethodField() submitter = PersonSerializer(read_only=True) + tags = SerializerMethodField() def get_web_url(self, instance): request = self.context.get('request') @@ -43,6 +45,20 @@ class CommentListSerializer(BaseHyperlinkedModelSerializer): return email.parser.Parser().parsestr(comment.headers, True).get('Subject', '') + def get_tags(self, instance): + if not instance.submission.project.use_tags: + return {} + + tags = SubmissionTag.objects.filter( + comment=instance + ).values_list('tag__name', 'value') + + result = {} + for name, value in tags: + result.setdefault(name, []).append(value) + + return result + def get_headers(self, comment): headers = {} @@ -60,7 +76,7 @@ class CommentListSerializer(BaseHyperlinkedModelSerializer): class Meta: model = Comment fields = ('id', 'web_url', 'msgid', 'date', 'subject', 'submitter', - 'content', 'headers') + 'content', 'headers', 'tags') read_only_fields = fields versioned_fields = { '1.1': ('web_url', ), diff --git a/patchwork/api/cover.py b/patchwork/api/cover.py index b497fd8..b17b9f7 100644 --- a/patchwork/api/cover.py +++ b/patchwork/api/cover.py @@ -30,6 +30,7 @@ from patchwork.api.embedded import PersonSerializer from patchwork.api.embedded import ProjectSerializer from patchwork.api.embedded import SeriesSerializer from patchwork.models import CoverLetter +from patchwork.models import SubmissionTag class CoverLetterListSerializer(BaseHyperlinkedModelSerializer): @@ -40,6 +41,7 @@ class CoverLetterListSerializer(BaseHyperlinkedModelSerializer): mbox = SerializerMethodField() series = SeriesSerializer(many=True, read_only=True) comments = SerializerMethodField() + tags = SerializerMethodField() def get_web_url(self, instance): request = self.context.get('request') @@ -53,13 +55,26 @@ class CoverLetterListSerializer(BaseHyperlinkedModelSerializer): return self.context.get('request').build_absolute_uri( reverse('api-cover-comment-list', kwargs={'pk': cover.id})) + def get_tags(self, instance): + if not instance.project.use_tags: + return {} + + tags = SubmissionTag.objects.filter( + submission=instance).values_list('tag__name', 'value').distinct() + + result = {} + for name, value in tags: + result.setdefault(name, []).append(value) + + return result + class Meta: model = CoverLetter fields = ('id', 'url', 'web_url', 'project', 'msgid', 'date', 'name', - 'submitter', 'mbox', 'series', 'comments') + 'submitter', 'mbox', 'series', 'comments', 'tags') read_only_fields = fields versioned_fields = { - '1.1': ('web_url', 'mbox', 'comments'), + '1.1': ('web_url', 'mbox', 'comments', 'tags'), } extra_kwargs = { 'url': {'view_name': 'api-cover-detail'}, diff --git a/patchwork/api/filters.py b/patchwork/api/filters.py index 73353d9..e213181 100644 --- a/patchwork/api/filters.py +++ b/patchwork/api/filters.py @@ -21,9 +21,11 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db.models import Q from django_filters.rest_framework import FilterSet +from django_filters import Filter from django_filters import IsoDateTimeFilter from django_filters import ModelMultipleChoiceFilter from django.forms import ModelMultipleChoiceField as BaseMultipleChoiceField +from django.forms.widgets import HiddenInput from django.forms.widgets import MultipleHiddenInput from patchwork.models import Bundle @@ -35,6 +37,7 @@ from patchwork.models import Person from patchwork.models import Project from patchwork.models import Series from patchwork.models import State +from patchwork.models import SubmissionTag # custom fields, filters @@ -136,6 +139,60 @@ class StateFilter(ModelMultipleChoiceFilter): field_class = StateChoiceField +class TagNameFilter(Filter): + + def filter(self, qs, tag_name): + submissions_series = SubmissionTag.objects.filter( + tag__name__iexact=tag_name + ).values_list('submission__id', 'series') + + submission_list = [] + series_list = [] + for submission, series in submissions_series: + submission_list.append(submission) + series_list.append(series) + + return qs.filter(Q(id__in=submission_list) | Q(series__in=series_list)) + + +class TagValueFilter(Filter): + + def filter(self, qs, tag_value): + submissions_series = SubmissionTag.objects.filter( + value__icontains=tag_value + ).values_list('submission__id', 'series') + + submission_list = [] + series_list = [] + for submission, series in submissions_series: + submission_list.append(submission) + series_list.append(series) + + return qs.filter(Q(id__in=submission_list) | Q(series__in=series_list)) + + +class TagFilter(Filter): + + def filter(self, qs, query): + try: + tag_name, tag_value = query.split(',', 1) + except ValueError: + raise ValidationError('Query in format `tag=name,value` expected!') + + submissions_series = SubmissionTag.objects.filter( + tag__name__iexact=tag_name, + value__icontains=tag_value + ).values_list('submission__id', 'series') + + submission_list = [] + series_list = [] + for submission, series in submissions_series: + submission_list.append(submission) + series_list.append(series) + + return qs.filter(Q(id__in=submission_list) | Q(series__in=series_list)) + + class UserChoiceField(ModelMultipleChoiceField): alternate_lookup = 'username__iexact' @@ -173,10 +230,14 @@ class CoverLetterFilterSet(TimestampMixin, FilterSet): series = BaseFilter(queryset=Project.objects.all(), widget=MultipleHiddenInput) submitter = PersonFilter(queryset=Person.objects.all()) + tagname = TagNameFilter(label='Tag name') + tagvalue = TagValueFilter(widget=HiddenInput) + tag = TagFilter(widget=HiddenInput) class Meta: model = CoverLetter - fields = ('project', 'series', 'submitter') + fields = ('project', 'series', 'submitter', 'tagname', 'tagvalue', + 'tag') class PatchFilterSet(TimestampMixin, FilterSet): @@ -189,11 +250,14 @@ class PatchFilterSet(TimestampMixin, FilterSet): submitter = PersonFilter(queryset=Person.objects.all()) delegate = UserFilter(queryset=User.objects.all()) state = StateFilter(queryset=State.objects.all()) + tagname = TagNameFilter(label='Tag name') + tagvalue = TagValueFilter(widget=HiddenInput) + tag = TagFilter(widget=HiddenInput) class Meta: model = Patch fields = ('project', 'series', 'submitter', 'delegate', - 'state', 'archived') + 'state', 'archived', 'tagname', 'tagvalue', 'tag') class CheckFilterSet(TimestampMixin, FilterSet): diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py index 9d890eb..cf97c40 100644 --- a/patchwork/api/patch.py +++ b/patchwork/api/patch.py @@ -19,6 +19,7 @@ import email.parser +from django.db.models import Q from django.utils.translation import ugettext_lazy as _ from rest_framework.generics import ListAPIView from rest_framework.generics import RetrieveUpdateAPIView @@ -35,6 +36,7 @@ from patchwork.api.embedded import SeriesSerializer from patchwork.api.embedded import UserSerializer from patchwork.models import Patch from patchwork.models import State +from patchwork.models import SubmissionTag from patchwork.parser import clean_subject @@ -109,9 +111,18 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer): reverse('api-check-list', kwargs={'patch_id': instance.id})) def get_tags(self, instance): - # TODO(stephenfin): Make tags performant, possibly by reworking the - # model - return {} + if not instance.project.use_tags: + return {} + + tags = SubmissionTag.objects.filter( + Q(submission=instance) | Q(series__in=instance.series.all()) + ).values_list('tag__name', 'value').distinct() + + result = {} + for name, value in tags: + result.setdefault(name, []).append(value) + + return result class Meta: model = Patch @@ -160,6 +171,7 @@ class PatchDetailSerializer(PatchListSerializer): 'headers', 'content', 'diff', 'prefixes') versioned_fields = PatchListSerializer.Meta.versioned_fields extra_kwargs = PatchListSerializer.Meta.extra_kwargs + versioned_fields = PatchListSerializer.Meta.versioned_fields class PatchList(ListAPIView): diff --git a/patchwork/models.py b/patchwork/models.py index 6268f5b..ba53278 100644 --- a/patchwork/models.py +++ b/patchwork/models.py @@ -20,8 +20,6 @@ from __future__ import absolute_import -from collections import Counter -from collections import OrderedDict import datetime import random import re @@ -31,6 +29,7 @@ from django.conf import settings from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models +from django.db.models import Q from django.utils.encoding import python_2_unicode_compatible from django.utils.functional import cached_property @@ -252,10 +251,6 @@ class Tag(models.Model): ' tag\'s count in the patch list view', default=True) - @property - def attr_name(self): - return 'tag_%d_count' % self.id - def __str__(self): return self.name @@ -263,64 +258,21 @@ class Tag(models.Model): ordering = ['abbrev'] -class PatchTag(models.Model): - patch = models.ForeignKey('Patch', on_delete=models.CASCADE) +class SubmissionTag(models.Model): + submission = models.ForeignKey('Submission', on_delete=models.CASCADE) tag = models.ForeignKey('Tag', on_delete=models.CASCADE) - count = models.IntegerField(default=1) + value = models.CharField(max_length=255) + comment = models.ForeignKey('Comment', null=True, on_delete=models.CASCADE) + series = models.ForeignKey('Series', null=True, on_delete=models.CASCADE) class Meta: - unique_together = [('patch', 'tag')] + unique_together = [('submission', 'tag', 'value', 'comment')] def get_default_initial_patch_state(): return State.objects.get(ordering=0) -class PatchQuerySet(models.query.QuerySet): - - def with_tag_counts(self, project=None): - if project and not project.use_tags: - return self - - # We need the project's use_tags field loaded for Project.tags(). - # Using prefetch_related means we'll share the one instance of - # Project, and share the project.tags cache between all patch.project - # references. - qs = self.prefetch_related('project') - select = OrderedDict() - select_params = [] - - # All projects have the same tags, so we're good to go here - if project: - tags = project.tags - else: - tags = Tag.objects.all() - - for tag in tags: - select[tag.attr_name] = ( - "coalesce(" - "(SELECT count FROM patchwork_patchtag" - " WHERE patchwork_patchtag.patch_id=" - "patchwork_patch.submission_ptr_id" - " AND patchwork_patchtag.tag_id=%s), 0)") - select_params.append(tag.id) - - return qs.extra(select=select, select_params=select_params) - - -class PatchManager(models.Manager): - use_for_related_fields = True - # NOTE(stephenfin): This is necessary to silence a warning with Django >= - # 1.10. Remove when 1.10 is the minimum supported version. - silence_use_for_related_fields_deprecation = True - - def get_queryset(self): - return PatchQuerySet(self.model, using=self.db) - - def with_tag_counts(self, project): - return self.get_queryset().with_tag_counts(project) - - class EmailMixin(models.Model): """Mixin for models with an email-origin.""" # email metadata @@ -334,17 +286,16 @@ class EmailMixin(models.Model): submitter = models.ForeignKey(Person, on_delete=models.CASCADE) content = models.TextField(null=True, blank=True) - response_re = re.compile( - r'^(Tested|Reviewed|Acked|Signed-off|Nacked|Reported)-by:.*$', - re.M | re.I) + def _extract_tags(self, tags): + found_tags = {} - @property - def patch_responses(self): if not self.content: - return '' + return found_tags - return ''.join([match.group(0) + '\n' for match in - self.response_re.finditer(self.content)]) + for tag in tags: + regex = re.compile(tag.pattern + r'\s*(.*)', re.M | re.I | re.U) + found_tags[tag] = regex.findall(self.content) + return found_tags def save(self, *args, **kwargs): # Modifying a submission via admin interface changes '\n' newlines in @@ -377,6 +328,40 @@ class Submission(FilenameMixin, EmailMixin, models.Model): # submission metadata name = models.CharField(max_length=255) + related_tags = models.ManyToManyField(Tag, through=SubmissionTag) + + def add_tags(self, tag, values, comment=None): + if hasattr(self, 'patch'): + series = None + else: + series = self.coverletter.series.first() + + current_objs = SubmissionTag.objects.filter(submission=self, + comment=comment, + tag=tag, + series=series) + + if not values: + current_objs.delete() + return + + # In case the origin is modified, delete tags that were removed + current_objs.exclude(value__in=values).delete() + + values_to_add = set(values) - set(current_objs.values_list('value', + flat=True)) + SubmissionTag.objects.bulk_create([SubmissionTag( + submission=self, + tag=tag, + value=value, + comment=comment, + series=series + ) for value in values_to_add]) + + def refresh_tags(self): + submission_tags = self._extract_tags(Tag.objects.all()) + for tag in submission_tags: + self.add_tags(tag, submission_tags[tag]) # patchwork metadata @@ -426,7 +411,6 @@ class Patch(SeriesMixin, Submission): diff = models.TextField(null=True, blank=True) commit_ref = models.CharField(max_length=255, null=True, blank=True) pull_url = models.CharField(max_length=255, null=True, blank=True) - tags = models.ManyToManyField(Tag, through=PatchTag) # patchwork metadata @@ -440,40 +424,6 @@ class Patch(SeriesMixin, Submission): # patches in a project without needing to do a JOIN. patch_project = models.ForeignKey(Project, on_delete=models.CASCADE) - objects = PatchManager() - - @staticmethod - def extract_tags(content, tags): - counts = Counter() - - for tag in tags: - regex = re.compile(tag.pattern, re.MULTILINE | re.IGNORECASE) - counts[tag] = len(regex.findall(content)) - - return counts - - def _set_tag(self, tag, count): - if count == 0: - self.patchtag_set.filter(tag=tag).delete() - return - patchtag, _ = PatchTag.objects.get_or_create(patch=self, tag=tag) - if patchtag.count != count: - patchtag.count = count - patchtag.save() - - def refresh_tag_counts(self): - tags = self.project.tags - counter = Counter() - - if self.content: - counter += self.extract_tags(self.content, tags) - - for comment in self.comments.all(): - counter = counter + self.extract_tags(comment.content, tags) - - for tag in tags: - self._set_tag(tag, counter[tag]) - def save(self, *args, **kwargs): if not hasattr(self, 'state') or not self.state: self.state = get_default_initial_patch_state() @@ -482,8 +432,7 @@ class Patch(SeriesMixin, Submission): self.hash = hash_diff(self.diff) super(Patch, self).save(**kwargs) - - self.refresh_tag_counts() + self.refresh_tags() def is_editable(self, user): if not is_authenticated(user): @@ -495,6 +444,18 @@ class Patch(SeriesMixin, Submission): return self.project.is_editable(user) @property + def all_tags(self): + related_tags = SubmissionTag.objects.filter( + Q(submission=self) | Q(series__in=self.series.all()) + ).values_list('tag__name', 'value').distinct() + + patch_tags = {} + for tag in self.project.tags: + patch_tags[tag] = [related_tag[1] for related_tag in related_tags + if related_tag[0] == tag.name] + return patch_tags + + @property def combined_check_state(self): """Return the combined state for all checks. @@ -611,13 +572,12 @@ class Comment(EmailMixin, models.Model): def save(self, *args, **kwargs): super(Comment, self).save(*args, **kwargs) - if hasattr(self.submission, 'patch'): - self.submission.patch.refresh_tag_counts() + self.refresh_tags() - def delete(self, *args, **kwargs): - super(Comment, self).delete(*args, **kwargs) - if hasattr(self.submission, 'patch'): - self.submission.patch.refresh_tag_counts() + def refresh_tags(self): + comment_tags = self._extract_tags(Tag.objects.all()) + for tag in comment_tags: + self.submission.add_tags(tag, comment_tags[tag], comment=self) def is_editable(self, user): return False @@ -716,6 +676,7 @@ class Series(FilenameMixin, models.Model): self.name = self._format_name(cover) self.save() + cover.refresh_tags() def add_patch(self, patch, number): """Add a patch to the series.""" diff --git a/patchwork/templatetags/patch.py b/patchwork/templatetags/patch.py index 4350e09..f226dc0 100644 --- a/patchwork/templatetags/patch.py +++ b/patchwork/templatetags/patch.py @@ -34,8 +34,9 @@ register = template.Library() def patch_tags(patch): counts = [] titles = [] + all_tags = patch.all_tags for tag in [t for t in patch.project.tags if t.show_column]: - count = getattr(patch, tag.attr_name) + count = len(all_tags[tag]) titles.append('%d %s' % (count, tag.name)) if count == 0: counts.append("-") diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py index f8d23a3..8c41df6 100644 --- a/patchwork/views/__init__.py +++ b/patchwork/views/__init__.py @@ -272,9 +272,6 @@ def generic_list(request, project, view, view_args=None, filter_settings=None, if patches is None: patches = Patch.objects.filter(patch_project=project) - # annotate with tag counts - patches = patches.with_tag_counts(project) - patches = context['filters'].apply(patches) if not editable_order: patches = order.apply(patches) diff --git a/patchwork/views/utils.py b/patchwork/views/utils.py index 2357ab8..f1f78c8 100644 --- a/patchwork/views/utils.py +++ b/patchwork/views/utils.py @@ -27,11 +27,13 @@ import email.utils import re from django.conf import settings +from django.db.models import Q from django.http import Http404 from django.utils import six from patchwork.models import Comment from patchwork.models import Patch +from patchwork.models import SubmissionTag from patchwork.models import Series if settings.ENABLE_REST_API: @@ -75,9 +77,11 @@ def _submission_to_mbox(submission): else: postscript = '' - # TODO(stephenfin): Make this use the tags infrastructure - for comment in Comment.objects.filter(submission=submission): - body += comment.patch_responses + for (tagname, value) in SubmissionTag.objects.filter( + Q(submission=submission) + | Q(series__in=submission.series.all())).values_list( + 'tag__name', 'value').distinct(): + body += '%s: %s\n' % (tagname, value) if postscript: body += '---\n' + postscript + '\n' From patchwork Mon Jul 9 16:10:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Veronika Kabatova X-Patchwork-Id: 941412 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41PVgy2MNSz9ryt for ; Tue, 10 Jul 2018 02:11:26 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 41PVgy17FQzDrTg for ; Tue, 10 Jul 2018 02:11:26 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=redhat.com (client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=vkabatov@redhat.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 41PVg16NKQzDr1S for ; Tue, 10 Jul 2018 02:10:37 +1000 (AEST) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A597F401EF09 for ; Mon, 9 Jul 2018 16:10:35 +0000 (UTC) Received: from vkabatova.usersys.redhat.com (unknown [10.43.17.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0D5E02156891; Mon, 9 Jul 2018 16:10:34 +0000 (UTC) From: vkabatov@redhat.com To: patchwork@lists.ozlabs.org Subject: [PATCH RESEND 2/2] Rework tagging infrastructure Date: Mon, 9 Jul 2018 18:10:22 +0200 Message-Id: <20180709161022.32622-3-vkabatov@redhat.com> In-Reply-To: <20180709161022.32622-1-vkabatov@redhat.com> References: <20180709161022.32622-1-vkabatov@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Mon, 09 Jul 2018 16:10:35 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Mon, 09 Jul 2018 16:10:35 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'vkabatov@redhat.com' RCPT:'' X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" From: Veronika Kabatova Solve #113 and #57 GitHub issues, fix up returning tags in the API, keep track of tag origin to be able to add tags to comments in the API. Use relations Tag-Patch and Tag-CoverLetter to avoid duplication of tags for each patch in series, and use `series` attribute of SubmissionTag as a notion of a tag which is related to each patch in the series (because it comes from cover letter or it's comments) Signed-off-by: Veronika Kabatova --- docs/usage/overview.rst | 9 +- patchwork/management/commands/retag.py | 16 ++- patchwork/migrations/0027_rework_tagging.py | 146 +++++++++++++++++++++ patchwork/tests/api/test_patch.py | 2 +- patchwork/tests/test_mboxviews.py | 18 ++- patchwork/tests/test_parser.py | 23 ++-- patchwork/tests/test_tags.py | 64 ++++----- .../notes/tagging-rework-9907e9dc3f835566.yaml | 18 +++ 8 files changed, 233 insertions(+), 63 deletions(-) create mode 100644 patchwork/migrations/0027_rework_tagging.py create mode 100644 releasenotes/notes/tagging-rework-9907e9dc3f835566.yaml diff --git a/docs/usage/overview.rst b/docs/usage/overview.rst index e84e13d..d3ad2f6 100644 --- a/docs/usage/overview.rst +++ b/docs/usage/overview.rst @@ -119,10 +119,11 @@ one delegate can be assigned to a patch. Tags ~~~~ -Tags are specially formatted metadata appended to the foot the body of a patch -or a comment on a patch. Patchwork extracts these tags at parse time and -associates them with the patch. You add extra tags to an email by replying to -the email. The following tags are available on a standard Patchwork install: +Tags are specially formatted metadata appended to the foot the body of a patch, +cover letter or a comment related to them. Patchwork extracts these tags at +parse time and associates them with patches. You add extra tags to an email by +replying to the email. The following tags are available on a standard Patchwork +install: ``Acked-by:`` For example:: diff --git a/patchwork/management/commands/retag.py b/patchwork/management/commands/retag.py index 8617ff4..150dbf1 100644 --- a/patchwork/management/commands/retag.py +++ b/patchwork/management/commands/retag.py @@ -19,11 +19,13 @@ from django.core.management.base import BaseCommand +from patchwork.models import Cover from patchwork.models import Patch +from patchwork.models import SeriesPatch class Command(BaseCommand): - help = 'Update the tag (Ack/Review/Test) counts on existing patches' + help = 'Update tags on existing patches' args = '[...]' def handle(self, *args, **options): @@ -37,7 +39,17 @@ class Command(BaseCommand): count = query.count() for i, patch in enumerate(query.iterator()): - patch.refresh_tag_counts() + patch.refresh_tags() + for comment in patch.comments.all(): + comment.refresh_tags() + + series_patches = SeriesPatch.objects.filter(patch_id=patch.id) + for series_patch in series_patches: + cover = series_patch.series.cover_letter + cover.refresh_tags() + for comment in cover.comments.all(): + comment.refresh_tags() + if (i % 10) == 0: self.stdout.write('%06d/%06d\r' % (i, count), ending='') self.stdout.flush() diff --git a/patchwork/migrations/0027_rework_tagging.py b/patchwork/migrations/0027_rework_tagging.py new file mode 100644 index 0000000..009546f --- /dev/null +++ b/patchwork/migrations/0027_rework_tagging.py @@ -0,0 +1,146 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + +import re + + +# Django migrations don't allow us to call models' methods because the +# migration will break if the methods change. Therefore we need to use an +# altered copy of all the code needed. +def extract_tags(extract_from, tags): + found_tags = {} + + if not extract_from.content: + return found_tags + + for tag in tags: + regex = re.compile(tag.pattern + r'\s(.*)', re.M | re.I) + found_tags[tag] = regex.findall(extract_from.content) + + return found_tags + + +def add_tags(apps, submission, tag, values, comment=None): + if not values: + # We don't need to delete tags since none exist yet and we can't + # delete comments etc. during the migration + return + + if hasattr(submission, 'patch'): + series = None + else: + series = submission.coverletter.series.first() + + SubmissionTag = apps.get_model('patchwork', 'SubmissionTag') + current_objs = SubmissionTag.objects.filter(submission=self, + comment=comment, + tag=tag, + series=series) + + # Only create nonexistent tags + values_to_add = set(values) - set(current_objs.values_list('value', + flat=True)) + SubmissionTag.objects.bulk_create([SubmissionTag( + submission=submission, + tag=tag, + value=value, + comment=comment, + series=series + ) for value in values_to_add]) + + +def create_all(apps, schema_editor): + Tag = apps.get_model('patchwork', 'Tag') + tags = Tag.objects.all() + + Submission = apps.get_model('patchwork', 'Submission') + for submission in Submission.objects.all(): + extracted = extract_tags(submission, tags) + for tag in extracted: + add_tags(apps, submission, tag, extracted[tag]) + + Comment = apps.get_model('patchwork', 'Comment') + for comment in Comment.objects.all(): + extracted = extract_tags(comment, tags) + for tag in extracted: + add_tags(apps, + comment.submission, + tag, + extracted[tag], + comment=comment) + + +class Migration(migrations.Migration): + + dependencies = [ + ('patchwork', '0026_add_user_bundles_backref'), + ] + + operations = [ + migrations.CreateModel( + name='SubmissionTag', + fields=[ + ('id', models.AutoField(auto_created=True, + primary_key=True, + serialize=False, + verbose_name='ID')), + ('value', models.CharField(max_length=255)), + ('comment', models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to='patchwork.Comment', + null=True + )), + ('submission', models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to='patchwork.Submission' + )), + ('tag', models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to='patchwork.Tag' + )), + ('series', models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to='patchwork.Series', + null=True + )), + ], + ), + migrations.AlterUniqueTogether( + name='patchtag', + unique_together=set([]), + ), + migrations.RemoveField( + model_name='patchtag', + name='patch', + ), + migrations.RemoveField( + model_name='patchtag', + name='tag', + ), + migrations.RemoveField( + model_name='patch', + name='tags', + ), + migrations.DeleteModel( + name='PatchTag', + ), + migrations.AddField( + model_name='submission', + name='related_tags', + field=models.ManyToManyField( + through='patchwork.SubmissionTag', + to='patchwork.Tag' + ), + ), + migrations.AlterUniqueTogether( + name='submissiontag', + unique_together=set([('submission', + 'tag', + 'value', + 'comment')]), + ), + migrations.RunPython(create_all, atomic=False), + ] diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py index 27b9924..55b8efd 100644 --- a/patchwork/tests/api/test_patch.py +++ b/patchwork/tests/api/test_patch.py @@ -166,7 +166,7 @@ class TestPatchAPI(APITestCase): self.assertEqual(patch.content, resp.data['content']) self.assertEqual(patch.diff, resp.data['diff']) - self.assertEqual(0, len(resp.data['tags'])) + self.assertEqual(1, len(resp.data['tags'])) def test_detail_version_1_0(self): patch = create_patch() diff --git a/patchwork/tests/test_mboxviews.py b/patchwork/tests/test_mboxviews.py index 8eb3581..16b5b0c 100644 --- a/patchwork/tests/test_mboxviews.py +++ b/patchwork/tests/test_mboxviews.py @@ -39,6 +39,8 @@ class MboxPatchResponseTest(TestCase): """Test that the mbox view appends the Acked-by from a patch comment.""" + fixtures = ['default_tags'] + def setUp(self): self.project = create_project() self.person = create_person() @@ -53,7 +55,9 @@ class MboxPatchResponseTest(TestCase): submitter=self.person, content='comment 2 text\nAcked-by: 2\n') response = self.client.get(reverse('patch-mbox', args=[patch.id])) - self.assertContains(response, 'Acked-by: 1\nAcked-by: 2\n') + # Can't guarantee the order in which the tags are returned + self.assertContains(response, 'Acked-by: 1\n') + self.assertContains(response, 'Acked-by: 2\n') def test_patch_utf8_nbsp(self): patch = create_patch( @@ -73,6 +77,8 @@ class MboxPatchSplitResponseTest(TestCase): """Test that the mbox view appends the Acked-by from a patch comment, and places it before an '---' update line.""" + fixtures = ['default_tags'] + def setUp(self): project = create_project() self.person = create_person() @@ -88,7 +94,15 @@ class MboxPatchSplitResponseTest(TestCase): def test_patch_response(self): response = self.client.get(reverse('patch-mbox', args=[self.patch.id])) - self.assertContains(response, 'Acked-by: 1\nAcked-by: 2\n') + # Can't guarantee the order in which the tags are returned + self.assertContains(response, 'Acked-by: 1\n') + self.assertContains(response, 'Acked-by: 2\n') + # We need to check for 3 Acked-by strings, one comes from the body of + # the patch and the other two are the tags themselves. + self.assertRegex( + response.content.decode(), + '(?s).*Acked-by: 1\n.*Acked-by.*Acked-by.*---\nupdate.*' + ) class MboxHeaderTest(TestCase): diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py index e99cf21..a29066f 100644 --- a/patchwork/tests/test_parser.py +++ b/patchwork/tests/test_parser.py @@ -802,12 +802,12 @@ class ParseInitialTagsTest(PatchTest): def test_tags(self): self.assertEqual(Patch.objects.count(), 1) patch = Patch.objects.all()[0] - self.assertEqual(patch.patchtag_set.filter( - tag__name='Acked-by').count(), 0) - self.assertEqual(patch.patchtag_set.get( - tag__name='Reviewed-by').count, 1) - self.assertEqual(patch.patchtag_set.get( - tag__name='Tested-by').count, 1) + self.assertEqual(patch.related_tags.filter(name='Acked-by').count(), + 0) + self.assertEqual(patch.related_tags.filter(name='Reviewed-by').count(), + 1) + self.assertEqual(patch.related_tags.filter(name='Tested-by').count(), + 1) class ParseCommentTagsTest(PatchTest): @@ -830,12 +830,11 @@ class ParseCommentTagsTest(PatchTest): def test_tags(self): self.assertEqual(Patch.objects.count(), 1) patch = Patch.objects.all()[0] - self.assertEqual(patch.patchtag_set.filter( - tag__name='Acked-by').count(), 0) - self.assertEqual(patch.patchtag_set.get( - tag__name='Reviewed-by').count, 1) - self.assertEqual(patch.patchtag_set.get( - tag__name='Tested-by').count, 1) + self.assertEqual(patch.related_tags.filter(name='Acked-by').count(), 0) + self.assertEqual(patch.related_tags.filter(name='Reviewed-by')count(), + 1) + self.assertEqual(patch.related_tags.filter(name='Tested-by').count(), + 1) class SubjectTest(TestCase): diff --git a/patchwork/tests/test_tags.py b/patchwork/tests/test_tags.py index 4fd1bf2..f1df99f 100644 --- a/patchwork/tests/test_tags.py +++ b/patchwork/tests/test_tags.py @@ -21,7 +21,7 @@ from django.test import TestCase from django.test import TransactionTestCase from patchwork.models import Patch -from patchwork.models import PatchTag +from patchwork.models import SubmissionTag from patchwork.models import Tag from patchwork.tests.utils import create_comment from patchwork.tests.utils import create_patch @@ -34,11 +34,14 @@ class ExtractTagsTest(TestCase): name_email = 'test name <' + email + '>' def assertTagsEqual(self, str, acks, reviews, tests): # noqa - counts = Patch.extract_tags(str, Tag.objects.all()) - self.assertEqual((acks, reviews, tests), - (counts[Tag.objects.get(name='Acked-by')], - counts[Tag.objects.get(name='Reviewed-by')], - counts[Tag.objects.get(name='Tested-by')])) + patch = create_patch(content=str) + extracted = patch._extract_tags(Tag.objects.all()) + self.assertEqual( + (acks, reviews, tests), + (len(extracted.get(Tag.objects.get(name='Acked-by'), [])), + len(extracted.get(Tag.objects.get(name='Reviewed-by'), [])), + len(extracted.get(Tag.objects.get(name='Tested-by'), []))) + ) def test_empty(self): self.assertTagsEqual('', 0, 0, 0) @@ -80,7 +83,7 @@ class ExtractTagsTest(TestCase): self.assertTagsEqual('> Acked-by: %s\n' % self.name_email, 0, 0, 0) -class PatchTagsTest(TransactionTestCase): +class SubmissionTagsTest(TransactionTestCase): fixtures = ['default_tags'] ACK = 1 @@ -95,16 +98,14 @@ class PatchTagsTest(TransactionTestCase): def assertTagsEqual(self, patch, acks, reviews, tests): # noqa patch = Patch.objects.get(pk=patch.pk) - def count(name): - try: - return patch.patchtag_set.get(tag__name=name).count - except PatchTag.DoesNotExist: - return 0 + def count(submission, name): + return SubmissionTag.objects.filter(submission=patch, + tag__name=name).count() counts = ( - count(name='Acked-by'), - count(name='Reviewed-by'), - count(name='Tested-by'), + count(patch, 'Acked-by'), + count(patch, 'Reviewed-by'), + count(patch, 'Tested-by'), ) self.assertEqual(counts, (acks, reviews, tests)) @@ -118,7 +119,12 @@ class PatchTagsTest(TransactionTestCase): if tagtype not in tags: return '' - return '%s-by: Test Tagger \n' % tags[tagtype] + index = SubmissionTag.objects.filter( + tag__name=tags[tagtype] + '-by' + ).count() + return '%s-by: Test Taggeri%d \n' % ( + tags[tagtype], index + 1 + ) def create_tag_comment(self, patch, tagtype=None): comment = create_comment( @@ -179,29 +185,3 @@ class PatchTagsTest(TransactionTestCase): c1.content += self.create_tag(self.REVIEW) c1.save() self.assertTagsEqual(self.patch, 1, 1, 0) - - -class PatchTagManagerTest(PatchTagsTest): - - def assertTagsEqual(self, patch, acks, reviews, tests): # noqa - tagattrs = {} - for tag in Tag.objects.all(): - tagattrs[tag.name] = tag.attr_name - - # force project.tags to be queried outside of the assertNumQueries - patch.project.tags - - # we should be able to do this with two queries: one for - # the patch table lookup, and the prefetch_related for the - # projects table. - with self.assertNumQueries(2): - patch = Patch.objects.with_tag_counts(project=patch.project) \ - .get(pk=patch.pk) - - counts = ( - getattr(patch, tagattrs['Acked-by']), - getattr(patch, tagattrs['Reviewed-by']), - getattr(patch, tagattrs['Tested-by']), - ) - - self.assertEqual(counts, (acks, reviews, tests)) diff --git a/releasenotes/notes/tagging-rework-9907e9dc3f835566.yaml b/releasenotes/notes/tagging-rework-9907e9dc3f835566.yaml new file mode 100644 index 0000000..1151be9 --- /dev/null +++ b/releasenotes/notes/tagging-rework-9907e9dc3f835566.yaml @@ -0,0 +1,18 @@ +--- +features: + - | + Tagging is completely reworked. Instead of counts, real values are + extracted. This fixes wrong counts when for example the same person + accidentally sent the Acked-by email twice, since only a single same pair + tagname-value can be assigned to a patch. Tags from cover letters are now + counted towards each patch in the series and added to mbox files and patch + API as well. Sources of tags are tracked and `tags` field is populated in + the API for cover letters and comments so it's much easier to find where + a specific tag originated from. Three different filters are added for patch + and cover letter filtering in the REST API - `tagname=xxx` is a + case-insensitive filter on tag name (eg. find patches that were Tested-by + someone), `tagvalue=xxx` checks for case-insensitive substrings in the + value of the tags (is Johny slacking off and not acking/reviewing + patches?), and `tag=name,value` combines the two and filters on the + tagname-value pair (is there a patch that fixes the bug with this issue + link?).