Skip to content

Commit 4719100

Browse files
author
Vladimir Kotal
authored
append string to Message text for disabled messages (#3135)
1 parent fa6d886 commit 4719100

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

opengrok-tools/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ requests==2.20.0
1111
Resource==0.2.1
1212
six==1.11.0
1313
urllib3==1.23
14-
GitPython==3.0.2
14+
GitPython==3.0.6

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
PROXY_PROPERTY = 'proxy'
5555
COMMANDS_PROPERTY = 'commands'
5656
DISABLED_PROPERTY = 'disabled'
57+
DISABLED_REASON_PROPERTY = 'disabled-reason'
5758
HOOKDIR_PROPERTY = 'hookdir'
5859
HOOKS_PROPERTY = 'hooks'
5960
LOGDIR_PROPERTY = 'logdir'
@@ -304,14 +305,27 @@ def run_command(cmd, project_name):
304305
cmd.getoutputstr()))
305306

306307

307-
def handle_disabled_project(config, project_name):
308+
def handle_disabled_project(config, project_name, disabled_msg):
308309
disabled_command = config.get(DISABLED_CMD_PROPERTY)
309310
if disabled_command:
310311
logger = logging.getLogger(__name__)
311312

312313
logger.debug("Calling disabled command: {}".format(disabled_command))
313314
command_args = disabled_command.get(COMMAND_PROPERTY)
314-
if is_web_uri(command_args[0]):
315+
uri = command_args[0]
316+
if is_web_uri(uri):
317+
# Is this perhaps OpenGrok API call to supply a Message ?
318+
# If so and there was a string supplied, append it
319+
# to the message text.
320+
data = command_args[2]
321+
text = None
322+
if type(data) is dict:
323+
text = data.get("text")
324+
if text and uri.find("/api/v1/") > 0 and type(disabled_msg) is str:
325+
logger.debug("Appending text to message: {}".
326+
format(disabled_msg))
327+
command_args[2]["text"] = text + ": " + disabled_msg
328+
315329
r = call_rest_api(disabled_command, PROJECT_SUBST, project_name)
316330
try:
317331
r.raise_for_status()
@@ -320,11 +334,15 @@ def handle_disabled_project(config, project_name):
320334
"project '{}': {}".
321335
format(project_name, r))
322336
else:
337+
args = [project_name]
338+
if disabled_msg and type(disabled_msg) is str:
339+
args.append(disabled_command)
340+
323341
cmd = Command(command_args,
324342
env_vars=disabled_command.get("env"),
325343
resource_limits=disabled_command.get("limits"),
326344
args_subst={PROJECT_SUBST: project_name},
327-
args_append=[project_name], excl_subst=True)
345+
args_append=args, excl_subst=True)
328346
run_command(cmd, project_name)
329347

330348

@@ -363,7 +381,9 @@ def mirror_project(config, project_name, check_changes, uri,
363381
# We want this to be logged to the log file (if any).
364382
if project_config:
365383
if project_config.get(DISABLED_PROPERTY):
366-
handle_disabled_project(config, project_name)
384+
handle_disabled_project(config, project_name,
385+
project_config.
386+
get(DISABLED_REASON_PROPERTY))
367387
logger.info("Project '{}' disabled, exiting".
368388
format(project_name))
369389
return CONTINUE_EXITVAL
@@ -429,7 +449,8 @@ def check_project_configuration(multiple_project_config, hookdir=False,
429449
# Quick sanity check.
430450
known_project_tunables = [DISABLED_PROPERTY, CMD_TIMEOUT_PROPERTY,
431451
HOOK_TIMEOUT_PROPERTY, PROXY_PROPERTY,
432-
IGNORED_REPOS_PROPERTY, HOOKS_PROPERTY]
452+
IGNORED_REPOS_PROPERTY, HOOKS_PROPERTY,
453+
DISABLED_REASON_PROPERTY]
433454

434455
if not multiple_project_config:
435456
return True

opengrok-tools/src/test/python/test_mirror.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
check_configuration, mirror_project, run_command, get_repos_for_project, \
4040
HOOKS_PROPERTY, PROXY_PROPERTY, IGNORED_REPOS_PROPERTY, \
4141
PROJECTS_PROPERTY, DISABLED_CMD_PROPERTY, DISABLED_PROPERTY, \
42-
CMD_TIMEOUT_PROPERTY, HOOK_TIMEOUT_PROPERTY
42+
CMD_TIMEOUT_PROPERTY, HOOK_TIMEOUT_PROPERTY, DISABLED_REASON_PROPERTY
4343
import opengrok_tools.mirror
4444
from opengrok_tools.utils.exitvals import (
4545
CONTINUE_EXITVAL, FAILURE_EXITVAL
@@ -181,6 +181,10 @@ def test_empty_project_config():
181181

182182

183183
def test_disabled_command_api():
184+
"""
185+
Test that mirror_project() calls call_rest_api() if API
186+
call is specified in the configuration for disabled project.
187+
"""
184188
with patch(opengrok_tools.utils.mirror.call_rest_api,
185189
lambda a, b, c: mock(spec=requests.Response)):
186190
project_name = "foo"
@@ -197,6 +201,46 @@ def test_disabled_command_api():
197201
PROJECT_SUBST, project_name)
198202

199203

204+
def test_disabled_command_api_text_append(monkeypatch):
205+
"""
206+
Test that message text is appended if DISABLED_PROPERTY is a string.
207+
"""
208+
209+
text_to_append = "foo bar"
210+
211+
def mock_call_rest_api(command, b, c):
212+
disabled_command = config.get(DISABLED_CMD_PROPERTY)
213+
assert disabled_command
214+
command_args = disabled_command.get(COMMAND_PROPERTY)
215+
assert command_args
216+
data = command_args[2]
217+
assert data
218+
text = data.get("text")
219+
assert text
220+
assert text.find(text_to_append)
221+
222+
return mock(spec=requests.Response)
223+
224+
with monkeypatch.context() as m:
225+
m.setattr("opengrok_tools.utils.mirror.call_rest_api",
226+
mock_call_rest_api)
227+
228+
project_name = "foo"
229+
data = {'messageLevel': 'info', 'duration': 'PT5M',
230+
'tags': ['%PROJECT%'],
231+
'text': 'disabled project'}
232+
config = {DISABLED_CMD_PROPERTY:
233+
{COMMAND_PROPERTY:
234+
["http://localhost:8080/source/api/v1/foo",
235+
"POST", data]},
236+
PROJECTS_PROPERTY: {project_name:
237+
{DISABLED_REASON_PROPERTY:
238+
text_to_append,
239+
DISABLED_PROPERTY: True}}}
240+
241+
mirror_project(config, project_name, False, None, None)
242+
243+
200244
def test_disabled_command_run():
201245
"""
202246
Make sure that mirror_project() results in calling run_command().

0 commit comments

Comments
 (0)