Skip to content

append string to Message text for disabled messages #3135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opengrok-tools/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ requests==2.20.0
Resource==0.2.1
six==1.11.0
urllib3==1.23
GitPython==3.0.2
GitPython==3.0.6
31 changes: 26 additions & 5 deletions opengrok-tools/src/main/python/opengrok_tools/utils/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
PROXY_PROPERTY = 'proxy'
COMMANDS_PROPERTY = 'commands'
DISABLED_PROPERTY = 'disabled'
DISABLED_REASON_PROPERTY = 'disabled-reason'
HOOKDIR_PROPERTY = 'hookdir'
HOOKS_PROPERTY = 'hooks'
LOGDIR_PROPERTY = 'logdir'
Expand Down Expand Up @@ -304,14 +305,27 @@ def run_command(cmd, project_name):
cmd.getoutputstr()))


def handle_disabled_project(config, project_name):
def handle_disabled_project(config, project_name, disabled_msg):
disabled_command = config.get(DISABLED_CMD_PROPERTY)
if disabled_command:
logger = logging.getLogger(__name__)

logger.debug("Calling disabled command: {}".format(disabled_command))
command_args = disabled_command.get(COMMAND_PROPERTY)
if is_web_uri(command_args[0]):
uri = command_args[0]
if is_web_uri(uri):
# Is this perhaps OpenGrok API call to supply a Message ?
# If so and there was a string supplied, append it
# to the message text.
data = command_args[2]
text = None
if type(data) is dict:
text = data.get("text")
if text and uri.find("/api/v1/") > 0 and type(disabled_msg) is str:
logger.debug("Appending text to message: {}".
format(disabled_msg))
command_args[2]["text"] = text + ": " + disabled_msg

r = call_rest_api(disabled_command, PROJECT_SUBST, project_name)
try:
r.raise_for_status()
Expand All @@ -320,11 +334,15 @@ def handle_disabled_project(config, project_name):
"project '{}': {}".
format(project_name, r))
else:
args = [project_name]
if disabled_msg and type(disabled_msg) is str:
args.append(disabled_command)

cmd = Command(command_args,
env_vars=disabled_command.get("env"),
resource_limits=disabled_command.get("limits"),
args_subst={PROJECT_SUBST: project_name},
args_append=[project_name], excl_subst=True)
args_append=args, excl_subst=True)
run_command(cmd, project_name)


Expand Down Expand Up @@ -363,7 +381,9 @@ def mirror_project(config, project_name, check_changes, uri,
# We want this to be logged to the log file (if any).
if project_config:
if project_config.get(DISABLED_PROPERTY):
handle_disabled_project(config, project_name)
handle_disabled_project(config, project_name,
project_config.
get(DISABLED_REASON_PROPERTY))
logger.info("Project '{}' disabled, exiting".
format(project_name))
return CONTINUE_EXITVAL
Expand Down Expand Up @@ -429,7 +449,8 @@ def check_project_configuration(multiple_project_config, hookdir=False,
# Quick sanity check.
known_project_tunables = [DISABLED_PROPERTY, CMD_TIMEOUT_PROPERTY,
HOOK_TIMEOUT_PROPERTY, PROXY_PROPERTY,
IGNORED_REPOS_PROPERTY, HOOKS_PROPERTY]
IGNORED_REPOS_PROPERTY, HOOKS_PROPERTY,
DISABLED_REASON_PROPERTY]

if not multiple_project_config:
return True
Expand Down
46 changes: 45 additions & 1 deletion opengrok-tools/src/test/python/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
check_configuration, mirror_project, run_command, get_repos_for_project, \
HOOKS_PROPERTY, PROXY_PROPERTY, IGNORED_REPOS_PROPERTY, \
PROJECTS_PROPERTY, DISABLED_CMD_PROPERTY, DISABLED_PROPERTY, \
CMD_TIMEOUT_PROPERTY, HOOK_TIMEOUT_PROPERTY
CMD_TIMEOUT_PROPERTY, HOOK_TIMEOUT_PROPERTY, DISABLED_REASON_PROPERTY
import opengrok_tools.mirror
from opengrok_tools.utils.exitvals import (
CONTINUE_EXITVAL, FAILURE_EXITVAL
Expand Down Expand Up @@ -181,6 +181,10 @@ def test_empty_project_config():


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


def test_disabled_command_api_text_append(monkeypatch):
"""
Test that message text is appended if DISABLED_PROPERTY is a string.
"""

text_to_append = "foo bar"

def mock_call_rest_api(command, b, c):
disabled_command = config.get(DISABLED_CMD_PROPERTY)
assert disabled_command
command_args = disabled_command.get(COMMAND_PROPERTY)
assert command_args
data = command_args[2]
assert data
text = data.get("text")
assert text
assert text.find(text_to_append)

return mock(spec=requests.Response)

with monkeypatch.context() as m:
m.setattr("opengrok_tools.utils.mirror.call_rest_api",
mock_call_rest_api)

project_name = "foo"
data = {'messageLevel': 'info', 'duration': 'PT5M',
'tags': ['%PROJECT%'],
'text': 'disabled project'}
config = {DISABLED_CMD_PROPERTY:
{COMMAND_PROPERTY:
["http://localhost:8080/source/api/v1/foo",
"POST", data]},
PROJECTS_PROPERTY: {project_name:
{DISABLED_REASON_PROPERTY:
text_to_append,
DISABLED_PROPERTY: True}}}

mirror_project(config, project_name, False, None, None)


def test_disabled_command_run():
"""
Make sure that mirror_project() results in calling run_command().
Expand Down