From patchwork Mon Oct 7 22:16:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Herland X-Patchwork-Id: 1172977 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.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46nFHm60RPz9s7T for ; Tue, 8 Oct 2019 09:18:44 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=herland.net Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 46nFHm4jgTzDqJW for ; Tue, 8 Oct 2019 09:18:44 +1100 (AEDT) X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gmail.com (client-ip=209.85.208.182; helo=mail-lj1-f182.google.com; envelope-from=jherland@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=herland.net Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 46nFFh3XMZzDqJC for ; Tue, 8 Oct 2019 09:16:56 +1100 (AEDT) Received: by mail-lj1-f182.google.com with SMTP id v24so15399440ljj.3 for ; Mon, 07 Oct 2019 15:16:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6bwzIcXvel9Zly/gXYnb6rIkaluCYulCMUL/6jdDZNo=; b=TctacfFvVRoftQ5EflW3LRnj35NrKCZWrF5IljAL2ttTtdmXycWuj7TFWwxCweGTiJ bYb0WYSbrUoIqjvUDBTUwwhhog1abXSNuEav5lu+cOp6FvErI87l5Fl+ohpxIg1uhTM8 9KBS4aZ0Eo8cupDg26rPDRZKLS4uoa5kz79iixkKPDpeKVKLlWqe3DHdY4wTDroh6TVW jMCkVIzL1heg1wmoozt1cb08UpMo8ZiulkHjEeM47Mo6ffFXxtRU6NB0SOH9qE3s71uJ rSn3c8uDtdH5tbKOqiuwjmX9pxmE437WtWtekpibJVzt5XqEAaVQHkEkmK2TCTZpUOua d4BA== X-Gm-Message-State: APjAAAU1ouTalhncJAQiUdiALTqbPLjvqYnNLdcq6FrAhO70tfMbEjnL pB9PS4jhjLeYcAmR68/tfJ4v5Yj5g/E= X-Google-Smtp-Source: APXvYqx17aYMDFX32KNiYUHFR8cXW813jvDyBnk9ptkekquOG23chfH7ANVVeCrDFyg0cRQ0+1oP8g== X-Received: by 2002:a2e:98d2:: with SMTP id s18mr20093409ljj.68.1570486612506; Mon, 07 Oct 2019 15:16:52 -0700 (PDT) Received: from beta.herland (160.37-191-159.fiber.lynet.no. [37.191.159.160]) by smtp.gmail.com with ESMTPSA id s7sm3370081ljs.16.2019.10.07.15.16.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2019 15:16:51 -0700 (PDT) From: Johan Herland To: patchwork@lists.ozlabs.org Subject: [PATCH v2 3/3] /api/events: Add 'actor' field to generated JSON Date: Tue, 8 Oct 2019 00:16:45 +0200 Message-Id: <20191007221645.13186-4-johan@herland.net> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20191007221645.13186-1-johan@herland.net> References: <20191007221645.13186-1-johan@herland.net> MIME-Version: 1.0 X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mauro Carvalho Chehab Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" Cc: Mauro Carvalho Chehab Signed-off-by: Johan Herland Acked-by: Daniel Axtens --- docs/api/schemas/latest/patchwork.yaml | 6 ++++++ docs/api/schemas/patchwork.j2 | 8 ++++++++ docs/api/schemas/v1.2/patchwork.yaml | 6 ++++++ patchwork/api/event.py | 10 +++++++--- patchwork/tests/api/test_event.py | 24 ++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/docs/api/schemas/latest/patchwork.yaml b/docs/api/schemas/latest/patchwork.yaml index 45a6118..58d5d44 100644 --- a/docs/api/schemas/latest/patchwork.yaml +++ b/docs/api/schemas/latest/patchwork.yaml @@ -1495,6 +1495,12 @@ components: type: string format: iso8601 readOnly: true + actor: + type: object + title: The user that caused/created this event + readOnly: true + allOf: + - $ref: '#/components/schemas/UserEmbedded' payload: type: object EventCoverCreated: diff --git a/docs/api/schemas/patchwork.j2 b/docs/api/schemas/patchwork.j2 index 16d85a3..1f1a7bd 100644 --- a/docs/api/schemas/patchwork.j2 +++ b/docs/api/schemas/patchwork.j2 @@ -1510,6 +1510,14 @@ components: type: string format: iso8601 readOnly: true +{% if version >= (1, 2) %} + actor: + type: object + title: The user that caused/created this event + readOnly: true + allOf: + - $ref: '#/components/schemas/UserEmbedded' +{% endif %} payload: type: object EventCoverCreated: diff --git a/docs/api/schemas/v1.2/patchwork.yaml b/docs/api/schemas/v1.2/patchwork.yaml index 3a96aa3..2aaf393 100644 --- a/docs/api/schemas/v1.2/patchwork.yaml +++ b/docs/api/schemas/v1.2/patchwork.yaml @@ -1495,6 +1495,12 @@ components: type: string format: iso8601 readOnly: true + actor: + type: object + title: The user that caused/created this event + readOnly: true + allOf: + - $ref: '#/components/schemas/UserEmbedded' payload: type: object EventCoverCreated: diff --git a/patchwork/api/event.py b/patchwork/api/event.py index c0d973d..33ea104 100644 --- a/patchwork/api/event.py +++ b/patchwork/api/event.py @@ -23,6 +23,7 @@ from patchwork.models import Event class EventSerializer(ModelSerializer): project = ProjectSerializer(read_only=True) + actor = UserSerializer() patch = PatchSerializer(read_only=True) series = SeriesSerializer(read_only=True) cover = CoverLetterSerializer(read_only=True) @@ -50,7 +51,7 @@ class EventSerializer(ModelSerializer): data = super(EventSerializer, self).to_representation(instance) payload = OrderedDict() kept_fields = self._category_map[instance.category] + [ - 'id', 'category', 'project', 'date'] + 'id', 'category', 'project', 'date', 'actor'] for field in [x for x in data]: if field not in kept_fields: @@ -65,10 +66,13 @@ class EventSerializer(ModelSerializer): class Meta: model = Event - fields = ('id', 'category', 'project', 'date', 'patch', 'series', - 'cover', 'previous_state', 'current_state', + fields = ('id', 'category', 'project', 'date', 'actor', 'patch', + 'series', 'cover', 'previous_state', 'current_state', 'previous_delegate', 'current_delegate', 'created_check') read_only_fields = fields + versioned_fields = { + '1.2': ('actor', ), + } class EventList(ListAPIView): diff --git a/patchwork/tests/api/test_event.py b/patchwork/tests/api/test_event.py index 8816538..5e47ff3 100644 --- a/patchwork/tests/api/test_event.py +++ b/patchwork/tests/api/test_event.py @@ -35,11 +35,16 @@ class TestEventAPI(utils.APITestCase): def assertSerialized(self, event_obj, event_json): self.assertEqual(event_obj.id, event_json['id']) self.assertEqual(event_obj.category, event_json['category']) + if event_obj.actor is None: + self.assertIsNone(event_json['actor']) # nested fields self.assertEqual(event_obj.project.id, event_json['project']['id']) + if event_obj.actor is not None: + self.assertEqual(event_obj.actor.id, + event_json['actor']['id']) # TODO(stephenfin): Check other fields @@ -66,10 +71,12 @@ class TestEventAPI(utils.APITestCase): # check-created create_check(patch=patch) # patch-delegated, patch-state-changed + actor = create_maintainer(project=patch.project) user = create_maintainer(project=patch.project) state = create_state() patch.delegate = user patch.state = state + self.assertTrue(patch.is_editable(actor)) patch.save() return Event.objects.all() @@ -158,3 +165,20 @@ class TestEventAPI(utils.APITestCase): self.client.force_authenticate(user=user) resp = self.client.post(self.api_url(), {'category': 'patch-created'}) self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) + + def test_change_delegate(self): + """Ensure changing patch delegate via API produces expected event""" + patch = create_patch() + delegate = create_maintainer(project=patch.project) + actor = create_maintainer(project=patch.project) + + self.client.force_authenticate(user=actor) + patch_url = reverse('api-patch-detail', kwargs={'pk': patch.id}) + resp = self.client.patch(patch_url, {'delegate': delegate.id}) + self.assertEqual(status.HTTP_200_OK, resp.status_code, resp) + + events = Event.objects.all() + delegation_event = events.get(category='patch-delegated') + self.assertEqual(actor.id, delegation_event.actor.id) + self.assertEqual(None, delegation_event.previous_delegate) + self.assertEqual(delegate.id, delegation_event.current_delegate.id)