From e55b3d408c9b7162e83c909bb8ba12e746d2308b Mon Sep 17 00:00:00 2001 From: Yolande Amate Date: Sat, 12 Aug 2017 23:52:47 +0100 Subject: [PATCH] Add support for jira-changelog trigger Change-Id: Ib66ed6ed2cc0ae909baf8657e122c8c4d3e42331 Signed-off-by: Thanh Ha Signed-off-by: Sorin Sbarnea --- jenkins_jobs/modules/triggers.py | 100 ++++++++++++++++++ .../triggers/fixtures/jira-changelog-full.xml | 32 ++++++ .../fixtures/jira-changelog-full.yaml | 19 ++++ .../fixtures/jira-changelog-minimal.xml | 10 ++ .../fixtures/jira-changelog-minimal.yaml | 2 + 5 files changed, 163 insertions(+) create mode 100644 tests/triggers/fixtures/jira-changelog-full.xml create mode 100644 tests/triggers/fixtures/jira-changelog-full.yaml create mode 100644 tests/triggers/fixtures/jira-changelog-minimal.xml create mode 100644 tests/triggers/fixtures/jira-changelog-minimal.yaml diff --git a/jenkins_jobs/modules/triggers.py b/jenkins_jobs/modules/triggers.py index d5f3ba1d2..181f0df3c 100644 --- a/jenkins_jobs/modules/triggers.py +++ b/jenkins_jobs/modules/triggers.py @@ -1875,6 +1875,106 @@ def parameterized_timer(parser, xml_parent, data): convert_mapping_to_xml(param_timer, data, mapping, fail_required=True) +def jira_changelog(registry, xml_parent, data): + """yaml: jira-changelog + Sets up a trigger that listens to JIRA issue changes + Requires the Jenkins :jenkins-wiki:`JIRA Trigger Plugin + `. + + :arg str jql-filter: Must match updated issues to trigger a build. + (default '') + :arg list changelog-matchers: + + :Custom Field Matcher: + * **custom-field-name** (`str`) -- The custom field + name that has been changed during the issue update. + (default '') + * **compare-new-value** (`bool`) -- Compare the + new value of the updated field. (default false) + * **new-value** (`str`) -- The new value of the updated field. + (default '') + * **compare-old-value** (`bool`) -- Compare the + old value of the updated field. (default false) + * **old-value** (`str`) -- The value + before the field is updated. (default '') + + :JIRA Field Matcher: + * **jira-field-ID** (`str`) -- The JIRA Field ID that + has been changed during the issue update. (default '') + * **compare-new-value** (`bool`) -- Compare the new value + of the updated field. (default false) + * **new-value** (`str`) -- The new value of the updated field. + (default '') + * **compare-old-value** (`bool`) -- Compare the old value + of the updated field. (default false) + * **old-value** (`str`) -- The value before + the field is updated. (default '') + + :arg list parameter-mapping: + + :Issue Attribute Path: + * **jenkins-parameter** (`str`) -- Jenkins parameter name + (default '') + * **issue-attribute-path** (`str`) -- Attribute path (default '') + + Minimal Example: + + .. literalinclude:: + /../../tests/triggers/fixtures/jira-changelog-minimal.yaml + :language: yaml + + Full Example: + + .. literalinclude:: + /../../tests/triggers/fixtures/jira-changelog-full.yaml + :language: yaml + """ + jcht = XML.SubElement(xml_parent, 'com.ceilfors.jenkins.plugins.' + 'jiratrigger.JiraChangelogTrigger') + jcht.set('plugin', 'jira-trigger') + + mapping = [('jql-filter', 'jqlFilter', '')] + convert_mapping_to_xml(jcht, data, mapping, fail_required=True) + + changelog = XML.SubElement(jcht, 'changelogMatchers') + mappings = [ + ('field', 'field', ''), + ('new-value', 'newValue', ''), + ('old-value', 'oldValue', ''), + ('compare-new-value', 'comparingNewValue', False), + ('compare-old-value', 'comparingOldValue', False), + ] + for matcher in data.get('changelog-matchers', []): + + fieldtype = matcher.get('field-type') + if fieldtype == 'CUSTOM': + parent_tag = XML.SubElement(changelog, 'com.ceilfors.jenkins.' + 'plugins.jiratrigger.changelog.' + 'CustomFieldChangelogMatcher') + XML.SubElement(parent_tag, 'fieldType').text = 'CUSTOM' + + elif fieldtype == 'JIRA': + parent_tag = XML.SubElement(changelog, 'com.ceilfors.jenkins.' + 'plugins.jiratrigger.changelog.' + 'JiraFieldChangelogMatcher') + XML.SubElement(parent_tag, 'fieldType').text = 'JIRA' + + convert_mapping_to_xml(parent_tag, matcher, + mappings, fail_required=True) + + param = XML.SubElement(jcht, 'parameterMappings') + parameter_mappings = [ + ('jenkins-parameter', 'jenkinsParameter', ''), + ('issue-attribute-path', 'issueAttributePath', ''), + ] + for parameter in data.get('parameter-mapping', []): + parent = XML.SubElement(param, 'com.ceilfors.jenkins.plugins.' + 'jiratrigger.parameter.' + 'IssueAttributePathParameterMapping') + convert_mapping_to_xml( + parent, parameter, parameter_mappings, fail_required=True) + + def jira_comment_trigger(registry, xml_parent, data): """yaml: jira-comment-trigger Trigger builds when a comment is added to JIRA. diff --git a/tests/triggers/fixtures/jira-changelog-full.xml b/tests/triggers/fixtures/jira-changelog-full.xml new file mode 100644 index 000000000..0a0cd80f4 --- /dev/null +++ b/tests/triggers/fixtures/jira-changelog-full.xml @@ -0,0 +1,32 @@ + + + + + filter + + + CUSTOM + name + val1 + val2 + true + true + + + JIRA + versions + val3 + val4 + true + true + + + + + param + path + + + + + diff --git a/tests/triggers/fixtures/jira-changelog-full.yaml b/tests/triggers/fixtures/jira-changelog-full.yaml new file mode 100644 index 000000000..3a3419d62 --- /dev/null +++ b/tests/triggers/fixtures/jira-changelog-full.yaml @@ -0,0 +1,19 @@ +triggers: + - jira-changelog: + jql-filter: filter + changelog-matchers: + - field-type: 'CUSTOM' + field: name + new-value: val1 + old-value: val2 + compare-new-value: true + compare-old-value: true + - field-type: 'JIRA' + field: versions + new-value: val3 + old-value: val4 + compare-new-value: true + compare-old-value: true + parameter-mapping: + - jenkins-parameter: param + issue-attribute-path: path diff --git a/tests/triggers/fixtures/jira-changelog-minimal.xml b/tests/triggers/fixtures/jira-changelog-minimal.xml new file mode 100644 index 000000000..0a2a8bf0a --- /dev/null +++ b/tests/triggers/fixtures/jira-changelog-minimal.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/tests/triggers/fixtures/jira-changelog-minimal.yaml b/tests/triggers/fixtures/jira-changelog-minimal.yaml new file mode 100644 index 000000000..524d87e16 --- /dev/null +++ b/tests/triggers/fixtures/jira-changelog-minimal.yaml @@ -0,0 +1,2 @@ +triggers: + - jira-changelog