Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit d0a1bf6

Browse files
committed
Implemented
1 parent cc52240 commit d0a1bf6

File tree

2 files changed

+63
-51
lines changed

2 files changed

+63
-51
lines changed

src/test/groovy/GetRegionFromPatternStepTests.groovy

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,10 @@ class GetRegionFromPatternStepTests extends ApmBasePipelineTest {
6060
@Test
6161
void test_simple_match() throws Exception {
6262
def script = loadScript(scriptName)
63-
def changeset = 'file.txt'
63+
def changeset = 'foo/bar/file.txt'
6464
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
65-
def module = script.call(pattern: '^file.txt')
66-
printCallStack()
67-
// TODO
68-
assertEquals('', module)
69-
assertTrue(assertMethodCallContainsPattern('log', "getRegionFromPattern: not found with regex ^file.txt"))
65+
def module = script.call(pattern: '([^\\/]+)\\/.*')
66+
assertEquals('foo', module)
7067
assertJobStatusSuccess()
7168
}
7269

@@ -75,31 +72,47 @@ class GetRegionFromPatternStepTests extends ApmBasePipelineTest {
7572
env.GIT_PREVIOUS_COMMIT = "foo-1"
7673
env.remove('CHANGE_TARGET')
7774
def script = loadScript(scriptName)
78-
def changeset = 'file.txt'
75+
def changeset = 'foo/bar/file.txt'
7976
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
80-
def module = script.call(pattern: '^file.txt')
77+
def module = script.call(pattern: '([^\\/]+)\\/.*')
8178
printCallStack()
82-
// TODO
83-
assertEquals('', module)
79+
assertEquals('foo', module)
8480
assertJobStatusSuccess()
8581
}
8682

8783
@Test
88-
void test_complex_match() throws Exception {
84+
void test_multiple_match() throws Exception {
8985
def script = loadScript(scriptName)
90-
def changeset = 'foo/anotherfolder/file.txt'
86+
def changeset = '''foo/bar/file.txt
87+
foo/bar/subfolder'''.stripMargin().stripIndent()
9188
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
92-
helper.registerAllowedMethod('sh', [Map.class], { m ->
93-
if (m.script.contains('git diff')) {
94-
return 'foo/anotherfolder/file.txt'
95-
} else {
96-
return 0
97-
}
98-
})
99-
def module = script.call(pattern: '^foo/.*/file.txt')
100-
printCallStack()
101-
// TODO
89+
def module = script.call(pattern: '([^\\/]+)\\/.*')
90+
assertEquals('foo', module)
91+
assertJobStatusSuccess()
92+
}
93+
94+
@Test
95+
void test_multiple_match_2() throws Exception {
96+
def script = loadScript(scriptName)
97+
def changeset = '''filebeat/README.md
98+
filebeat/Dockerfile
99+
filebeat/docs/faq.asciidoc
100+
filebeat/autodiscover/builder/hints/config.go'''.stripMargin().stripIndent()
101+
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
102+
def module = script.call(pattern: '([^\\/]+)\\/.*')
103+
assertEquals('filebeat', module)
104+
assertJobStatusSuccess()
105+
}
106+
107+
@Test
108+
void test_multiple_without_match() throws Exception {
109+
def script = loadScript(scriptName)
110+
def changeset = '''foo/bar/file.txt
111+
bar/foo/subfolder'''.stripMargin().stripIndent()
112+
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
113+
def module = script.call(pattern: '([^\\/]+)\\/.*')
102114
assertEquals('', module)
115+
assertTrue(assertMethodCallContainsPattern('log', 'getRegionFromPattern: not found'))
103116
assertJobStatusSuccess()
104117
}
105118

@@ -110,9 +123,8 @@ class GetRegionFromPatternStepTests extends ApmBasePipelineTest {
110123
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
111124
def module = script.call(pattern: '^unknown.txt')
112125
printCallStack()
113-
// TODO
114126
assertEquals('', module)
115-
assertTrue(assertMethodCallContainsPattern('log', "getRegionFromPattern: not found with regex ^unknown.txt"))
127+
assertTrue(assertMethodCallContainsPattern('log', 'getRegionFromPattern: not found with regex ^unknown.txt'))
116128
assertJobStatusSuccess()
117129
}
118130

@@ -135,15 +147,14 @@ class GetRegionFromPatternStepTests extends ApmBasePipelineTest {
135147
env.GIT_PREVIOUS_COMMIT = "foo-1"
136148
env.remove('CHANGE_TARGET')
137149
def script = loadScript(scriptName)
138-
def changeset = 'foo/anotherfolder/file.txt'
150+
def changeset = 'foo/bar/file.txt'
139151
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
140152
helper.registerAllowedMethod('sh', [Map.class], { m ->
141-
assertFalse(m.script.contains('origin/'))
142-
})
143-
def module = script.call(pattern: 'foo')
153+
assertFalse(m.script.contains('origin/'))
154+
})
155+
def module = script.call(pattern: '([^\\/]+)\\/.*')
144156
printCallStack()
145-
// TODO
146-
assertEquals('', module)
157+
assertEquals('foo', module)
147158
assertJobStatusSuccess()
148159
}
149160

