From nobody Tue May 13 13:13:05 2025 Delivered-To: importer2@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1557925169; cv=none; d=zoho.com; s=zohoarc; b=a3Bg8fxlDHMgyDZyhpc9wY3f2YcpKs3PZUqrZQwPx+cCf4tMs9bAauOUYfkShqhcPDIJ/Genb+Dhx/Mn4VqKhXSy3aEz+24NniK8MlYZdHKZrG09B9S6ojYvZ6Olb3s0HQSadGvLeO+KsIAuZ/Rcf0UB9fjzdAv41eBAetPPlxs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1557925169; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=YDaCzXPjAkYUpsfn3XYK/Q2GeHx8qpxSodoTOp+87kY=; b=kehsZHnaOcYlbU8HSTi+kHvL4pRtZhQBUgbWDDxyjMFmrMZylaO6PJwFGli4dFbEKr8HdH58uCDP8gW4fasacvXttKlMVYkTdtjYJmUWegwvEohFAd1iVndkIRoUIbknCXV7Kwaa989+GCYLdTuJFEZKP/iHcN08LXM++LE3hpM= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1557925169005380.13119094474416; Wed, 15 May 2019 05:59:29 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CEA623DBDB; Wed, 15 May 2019 12:59:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C307E6590A; Wed, 15 May 2019 12:59:27 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B831A4BB43; Wed, 15 May 2019 12:59:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x4FCxQpS012734 for ; Wed, 15 May 2019 08:59:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1CE4360CDA; Wed, 15 May 2019 12:59:26 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-34.ams2.redhat.com [10.36.112.34]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7FAFB60FA7 for ; Wed, 15 May 2019 12:59:21 +0000 (UTC) From: Paolo Bonzini To: patchew-devel@redhat.com Date: Wed, 15 May 2019 14:58:07 +0200 Message-Id: <20190515125808.27716-11-pbonzini@redhat.com> In-Reply-To: <20190515125808.27716-1-pbonzini@redhat.com> References: <20190515125808.27716-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH 10/11] git: automatically derive the URL for result PUT X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 15 May 2019 12:59:27 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Similar to commit 4efd0d21145a973f3321e9ec93700fb061b289f5 (git: allow not passing the URL to applier-report) but for the REST API. --- api/rest.py | 25 ++++++++++++++++++++++--- mods/git.py | 12 +++++++++++- mods/testing.py | 3 ++- tests/test_git.py | 13 +++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/api/rest.py b/api/rest.py index c3ec3e1..e893e74 100644 --- a/api/rest.py +++ b/api/rest.py @@ -609,6 +609,16 @@ class HyperlinkedResultField(HyperlinkedIdentityField): return self.reverse(view_name, kwargs=3Dkwargs, request=3Drequest,= format=3Dformat) =20 =20 +class ResultDataSerializer(serializers.Serializer): + def __init__(self, *args, **kwargs): + # Don't pass the 'project' arg up to the superclass + self.project =3D kwargs.pop('project', None) + super(ResultDataSerializer, self).__init__(*args, **kwargs) + + def create(self, data): + return data + + class ResultSerializer(serializers.ModelSerializer): class Meta: model =3D Result @@ -623,11 +633,19 @@ class ResultSerializer(serializers.ModelSerializer): request =3D self.context['request'] return obj.get_log_url(request) =20 + def get_data_serializer(self, *args, **kwargs): + project =3D self.context['parent'].project + data_serializer_class =3D self.context['renderer'].result_data_ser= ializer_class + return data_serializer_class(*args, **kwargs, project=3Dproject, c= ontext=3Dself.context) + + def update(self, instance, validated_data): + if 'data' in validated_data: + validated_data['data'] =3D self.get_data_serializer().create(v= alidated_data['data']) + return super(ResultSerializer, self).update(instance, validated_da= ta) + def validate(self, data): if 'data' in data: - data_serializer_class =3D self.context['renderer'].result_data= _serializer_class - data_serializer_class(data=3Ddata['data'], - context=3Dself.context).is_valid(raise_e= xception=3DTrue) + self.get_data_serializer(data=3Ddata['data']).is_valid(raise_e= xception=3DTrue) return data =20 =20 @@ -671,6 +689,7 @@ class ResultsViewSet(mixins.ListModelMixin, mixins.Retr= ieveModelMixin, context =3D super(ResultsViewSet, self).get_serializer_context() if 'name' in self.kwargs: context['renderer'] =3D self.result_renderer + context['parent'] =3D self return context =20 def get_serializer_class(self, *args, **kwargs): diff --git a/mods/git.py b/mods/git.py index 78939b8..b544b58 100644 --- a/mods/git.py +++ b/mods/git.py @@ -22,6 +22,7 @@ from django.db.models import Q from mod import PatchewModule from event import declare_event, register_handler, emit_event from api.models import (Message, Project, Result) +import api.rest from api.rest import PluginMethodField, SeriesSerializer, reverse_detail from api.views import APILoginRequiredView, prepare_series from patchew.logviewer import LogView @@ -47,7 +48,7 @@ class GitLogViewer(LogView): raise Http404("Object not found: " + series) return obj.git_result =20 -class ResultDataSerializer(serializers.Serializer): +class ResultDataSerializer(api.rest.ResultDataSerializer): # TODO: should be present iff the result is success or failure base =3D CharField(required=3DFalse) =20 @@ -56,6 +57,15 @@ class ResultDataSerializer(serializers.Serializer): url =3D CharField(required=3DFalse) tag =3D CharField(required=3DFalse) =20 + def create(self, data): + if 'tag' in data and 'url' not in data and self.project: + config =3D _instance.get_project_config(self.project) + url_template =3D config.get("url_template") + tag =3D data['tag'] + if url_template and tag.startswith('refs/tags/'): + data['url'] =3D url_template.replace("%t", tag[10:]) + return super(ResultDataSerializer, self).create(data) + class GitModule(PatchewModule): """Git module""" =20 diff --git a/mods/testing.py b/mods/testing.py index 1b914ce..16e652a 100644 --- a/mods/testing.py +++ b/mods/testing.py @@ -20,6 +20,7 @@ import time import math from api.views import APILoginRequiredView from api.models import (Message, MessageResult, Project, ProjectResult, Re= sult) +import api.rest from api.rest import PluginMethodField, TestPermission, reverse_detail from api.search import SearchEngine from event import emit_event, declare_event, register_handler @@ -50,7 +51,7 @@ class TestingLogViewer(LogView): raise Http404("Object not found: " + project_or_series) return _instance.get_testing_result(obj, testing_name) =20 -class ResultDataSerializer(serializers.Serializer): +class ResultDataSerializer(api.rest.ResultDataSerializer): # TODO: is_timeout should be present iff the result is a failure is_timeout =3D BooleanField(required=3DFalse) head =3D CharField() diff --git a/tests/test_git.py b/tests/test_git.py index e99755f..9b8de6d 100755 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -144,6 +144,19 @@ class GitTest(PatchewTestCase): self.assertEqual(log.status_code, 200) self.assertEqual(log.content.decode(), resp.data['log']) =20 + def test_result_data_automatic_url(self): + self.cli_import('0001-simple-patch.mbox.gz') + self.api_client.login(username=3Dself.user, password=3Dself.passwo= rd) + resp =3D self.api_client.put(self.PROJECT_BASE + 'series/201606280= 14747.20971-1-famz@redhat.com/results/git/', { + 'status': 'success', + 'data': { + 'repo': self.repo, + 'tag': 'refs/tags/patchew/20160628014747.20971-1-famz@= redhat.com' + } + }, format=3D'json') + resp =3D self.api_client.get(self.PROJECT_BASE + 'series/201606280= 14747.20971-1-famz@redhat.com/results/git/') + self.assertIn('url', resp.data['data']) + def test_rest_unapplied(self): self.cli_import('0004-multiple-patch-reviewed.mbox.gz') self.cli_import('0001-simple-patch.mbox.gz') --=20 2.21.0 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel