Skip to content

Commit c41895f

Browse files
committed
convert API call configuration to dictionary
fixes #3453 fixes #3923
1 parent 741f2ca commit c41895f

File tree

12 files changed

+154
-113
lines changed

12 files changed

+154
-113
lines changed

tools/src/main/python/opengrok_tools/mirror.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
from .utils.opengrok import get_config_value, list_indexed_projects
5050
from .utils.parsers import get_base_parser, add_http_headers, get_headers
5151
from .utils.readconfig import read_config
52-
from .utils.utils import get_int, is_web_uri
52+
from .utils.utils import get_int
53+
from .utils.webutil import is_web_uri
5354
from .utils.mirror import check_configuration, LOGDIR_PROPERTY, \
5455
mirror_project, HOOKDIR_PROPERTY, CMD_TIMEOUT_PROPERTY, \
5556
HOOK_TIMEOUT_PROPERTY
@@ -58,7 +59,7 @@
5859
if major_version < 3:
5960
fatal("Need Python 3, you are running {}".format(major_version))
6061

61-
__version__ = "1.2"
62+
__version__ = "1.3"
6263

6364
OPENGROK_NO_MIRROR_ENV = "OPENGROK_NO_MIRROR"
6465

tools/src/main/python/opengrok_tools/projadm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
from .utils.opengrok import get_configuration, set_configuration, \
4343
add_project, delete_project, get_config_value, get_repos
4444
from .utils.parsers import get_base_parser, get_headers, add_http_headers
45-
from .utils.utils import get_command, is_web_uri
45+
from .utils.utils import get_command
46+
from .utils.webutil import is_web_uri
4647
from .utils.exitvals import (
4748
FAILURE_EXITVAL,
4849
SUCCESS_EXITVAL

tools/src/main/python/opengrok_tools/sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from .utils.opengrok import list_indexed_projects, get_config_value
4242
from .utils.parsers import get_base_parser, add_http_headers, get_headers
4343
from .utils.readconfig import read_config
44-
from .utils.utils import is_web_uri
44+
from .utils.webutil import is_web_uri
4545
from .utils.exitvals import (
4646
FAILURE_EXITVAL,
4747
SUCCESS_EXITVAL
@@ -52,7 +52,7 @@
5252
print("Need Python 3, you are running {}".format(major_version))
5353
sys.exit(1)
5454

55-
__version__ = "1.3"
55+
__version__ = "1.4"
5656

5757

5858
def worker(base):

tools/src/main/python/opengrok_tools/utils/commandsequence.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
from requests.exceptions import RequestException
2727

2828
from .command import Command
29-
from .utils import is_web_uri
3029
from .exitvals import (
3130
CONTINUE_EXITVAL,
3231
SUCCESS_EXITVAL,
3332
FAILURE_EXITVAL
3433
)
3534
from .restful import call_rest_api
36-
from .patterns import PROJECT_SUBST, COMMAND_PROPERTY, URL_SUBST
35+
from .patterns import PROJECT_SUBST, COMMAND_PROPERTY, CALL_PROPERTY, URL_SUBST
3736
import re
3837

3938

@@ -51,13 +50,16 @@ def check_command_property(command):
5150
raise CommandConfigurationException("command '{}' is not a dictionary".format(command))
5251

5352
command_value = command.get(COMMAND_PROPERTY)
54-
if command_value is None:
55-
raise CommandConfigurationException("command dictionary has no '{}' key: {}".
56-
format(COMMAND_PROPERTY, command))
53+
call_value = command.get(CALL_PROPERTY)
54+
if command_value is None and call_value is None:
55+
raise CommandConfigurationException(f"command dictionary has unknown key: {command}")
5756

58-
if not isinstance(command_value, list):
57+
if command_value and not isinstance(command_value, list):
5958
raise CommandConfigurationException("command value not a list: {}".
6059
format(command_value))
60+
if call_value and not isinstance(call_value, dict):
61+
raise CommandConfigurationException("call value not a dictionary: {}".
62+
format(call_value))
6163

6264

6365
class CommandSequenceBase:
@@ -123,7 +125,6 @@ def fill(self, retcodes, outputs, failed):
123125

124126

125127
class CommandSequence(CommandSequenceBase):
126-
127128
re_program = re.compile('ERROR[:]*\\s+')
128129

129130
def __init__(self, base):
@@ -163,20 +164,20 @@ def run(self):
163164
"""
164165

165166
for command in self.commands:
166-
cmd_value = command.get(COMMAND_PROPERTY)[0]
167-
if cmd_value.startswith(URL_SUBST) or is_web_uri(cmd_value):
167+
if command.get(CALL_PROPERTY):
168168
try:
169-
call_rest_api(command, {PROJECT_SUBST: self.name,
170-
URL_SUBST: self.url},
169+
call_rest_api(command.get(CALL_PROPERTY),
170+
{PROJECT_SUBST: self.name,
171+
URL_SUBST: self.url},
171172
self.http_headers, self.api_timeout)
172173
except RequestException as e:
173-
self.logger.error("RESTful command {} failed: {}".
174+
self.logger.error("REST API call {} failed: {}".
174175
format(command, e))
175176
self.failed = True
176177
self.retcodes[str(command)] = FAILURE_EXITVAL
177178

178179
break
179-
else:
180+
elif command.get(COMMAND_PROPERTY):
180181
command_args = command.get(COMMAND_PROPERTY)
181182
command = Command(command_args,
182183
env_vars=command.get("env"),
@@ -185,12 +186,12 @@ def run(self):
185186
args_subst={PROJECT_SUBST: self.name,
186187
URL_SUBST: self.url},
187188
args_append=[self.name], excl_subst=True)
188-
retcode = self.run_command(command)
189+
ret_code = self.run_command(command)
189190

190191
# If a command exits with non-zero return code,
191192
# terminate the sequence of commands.
192-
if retcode != SUCCESS_EXITVAL:
193-
if retcode == CONTINUE_EXITVAL:
193+
if ret_code != SUCCESS_EXITVAL:
194+
if ret_code == CONTINUE_EXITVAL:
194195
if not self.driveon:
195196
self.logger.debug("command '{}' for project {} "
196197
"requested break".
@@ -206,11 +207,13 @@ def run(self):
206207
else:
207208
self.logger.error("command '{}' for project {} failed "
208209
"with code {}, breaking".
209-
format(command, self.name, retcode))
210+
format(command, self.name, ret_code))
210211
self.failed = True
211212
self.run_cleanup()
212213

213214
break
215+
else:
216+
raise Exception(f"unknown command: {command}")
214217

215218
def run_cleanup(self):
216219
"""
@@ -221,16 +224,16 @@ def run_cleanup(self):
221224
return
222225

223226
for cleanup_cmd in self.cleanup:
224-
arg0 = cleanup_cmd.get(COMMAND_PROPERTY)[0]
225-
if arg0.startswith(URL_SUBST) or is_web_uri(arg0):
227+
if cleanup_cmd.get(CALL_PROPERTY):
226228
try:
227-
call_rest_api(cleanup_cmd, {PROJECT_SUBST: self.name,
228-
URL_SUBST: self.url},
229+
call_rest_api(cleanup_cmd.get(CALL_PROPERTY),
230+
{PROJECT_SUBST: self.name,
231+
URL_SUBST: self.url},
229232
self.http_headers, self.api_timeout)
230233
except RequestException as e:
231-
self.logger.error("RESTful command {} failed: {}".
234+
self.logger.error("API call {} failed: {}".
232235
format(cleanup_cmd, e))
233-
else:
236+
elif cleanup_cmd.get(COMMAND_PROPERTY):
234237
command_args = cleanup_cmd.get(COMMAND_PROPERTY)
235238
self.logger.debug("Running cleanup command '{}'".
236239
format(command_args))
@@ -245,6 +248,8 @@ def run_cleanup(self):
245248
"code {}".
246249
format(cmd.cmd, cmd.getretcode()))
247250
self.logger.info('output: {}'.format(cmd.getoutputstr()))
251+
else:
252+
raise Exception(f"unknown type of action: {cleanup_cmd}")
248253

249254
def print_outputs(self, logger, loglevel=logging.INFO, lines=False):
250255
"""

tools/src/main/python/opengrok_tools/utils/mirror.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
CONTINUE_EXITVAL,
3737
SUCCESS_EXITVAL
3838
)
39-
from .patterns import PROJECT_SUBST, COMMAND_PROPERTY
40-
from .utils import is_exe, check_create_dir, get_int, is_web_uri, get_bool
39+
from .patterns import PROJECT_SUBST, COMMAND_PROPERTY, CALL_PROPERTY
40+
from .utils import is_exe, check_create_dir, get_int, get_bool
4141
from .opengrok import get_repos, get_repo_type, get_uri, delete_project_data
4242
from .hook import run_hook
4343
from .command import Command
44-
from .restful import call_rest_api, do_api_call
44+
from .restful import call_rest_api, do_api_call, get_call_props
4545

4646
from ..scm.repofactory import get_repository
4747
from ..scm.repository import RepositoryException
@@ -355,30 +355,30 @@ def handle_disabled_project(config, project_name, disabled_msg, headers=None,
355355
if disabled_command:
356356
logger = logging.getLogger(__name__)
357357

358-
logger.debug("Calling disabled command: {}".format(disabled_command))
359-
command_args = disabled_command.get(COMMAND_PROPERTY)
360-
uri = command_args[0]
361-
if is_web_uri(uri):
362-
# Is this perhaps OpenGrok API call to supply a Message ?
363-
# If so and there was a string supplied, append it
364-
# to the message text.
365-
data = command_args[2]
358+
if disabled_command.get(CALL_PROPERTY):
359+
call = disabled_command.get(CALL_PROPERTY)
360+
uri, _, data, _ = get_call_props(call)
366361
text = None
362+
367363
if type(data) is dict:
368364
text = data.get("text")
365+
366+
# Is this perhaps OpenGrok API call to supply a Message for the UI ?
367+
# If so and there was a string supplied, append it to the message text.
369368
if text and uri.find("/api/v1/") > 0 and type(disabled_msg) is str:
370369
logger.debug("Appending text to message: {}".
371370
format(disabled_msg))
372-
command_args[2]["text"] = text + ": " + disabled_msg
371+
data["text"] = text + ": " + disabled_msg
373372

374373
try:
375-
call_rest_api(disabled_command, {PROJECT_SUBST: project_name},
374+
call_rest_api(call, {PROJECT_SUBST: project_name},
376375
http_headers=headers, timeout=timeout, api_timeout=api_timeout)
377376
except RequestException as e:
378377
logger.error("API call failed for disabled command of "
379378
"project '{}': {}".
380379
format(project_name, e))
381-
else:
380+
elif disabled_command.get(COMMAND_PROPERTY):
381+
command_args = disabled_command.get(COMMAND_PROPERTY)
382382
args = [project_name]
383383
if disabled_msg and type(disabled_msg) is str:
384384
args.append(disabled_command)
@@ -389,6 +389,8 @@ def handle_disabled_project(config, project_name, disabled_msg, headers=None,
389389
args_subst={PROJECT_SUBST: project_name},
390390
args_append=args, excl_subst=True)
391391
run_command(cmd, project_name)
392+
else:
393+
raise Exception(f"unknown disabled action: {disabled_command}")
392394

393395

394396
def get_mirror_retcode(ignore_errors, value):

tools/src/main/python/opengrok_tools/utils/patterns.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

2424
PROJECT_SUBST = '%PROJECT%'
2525
URL_SUBST = '%URL%'
2626
COMMAND_PROPERTY = "command"
27+
CALL_PROPERTY = "call"

0 commit comments

Comments
 (0)