This repository was archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
ci: getGitMatchingGroup step #535
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c5d4f61
wip ci: getRegionFromPattern step
v1v cc52240
ci: minor changes
v1v d0a1bf6
Implemented
v1v feba9cc
rename
v1v bece6a2
Merge branch 'master' into feature/getRegionFromPattern
v1v 427739c
add exclude parameter
v1v 8972e1b
fix: serialisation issue
v1v c2cca9e
test: ITs
v1v 2476123
rename
v1v a4824dc
Apply suggestions from code review
v1v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import static org.junit.Assert.assertFalse | ||
| import static org.junit.Assert.assertEquals | ||
| import static org.junit.Assert.assertTrue | ||
|
|
||
| class GetGitMatchingGroupStepTests extends ApmBasePipelineTest { | ||
| String scriptName = 'vars/getGitMatchingGroup.groovy' | ||
|
|
||
| def realData = '''CHANGELOG.next.asciidoc | ||
| metricbeat/docs/modules/zookeeper.asciidoc | ||
| metricbeat/docs/modules/zookeeper/connection.asciidoc | ||
| metricbeat/docs/modules_list.asciidoc | ||
| metricbeat/module/zookeeper/_meta/docs.asciidoc | ||
| metricbeat/module/zookeeper/connection/_meta/docs.asciidoc | ||
| metricbeat/module/zookeeper/connection/_meta/fields.yml | ||
| metricbeat/module/zookeeper/connection/connection.go | ||
| metricbeat/module/zookeeper/fields.go | ||
| metricbeat/module/zookeeper/mntr/_meta/docs.asciidoc | ||
| metricbeat/module/zookeeper/server/_meta/docs.asciidoc'''.stripMargin().stripIndent() | ||
|
|
||
| @Override | ||
| @Before | ||
| void setUp() throws Exception { | ||
| super.setUp() | ||
| env.CHANGE_TARGET = 'foo' | ||
| env.GIT_BASE_COMMIT = 'bar' | ||
| } | ||
|
|
||
| @Test | ||
| void test_without_pattern_parameter() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| try { | ||
| script.call() | ||
| } catch(e){ | ||
| //NOOP | ||
| } | ||
| printCallStack() | ||
| assertTrue(assertMethodCallContainsPattern('error', 'Missing pattern argument.')) | ||
| assertJobStatusFailure() | ||
| } | ||
|
|
||
| @Test | ||
| void test_without_env_variables() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def result = true | ||
| env.remove('CHANGE_TARGET') | ||
| def module = script.call(pattern: 'foo') | ||
| printCallStack() | ||
| assertEquals('', module) | ||
| assertTrue(assertMethodCallContainsPattern('log', 'CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_simple_match() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def changeset = 'foo/bar/file.txt' | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*') | ||
| assertEquals('foo', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_simple_match_with_previous_commit_env_variable() throws Exception { | ||
| env.GIT_PREVIOUS_COMMIT = "foo-1" | ||
| env.remove('CHANGE_TARGET') | ||
| def script = loadScript(scriptName) | ||
| def changeset = 'foo/bar/file.txt' | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*') | ||
| printCallStack() | ||
| assertEquals('foo', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_multiple_match() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def changeset = '''foo/bar/file.txt | ||
| foo/bar/subfolder'''.stripMargin().stripIndent() | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*') | ||
| assertEquals('foo', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_multiple_match_2() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def changeset = '''filebeat/README.md | ||
| filebeat/Dockerfile | ||
| filebeat/docs/faq.asciidoc | ||
| filebeat/autodiscover/builder/hints/config.go'''.stripMargin().stripIndent() | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*') | ||
| assertEquals('filebeat', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_multiple_without_match() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def changeset = '''foo/bar/file.txt | ||
| bar/foo/subfolder'''.stripMargin().stripIndent() | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*') | ||
| assertEquals('', module) | ||
| assertTrue(assertMethodCallContainsPattern('log', 'not found')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_simple_unmatch() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def changeset = 'foo/anotherfolder/file.txt' | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| def module = script.call(pattern: '^unknown.txt') | ||
| printCallStack() | ||
| assertEquals('', module) | ||
| assertTrue(assertMethodCallContainsPattern('log', 'not found with regex ^unknown.txt')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_windows() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| helper.registerAllowedMethod('isUnix', [], { false }) | ||
| try { | ||
| script.call() | ||
| } catch(e){ | ||
| //NOOP | ||
| } | ||
| printCallStack() | ||
| assertTrue(assertMethodCallContainsPattern('error', 'windows is not supported yet.')) | ||
| assertJobStatusFailure() | ||
| } | ||
|
|
||
| @Test | ||
| void test_without_change_request_env_variable() throws Exception { | ||
| env.GIT_PREVIOUS_COMMIT = "foo-1" | ||
| env.remove('CHANGE_TARGET') | ||
| def script = loadScript(scriptName) | ||
| def changeset = 'foo/bar/file.txt' | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| helper.registerAllowedMethod('sh', [Map.class], { m -> | ||
| assertFalse(m.script.contains('origin/')) | ||
| }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*') | ||
| printCallStack() | ||
| assertEquals('foo', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_with_empty_change_target_env_variable() throws Exception { | ||
| env.CHANGE_TARGET = " " | ||
| def script = loadScript(scriptName) | ||
| def changeset = 'foo/anotherfolder/file.txt' | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| helper.registerAllowedMethod('sh', [Map.class], { m -> | ||
| assertFalse(m.script.contains('origin/')) | ||
| }) | ||
| def module = script.call(pattern: 'foo') | ||
| printCallStack() | ||
| assertEquals('', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_with_from_parameter() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def changeset = 'foo/anotherfolder/file.txt' | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something') | ||
| printCallStack() | ||
| assertEquals('foo', module) | ||
| assertTrue(assertMethodCallContainsPattern('sh', 'something...bar')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_with_from_and_to_parameters() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def changeset = 'foo/bar/file.txt' | ||
| helper.registerAllowedMethod('readFile', [String.class], { return changeset }) | ||
| helper.registerAllowedMethod('sh', [Map.class], { m -> | ||
| assertFalse(m.script.contains('origin/')) | ||
| }) | ||
| def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something', to: 'else') | ||
| printCallStack() | ||
| assertEquals('foo', module) | ||
| assertTrue(assertMethodCallContainsPattern('sh', 'something...else')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_with_empty_values_for_from_and_to_parameters() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def module = script.call(pattern: '^foo/.*/file.txt', from: '', to: '') | ||
| printCallStack() | ||
| assertEquals('', module) | ||
| assertTrue(assertMethodCallContainsPattern('log', 'CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_with_empty_value_for_to_parameter() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| def module = script.call(pattern: '^foo/.*/file.txt', to: '') | ||
| printCallStack() | ||
| assertEquals('', module) | ||
| assertTrue(assertMethodCallContainsPattern('log', 'CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.')) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_multiple_match_with_real_data_with_exclude() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| helper.registerAllowedMethod('readFile', [String.class], { return realData }) | ||
| def module = script.call(pattern: '.*\\/module\\/([^\\/]+)\\/.*', exclude: '(.*\\/docs\\/.*|.*\\.asciidoc)' ) | ||
| assertEquals('zookeeper', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_multiple_match_with_real_data_without_exclude() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| helper.registerAllowedMethod('readFile', [String.class], { return realData }) | ||
| def module = script.call(pattern: '.*\\/module\\/([^\\/]+)\\/.*') | ||
| assertEquals('', module) | ||
| assertJobStatusSuccess() | ||
| } | ||
|
|
||
| @Test | ||
| void test_is_excluded() throws Exception { | ||
| def script = loadScript(scriptName) | ||
| assertFalse(script.isExcluded('', '')) | ||
| assertTrue(script.isExcluded('metricbeat/docs/modules/zookeeper.asciidoc', '(.*\\/docs\\/.*|.*\\.asciidoc)')) | ||
| assertFalse(script.isExcluded('metricbeat/zookeeper.asciido', '(.*\\/docs\\/.*|.*\\.asciidoc)')) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| NAME = 'it/getBeatsModule' | ||
| DSL = '''pipeline { | ||
| agent any | ||
| environment { | ||
| PIPELINE_LOG_LEVEL = 'DEBUG' | ||
| } | ||
| stages { | ||
| stage('checkout') { | ||
| steps { | ||
| gitCheckout(credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7-UserAndToken', | ||
| repo: 'https://github.com/elastic/apm-pipeline-library.git', | ||
| branch: 'master', | ||
| basedir: 'sub-folder', | ||
| shallow: false) | ||
| } | ||
| } | ||
| stage('match') { | ||
| steps { | ||
| dir('sub-folder') { | ||
| script { | ||
| def module = getBeatsModule(pattern: '^\\\\.ci\\\\/([^\\\\/]+)\\\\/.*', | ||
v1v marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from: '347185bd7e2b402ba8f6befa5ef4428ad417fbbc', | ||
| to: '4d9fc25d258622c767ec4d38df38520647cc7dda') | ||
| whenFalse(module.equals('jobs')){ | ||
| error("Expected module name 'jobs'") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| stage('no_match') { | ||
| steps { | ||
| dir('sub-folder') { | ||
| script { | ||
| def module = getBeatsModule(pattern: '^\\\\ci\\\\/([^\\\\/]+)\\\\/.*', | ||
v1v marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from: '347185bd7e2b402ba8f6befa5ef4428ad417fbbc', | ||
| to: '4d9fc25d258622c767ec4d38df38520647cc7dda') | ||
| whenFalse(module.equals('')){ | ||
| error("Expected module name ''") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| stage('match_with_exclude') { | ||
| steps { | ||
| dir('sub-folder') { | ||
| script { | ||
| def module = getBeatsModule(pattern: '^test-infra\\\\/([^\\\\/]+)\\\\/.*', | ||
v1v marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from: 'b0de59d0ec1e2ae52103a238a2e9cb5b0d7fd9b8', | ||
| to: '641fd600836abafa51def05260d63fab6eed4707', | ||
| exclude: '^resources.*') | ||
| whenFalse(module.equals('apm-ci')){ | ||
| error("Expected module name 'apm-ci'") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }''' | ||
|
|
||
| pipelineJob(NAME) { | ||
| definition { | ||
| cps { | ||
| script(DSL.stripIndent()) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.