tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

release_notifications.py (2930B)


      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 via taskcluster-notify for release tasks
      6 """
      7 
      8 from string import Formatter
      9 
     10 from taskgraph.transforms.base import TransformSequence
     11 from taskgraph.util.schema import resolve_keyed_by
     12 
     13 from gecko_taskgraph.util.scriptworker import get_release_config
     14 
     15 transforms = TransformSequence()
     16 
     17 
     18 class TitleCaseFormatter(Formatter):
     19    """Support title formatter for strings"""
     20 
     21    def convert_field(self, value, conversion):
     22        if conversion == "t":
     23            return str(value).title()
     24        super().convert_field(value, conversion)
     25        return value
     26 
     27 
     28 titleformatter = TitleCaseFormatter()
     29 
     30 
     31 @transforms.add
     32 def add_notifications(config, jobs):
     33    release_config = get_release_config(config)
     34 
     35    for job in jobs:
     36        label = "{}-{}".format(config.kind, job["name"])
     37 
     38        notifications = job.pop("notifications", None)
     39        if notifications:
     40            resolve_keyed_by(
     41                notifications, "emails", label, project=config.params["project"]
     42            )
     43            resolve_keyed_by(
     44                notifications,
     45                "message",
     46                label,
     47                project=config.params["project"],
     48                level=config.params["level"],
     49            )
     50            resolve_keyed_by(
     51                notifications,
     52                "subject",
     53                label,
     54                project=config.params["project"],
     55                level=config.params["level"],
     56            )
     57            emails = notifications["emails"]
     58            format_kwargs = dict(
     59                task=job,
     60                config=config.__dict__,
     61                release_config=release_config,
     62            )
     63            subject = titleformatter.format(notifications["subject"], **format_kwargs)
     64            message = titleformatter.format(notifications["message"], **format_kwargs)
     65            emails = [email.format(**format_kwargs) for email in emails]
     66 
     67            # By default, we only send mail on success to avoid messages like 'blah is in the
     68            # candidates dir' when cancelling graphs, dummy job failure, etc
     69            status_types = notifications.get("status-types", ["on-completed"])
     70            for s in status_types:
     71                job.setdefault("routes", []).extend([
     72                    f"notify.email.{email}.{s}" for email in emails
     73                ])
     74 
     75            # Customize the email subject to include release name and build number
     76            job.setdefault("extra", {}).update({
     77                "notify": {
     78                    "email": {
     79                        "subject": subject,
     80                    }
     81                }
     82            })
     83            if message:
     84                job["extra"]["notify"]["email"]["content"] = message
     85 
     86        yield job