Title: [288688] trunk/Tools
Revision
288688
Author
[email protected]
Date
2022-01-27 11:35:12 -0800 (Thu, 27 Jan 2022)

Log Message

[webkitbugspy] JSON encode trackers
https://bugs.webkit.org/show_bug.cgi?id=235426
<rdar://problem/87856802>

Reviewed by Dewei Zhu.

* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/bugzilla.py:
(Tracker.Encoder.default): Encode bugzilla Tracker object.
* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py:
(Tracker.Encoder.default): Encode github Tracker object.
* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/radar.py:
(Tracker.Encoder.default): Encode radar Tracker object.
* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/bugzilla_unittest.py:
(TestBugzilla.test_encoding):
(TestBugzilla.test_decoding):
* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/github_unittest.py:
(TestGitHub.test_encoding):
(TestGitHub.test_decoding):
* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/radar_unittest.py:
(TestRadar.test_encoding):
(TestRadar.test_decoding):
* Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tracker.py:
(Tracker.Encoder.default): Generic encoding for all Tracker objects.
(Tracker.from_json): Decode dictionary into Tracker objects.

Canonical link: https://commits.webkit.org/246487@main

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (288687 => 288688)


--- trunk/Tools/ChangeLog	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/ChangeLog	2022-01-27 19:35:12 UTC (rev 288688)
@@ -1,3 +1,32 @@
+2022-01-26  Jonathan Bedard  <[email protected]>
+
+        [webkitbugspy] JSON encode trackers
+        https://bugs.webkit.org/show_bug.cgi?id=235426
+        <rdar://problem/87856802>
+
+        Reviewed by Dewei Zhu.
+
+        * Scripts/libraries/webkitbugspy/setup.py: Bump version.
+        * Scripts/libraries/webkitbugspy/webkitbugspy/__init__.py: Ditto.
+        * Scripts/libraries/webkitbugspy/webkitbugspy/bugzilla.py:
+        (Tracker.Encoder.default): Encode bugzilla Tracker object.
+        * Scripts/libraries/webkitbugspy/webkitbugspy/github.py:
+        (Tracker.Encoder.default): Encode github Tracker object.
+        * Scripts/libraries/webkitbugspy/webkitbugspy/radar.py:
+        (Tracker.Encoder.default): Encode radar Tracker object.
+        * Scripts/libraries/webkitbugspy/webkitbugspy/tests/bugzilla_unittest.py:
+        (TestBugzilla.test_encoding):
+        (TestBugzilla.test_decoding):
+        * Scripts/libraries/webkitbugspy/webkitbugspy/tests/github_unittest.py:
+        (TestGitHub.test_encoding):
+        (TestGitHub.test_decoding):
+        * Scripts/libraries/webkitbugspy/webkitbugspy/tests/radar_unittest.py:
+        (TestRadar.test_encoding):
+        (TestRadar.test_decoding):
+        * Scripts/libraries/webkitbugspy/webkitbugspy/tracker.py:
+        (Tracker.Encoder.default): Generic encoding for all Tracker objects.
+        (Tracker.from_json): Decode dictionary into Tracker objects.
+
 2022-01-27  Kimmo Kinnunen  <[email protected]>
 
         Update WebGL conformance test suite to 2022-01-12

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/setup.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/setup.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/setup.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -30,7 +30,7 @@
 
 setup(
     name='webkitbugspy',
-    version='0.1.0',
+    version='0.1.1',
     description='Library containing a shared API for various bug trackers.',
     long_description=readme(),
     classifiers=[

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/__init__.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/__init__.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/__init__.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -46,7 +46,7 @@
         "Please install webkitcorepy with `pip install webkitcorepy --extra-index-url <package index URL>`"
     )
 
-version = Version(0, 1, 0)
+version = Version(0, 1, 1)
 
 from .user import User
 from .issue import Issue

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/bugzilla.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/bugzilla.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/bugzilla.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -36,6 +36,22 @@
 class Tracker(GenericTracker):
     ROOT_RE = re.compile(r'\Ahttps?://(?P<domain>\S+)\Z')
 
+    class Encoder(GenericTracker.Encoder):
+        @webkitcorepy.decorators.hybridmethod
+        def default(context, obj):
+            if isinstance(obj, Tracker):
+                result = dict(
+                    type='bugzilla',
+                    url=""
+                )
+                if obj._res[2:]:
+                    result['res'] = [compiled.pattern for compiled in obj._res[2:]]
+                return result
+            if isinstance(context, type):
+                raise TypeError('Cannot invoke parent class when classmethod')
+            return super(Tracker.Encoder, context).default(obj)
+
+
     def __init__(self, url, users=None, res=None):
         super(Tracker, self).__init__(users=users)
 

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/github.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -1,4 +1,4 @@
-# Copyright (C) 2021 Apple Inc. All rights reserved.
+# Copyright (C) 2021-2022 Apple Inc. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -39,6 +39,22 @@
     ISSUE_LINK_RE = re.compile(r'#(?P<id>\d+)')
     USERNAME_RE = re.compile(r'(^|\s|\'|")@(?P<username>[^\s"\'<>]+)')
 
+    class Encoder(GenericTracker.Encoder):
+        @webkitcorepy.decorators.hybridmethod
+        def default(context, obj):
+            if isinstance(obj, Tracker):
+                result = dict(
+                    type='github',
+                    url=""
+                )
+                if obj._res[2:]:
+                    result['res'] = [compiled.pattern for compiled in obj._res[2:]]
+                return result
+            if isinstance(context, type):
+                raise TypeError('Cannot invoke parent class when classmethod')
+            return super(Tracker.Encoder, context).default(obj)
+
+
     def __init__(self, url, users=None, res=None):
         super(Tracker, self).__init__(users=users)
 

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/radar.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/radar.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/radar.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -37,6 +37,16 @@
         re.compile(r'<?radar:\/\/(?P<id>\d+)>?'),
     ]
 
