From 3b3bb47a985594001c64a3a89e97ea0835de96c4 Mon Sep 17 00:00:00 2001 From: Jaroslav Henner Date: Sat, 27 Sep 2014 08:59:05 +0200 Subject: [PATCH] Allow multiple comment-added events in gerrit trig. In order to be able to react on multiple verdict categories (for example Verify +1, Review +2) with one job, we need multiple definitions of trigger-on events of the same-kind (comment-added-event). This was not possible with the previous structure of event definitions. The new yaml structure definition also better reflects the XML structure. Change-Id: Idec0f2b07479c0611497126e984c13adf543ffc0 --- jenkins_jobs/modules/triggers.py | 149 +++++++++++++++++++++---- tests/triggers/fixtures/gerrit004.xml | 1 + tests/triggers/fixtures/gerrit004.yaml | 8 +- tests/triggers/fixtures/gerrit005.xml | 52 +++++++++ tests/triggers/fixtures/gerrit005.yaml | 14 +++ 5 files changed, 199 insertions(+), 25 deletions(-) create mode 100644 tests/triggers/fixtures/gerrit005.xml create mode 100644 tests/triggers/fixtures/gerrit005.yaml diff --git a/jenkins_jobs/modules/triggers.py b/jenkins_jobs/modules/triggers.py index 39aeec481..5569e2d4c 100644 --- a/jenkins_jobs/modules/triggers.py +++ b/jenkins_jobs/modules/triggers.py @@ -30,19 +30,24 @@ Example:: """ +import six import xml.etree.ElementTree as XML from jenkins_jobs.errors import JenkinsJobsException import jenkins_jobs.modules.base from jenkins_jobs.modules import hudson_model import logging import re +try: + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict + +logger = logging.getLogger(str(__name__)) def gerrit_handle_legacy_configuration(data): hyphenizer = re.compile("[A-Z]") - logger = logging.getLogger("%s:gerrit" % __name__) - def hyphenize(attr): """Convert strings like triggerOn to trigger-on. """ @@ -74,6 +79,7 @@ def gerrit_handle_legacy_configuration(data): 'failureMessage', 'skipVote', ]) + for project in data['projects']: convert_dict(project, [ 'projectCompareType', @@ -82,33 +88,72 @@ def gerrit_handle_legacy_configuration(data): 'branchPattern', ]) + old_format_events = OrderedDict( + (key, should_register) for key, should_register in six.iteritems(data) + if key.startswith('trigger-on-')) + trigger_on = data.setdefault('trigger-on', []) + if old_format_events: + logger.warn("The events: %s; which you used is/are deprecated. " + "Please use 'trigger-on' instead.", + ', '.join(old_format_events)) + + if old_format_events and trigger_on: + raise JenkinsJobsException( + 'Both, the new format (trigger-on) and old format (trigger-on-*) ' + 'gerrit events format found. Please use either the new or the old ' + 'format of trigger events definition.') + + trigger_on.extend(event_name[len('trigger-on-'):] + for event_name, should_register + in six.iteritems(old_format_events) if should_register) + + for idx, event in enumerate(trigger_on): + if event == 'comment-added-event': + trigger_on[idx] = events = OrderedDict() + events['comment-added-event'] = OrderedDict(( + ('approval-category', data['trigger-approval-category']), + ('approval-value', data['trigger-approval-value']) + )) + def build_gerrit_triggers(xml_parent, data): available_simple_triggers = { - 'trigger-on-change-abandoned-event': 'PluginChangeAbandonedEvent', - 'trigger-on-change-merged-event': 'PluginChangeMergedEvent', - 'trigger-on-change-restored-event': 'PluginChangeRestoredEvent', - 'trigger-on-draft-published-event': 'PluginDraftPublishedEvent', - 'trigger-on-patchset-uploaded-event': 'PluginPatchsetCreatedEvent', - 'trigger-on-ref-updated-event': 'PluginRefUpdatedEvent', + 'change-abandoned-event': 'PluginChangeAbandonedEvent', + 'change-merged-event': 'PluginChangeMergedEvent', + 'change-restored-event': 'PluginChangeRestoredEvent', + 'draft-published-event': 'PluginDraftPublishedEvent', + 'patchset-uploaded-event': 'PluginPatchsetCreatedEvent', + 'patchset-created-event': 'PluginPatchsetCreatedEvent', + 'ref-updated-event': 'PluginRefUpdatedEvent', } tag_namespace = 'com.sonyericsson.hudson.plugins.gerrit.trigger.' \ 'hudsontrigger.events' trigger_on_events = XML.SubElement(xml_parent, 'triggerOnEvents') - for config_key, tag_name in available_simple_triggers.items(): - if data.get(config_key, False): + + for event in data.get('trigger-on', []): + if isinstance(event, six.string_types): + tag_name = available_simple_triggers.get(event) + if event == 'patchset-uploaded-event': + logger.warn("'%s' is deprecated. Use 'patchset-created-event'" + "instead.", event) + if not tag_name: + known = ', '.join(available_simple_triggers.keys() + + ['comment-added-event']) + msg = ("The event '%s' under 'trigger-on' is not one of the " + "known: %s.") % (event, known) + raise JenkinsJobsException(msg) XML.SubElement(trigger_on_events, '%s.%s' % (tag_namespace, tag_name)) - - if data.get('trigger-on-comment-added-event', False): - cadded = XML.SubElement(trigger_on_events, - '%s.%s' % (tag_namespace, - 'PluginCommentAddedEvent')) - XML.SubElement(cadded, 'verdictCategory').text = \ - data['trigger-approval-category'] - XML.SubElement(cadded, 'commentAddedTriggerApprovalValue').text = \ - str(data['trigger-approval-value']) + else: + comment_added_event = event['comment-added-event'] + cadded = XML.SubElement( + trigger_on_events, + '%s.%s' % (tag_namespace, 'PluginCommentAddedEvent')) + XML.SubElement(cadded, 'verdictCategory').text = \ + comment_added_event['approval-category'] + XML.SubElement(cadded, 'commentAddedTriggerApprovalValue').text = \ + str(comment_added_event['approval-value']) def build_gerrit_skip_votes(xml_parent, data): @@ -128,22 +173,82 @@ def build_gerrit_skip_votes(xml_parent, data): def gerrit(parser, xml_parent, data): """yaml: gerrit + Trigger on a Gerrit event. Requires the Jenkins `Gerrit Trigger Plugin `_ version >= 2.6.0. - :arg bool trigger-on-patchset-uploaded-event: Trigger on patchset upload + :arg list trigger-on: Events to react on. Please use either the new + **trigger-on**, or the old **trigger-on-*** events definitions. You + cannot use both at once. + + .. _trigger_on: + + :Trigger on: + + * **patchset-created-event** -- Trigger upon patchset creation. + * **patchset-uploaded-event** -- Trigger upon patchset creation + (this is a alias for `patchset-created-event`). + + .. deprecated:: 1.1.0 Please use :ref:`trigger-on `. + + * **change-abandoned-event** -- Trigger on patchset abandoned. + Requires Gerrit Trigger Plugin version >= 2.8.0. + * **change-merged-event** -- Trigger on change merged + * **change-restored-event** -- Trigger on change restored. Requires + Gerrit Trigger Plugin version >= 2.8.0 + * **draft-published-event** -- Trigger on draft published event. + * **ref-updated-event** -- Trigger on ref-updated. + * **comment-added-event** (`dict`) -- Trigger on comment added. + + :Comment added: + * **approval-category** (`str`) -- Approval (verdict) category + (for example 'APRV', 'CRVW', 'VRIF' -- see `Gerrit access + control + `_ + + * **approval-value** -- Approval value for the comment added. + + :arg bool trigger-on-patchset-uploaded-event: Trigger on patchset upload. + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg bool trigger-on-change-abandoned-event: Trigger on change abandoned. Requires Gerrit Trigger Plugin version >= 2.8.0 + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg bool trigger-on-change-merged-event: Trigger on change merged + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg bool trigger-on-change-restored-event: Trigger on change restored. Requires Gerrit Trigger Plugin version >= 2.8.0 + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg bool trigger-on-comment-added-event: Trigger on comment added + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg bool trigger-on-draft-published-event: Trigger on draft published event + + .. deprecated:: 1.1.0 Please use :ref:`trigger-on `. + :arg bool trigger-on-ref-updated-event: Trigger on ref-updated + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg str trigger-approval-category: Approval category for comment added + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg int trigger-approval-value: Approval value for comment added + + .. deprecated:: 1.1.0. Please use :ref:`trigger-on `. + :arg bool override-votes: Override default vote values :arg int gerrit-build-successful-verified-value: Successful ''Verified'' value @@ -233,9 +338,9 @@ def gerrit(parser, xml_parent, data): Example: .. literalinclude:: /../../tests/triggers/fixtures/gerrit004.yaml - """ + :language: yaml - logger = logging.getLogger("%s:gerrit" % __name__) + """ gerrit_handle_legacy_configuration(data) diff --git a/tests/triggers/fixtures/gerrit004.xml b/tests/triggers/fixtures/gerrit004.xml index 44dd24742..62e1483c9 100644 --- a/tests/triggers/fixtures/gerrit004.xml +++ b/tests/triggers/fixtures/gerrit004.xml @@ -44,6 +44,7 @@ http://myhost/mytrigger true + APRV 1 diff --git a/tests/triggers/fixtures/gerrit004.yaml b/tests/triggers/fixtures/gerrit004.yaml index 242c03e62..84a45ce9c 100644 --- a/tests/triggers/fixtures/gerrit004.yaml +++ b/tests/triggers/fixtures/gerrit004.yaml @@ -1,8 +1,10 @@ triggers: - gerrit: - trigger-on-comment-added-event: true - trigger-approval-category: 'APRV' - trigger-approval-value: 1 + trigger-on: + - patchset-created-event + - comment-added-event: + approval-category: 'APRV' + approval-value: 1 projects: - project-compare-type: 'PLAIN' project-pattern: 'test-project' diff --git a/tests/triggers/fixtures/gerrit005.xml b/tests/triggers/fixtures/gerrit005.xml new file mode 100644 index 000000000..098f0c657 --- /dev/null +++ b/tests/triggers/fixtures/gerrit005.xml @@ -0,0 +1,52 @@ + + + + + + + + PLAIN + test-project + + + ANT + ** + + + + + ANT + subdirectory/** + + + + + + false + false + false + false + + false + true + false + False + + false + + + APRV + -1 + + + + + + + + + + __ANY__ + + + diff --git a/tests/triggers/fixtures/gerrit005.yaml b/tests/triggers/fixtures/gerrit005.yaml new file mode 100644 index 000000000..a24de1ec5 --- /dev/null +++ b/tests/triggers/fixtures/gerrit005.yaml @@ -0,0 +1,14 @@ +triggers: + - gerrit: + trigger-on-comment-added-event: true + trigger-approval-category: 'APRV' + trigger-approval-value: -1 + trigger-on-change-abandoned-event: true + projects: + - project-compare-type: 'PLAIN' + project-pattern: 'test-project' + branch-compare-type: 'ANT' + branch-pattern: '**' + file-paths: + - compare-type: ANT + pattern: subdirectory/**