Skip to content

Commit fd6962f

Browse files
committed
RUN-660: Add new feature to the plugin so that it can capture multiple key-value pairs for each matched line
1 parent e6e7786 commit fd6962f

File tree

2 files changed

+104
-23
lines changed

2 files changed

+104
-23
lines changed

src/main/groovy/com/rundeck/plugin/DataKeyFilterMultiLines.groovy

+44-23
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ See the [Java Pattern](https://docs.oracle.com/javase/8/docs/api/java/util/regex
5050
)
5151
Boolean hideOutput
5252

53+
@PluginProperty(
54+
title = 'Capture multiple values',
55+
description = '''If true, each line is scanned separately to match the regex, capturing multiple key/value pairs.
56+
If it just capture one group, each matched value will be added for the same key''',
57+
defaultValue = 'false'
58+
)
59+
Boolean captureMultipleKeysValues
60+
5361
@PluginProperty(
5462
title = 'Log Data',
5563
description = '''If true, log the captured data''',
@@ -87,7 +95,11 @@ See the [Java Pattern](https://docs.oracle.com/javase/8/docs/api/java/util/regex
8795
@Override
8896
void handleEvent(final PluginLoggingContext context, final LogEventControl event) {
8997
if (event.eventType == 'log' && event.loglevel == LogLevel.NORMAL && event.message?.length() > 0) {
90-
buffer.append(event.message).append(System.getProperty("line.separator"))
98+
if(captureMultipleKeysValues){
99+
extractKeyValue(event.message)
100+
} else {
101+
buffer.append(event.message).append(System.getProperty("line.separator"))
102+
}
91103

92104
if(hideOutput){
93105
event.loglevel = LogLevel.DEBUG
@@ -98,30 +110,12 @@ See the [Java Pattern](https://docs.oracle.com/javase/8/docs/api/java/util/regex
98110

99111
@Override
100112
void complete(final PluginLoggingContext context) {
101-
102113
if(buffer.size()>0){
103-
Matcher match = dataPattern.matcher(buffer.toString())
104-
105-
if (match.matches()) {
106-
def key,value
107-
108-
if(match.groupCount()>0){
109-
if(match.groupCount()==1 && name){
110-
key = name
111-
value = match.group(1)
112-
}else {
113-
if(match.groupCount()>1){
114-
key = match.group(1)
115-
value = match.group(2)
116-
}
117-
}
114+
extractKeyValue(buffer.toString())
115+
}
118116

119-
if (key && value) {
120-
allData[key] = value
121-
outputContext.addOutput("data", key, value)
122-
}
123-
}
124-
}
117+
if(!allData.isEmpty()){
118+
allData.each {outputContext.addOutput("data", it.getKey(), it.getValue())}
125119

126120
if (logData) {
127121
context.log(
@@ -134,6 +128,33 @@ See the [Java Pattern](https://docs.oracle.com/javase/8/docs/api/java/util/regex
134128
)
135129
}
136130
}
131+
}
132+
133+
private void extractKeyValue(String output){
134+
Matcher match = dataPattern.matcher(output)
135+
136+
if (match.matches()) {
137+
def key,value
138+
139+
if(match.groupCount()>0){
140+
if(match.groupCount()==1 && name){
141+
key = name
142+
value = match.group(1)
143+
}else {
144+
if(match.groupCount()>1){
145+
key = match.group(1)
146+
value = match.group(2)
147+
}
148+
}
137149

150+
if (key && value) {
151+
if(allData[key]) {
152+
allData[key] += "\n$value"
153+
} else {
154+
allData[key] = value
155+
}
156+
}
157+
}
158+
}
138159
}
139160
}

src/test/groovy/com/rundeck/plugin/DataKeyFilterMultiLinesSpec.groovy

+60
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,64 @@ class DataKeyFilterMultiLinesSpec extends Specification {
120120
'',
121121
'done'] | null
122122
}
123+
124+
@Unroll
125+
def "multiline-filter-multiple-key-value-pairs"() {
126+
given:
127+
def plugin = new DataKeyFilterMultiLines()
128+
plugin.regex = regex
129+
plugin.logData = dolog
130+
plugin.name = name
131+
plugin.captureMultipleKeysValues = true
132+
def sharedoutput = new DataOutput(ContextView.global())
133+
def context = Mock(PluginLoggingContext) {
134+
getOutputContext() >> sharedoutput
135+
}
136+
def events = []
137+
lines.each { line ->
138+
events << Mock(LogEventControl) {
139+
getMessage() >> line
140+
getEventType() >> 'log'
141+
getLoglevel() >> LogLevel.NORMAL
142+
}
143+
}
144+
when:
145+
plugin.init(context)
146+
events.each {
147+
plugin.handleEvent(context, it)
148+
}
149+
plugin.complete(context)
150+
then:
151+
152+
sharedoutput.getSharedContext().getData(ContextView.global())?.getData() == (expect ? ['data': expect] : null)
153+
if (expect) {
154+
if (dolog) {
155+
1 * context.log(2, _, _)
156+
} else {
157+
0 * context.log(*_)
158+
}
159+
}
160+
161+
162+
where:
163+
dolog | name | regex | lines | expect
164+
true | null | "(\\w+)\\s(\\d+)" | ['hello 1',
165+
'world 2',
166+
'test 456',
167+
'',
168+
'done'] | [test: "456", hello: "1", world: "2"]
169+
170+
171+
true | "any_name" | "(\\w+\\s\\d+)" | ['hello 1',
172+
'world 2',
173+
'test 456',
174+
'',
175+
'done'] | ["any_name": "hello 1\nworld 2\ntest 456"]
176+
177+
true | null | "(\\w+\\s\\d+)" | ['hello 1',
178+
'world 2',
179+
'test 456',
180+
'',
181+
'done'] | null
182+
}
123183
}

0 commit comments

Comments
 (0)