[PATCH v2] models: Add commit_url_format to Project

Michael Ellerman mpe at ellerman.id.au
Tue Aug 27 16:13:13 AEST 2019


Add a new field to Project, commit_url_format, which specifies a
format string that can be used to generate a link to a particular
commit for a project.

This is used in the display of a patch, to render the patch's commit
as a clickable link back to the commit on the SCM website.

Signed-off-by: Michael Ellerman <mpe at ellerman.id.au>
---
 docs/api/schemas/latest/patchwork.yaml        |  7 +++++++
 docs/api/schemas/patchwork.j2                 |  7 +++++++
 docs/api/schemas/v1.2/patchwork.yaml          |  7 +++++++
 patchwork/api/embedded.py                     |  6 ++++--
 patchwork/api/project.py                      |  7 ++++---
 patchwork/fixtures/default_projects.xml       |  1 +
 .../0036_project_commit_url_format.py         | 20 +++++++++++++++++++
 patchwork/models.py                           |  5 +++++
 patchwork/templates/patchwork/submission.html |  2 +-
 patchwork/templatetags/patch.py               | 12 +++++++++++
 10 files changed, 68 insertions(+), 6 deletions(-)
 create mode 100644 patchwork/migrations/0036_project_commit_url_format.py

v2: Rebase on top of list archive changes.
    Added to the default projects fixture.
    Reworked the escaping to be more paranoid, it now escapes the result
    of applying the format string. Meaning even an admin shouldn't be
    able to do anything other than making the link point somewhere
    weird.

diff --git a/docs/api/schemas/latest/patchwork.yaml b/docs/api/schemas/latest/patchwork.yaml
index 394655d..45a6118 100644
--- a/docs/api/schemas/latest/patchwork.yaml
+++ b/docs/api/schemas/latest/patchwork.yaml
@@ -1893,6 +1893,9 @@ openapi: '3.0.0'
           description: >
             URL format for the list archive's Message-ID redirector. {} will be
             replaced by the Message-ID.
+        commit_url_format:
+          title: Web SCM URL format for a particular commit
+          type: string
     Series:
       type: object
       properties:
@@ -2217,6 +2220,10 @@ openapi: '3.0.0'
           description: >
             URL format for the list archive's Message-ID redirector. {} will be
             replaced by the Message-ID.
+        commit_url_format:
+          title: Web SCM URL format for a particular commit
+          type: string
+          readOnly: true
     SeriesEmbedded:
       type: object
       properties:
diff --git a/docs/api/schemas/patchwork.j2 b/docs/api/schemas/patchwork.j2
index 55e4c3b..843981f 100644
--- a/docs/api/schemas/patchwork.j2
+++ b/docs/api/schemas/patchwork.j2
@@ -1917,6 +1917,9 @@ openapi: '3.0.0'
           description: >
             URL format for the list archive's Message-ID redirector. {} will be
             replaced by the Message-ID.
+        commit_url_format:
+          title: Web SCM URL format for a particular commit
+          type: string
 {% endif %}
     Series:
       type: object
@@ -2253,6 +2256,10 @@ openapi: '3.0.0'
           description: >
             URL format for the list archive's Message-ID redirector. {} will be
             replaced by the Message-ID.
+        commit_url_format:
+          title: Web SCM URL format for a particular commit
+          type: string
+          readOnly: true
 {% endif %}
     SeriesEmbedded:
       type: object
diff --git a/docs/api/schemas/v1.2/patchwork.yaml b/docs/api/schemas/v1.2/patchwork.yaml
index ab351e9..3a96aa3 100644
--- a/docs/api/schemas/v1.2/patchwork.yaml
+++ b/docs/api/schemas/v1.2/patchwork.yaml
@@ -1893,6 +1893,9 @@ openapi: '3.0.0'
           description: >
             URL format for the list archive's Message-ID redirector. {} will be
             replaced by the Message-ID.
+        commit_url_format:
+          title: Web SCM URL format for a particular commit
+          type: string
     Series:
       type: object
       properties:
@@ -2217,6 +2220,10 @@ openapi: '3.0.0'
           description: >
             URL format for the list archive's Message-ID redirector. {} will be
             replaced by the Message-ID.
+        commit_url_format:
+          title: Web SCM URL format for a particular commit
+          type: string
+          readOnly: true
     SeriesEmbedded:
       type: object
       properties:
diff --git a/patchwork/api/embedded.py b/patchwork/api/embedded.py
index 968cb7f..de4f311 100644
--- a/patchwork/api/embedded.py
+++ b/patchwork/api/embedded.py
@@ -163,13 +163,15 @@ from patchwork import models
             model = models.Project
             fields = ('id', 'url', 'name', 'link_name', 'list_id',
                       'list_email', 'web_url', 'scm_url', 'webscm_url',
-                      'list_archive_url', 'list_archive_url_format')
+                      'list_archive_url', 'list_archive_url_format',
+                      'commit_url_format')
             read_only_fields = fields
             extra_kwargs = {
                 'url': {'view_name': 'api-project-detail'},
             }
             versioned_fields = {
-                '1.2': ('list_archive_url', 'list_archive_url_format'),
+                '1.2': ('list_archive_url', 'list_archive_url_format',
+                        'commit_url_format'),
             }
 
 
