[Patchew-devel] [PATCH 8/9] extract find_series_from_tag

Paolo Bonzini pbonzini at redhat.com
Thu Jan 16 15:09:08 UTC 2020


The same algorithm used for Based-on will be used also for
Supersedes; extract it to a utility method.  While at it
allow passing a Project instance to the various prebuild
query methods exposed by MessageManager.

Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
---
 api/models.py | 22 +++++++++++++++++-----
 mods/git.py   |  9 +--------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/api/models.py b/api/models.py
index 6ae2bae..6fd04fa 100644
--- a/api/models.py
+++ b/api/models.py
@@ -380,7 +380,9 @@ class MessageManager(models.Manager):
 
     def project_messages(self, project):
         po = None
-        if isinstance(project, str):
+        if isinstance(project, Project):
+            po = project
+        elif isinstance(project, str):
             po = Project.objects.filter(name=project).first()
         elif isinstance(project, int):
             po = Project.objects.filter(id=project).first()
@@ -400,18 +402,28 @@ class MessageManager(models.Manager):
             q = self.get_queryset()
         return q.filter(topic__isnull=False).prefetch_related("project")
 
-    def find_series(self, message_id, project_name=None):
-        heads = self.series_heads(project_name)
+    def find_series(self, message_id, project=None):
+        heads = self.series_heads(project)
         if heads is None:
             return None
         return heads.filter(message_id=message_id).first()
 
-    def find_message(self, message_id, project_name):
-        messages = self.project_messages(project_name)
+    def find_message(self, message_id, project):
+        messages = self.project_messages(project)
         if messages is None:
             return None
         return messages.filter(message_id=message_id).first()
 
+    def find_series_from_tag(self, tag, project):
+        try:
+            colon = tag.index(":")
+        except ValueError:
+            return None
+        msgid = tag[colon + 1 :].strip()
+        if msgid.startswith("<") and msgid.endswith(">"):
+            msgid = msgid[1:-1]
+        return self.find_series(msgid, project)
+
     def patches(self):
         return self.get_queryset().filter(is_patch=True)
 
diff --git a/mods/git.py b/mods/git.py
index c17186f..d10cbeb 100644
--- a/mods/git.py
+++ b/mods/git.py
@@ -237,14 +237,7 @@ class GitModule(PatchewModule):
         for tag in series.tags:
             if not tag.startswith("Based-on:"):
                 continue
-            base_id = tag[len("Based-on:") :].strip()
-            if base_id.startswith("<") and base_id.endswith(">"):
-                base_id = base_id[1:-1]
-            base = (
-                Message.objects.series_heads()
-                .filter(project=series.project, message_id=base_id)
-                .first()
-            )
+            base = Message.objects.find_series_from_tag(tag, series.project)
             if not base:
                 return None
             r = base.git_result
-- 
2.21.0





More information about the Patchew-devel mailing list