+    class Encoder(GenericTracker.Encoder):
+        @decorators.hybridmethod
+        def default(context, obj):
+            if isinstance(obj, Tracker):
+                return dict(type='radar')
+            if isinstance(context, type):
+                raise TypeError('Cannot invoke parent class when classmethod')
+            return super(Tracker.Encoder, context).default(obj)
+
+
     @staticmethod
     def radarclient():
         try:

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/bugzilla_unittest.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/bugzilla_unittest.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/bugzilla_unittest.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -1,4 +1,4 @@
-# Copyright (C) 2021 Apple Inc. All rights reserved.
+# Copyright (C) 2021-2022 Apple Inc. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -19,10 +19,13 @@
 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import json
 import os
+import re
 import unittest
 
-from webkitbugspy import Issue, User, bugzilla, mocks
+from webkitbugspy import Issue, Tracker, User, bugzilla, mocks
 from webkitcorepy import mocks as wkmocks
 
 
@@ -29,6 +32,27 @@
 class TestBugzilla(unittest.TestCase):
     URL = ''
 
+    def test_encoding(self):
+        self.assertEqual(
+            bugzilla.Tracker.Encoder().default(bugzilla.Tracker(
+                self.URL,
+                res=[re.compile(r'\Aexample.com/b/(?P<id>\d+)\Z')],
+            )), dict(
+                type='bugzilla',
+                url='',
+                res=['\\Aexample.com/b/(?P<id>\\d+)\\Z']
+            ),
+        )
+
+    def test_decoding(self):
+        decoded = Tracker.from_json(json.dumps(bugzilla.Tracker(
+            self.URL,
+            res=[re.compile(r'\Aexample.com/b/(?P<id>\d+)\Z')],
+        ), cls=Tracker.Encoder))
+        self.assertIsInstance(decoded, bugzilla.Tracker)
+        self.assertEqual(decoded.url, 'https://bugs.example.com')
+        self.assertEqual(decoded.from_string('example.com/b/1234').id, 1234)
+
     def test_no_users(self):
         with mocks.Bugzilla(self.URL.split('://')[1]):
             self.assertEqual(

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/github_unittest.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/github_unittest.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/github_unittest.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -1,4 +1,4 @@
-# Copyright (C) 2021 Apple Inc. All rights reserved.
+# Copyright (C) 2021-2022 Apple Inc. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
@@ -20,9 +20,11 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+import re
+import json
 import unittest
 
-from webkitbugspy import Issue, User, github, mocks
+from webkitbugspy import Issue, Tracker, User, github, mocks
 from webkitcorepy import mocks as wkmocks
 
 
@@ -29,6 +31,27 @@
 class TestGitHub(unittest.TestCase):
     URL = ''
 
+    def test_encoding(self):
+        self.assertEqual(
+            github.Tracker.Encoder().default(github.Tracker(
+                self.URL,
+                res=[re.compile(r'\Aexample.com/b/(?P<id>\d+)\Z')],
+            )), dict(
+                type='github',
+                url='',
+                res=['\\Aexample.com/b/(?P<id>\\d+)\\Z']
+            ),
+        )
+
+    def test_decoding(self):
+        decoded = Tracker.from_json(json.dumps(github.Tracker(
+            self.URL,
+            res=[re.compile(r'\Aexample.com/b/(?P<id>\d+)\Z')],
+        ), cls=Tracker.Encoder))
+        self.assertIsInstance(decoded, github.Tracker)
+        self.assertEqual(decoded.url, 'https://github.example.com/WebKit/WebKit')
+        self.assertEqual(decoded.from_string('example.com/b/1234').id, 1234)
+
     def test_users(self):
         with mocks.GitHub(self.URL.split('://')[1], users=mocks.USERS):
             tracker = github.Tracker(self.URL)

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/radar_unittest.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/radar_unittest.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tests/radar_unittest.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -20,13 +20,25 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+import json
 import unittest
 
-from webkitbugspy import Issue, User, radar, mocks
+from webkitbugspy import Issue, Tracker, User, radar, mocks
 from webkitcorepy import mocks as wkmocks
 
 
 class TestRadar(unittest.TestCase):
+    def test_encoding(self):
+        self.assertEqual(
+            radar.Tracker.Encoder().default(radar.Tracker()),
+            dict(type='radar'),
+        )
+
+    def test_decoding(self):
+        decoded = Tracker.from_json(json.dumps(radar.Tracker(), cls=Tracker.Encoder))
+        self.assertIsInstance(decoded, radar.Tracker)
+        self.assertEqual(decoded.from_string('rdar://1234').id, 1234)
+
     def test_no_radar(self):
         with mocks.NoRadar():
             tracker = radar.Tracker()

Modified: trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tracker.py (288687 => 288688)


--- trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tracker.py	2022-01-27 19:25:28 UTC (rev 288687)
+++ trunk/Tools/Scripts/libraries/webkitbugspy/webkitbugspy/tracker.py	2022-01-27 19:35:12 UTC (rev 288688)
@@ -20,6 +20,7 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+import json
 import re
 
 from .user import User
@@ -32,7 +33,37 @@
 
     _trackers = []
 
+    class Encoder(json.JSONEncoder):
+        def default(self, obj):
+            if isinstance(obj, dict):
+                return {key: self.default(value) for key, value in obj.items()}
+            if isinstance(obj, list):
+                return [self.default(value) for value in obj]
+            if not isinstance(obj, Tracker):
+                return super(Tracker.Encoder, self).default(obj)
+            return obj.Encoder.default(obj)
+
     @classmethod
+    def from_json(cls, data):
+        from . import bugzilla, github, radar
+
+        data = "" if isinstance(data, dict) else json.loads(data)
+        if isinstance(data, (list, tuple)):
+            return [cls.from_json(datum) for datum in data]
+        if data.get('type') in ('bugzilla', 'github'):
+            return dict(
+                bugzilla=bugzilla.Tracker,
+                github=github.Tracker
+            )[data['type']](
+                url=""
+                res=[re.compile(r) for r in data.get('res', [])],
+            )
+        if data.get('type') == 'radar':
+            return radar.Tracker()
+        raise TypeError("'{}' is not a recognized tracker type".format(data.get('type')))
+
+
+    @classmethod
     def register(cls, tracker):
         if tracker not in cls._trackers:
             setattr(cls, str(type(tracker)).split('.')[-2], tracker)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to