diff --git a/patchwork/api/project.py b/patchwork/api/project.py
index 62a8c3e..294d90b 100644
--- a/patchwork/api/project.py
+++ b/patchwork/api/project.py
@@ -27,12 +27,13 @@ from patchwork.models import Project
         fields = ('id', 'url', 'name', 'link_name', 'list_id', 'list_email',
                   'web_url', 'scm_url', 'webscm_url', 'maintainers',
                   'subject_match', 'list_archive_url',
-                  'list_archive_url_format')
+                  'list_archive_url_format', 'commit_url_format')
         read_only_fields = ('name', 'link_name', 'list_id', 'list_email',
                             'maintainers', 'subject_match')
         versioned_fields = {
             '1.1': ('subject_match', ),
-            '1.2': ('list_archive_url', 'list_archive_url_format'),
+            '1.2': ('list_archive_url', 'list_archive_url_format',
+                    'commit_url_format'),
         }
         extra_kwargs = {
             'url': {'view_name': 'api-project-detail'},
@@ -71,7 +72,7 @@ from patchwork.models import Project
 
     search_fields = ('link_name', 'list_id', 'list_email', 'web_url',
                      'scm_url', 'webscm_url', 'list_archive_url',
-                     'list_archive_url_format')
+                     'list_archive_url_format', 'commit_url_format')
     ordering_fields = ('id', 'name', 'link_name', 'list_id')
     ordering = 'id'
 
diff --git a/patchwork/fixtures/default_projects.xml b/patchwork/fixtures/default_projects.xml
index a0877d9..e6b26bb 100644
--- a/patchwork/fixtures/default_projects.xml
+++ b/patchwork/fixtures/default_projects.xml
@@ -7,5 +7,6 @@
     <field type="CharField" name="listemail">patchwork at lists.ozlabs.org</field>
     <field type="CharField" name="list_archive_url">https://lists.ozlabs.org/pipermail/patchwork/</field>
     <field type="CharField" name="list_archive_url_format">http://mid.mail-archive.com/{}</field>
+    <field type="CharField" name="commit_url_format">https://github.com/torvalds/linux/commit/{}</field>
   </object>
 </django-objects>
diff --git a/patchwork/migrations/0036_project_commit_url_format.py b/patchwork/migrations/0036_project_commit_url_format.py
new file mode 100644
index 0000000..ab296d2
--- /dev/null
+++ b/patchwork/migrations/0036_project_commit_url_format.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.22 on 2019-08-23 17:16
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('patchwork', '0035_project_list_archive_url_format'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='project',
+            name='commit_url_format',
+            field=models.CharField(blank=True, help_text=b'URL format for a particular commit. {} will be replaced by the commit SHA.', max_length=2000),
+        ),
+    ]
diff --git a/patchwork/models.py b/patchwork/models.py
index 4d23396..32d1b3c 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -82,6 +82,11 @@ from patchwork.hasher import hash_diff
         max_length=2000, blank=True,
         help_text="URL format for the list archive's Message-ID redirector. "
         "{} will be replaced by the Message-ID.")
+    commit_url_format = models.CharField(
+        max_length=2000,
+        blank=True,
+        help_text='URL format for a particular commit. '
+        '{} will be replaced by the commit SHA.')
 
     # configuration options
 
diff --git a/patchwork/templates/patchwork/submission.html b/patchwork/templates/patchwork/submission.html
index 9cebbbe..e79dd92 100644
--- a/patchwork/templates/patchwork/submission.html
+++ b/patchwork/templates/patchwork/submission.html
@@ -49,7 +49,7 @@ function toggle_div(link_id, headers_id)
 {% if submission.commit_ref %}
  <tr>
   <th>Commit</th>
-  <td>{{ submission.commit_ref }}</td>
+  <td>{{ submission|patch_commit_display }}</td>
  </tr>
 {% endif %}
 {% if submission.delegate %}
diff --git a/patchwork/templatetags/patch.py b/patchwork/templatetags/patch.py
index 757f873..d2537ba 100644
--- a/patchwork/templatetags/patch.py
+++ b/patchwork/templatetags/patch.py
@@ -66,3 +66,15 @@ register = template.Library()
 @stringfilter
 def msgid(value):
     return escape(value.strip('<>'))
+
+
+ at register.filter(name='patch_commit_display')
+def patch_commit_display(patch):
+    commit = patch.commit_ref
+    fmt = patch.project.commit_url_format
+
+    if not fmt:
+        return escape(commit)
+
+    return mark_safe('<a href="%s">%s</a>' % (escape(fmt.format(commit)),
+                                              escape(commit)))
-- 
2.21.0



More information about the Patchwork mailing list