From b67c0bc754bfa2dd79921f4eb7a47ec1ded6690e Mon Sep 17 00:00:00 2001 From: Kyr Shatskyy Date: Tue, 26 Feb 2019 15:04:54 +0100 Subject: [PATCH] Add support for 'RocketChat Notifier Plugin' Change-Id: I78755fb1d116a6e164817589457fe132b81a89eb Signed-off-by: Kyr Shatskyy --- jenkins_jobs/modules/publishers.py | 127 +++++++++++++++++++++++ tests/publishers/fixtures/rocket001.xml | 25 +++++ tests/publishers/fixtures/rocket001.yaml | 3 + tests/publishers/fixtures/rocket002.xml | 56 ++++++++++ tests/publishers/fixtures/rocket002.yaml | 42 ++++++++ 5 files changed, 253 insertions(+) create mode 100644 tests/publishers/fixtures/rocket001.xml create mode 100644 tests/publishers/fixtures/rocket001.yaml create mode 100644 tests/publishers/fixtures/rocket002.xml create mode 100644 tests/publishers/fixtures/rocket002.yaml diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index 38cf575c9..c20236b03 100644 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -1267,6 +1267,133 @@ def ftp_publisher(registry, xml_parent, data): helpers.convert_mapping_to_xml(ftp, data, mapping, fail_required=True) +def rocket(registry, xml_parent, data): + """yaml: rocket + RocketChat notification on build completion, + Requires the `RocketChat Notifier Plugin`. + + :arg str channel: Comma separated list of rooms (e.g. #project) + or persons (e.g. @john) + :arg bool abort: Notify abort (default false) + :arg bool start: Notify start (default false) + :arg bool success: Notify success (default false) + :arg bool failure: Notify failure (default false) + :arg bool repeated-failure: Notify repeated failure (default false) + :arg bool back-to-normal: Notify back to normal (default false) + :arg bool not-built: Notify if not built (default false) + :arg bool unstable: Notify if unstable (default false) + :arg str webhook-token: Webhook token for posting messages. + Overrides global authentication data and channel + :arg str commit-info: What commit information to include into + notification message. + + :commit-info values: + * **none** + * **authors** + * **authors-and-titles** + + :arg str custom-message: Custom text message (default '') + :arg bool trust-ssl: Trust RocketChat server certificate, if checked, + the SSL certificate will not be checked (default false) + :arg str build-server: Build Server URL + :arg list attachments: Optional list of attachments + + :attachments: + * **title** (`str`) Attachment title (required) + * **title-link** (`str`) + * **title-link-download** (`bool`) + * **color** (`str`) + * **text** (`str`) + * **collapsed** (`bool`) + * **message-link** (`str`) + * **author-name** (`str`) + * **author-link** (`str`) + * **author-icon** (`str`) + * **thumb** (`str`) Thumb URL + * **image** (`str`) Image URL + * **audio** (`str`) Audio URL + * **video** (`str`) Video URL + + Minimal example using defaults: + + .. literalinclude:: /../../tests/publishers/fixtures/rocket001.yaml + :language: yaml + + Full example: + + .. literalinclude:: /../../tests/publishers/fixtures/rocket002.yaml + :language: yaml + """ + rocketchat = XML.SubElement(xml_parent, + 'jenkins.plugins.rocketchatnotifier.RocketChatNotifier') + rocketchat.set('plugin', 'rocketchatnotifier') + required_mapping = [ + ('channel', 'channel', ''), + ('start', 'startNotification', False), + ('success', 'notifySuccess', False), + ('abort', 'notifyAborted', False), + ('not-built', 'notifyNotBuilt', False), + ('unstable', 'notifyUnstable', False), + ('failure', 'notifyFailure', False), + ('back-to-normal', 'notifyBackToNormal', False), + ('repeated-failure', 'notifyRepeatedFailure', False), + ('include-test-summary', 'includeTestSummary', False), + ('include-test-log', 'includeTestLog', False), + ('include-custom-message', 'includeCustomMessage', False), + ('commit-info', 'commitInfoChoice', 'none', + { + 'none': 'NONE', + 'authors': 'AUTHORS', + 'authors-and-titles': 'AUTHORS_AND_TITLES' + }), + ('raw-message', 'rawMessage', False), + ('webhook-token', 'webhookToken', ''), + ('webhook-token-credential-id', 'webhookTokenCredentialId', ''), + ] + optional_mapping = [ + ('trust-ssl', 'trustSSL', None), + ('build-server', 'buildServerUrl', None), + ] + + helpers.convert_mapping_to_xml( + rocketchat, data, optional_mapping, fail_required=False) + helpers.convert_mapping_to_xml( + rocketchat, data, required_mapping, fail_required=True) + + attach_required_mapping = [ + ('title', 'title', None), + ] + attach_optional_mapping = [ + ('title-link', 'titleLink', None), + ('title-link-download', 'titleLinkDownload', None), + ('color', 'color', None), + ('text', 'text', None), + ('collapsed', 'collapsed', None), # false | true + ('message-link', 'messageLink', None), + ('author-name', 'authorName', None), + ('author-link', 'authorLink', None), + ('author-icon', 'authorIcon', None), + ('thumb', 'thumbUrl', None), + ('image', 'imageUrl', None), + ('audio', 'audioUrl', None), + ('video', 'videoUrl', None), + ] + attach_list = data.get('attachments', None) + + attachments = XML.SubElement(rocketchat, 'attachments') + if attach_list is not None: + for attach_data in attach_list: + item = XML.SubElement(attachments, + 'jenkins.plugins.rocketchatnotifier.model.MessageAttachment') + helpers.convert_mapping_to_xml(item, attach_data, + attach_required_mapping, fail_required=True) + helpers.convert_mapping_to_xml(item, attach_data, + attach_optional_mapping, fail_required=False) + + XML.SubElement(rocketchat, 'customMessage').text = \ + data.get('custom-message', '') + + def junit(registry, xml_parent, data): """yaml: junit Publish JUnit test results. diff --git a/tests/publishers/fixtures/rocket001.xml b/tests/publishers/fixtures/rocket001.xml new file mode 100644 index 000000000..0d95d12a1 --- /dev/null +++ b/tests/publishers/fixtures/rocket001.xml @@ -0,0 +1,25 @@ + + + + + + false + false + false + false + false + false + false + false + false + false + false + NONE + false + + + + + + + diff --git a/tests/publishers/fixtures/rocket001.yaml b/tests/publishers/fixtures/rocket001.yaml new file mode 100644 index 000000000..b4e7b5142 --- /dev/null +++ b/tests/publishers/fixtures/rocket001.yaml @@ -0,0 +1,3 @@ +publishers: +- rocket: + channel: '' diff --git a/tests/publishers/fixtures/rocket002.xml b/tests/publishers/fixtures/rocket002.xml new file mode 100644 index 000000000..8d0f36494 --- /dev/null +++ b/tests/publishers/fixtures/rocket002.xml @@ -0,0 +1,56 @@ + + + + + true + http://localhost:8080/ + #channel,@user + true + true + true + true + true + true + true + true + true + true + true + AUTHORS_AND_TITLES + true + non-secure-webhook-token + secret-text-id + + + The Black + + + The Red + red + + + The Blue + blue + The navy blue + + + The White + title_link + true + white + All&all + true + message_link + author_name + author_link + author_icon + http://hostname/thumb.png + http://hostname/image.jpg + http://hostname/audio.mp3 + http://hostname/video.avi + + + Hello World! + + + diff --git a/tests/publishers/fixtures/rocket002.yaml b/tests/publishers/fixtures/rocket002.yaml new file mode 100644 index 000000000..d94c25b4a --- /dev/null +++ b/tests/publishers/fixtures/rocket002.yaml @@ -0,0 +1,42 @@ +publishers: +- rocket: + channel: '#channel,@user' + abort: true + back-to-normal: true + failure: true + not-built: true + repeated-failure: true + start: true + success: true + unstable: true + trust-ssl: true + build-server: 'http://localhost:8080/' + webhook-token: 'non-secure-webhook-token' + webhook-token-credential-id: 'secret-text-id' + commit-info: 'authors-and-titles' + include-custom-message: true + include-test-log: true + include-test-summary: true + custom-message: 'Hello World!' + raw-message: true + attachments: + - title: The Black + - title: The Red + color: red + - title: The Blue + color: blue + text: The navy blue + - title: The White + title-link: title_link + title-link-download: true + message-link: message_link + color: white + text: 'All&all' + collapsed: true + author-name: author_name + author-link: author_link + author-icon: author_icon + thumb: 'http://hostname/thumb.png' + image: 'http://hostname/image.jpg' + audio: 'http://hostname/audio.mp3' + video: 'http://hostname/video.avi'