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
This commit is contained in:
Jaroslav Henner 2014-09-27 08:59:05 +02:00
parent 307343bf10
commit 3b3bb47a98
5 changed files with 199 additions and 25 deletions

View File

@ -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
<wiki.jenkins-ci.org/display/JENKINS/Gerrit+Trigger>`_ 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 <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
<http://gerrit.googlecode.com/svn/documentation/2.1/
access-control.html#categories>`_
* **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 <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 <trigger_on>`.
:arg bool trigger-on-change-merged-event: Trigger on change merged
.. deprecated:: 1.1.0. Please use :ref:`trigger-on <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 <trigger_on>`.
:arg bool trigger-on-comment-added-event: Trigger on comment added
.. deprecated:: 1.1.0. Please use :ref:`trigger-on <trigger_on>`.
:arg bool trigger-on-draft-published-event: Trigger on draft published
event
.. deprecated:: 1.1.0 Please use :ref:`trigger-on <trigger_on>`.
:arg bool trigger-on-ref-updated-event: Trigger on ref-updated
.. deprecated:: 1.1.0. Please use :ref:`trigger-on <trigger_on>`.
:arg str trigger-approval-category: Approval category for comment added
.. deprecated:: 1.1.0. Please use :ref:`trigger-on <trigger_on>`.
:arg int trigger-approval-value: Approval value for comment added
.. deprecated:: 1.1.0. Please use :ref:`trigger-on <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)

View File

@ -44,6 +44,7 @@
<triggerConfigURL>http://myhost/mytrigger</triggerConfigURL>
<allowTriggeringUnreviewedPatches>true</allowTriggeringUnreviewedPatches>
<triggerOnEvents>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent/>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginCommentAddedEvent>
<verdictCategory>APRV</verdictCategory>
<commentAddedTriggerApprovalValue>1</commentAddedTriggerApprovalValue>

View File

@ -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'

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<triggers class="vector">
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger>
<spec/>
<gerritProjects>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
<compareType>PLAIN</compareType>
<pattern>test-project</pattern>
<branches>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
<compareType>ANT</compareType>
<pattern>**</pattern>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
</branches>
<filePaths>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.FilePath>
<compareType>ANT</compareType>
<pattern>subdirectory/**</pattern>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.FilePath>
</filePaths>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
</gerritProjects>
<skipVote>
<onSuccessful>false</onSuccessful>
<onFailed>false</onFailed>
<onUnstable>false</onUnstable>
<onNotBuilt>false</onNotBuilt>
</skipVote>
<silentMode>false</silentMode>
<escapeQuotes>true</escapeQuotes>
<noNameAndEmailParameters>false</noNameAndEmailParameters>
<dynamicTriggerConfiguration>False</dynamicTriggerConfiguration>
<triggerConfigURL/>
<allowTriggeringUnreviewedPatches>false</allowTriggeringUnreviewedPatches>
<triggerOnEvents>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginCommentAddedEvent>
<verdictCategory>APRV</verdictCategory>
<commentAddedTriggerApprovalValue>-1</commentAddedTriggerApprovalValue>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginCommentAddedEvent>
<com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginChangeAbandonedEvent/>
</triggerOnEvents>
<buildStartMessage/>
<buildFailureMessage/>
<buildSuccessfulMessage/>
<buildUnstableMessage/>
<buildNotBuiltMessage/>
<customUrl/>
<serverName>__ANY__</serverName>
</com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger>
</triggers>
</project>

View File

@ -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/**