| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
""" |
| 5 |
Add notifications to tasks via Taskcluster's notify service. |
| 6 |
|
| 7 |
See https://docs.taskcluster.net/docs/reference/core/notify/usage for |
| 8 |
more information. |
| 9 |
""" |
| 10 |
|
| 11 |
from voluptuous import ALLOW_EXTRA, Any, Exclusive, Optional, Required |
| 12 |
|
| 13 |
from taskgraph.transforms.base import TransformSequence |
| 14 |
from taskgraph.util.schema import Schema, optionally_keyed_by, resolve_keyed_by |
| 15 |
|
| 16 |
_status_type = Any( |
| 17 |
"on-completed", |
| 18 |
"on-defined", |
| 19 |
"on-exception", |
| 20 |
"on-failed", |
| 21 |
"on-pending", |
| 22 |
"on-resolved", |
| 23 |
"on-running", |
| 24 |
) |
| 25 |
|
| 26 |
_recipients = [ |
| 27 |
{ |
| 28 |
Required("type"): "email", |
| 29 |
Required("address"): optionally_keyed_by("project", "level", str), |
| 30 |
Optional("status-type"): _status_type, |
| 31 |
}, |
| 32 |
{ |
| 33 |
Required("type"): "matrix-room", |
| 34 |
Required("room-id"): str, |
| 35 |
Optional("status-type"): _status_type, |
| 36 |
}, |
| 37 |
{ |
| 38 |
Required("type"): "pulse", |
| 39 |
Required("routing-key"): str, |
| 40 |
Optional("status-type"): _status_type, |
| 41 |
}, |
| 42 |
{ |
| 43 |
Required("type"): "slack-channel", |
| 44 |
Required("channel-id"): str, |
| 45 |
Optional("status-type"): _status_type, |
| 46 |
}, |
| 47 |
] |
| 48 |
|
| 49 |
_route_keys = { |
| 50 |
"email": "address", |
| 51 |
"matrix-room": "room-id", |
| 52 |
"pulse": "routing-key", |
| 53 |
"slack-channel": "channel-id", |
| 54 |
} |
| 55 |
"""Map each type to its primary key that will be used in the route.""" |
| 56 |
|
| 57 |
NOTIFY_SCHEMA = Schema( |
| 58 |
{ |
| 59 |
Exclusive("notify", "config"): { |
| 60 |
Required("recipients"): [Any(*_recipients)], |
| 61 |
Optional("content"): { |
| 62 |
Optional("email"): { |
| 63 |
Optional("subject"): str, |
| 64 |
Optional("content"): str, |
| 65 |
Optional("link"): { |
| 66 |
Required("text"): str, |
| 67 |
Required("href"): str, |
| 68 |
}, |
| 69 |
}, |
| 70 |
Optional("matrix"): { |
| 71 |
Optional("body"): str, |
| 72 |
Optional("formatted-body"): str, |
| 73 |
Optional("format"): str, |
| 74 |
Optional("msg-type"): str, |
| 75 |
}, |
| 76 |
Optional("slack"): { |
| 77 |
Optional("text"): str, |
| 78 |
Optional("blocks"): list, |
| 79 |
Optional("attachments"): list, |
| 80 |
}, |
| 81 |
}, |
| 82 |
}, |
| 83 |
# Continue supporting the legacy schema for backwards compat. |
| 84 |
Exclusive("notifications", "config"): { |
| 85 |
Required("emails"): optionally_keyed_by("project", "level", [str]), |
| 86 |
Required("subject"): str, |
| 87 |
Optional("message"): str, |
| 88 |
Optional("status-types"): [_status_type], |
| 89 |
}, |
| 90 |
}, |
| 91 |
extra=ALLOW_EXTRA, |
| 92 |
) |
| 93 |
"""Notify schema.""" |
| 94 |
|
| 95 |
transforms = TransformSequence() |
| 96 |
transforms.add_validate(NOTIFY_SCHEMA) |
| 97 |
|
| 98 |
|
| 99 |
def _convert_legacy(config, legacy, label): |
| 100 |
"""Convert the legacy format to the new one.""" |
| 101 |
notify = { |
| 102 |
"recipients": [], |
| 103 |
"content": {"email": {"subject": legacy["subject"]}}, |
| 104 |
} |
| 105 |
resolve_keyed_by( |
| 106 |
legacy, |
| 107 |
"emails", |
| 108 |
label, |
| 109 |
**{ |
| 110 |
"level": config.params["level"], |
| 111 |
"project": config.params["project"], |
| 112 |
}, |
| 113 |
) |
| 114 |
|
| 115 |
status_types = legacy.get("status-types", ["on-completed"]) |
| 116 |
for email in legacy["emails"]: |
| 117 |
for status_type in status_types: |
| 118 |
notify["recipients"].append( |
| 119 |
{"type": "email", "address": email, "status-type": status_type} |
| 120 |
) |
| 121 |
|
| 122 |
notify["content"]["email"]["content"] = legacy.get("message", legacy["subject"]) |
| 123 |
return notify |
| 124 |
|
| 125 |
|
| 126 |
def _convert_content(content): |
| 127 |
"""Convert the notify content to Taskcluster's format. |
| 128 |
|
| 129 |
The Taskcluster notification format is described here: |
| 130 |
https://docs.taskcluster.net/docs/reference/core/notify/usage |
| 131 |
""" |
| 132 |
tc = {} |
| 133 |
if "email" in content: |
| 134 |
tc["email"] = content.pop("email") |
| 135 |
|
| 136 |
for key, obj in content.items(): |
| 137 |
for name in obj.keys(): |
| 138 |
tc_name = "".join(part.capitalize() for part in name.split("-")) |
| 139 |
tc[f"{key}{tc_name}"] = obj[name] |
| 140 |
return tc |
| 141 |
|
| 142 |
|
| 143 |
@transforms.add |
| 144 |
def add_notifications(config, tasks): |
| 145 |
for task in tasks: |
| 146 |
label = "{}-{}".format(config.kind, task["name"]) |
| 147 |
if "notifications" in task: |
| 148 |
notify = _convert_legacy(config, task.pop("notifications"), label) |
| 149 |
else: |
| 150 |
notify = task.pop("notify", None) |
| 151 |
|
| 152 |
if not notify: |
| 153 |
yield task |
| 154 |
continue |
| 155 |
|
| 156 |
format_kwargs = dict( |
| 157 |
task=task, |
| 158 |
config=config.__dict__, |
| 159 |
) |
| 160 |
|
| 161 |
def substitute(ctx): |
| 162 |
"""Recursively find all strings in a simple nested dict (no lists), |
| 163 |
and format them in-place using `format_kwargs`.""" |
| 164 |
for key, val in ctx.items(): |
| 165 |
if isinstance(val, str): |
| 166 |
ctx[key] = val.format(**format_kwargs) |
| 167 |
elif isinstance(val, dict): |
| 168 |
ctx[key] = substitute(val) |
| 169 |
return ctx |
| 170 |
|
| 171 |
task.setdefault("routes", []) |
| 172 |
for recipient in notify["recipients"]: |
| 173 |
type = recipient["type"] |
| 174 |
recipient.setdefault("status-type", "on-completed") |
| 175 |
substitute(recipient) |
| 176 |
|
| 177 |
if type == "email": |
| 178 |
resolve_keyed_by( |
| 179 |
recipient, |
| 180 |
"address", |
| 181 |
label, |
| 182 |
**{ |
| 183 |
"level": config.params["level"], |
| 184 |
"project": config.params["project"], |
| 185 |
}, |
| 186 |
) |
| 187 |
|
| 188 |
task["routes"].append( |
| 189 |
f"notify.{type}.{recipient[_route_keys[type]]}.{recipient['status-type']}" |
| 190 |
) |
| 191 |
|
| 192 |
if "content" in notify: |
| 193 |
task.setdefault("extra", {}).update( |
| 194 |
{"notify": _convert_content(substitute(notify["content"]))} |
| 195 |
) |
| 196 |
yield task |