From bb2a4effa41ed86d0c2887f9085151e216cea4cd Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 4 Apr 2014 20:35:34 +0900 Subject: [PATCH] Allow to specify multiple branches per project in Gerrit trigger Allow to specify multiple branch patterns in a 'branches' block within the 'project' block. If no 'branches' block is present, fall back to using the single branch specified directly in the 'project' section. If a branch is specified both at the project level, and in the 'branches' section, ignore the one at project level. Closes-Bug: #1302502 Change-Id: I1fb2ce0bb3776d365f2c0e98a703c6dfba5fac7f --- jenkins_jobs/modules/triggers.py | 71 +++++++++++++++----------- tests/triggers/fixtures/gerrit002.xml | 52 +++++++++++++++++++ tests/triggers/fixtures/gerrit002.yaml | 26 ++++++++++ tests/triggers/fixtures/gerrit003.xml | 68 ++++++++++++++++++++++++ tests/triggers/fixtures/gerrit003.yaml | 33 ++++++++++++ 5 files changed, 220 insertions(+), 30 deletions(-) create mode 100644 tests/triggers/fixtures/gerrit002.xml create mode 100644 tests/triggers/fixtures/gerrit002.yaml create mode 100644 tests/triggers/fixtures/gerrit003.xml create mode 100644 tests/triggers/fixtures/gerrit003.yaml diff --git a/jenkins_jobs/modules/triggers.py b/jenkins_jobs/modules/triggers.py index 2794faf98..42e56b93e 100644 --- a/jenkins_jobs/modules/triggers.py +++ b/jenkins_jobs/modules/triggers.py @@ -32,6 +32,7 @@ Example:: import xml.etree.ElementTree as XML import jenkins_jobs.modules.base +import logging import re @@ -152,8 +153,18 @@ def gerrit(parser, xml_parent, data): ''REG_EXP'' * **project-pattern** (`str`) -- Project name pattern to match * **branch-compare-type** (`str`) -- ''PLAIN'', ''ANT'' or - ''REG_EXP'' + ''REG_EXP'' (not used if `branches` list is specified) * **branch-pattern** (`str`) -- Branch name pattern to match + (not used if `branches` list is specified) + * **branches** (`list`) -- List of branches to match + (optional) + + :Branch: * **branch-compare-type** (`str`) -- ''PLAIN'', + ''ANT'' or ''REG_EXP'' (optional, defaults to + ''PLAIN'') + * **branch-pattern** (`str`) -- Branch name pattern + to match + * **file-paths** (`list`) -- List of file paths to match (optional) @@ -197,33 +208,13 @@ def gerrit(parser, xml_parent, data): configure Gerrit Trigger Plugin, instead of hyphenated-keys. While still supported, camedCase keys are deprecated and should not be used. - Example:: + Example: - triggers: - - gerrit: - trigger-on-comment-added-event: true - trigger-approval-category: 'APRV' - trigger-approval-value: 1 - projects: - - project-compare-type: 'PLAIN' - project-pattern: 'test-project' - branch-compare-type: 'ANT' - branch-pattern: '**' - file-paths: - - compare-type: ANT - pattern: subdirectory/** - skip-vote: - successful: true - failed: true - unstable: true - notbuilt: true - silent: false - escape-quotes: false - no-name-and-email: false - dynamic-trigger-enabled: true - dynamic-trigger-url: http://myhost/mytrigger + .. literalinclude:: /../../tests/triggers/fixtures/gerrit003.yaml """ + logger = logging.getLogger("%s:gerrit" % __name__) + gerrit_handle_legacy_configuration(data) projects = data['projects'] @@ -239,12 +230,32 @@ def gerrit(parser, xml_parent, data): XML.SubElement(gproj, 'compareType').text = \ project['project-compare-type'] XML.SubElement(gproj, 'pattern').text = project['project-pattern'] + branches = XML.SubElement(gproj, 'branches') - gbranch = XML.SubElement(branches, 'com.sonyericsson.hudson.plugins.' - 'gerrit.trigger.hudsontrigger.data.Branch') - XML.SubElement(gbranch, 'compareType').text = \ - project['branch-compare-type'] - XML.SubElement(gbranch, 'pattern').text = project['branch-pattern'] + project_branches = project.get('branches', []) + + if 'branch-compare-type' in project and 'branch-pattern' in project: + warning = 'branch-compare-type and branch-pattern at project ' \ + 'level are deprecated and support will be removed ' \ + 'in a later version of Jenkins Job Builder; ' + if project_branches: + warning += 'discarding values and using values from ' \ + 'branches section' + else: + warning += 'please use branches section instead' + logger.warn(warning) + if not project_branches: + project_branches = [ + {'branch-compare-type': project['branch-compare-type'], + 'branch-pattern': project['branch-pattern']}] + for branch in project_branches: + gbranch = XML.SubElement( + branches, 'com.sonyericsson.hudson.plugins.' + 'gerrit.trigger.hudsontrigger.data.Branch') + XML.SubElement(gbranch, 'compareType').text = \ + branch['branch-compare-type'] + XML.SubElement(gbranch, 'pattern').text = branch['branch-pattern'] + project_file_paths = project.get('file-paths', []) if project_file_paths: fps_tag = XML.SubElement(gproj, 'filePaths') diff --git a/tests/triggers/fixtures/gerrit002.xml b/tests/triggers/fixtures/gerrit002.xml new file mode 100644 index 000000000..d240841b6 --- /dev/null +++ b/tests/triggers/fixtures/gerrit002.xml @@ -0,0 +1,52 @@ + + + + + + + + PLAIN + test-project + + + PLAIN + master + + + PLAIN + stable + + + + + ANT + subdirectory/** + + + + + + true + true + true + true + + false + false + false + True + http://myhost/mytrigger + + + APRV + 1 + + + + + + + + + + diff --git a/tests/triggers/fixtures/gerrit002.yaml b/tests/triggers/fixtures/gerrit002.yaml new file mode 100644 index 000000000..1597e1c42 --- /dev/null +++ b/tests/triggers/fixtures/gerrit002.yaml @@ -0,0 +1,26 @@ +triggers: + - gerrit: + trigger-on-comment-added-event: true + trigger-approval-category: 'APRV' + trigger-approval-value: 1 + projects: + - project-compare-type: 'PLAIN' + project-pattern: 'test-project' + branches: + - branch-compare-type: 'PLAIN' + branch-pattern: 'master' + - branch-compare-type: 'PLAIN' + branch-pattern: 'stable' + file-paths: + - compare-type: ANT + pattern: subdirectory/** + skip-vote: + successful: true + failed: true + unstable: true + notbuilt: true + silent: false + escape-quotes: false + no-name-and-email: false + dynamic-trigger-enabled: true + dynamic-trigger-url: http://myhost/mytrigger diff --git a/tests/triggers/fixtures/gerrit003.xml b/tests/triggers/fixtures/gerrit003.xml new file mode 100644 index 000000000..064eac2b2 --- /dev/null +++ b/tests/triggers/fixtures/gerrit003.xml @@ -0,0 +1,68 @@ + + + + + + + + PLAIN + test-project + + + ANT + ** + + + + + ANT + subdirectory/** + + + + + PLAIN + another-test-project + + + PLAIN + master + + + PLAIN + stable + + + + + ANT + subdirectory/** + + + + + + true + true + true + true + + false + false + false + True + http://myhost/mytrigger + + + APRV + 1 + + + + + + + + + + diff --git a/tests/triggers/fixtures/gerrit003.yaml b/tests/triggers/fixtures/gerrit003.yaml new file mode 100644 index 000000000..5e347e7d4 --- /dev/null +++ b/tests/triggers/fixtures/gerrit003.yaml @@ -0,0 +1,33 @@ +triggers: + - gerrit: + trigger-on-comment-added-event: true + trigger-approval-category: 'APRV' + trigger-approval-value: 1 + projects: + - project-compare-type: 'PLAIN' + project-pattern: 'test-project' + branch-compare-type: 'ANT' + branch-pattern: '**' + file-paths: + - compare-type: ANT + pattern: subdirectory/** + - project-compare-type: 'PLAIN' + project-pattern: 'another-test-project' + branches: + - branch-compare-type: 'PLAIN' + branch-pattern: 'master' + - branch-compare-type: 'PLAIN' + branch-pattern: 'stable' + file-paths: + - compare-type: ANT + pattern: subdirectory/** + skip-vote: + successful: true + failed: true + unstable: true + notbuilt: true + silent: false + escape-quotes: false + no-name-and-email: false + dynamic-trigger-enabled: true + dynamic-trigger-url: http://myhost/mytrigger