@@ -154,39 +165,37 @@ class GetRegionFromPatternStepTests extends ApmBasePipelineTest {
154165
def changeset = 'foo/anotherfolder/file.txt'
155166
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
156167
helper.registerAllowedMethod('sh', [Map.class], { m ->
157-
assertFalse(m.script.contains('origin/'))
158-
})
168+
assertFalse(m.script.contains('origin/'))
169+
})
159170
def module = script.call(pattern: 'foo')
160171
printCallStack()
161-
// TODO
162172
assertEquals('', module)
163173
assertJobStatusSuccess()
164174
}
165175

166176
@Test
167177
void test_with_from_parameter() throws Exception {
168178
def script = loadScript(scriptName)
169-
def changeset = ''' foo/bar/file.txt
170-
'''.stripMargin().stripIndent()
179+
def changeset = 'foo/anotherfolder/file.txt'
171180
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
172-
def module = script.call(pattern: '^foo/.*/file.txt', from: 'something')
181+
def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something')
173182
printCallStack()
174-
// TODO
175-
assertEquals('', module)
183+
assertEquals('foo', module)
176184
assertTrue(assertMethodCallContainsPattern('sh', 'something...bar'))
177185
assertJobStatusSuccess()
178186
}
179187

180188
@Test
181189
void test_with_from_and_to_parameters() throws Exception {
182190
def script = loadScript(scriptName)
183-
def changeset = ''' foo/bar/file.txt
184-
'''.stripMargin().stripIndent()
191+
def changeset = 'foo/bar/file.txt'
185192
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
186-
def module = script.call(pattern: '^foo/.*/file.txt', from: 'something', to: 'else')
193+
helper.registerAllowedMethod('sh', [Map.class], { m ->
194+
assertFalse(m.script.contains('origin/'))
195+
})
196+
def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something', to: 'else')
187197
printCallStack()
188-
// TODO
189-
assertEquals('', module)
198+
assertEquals('foo', module)
190199
assertTrue(assertMethodCallContainsPattern('sh', 'something...else'))
191200
assertJobStatusSuccess()
192201
}
@@ -196,7 +205,6 @@ class GetRegionFromPatternStepTests extends ApmBasePipelineTest {
196205
def script = loadScript(scriptName)
197206
def module = script.call(pattern: '^foo/.*/file.txt', from: '', to: '')
198207
printCallStack()
199-
// TODO
200208
assertEquals('', module)
201209
assertTrue(assertMethodCallContainsPattern('log', 'getRegionFromPattern: CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.'))
202210
assertJobStatusSuccess()
@@ -207,7 +215,6 @@ class GetRegionFromPatternStepTests extends ApmBasePipelineTest {
207215
def script = loadScript(scriptName)
208216
def module = script.call(pattern: '^foo/.*/file.txt', to: '')
209217
printCallStack()
210-
// TODO
211218
assertEquals('', module)
212219
assertTrue(assertMethodCallContainsPattern('log', 'getRegionFromPattern: CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.'))
213220
assertJobStatusSuccess()

vars/getRegionFromPattern.groovy

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,20 @@ def call(Map params = [:]) {
5252
return group
5353
}
5454

55-
def getGroup(gitDiffFile, patterns) {
56-
return ''
55+
def getGroup(gitDiffFile, pattern) {
5756
def fileContent = readFile(gitDiffFile)
58-
def match = true
57+
def modules = [:]
58+
// /beat\/module\/([^\/]+)\/.*/
5959
fileContent.split('\n').each { String line ->
6060
log(level: 'DEBUG', text: "changeset element: '${line}'")
61-
if (!patterns.every { line ==~ it }) {
62-
match = false
61+
matches = line =~ pattern
62+
def auxModule = matches.collect { it[1] }[0] ?: ''
63+
if (auxModule.trim()) {
64+
modules[auxModule] = auxModule
6365
}
6466
}
65-
return match
67+
if (modules.size() == 1) {
68+
return modules.values().toArray()[0]
69+
}
70+
return ''
6671
}

0 commit comments

Comments
 (0)