comparison libervia/cli/cmd_merge_request.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/cmd_merge_request.py@26b7ed2817da
children
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #!/usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
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
20
21 import os.path
22 from . import base
23 from libervia.backend.core.i18n import _
24 from libervia.backend.tools.common import data_format
25 from libervia.cli.constants import Const as C
26 from libervia.cli import xmlui_manager
27 from libervia.cli import common
28
29 __commands__ = ["MergeRequest"]
30
31
32 class Set(base.CommandBase):
33 def __init__(self, host):
34 base.CommandBase.__init__(
35 self,
36 host,
37 "set",
38 use_pubsub=True,
39 pubsub_defaults={"service": _("auto"), "node": _("auto")},
40 help=_("publish or update a merge request"),
41 )
42
43 def add_parser_options(self):
44 self.parser.add_argument(
45 "-i",
46 "--item",
47 default="",
48 help=_("id or URL of the request to update, or nothing for a new one"),
49 )
50 self.parser.add_argument(
51 "-r",
52 "--repository",
53 metavar="PATH",
54 default=".",
55 help=_("path of the repository (DEFAULT: current directory)"),
56 )
57 self.parser.add_argument(
58 "-f",
59 "--force",
60 action="store_true",
61 help=_("publish merge request without confirmation"),
62 )
63 self.parser.add_argument(
64 "-l",
65 "--label",
66 dest="labels",
67 action="append",
68 help=_("labels to categorize your request"),
69 )
70
71 async def start(self):
72 self.repository = os.path.expanduser(os.path.abspath(self.args.repository))
73 await common.fill_well_known_uri(self, self.repository, "merge requests")
74 if not self.args.force:
75 message = _(
76 "You are going to publish your changes to service "
77 "[{service}], are you sure ?"
78 ).format(service=self.args.service)
79 await self.host.confirm_or_quit(
80 message, _("merge request publication cancelled")
81 )
82
83 extra = {"update": True} if self.args.item else {}
84 values = {}
85 if self.args.labels is not None:
86 values["labels"] = self.args.labels
87 try:
88 published_id = await self.host.bridge.merge_request_set(
89 self.args.service,
90 self.args.node,
91 self.repository,
92 "auto",
93 values,
94 "",
95 self.args.item,
96 data_format.serialise(extra),
97 self.profile,
98 )
99 except Exception as e:
100 self.disp(f"can't create merge requests: {e}", error=True)
101 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
102
103 if published_id:
104 self.disp(
105 _("Merge request published at {published_id}").format(
106 published_id=published_id
107 )
108 )
109 else:
110 self.disp(_("Merge request published"))
111
112 self.host.quit(C.EXIT_OK)
113
114
115 class Get(base.CommandBase):
116 def __init__(self, host):
117 base.CommandBase.__init__(
118 self,
119 host,
120 "get",
121 use_verbose=True,
122 use_pubsub=True,
123 pubsub_flags={C.MULTI_ITEMS},
124 pubsub_defaults={"service": _("auto"), "node": _("auto")},
125 help=_("get a merge request"),
126 )
127
128 def add_parser_options(self):
129 pass
130
131 async def start(self):
132 await common.fill_well_known_uri(self, os.getcwd(), "merge requests", meta_map={})
133 extra = {}
134 try:
135 requests_data = data_format.deserialise(
136 await self.host.bridge.merge_requests_get(
137 self.args.service,
138 self.args.node,
139 self.args.max,
140 self.args.items,
141 "",
142 data_format.serialise(extra),
143 self.profile,
144 )
145 )
146 except Exception as e:
147 self.disp(f"can't get merge request: {e}", error=True)
148 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
149
150 if self.verbosity >= 1:
151 whitelist = None
152 else:
153 whitelist = {"id", "title", "body"}
154 for request_xmlui in requests_data["items"]:
155 xmlui = xmlui_manager.create(self.host, request_xmlui, whitelist=whitelist)
156 await xmlui.show(values_only=True)
157 self.disp("")
158 self.host.quit(C.EXIT_OK)
159
160
161 class Import(base.CommandBase):
162 def __init__(self, host):
163 base.CommandBase.__init__(
164 self,
165 host,
166 "import",
167 use_pubsub=True,
168 pubsub_flags={C.SINGLE_ITEM, C.ITEM},
169 pubsub_defaults={"service": _("auto"), "node": _("auto")},
170 help=_("import a merge request"),
171 )
172
173 def add_parser_options(self):
174 self.parser.add_argument(
175 "-r",
176 "--repository",
177 metavar="PATH",
178 default=".",
179 help=_("path of the repository (DEFAULT: current directory)"),
180 )
181
182 async def start(self):
183 self.repository = os.path.expanduser(os.path.abspath(self.args.repository))
184 await common.fill_well_known_uri(
185 self, self.repository, "merge requests", meta_map={}
186 )
187 extra = {}
188 try:
189 await self.host.bridge.merge_requests_import(
190 self.repository,
191 self.args.item,
192 self.args.service,
193 self.args.node,
194 extra,
195 self.profile,
196 )
197 except Exception as e:
198 self.disp(f"can't import merge request: {e}", error=True)
199 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
200 else:
201 self.host.quit()
202
203
204 class MergeRequest(base.CommandBase):
205 subcommands = (Set, Get, Import)
206
207 def __init__(self, host):
208 super(MergeRequest, self).__init__(
209 host, "merge-request", use_profile=False, help=_("merge-request management")
210 )