diff sat/plugins/plugin_misc_merge_requests.py @ 3309:71761e9fb984

plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation: those methods now return data serialised with `data_format.serialise`
author Goffi <goffi@goffi.org>
date Thu, 16 Jul 2020 09:07:26 +0200
parents 559a625a236b
children bb0225aaf4e6
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_merge_requests.py	Thu Jul 16 09:07:20 2020 +0200
+++ b/sat/plugins/plugin_misc_merge_requests.py	Thu Jul 16 09:07:26 2020 +0200
@@ -17,14 +17,16 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from collections import namedtuple
+from twisted.internet import defer
+from twisted.words.protocols.jabber import jid
 from sat.core.i18n import _
 from sat.core.constants import Const as C
 from sat.core import exceptions
-from twisted.internet import defer
-from twisted.words.protocols.jabber import jid
-from collections import namedtuple
+from sat.tools.common import data_format
 from sat.core.log import getLogger
 
+
 log = getLogger(__name__)
 
 NS_MERGE_REQUESTS = 'org.salut-a-toi.merge_requests:0'
@@ -74,7 +76,7 @@
         self._handlers_list = []  # handlers sorted by priority
         self._type_handlers = {}  # data type => handler map
         host.bridge.addMethod("mergeRequestsGet", ".plugin",
-                              in_sign='ssiassa{ss}s', out_sign='(asa{ss}aaa{ss})',
+                              in_sign='ssiassa{ss}s', out_sign='s',
                               method=self._get,
                               async_=True
                               )
@@ -136,6 +138,15 @@
                 continue
             self._type_handlers[data_type] = self._handlers[name]
 
+    def serialise(self, get_data):
+        tickets_xmlui, metadata, items_patches = get_data
+        tickets_xmlui_s, metadata = self._p.transItemsData((tickets_xmlui, metadata))
+        return data_format.serialise({
+            "items": tickets_xmlui_s,
+            "metadata": metadata,
+            "items_patches": items_patches,
+        })
+
     def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None,
              extra_dict=None, profile_key=C.PROF_KEY_NONE):
         if extra_dict and 'parse' in extra_dict:
@@ -144,10 +155,7 @@
             service, node, max_items, sub_id, extra_dict, profile_key)
         d = self.get(client, service, node or None, max_items, item_ids, sub_id or None,
                      extra.rsm_request, extra.extra)
-        d.addCallback(lambda tickets_metadata_parsed_patches: (
-            self._p.transItemsData((tickets_metadata_parsed_patches[0], tickets_metadata_parsed_patches[1])) +
-            ([[{key: str(value) for key, value in p.items()}
-                for p in patches] for patches in tickets_metadata_parsed_patches[2]],)))
+        d.addCallback(self.serialise)
         return d
 
     @defer.inlineCallbacks
@@ -197,13 +205,16 @@
              extra="", profile_key=C.PROF_KEY_NONE):
         client, service, node, schema, item_id, extra = self._s.prepareBridgeSet(
             service, node, schema, item_id, extra, profile_key)
-        d = self.set(client, service, node, repository, method, values, schema,
-                     item_id or None, extra, deserialise=True)
+        d = defer.ensureDeferred(
+            self.set(
+                client, service, node, repository, method, values, schema,
+                item_id or None, extra, deserialise=True
+            )
+        )
         d.addCallback(lambda ret: ret or '')
         return d
 
-    @defer.inlineCallbacks
-    def set(self, client, service, node, repository, method='auto', values=None,
+    async def set(self, client, service, node, repository, method='auto', values=None,
             schema=None, item_id=None, extra=None, deserialise=False):
         """Publish a tickets
 
@@ -234,7 +245,7 @@
             if method == 'auto':
                 for name in self._handlers_list:
                     handler = self._handlers[name].handler
-                    can_handle = yield handler.check(repository)
+                    can_handle = await handler.check(repository)
                     if can_handle:
                         log.info(_("{name} handler will be used").format(name=name))
                         break
@@ -250,20 +261,20 @@
                 except KeyError:
                     raise exceptions.NotFound(_("No handler of this name found"))
 
-            data = yield handler.export(repository)
+            data = await handler.export(repository)
             if not data.strip():
                 raise exceptions.DataError(_('export data is empty, do you have any '
                                              'change to send?'))
 
             if not values.get('title') or not values.get('body'):
-                patches = yield handler.parse(data, values.get(FIELD_DATA_TYPE))
+                patches = await handler.parse(data, values.get(FIELD_DATA_TYPE))
                 commits_msg = patches[-1][self.META_COMMIT_MSG]
                 msg_lines = commits_msg.splitlines()
                 if not values.get('title'):
                     values['title'] = msg_lines[0]
                 if not values.get('body'):
                     ts = self.host.plugins['TEXT_SYNTAXES']
-                    xhtml = yield ts.convert(
+                    xhtml = await ts.convert(
                         '\n'.join(msg_lines[1:]),
                         syntax_from = ts.SYNTAX_TEXT,
                         syntax_to = ts.SYNTAX_XHTML,
@@ -273,9 +284,9 @@
 
             values[FIELD_DATA] = data
 
-        item_id = yield self._t.set(client, service, node, values, schema, item_id, extra,
+        item_id = await self._t.set(client, service, node, values, schema, item_id, extra,
                                     deserialise, form_ns=NS_MERGE_REQUESTS)
-        defer.returnValue(item_id)
+        return item_id
 
     def _parseData(self, data_type, data):
         d = self.parseData(data_type, data)