Debian Codegraph @

jenkins_jobs/modules/notifications.py

raw
1 # Copyright 2012 Hewlett-Packard Development Company, L.P.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15
16 """
17 The Notifications module allows you to configure Jenkins to notify
18 other applications about various build phases. It requires the
19 Jenkins notification plugin.
20
21 **Component**: notifications
22 :Macro: notification
23 :Entry Point: jenkins_jobs.notifications
24
25 """
26
27 import xml.etree.ElementTree as XML
28
29 import jenkins_jobs.modules.base
30 import jenkins_jobs.modules.helpers as helpers
31
32
33 def http_endpoint(registry, xml_parent, data):
34 """yaml: http
35 Defines an HTTP notification endpoint.
36
37 Requires the Jenkins :jenkins-plugins:`Notification Plugin
38 <notification>`.
39
40 :arg str format: notification payload format, JSON (default) or XML
41 :arg str event: job events that trigger notifications: started,
42 completed, finalized or all (default)
43 :arg str url: URL of the endpoint
44 :arg int timeout: Timeout in milliseconds for sending notification
45 request (30 seconds by default)
46 :arg int retries: Nr of times to retry sending notification in case
47 sending notification fails. (0 by default)
48 :arg int log: Number lines of log messages to send (0 by default).
49 Use -1 for all (use with caution).
50
51 Example:
52
53 .. literalinclude:: \
54 /../../tests/notifications/fixtures/http-endpoint002.yaml
55 :language: yaml
56
57 """
58 endpoint_element = XML.SubElement(
59 xml_parent, "com.tikal.hudson.plugins.notification." "Endpoint"
60 )
61 supported_formats = ["JSON", "XML"]
62 supported_events = ["started", "completed", "finalized", "all"]
63 fmt = data.get("format", "JSON").upper()
64 event = data.get("event", "all").lower()
65 mapping = [
66 ("", "format", fmt, supported_formats),
67 ("", "protocol", "HTTP"),
68 ("", "event", event, supported_events),
69 ("timeout", "timeout", 30000),
70 ("retries", "retries", 0),
71 ("url", "url", None),
72 ("log", "loglines", 0),
73 ]
74 helpers.convert_mapping_to_xml(endpoint_element, data, mapping, fail_required=True)
75
76
77 class Notifications(jenkins_jobs.modules.base.Base):
78 sequence = 22
79
80 component_type = "notification"
81 component_list_type = "notifications"
82
83 def gen_xml(self, xml_parent, data):
84 properties = xml_parent.find("properties")
85 if properties is None:
86 properties = XML.SubElement(xml_parent, "properties")
87
88 notifications = data.get("notifications", [])
89 if notifications:
90 notify_element = XML.SubElement(
91 properties,
92 "com.tikal.hudson.plugins."
93 "notification."
94 "HudsonNotificationProperty",
95 )
96 endpoints_element = XML.SubElement(notify_element, "endpoints")
97
98 self.dispatch_component_list(
99 "notification", notifications, endpoints_element
100 )