Skip to content
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

RUN-2198: fix: quoting defaults to previous behavior if configuration value is null #13

Merged
merged 1 commit into from
Feb 8, 2024
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
21 changes: 17 additions & 4 deletions src/main/groovy/com/rundeck/plugins/logging/JsonLogFilter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ See [here](https://github.com/eiiches/jackson-jq#implementation-status-and-curre
)
Boolean logData

//Note: we don't set a defaultValue, so that we can detect a null value if the user has not set it
@PluginProperty(
title = 'Extra quotes',
description = '''If true, the result will be parse to string, that will add extra quotes to the result (compatible with rundeck 4.x)''',
defaultValue = 'false'
title = 'Extra quotes (Rundeck 4.x Compatibility)',
description = '''If true, the result will be parsed to a string with extra quotes to match Rundeck 4.x behavior.'''
)
Boolean extraQuotes=false
Boolean extraQuotes

private StringBuffer buffer;
OutputContext outputContext
Expand Down Expand Up @@ -82,6 +82,19 @@ See [here](https://github.com/eiiches/jackson-jq#implementation-status-and-curre
void complete(final PluginLoggingContext context) {

if(buffer.size()>0) {
if (extraQuotes == null) {
//fall back to previous behavior
extraQuotes = true
context.log(
1,
'JSON jq key/value mapper: defaulting to rundeck 4.x quoting behavior'
)
}else if(extraQuotes){
context.log(
1,
'JSON jq key/value mapper: Using rundeck 4.x quoting behavior'
)
}
//apply json transform
this.processJson(context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class JsonLogFilterSpec extends Specification {
plugin.filter = filter
plugin.prefix = "result"
plugin.logData = dolog
plugin.extraQuotes = false
def sharedoutput = new DataOutput(ContextView.global())
def context = Mock(PluginLoggingContext) {
getOutputContext() >> sharedoutput
Expand Down Expand Up @@ -62,6 +63,55 @@ class JsonLogFilterSpec extends Specification {
false | ".id" | ['{"id":"abc12345"}'] | [result: 'abc12345']
}

def "simple test legacy quoting"() {
given:
def plugin = new JsonLogFilter()
plugin.filter = filter
plugin.prefix = "result"
plugin.logData = dolog
plugin.extraQuotes = quoteVal
def sharedoutput = new DataOutput(ContextView.global())
def context = Mock(PluginLoggingContext) {
getOutputContext() >> sharedoutput
}
def events = []
lines.each { line ->
events << Mock(LogEventControl) {
getMessage() >> line
getEventType() >> 'log'
getLoglevel() >> LogLevel.NORMAL
}
}
when:
plugin.init(context)
events.each {
plugin.handleEvent(context, it)
}
plugin.complete(context)
then:

sharedoutput.getSharedContext().getData(ContextView.global())?.getData() == (expect ? ['data': expect] : null)
if (expect) {
if (dolog) {
1 * context.log(2, _, _)
} else {
0 * context.log(*_)
}
}


where:

quoteVal | dolog | filter | lines | expect
false | true | "." | ['{"test":"value"}'] | [test: 'value']
null | true | "." | ['{"test":"value"}'] | [test: '"value"']
true | true | "." | ['{"test":"value"}'] | [test: '"value"']
true | true | "." | ['["value"]'] | ['result.0': '"value"']
null | true | "." | ['["value"]'] | ['result.0': '"value"']
false | true | "." | ['["value"]'] | ['result.0': 'value']

}

def "array test"() {
given:
def filter = ".[]"
Expand All @@ -70,6 +120,7 @@ class JsonLogFilterSpec extends Specification {
plugin.filter = filter
plugin.prefix = "result"
plugin.logData = dolog
plugin.extraQuotes = false
def sharedoutput = new DataOutput(ContextView.global())
def context = Mock(PluginLoggingContext) {
getOutputContext() >> sharedoutput
Expand Down
Loading