Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_merge_requests.py @ 3641:0ffaa231138c
docker: Libervia revision can now be specified:
Using the `DOCKER_LIBERVIA_REV` environment variable, backend (and related repos) revision
can now be specified. By default (variable empty or not set), the current `tip` version is
used, and `dev` is used for tagging. If the variable is set, the revision will be used and
used for tagging the image.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 08 Sep 2021 11:14:17 +0200 |
parents | be6d91572633 |
children | 5f65f4e9f8cb |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2448 | 3 |
4 # SAT plugin for Pubsub Schemas | |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2448 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
20 from collections import namedtuple |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
21 from twisted.internet import defer |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
22 from twisted.words.protocols.jabber import jid |
2448 | 23 from sat.core.i18n import _ |
24 from sat.core.constants import Const as C | |
25 from sat.core import exceptions | |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
26 from sat.tools.common import data_format |
2448 | 27 from sat.core.log import getLogger |
3028 | 28 |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
29 |
2448 | 30 log = getLogger(__name__) |
31 | |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
32 APP_NS_MERGE_REQUESTS = 'org.salut-a-toi.merge_requests:0' |
2448 | 33 |
34 PLUGIN_INFO = { | |
35 C.PI_NAME: _("Merge requests management"), | |
36 C.PI_IMPORT_NAME: "MERGE_REQUESTS", | |
37 C.PI_TYPE: "EXP", | |
38 C.PI_PROTOCOLS: [], | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
39 C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0346", "LISTS", "TEXT_SYNTAXES"], |
2448 | 40 C.PI_MAIN: "MergeRequests", |
41 C.PI_HANDLER: "no", | |
42 C.PI_DESCRIPTION: _("""Merge requests management plugin""") | |
43 } | |
44 | |
3028 | 45 FIELD_DATA_TYPE = 'type' |
46 FIELD_DATA = 'request_data' | |
2448 | 47 |
48 | |
49 MergeRequestHandler = namedtuple("MergeRequestHandler", ['name', | |
50 'handler', | |
51 'data_types', | |
52 'short_desc', | |
53 'priority']) | |
54 | |
55 | |
56 class MergeRequests(object): | |
3028 | 57 META_AUTHOR = 'author' |
58 META_EMAIL = 'email' | |
59 META_TIMESTAMP = 'timestamp' | |
60 META_HASH = 'hash' | |
61 META_PARENT_HASH = 'parent_hash' | |
62 META_COMMIT_MSG = 'commit_msg' | |
63 META_DIFF = 'diff' | |
2448 | 64 # index of the diff in the whole data |
65 # needed to retrieve comments location | |
3028 | 66 META_DIFF_IDX = 'diff_idx' |
2448 | 67 |
68 def __init__(self, host): | |
3028 | 69 log.info(_("Merge requests plugin initialization")) |
2448 | 70 self.host = host |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
71 self._s = self.host.plugins["XEP-0346"] |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
72 self.namespace = self._s.getSubmittedNS(APP_NS_MERGE_REQUESTS) |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
73 host.registerNamespace('merge_requests', self.namespace) |
3028 | 74 self._p = self.host.plugins["XEP-0060"] |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
75 self._t = self.host.plugins["LISTS"] |
2448 | 76 self._handlers = {} |
77 self._handlers_list = [] # handlers sorted by priority | |
78 self._type_handlers = {} # data type => handler map | |
79 host.bridge.addMethod("mergeRequestsGet", ".plugin", | |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
80 in_sign='ssiassa{ss}s', out_sign='s', |
2448 | 81 method=self._get, |
3028 | 82 async_=True |
2448 | 83 ) |
84 host.bridge.addMethod("mergeRequestSet", ".plugin", | |
2959
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2910
diff
changeset
|
85 in_sign='ssssa{sas}ssss', out_sign='s', |
2448 | 86 method=self._set, |
3028 | 87 async_=True) |
2448 | 88 host.bridge.addMethod("mergeRequestsSchemaGet", ".plugin", |
89 in_sign='sss', out_sign='s', | |
3028 | 90 method=lambda service, nodeIdentifier, profile_key: |
91 self._s._getUISchema(service, | |
92 nodeIdentifier, | |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
93 default_node=self.namespace, |
3028 | 94 profile_key=profile_key), |
95 async_=True) | |
2448 | 96 host.bridge.addMethod("mergeRequestParseData", ".plugin", |
97 in_sign='ss', out_sign='aa{ss}', | |
98 method=self._parseData, | |
3028 | 99 async_=True) |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
100 host.bridge.addMethod("mergeRequestsImport", ".plugin", |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
101 in_sign='ssssa{ss}s', out_sign='', |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
102 method=self._import, |
3028 | 103 async_=True |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
104 ) |
2448 | 105 |
106 def register(self, name, handler, data_types, short_desc, priority=0): | |
107 """register an merge request handler | |
108 | |
109 @param name(unicode): name of the handler | |
110 @param handler(object): instance of the handler. | |
111 It must have the following methods, which may all return a Deferred: | |
3040 | 112 - check(repository)->bool: True if repository can be handled |
113 - export(repository)->str: return export data, i.e. the patches | |
2622 | 114 - parse(export_data): parse report data and return a list of dict |
115 (1 per patch) with: | |
2448 | 116 - title: title of the commit message (first line) |
117 - body: body of the commit message | |
118 @aram data_types(list[unicode]): data types that his handler can generate or parse | |
119 """ | |
120 if name in self._handlers: | |
3028 | 121 raise exceptions.ConflictError(_("a handler with name {name} already " |
122 "exists!").format(name = name)) | |
2448 | 123 self._handlers[name] = MergeRequestHandler(name, |
124 handler, | |
125 data_types, | |
126 short_desc, | |
127 priority) | |
128 self._handlers_list.append(name) | |
129 self._handlers_list.sort(key=lambda name: self._handlers[name].priority) | |
3028 | 130 if isinstance(data_types, str): |
2448 | 131 data_types = [data_types] |
132 for data_type in data_types: | |
133 if data_type in self._type_handlers: | |
3028 | 134 log.warning(_('merge requests of type {type} are already handled by ' |
135 '{old_handler}, ignoring {new_handler}').format( | |
2622 | 136 type = data_type, |
2448 | 137 old_handler = self._type_handlers[data_type].name, |
138 new_handler = name)) | |
139 continue | |
140 self._type_handlers[data_type] = self._handlers[name] | |
141 | |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
142 def serialise(self, get_data): |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
143 tickets_xmlui, metadata, items_patches = get_data |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
144 tickets_xmlui_s, metadata = self._p.transItemsData((tickets_xmlui, metadata)) |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
145 return data_format.serialise({ |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
146 "items": tickets_xmlui_s, |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
147 "metadata": metadata, |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
148 "items_patches": items_patches, |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
149 }) |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
150 |
2622 | 151 def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, |
152 extra_dict=None, profile_key=C.PROF_KEY_NONE): | |
2448 | 153 if extra_dict and 'parse' in extra_dict: |
154 extra_dict['parse'] = C.bool(extra_dict['parse']) | |
2622 | 155 client, service, node, max_items, extra, sub_id = self._s.prepareBridgeGet( |
156 service, node, max_items, sub_id, extra_dict, profile_key) | |
157 d = self.get(client, service, node or None, max_items, item_ids, sub_id or None, | |
158 extra.rsm_request, extra.extra) | |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
159 d.addCallback(self.serialise) |
2448 | 160 return d |
161 | |
162 @defer.inlineCallbacks | |
2622 | 163 def get(self, client, service=None, node=None, max_items=None, item_ids=None, |
164 sub_id=None, rsm_request=None, extra=None): | |
2448 | 165 """Retrieve merge requests and convert them to XMLUI |
166 | |
167 @param extra(XEP-0060.parse, None): can have following keys: | |
168 - update(bool): if True, will return list of parsed request data | |
169 other params are the same as for [TICKETS._get] | |
170 @return (tuple[list[unicode], list[dict[unicode, unicode]])): tuple with | |
171 - XMLUI of the tickets, like [TICKETS._get] | |
2541
65695b9343d3
jp (xmlui): added whitelist, read_only and values_only options:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
172 - node metadata |
2448 | 173 - list of parsed request data (if extra['parse'] is set, else empty list) |
174 """ | |
175 if not node: | |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
176 node = self.namespace |
2603
5d4ac5415b40
plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
177 if extra is None: |
5d4ac5415b40
plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
178 extra = {} |
5d4ac5415b40
plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
179 # XXX: Q&D way to get list for labels when displaying them, but text when we |
5d4ac5415b40
plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
180 # have to modify them |
5d4ac5415b40
plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
181 if C.bool(extra.get('labels_as_list', C.BOOL_FALSE)): |
3028 | 182 filters = {'labels': self._s.textbox2ListFilter} |
2603
5d4ac5415b40
plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
183 else: |
5d4ac5415b40
plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
184 filters = {} |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
185 tickets_xmlui, metadata = yield defer.ensureDeferred( |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
186 self._s.getDataFormItems( |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
187 client, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
188 service, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
189 node, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
190 max_items=max_items, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
191 item_ids=item_ids, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
192 sub_id=sub_id, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
193 rsm_request=rsm_request, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
194 extra=extra, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
195 form_ns=APP_NS_MERGE_REQUESTS, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
196 filters = filters) |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
197 ) |
2448 | 198 parsed_patches = [] |
199 if extra.get('parse', False): | |
200 for ticket in tickets_xmlui: | |
201 request_type = ticket.named_widgets[FIELD_DATA_TYPE].value | |
202 request_data = ticket.named_widgets[FIELD_DATA].value | |
203 parsed_data = yield self.parseData(request_type, request_data) | |
204 parsed_patches.append(parsed_data) | |
205 defer.returnValue((tickets_xmlui, metadata, parsed_patches)) | |
206 | |
2622 | 207 def _set(self, service, node, repository, method, values, schema=None, item_id=None, |
3028 | 208 extra="", profile_key=C.PROF_KEY_NONE): |
2622 | 209 client, service, node, schema, item_id, extra = self._s.prepareBridgeSet( |
210 service, node, schema, item_id, extra, profile_key) | |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
211 d = defer.ensureDeferred( |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
212 self.set( |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
213 client, service, node, repository, method, values, schema, |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
214 item_id or None, extra, deserialise=True |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
215 ) |
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
216 ) |
3028 | 217 d.addCallback(lambda ret: ret or '') |
2448 | 218 return d |
219 | |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
220 async def set(self, client, service, node, repository, method='auto', values=None, |
2622 | 221 schema=None, item_id=None, extra=None, deserialise=False): |
2448 | 222 """Publish a tickets |
223 | |
224 @param service(None, jid.JID): Pubsub service to use | |
225 @param node(unicode, None): Pubsub node to use | |
226 None to use default tickets node | |
227 @param repository(unicode): path to the repository where the code stands | |
2622 | 228 @param method(unicode): name of one of the registered handler, |
229 or "auto" to try autodetection. | |
2448 | 230 other arguments are same as for [TICKETS.set] |
231 @return (unicode): id of the created item | |
232 """ | |
2473
447c3de6b9e5
plugin merge-requests: fixed "set" method
Goffi <goffi@goffi.org>
parents:
2472
diff
changeset
|
233 if not node: |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
234 node = self.namespace |
2448 | 235 if values is None: |
236 values = {} | |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
237 update = extra.get('update', False) |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
238 if not repository and not update: |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
239 # in case of update, we may re-user former patches data |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
240 # so repository is not mandatory |
3028 | 241 raise exceptions.DataError(_("repository must be specified")) |
2448 | 242 |
243 if FIELD_DATA in values: | |
3028 | 244 raise exceptions.DataError(_("{field} is set by backend, you must not set " |
245 "it in frontend").format(field = FIELD_DATA)) | |
2448 | 246 |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
247 if repository: |
3028 | 248 if method == 'auto': |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
249 for name in self._handlers_list: |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
250 handler = self._handlers[name].handler |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
251 can_handle = await handler.check(repository) |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
252 if can_handle: |
3028 | 253 log.info(_("{name} handler will be used").format(name=name)) |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
254 break |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
255 else: |
3028 | 256 log.warning(_("repository {path} can't be handled by any installed " |
257 "handler").format( | |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
258 path = repository)) |
3028 | 259 raise exceptions.NotFound(_("no handler for this repository has " |
260 "been found")) | |
2448 | 261 else: |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
262 try: |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
263 handler = self._handlers[name].handler |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
264 except KeyError: |
3028 | 265 raise exceptions.NotFound(_("No handler of this name found")) |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
266 |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
267 data = await handler.export(repository) |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
268 if not data.strip(): |
3028 | 269 raise exceptions.DataError(_('export data is empty, do you have any ' |
270 'change to send?')) | |
2448 | 271 |
3028 | 272 if not values.get('title') or not values.get('body'): |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
273 patches = await handler.parse(data, values.get(FIELD_DATA_TYPE)) |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
274 commits_msg = patches[-1][self.META_COMMIT_MSG] |
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
275 msg_lines = commits_msg.splitlines() |
3028 | 276 if not values.get('title'): |
277 values['title'] = msg_lines[0] | |
278 if not values.get('body'): | |
2785
f18d8315929e
merge_requests: use XHTML for body
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
279 ts = self.host.plugins['TEXT_SYNTAXES'] |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
280 xhtml = await ts.convert( |
3028 | 281 '\n'.join(msg_lines[1:]), |
2785
f18d8315929e
merge_requests: use XHTML for body
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
282 syntax_from = ts.SYNTAX_TEXT, |
f18d8315929e
merge_requests: use XHTML for body
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
283 syntax_to = ts.SYNTAX_XHTML, |
f18d8315929e
merge_requests: use XHTML for body
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
284 profile = client.profile) |
3028 | 285 values['body'] = '<div xmlns="{ns}">{xhtml}</div>'.format( |
2785
f18d8315929e
merge_requests: use XHTML for body
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
286 ns=C.NS_XHTML, xhtml=xhtml) |
2448 | 287 |
2604
700327fa9281
plugin merge-requests: allow to set empty repository when "update" is set in extra parameters:
Goffi <goffi@goffi.org>
parents:
2603
diff
changeset
|
288 values[FIELD_DATA] = data |
2448 | 289 |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
290 item_id = await self._t.set(client, service, node, values, schema, item_id, extra, |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
291 deserialise, form_ns=APP_NS_MERGE_REQUESTS) |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
292 return item_id |
2448 | 293 |
294 def _parseData(self, data_type, data): | |
295 d = self.parseData(data_type, data) | |
296 d.addCallback(lambda parsed_patches: | |
3028 | 297 {key: str(value) for key, value in parsed_patches.items()}) |
2448 | 298 return d |
299 | |
300 def parseData(self, data_type, data): | |
301 """Parse a merge request data according to type | |
302 | |
303 @param data_type(unicode): type of the data to parse | |
304 @param data(unicode): data to parse | |
305 @return(list[dict[unicode, unicode]]): parsed data | |
306 key of dictionary are self.META_* or keys specifics to handler | |
307 @raise NotFound: no handler can parse this data_type | |
308 """ | |
309 try: | |
310 handler = self._type_handlers[data_type] | |
311 except KeyError: | |
3028 | 312 raise exceptions.NotFound(_('No handler can handle data type "{type}"') |
2622 | 313 .format(type=data_type)) |
2448 | 314 return defer.maybeDeferred(handler.handler.parse, data, data_type) |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
315 |
2622 | 316 def _import(self, repository, item_id, service=None, node=None, extra=None, |
317 profile_key=C.PROF_KEY_NONE): | |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
318 client = self.host.getClient(profile_key) |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
319 service = jid.JID(service) if service else None |
2622 | 320 d = self.import_request(client, repository, item_id, service, node or None, |
321 extra=extra or None) | |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
322 return d |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
323 |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
324 @defer.inlineCallbacks |
2622 | 325 def import_request(self, client, repository, item, service=None, node=None, |
326 extra=None): | |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
327 """Import a merge request in specified directory |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
328 |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
329 @param repository(unicode): path to the repository where the code stands |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
330 """ |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
331 if not node: |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
332 node = self.namespace |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
333 tickets_xmlui, metadata = yield defer.ensureDeferred( |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
334 self._s.getDataFormItems( |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
335 client, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
336 service, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
337 node, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
338 max_items=1, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
339 item_ids=[item], |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
340 form_ns=APP_NS_MERGE_REQUESTS) |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
341 ) |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
342 ticket_xmlui = tickets_xmlui[0] |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
343 data = ticket_xmlui.named_widgets[FIELD_DATA].value |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
344 data_type = ticket_xmlui.named_widgets[FIELD_DATA_TYPE].value |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
345 try: |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
346 handler = self._type_handlers[data_type] |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
347 except KeyError: |
3028 | 348 raise exceptions.NotFound(_('No handler found to import {data_type}') |
2622 | 349 .format(data_type=data_type)) |
3028 | 350 log.info(_("Importing patch [{item_id}] using {name} handler").format( |
2544
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
351 item_id = item, |
a64887289931
plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents:
2541
diff
changeset
|
352 name = handler.name)) |
2622 | 353 yield handler.handler.import_(repository, data, data_type, item, service, node, |
354 extra) |