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"""
5Transform the upload-symbols task description template,
6taskcluster/ci/upload-symbols/job-template.yml into an actual task description.
7"""
8
9
10from gecko_taskgraph.transforms.base import TransformSequence
11from gecko_taskgraph.util.attributes import RELEASE_PROJECTS
12from gecko_taskgraph.util.treeherder import join_symbol, inherit_treeherder_from_dep
13from gecko_taskgraph.util.attributes import copy_attributes_from_dependent_job
14
15import logging
16
17logger = logging.getLogger(__name__)
18
19transforms = TransformSequence()
20
21
22@transforms.add
23def check_nightlies(config, tasks):
24    """Ensure that we upload symbols for all shippable builds, so that crash-stats can
25    resolve any reports sent to it. Try may enable full symbols but not upload them.
26
27    Putting this check here (instead of the transforms for the build kind) lets us
28    leverage the any not-for-build-platforms set in the update-symbols kind."""
29    for task in tasks:
30        dep = task["primary-dependency"]
31        if (
32            config.params["project"] in RELEASE_PROJECTS
33            and dep.attributes.get("shippable")
34            and not dep.attributes.get("enable-full-crashsymbols")
35            and not dep.attributes.get("skip-upload-crashsymbols")
36        ):
37            raise Exception(
38                "Shippable job %s should have enable-full-crashsymbols attribute "
39                "set to true to enable symbol upload to crash-stats" % dep.label
40            )
41        yield task
42
43
44@transforms.add
45def fill_template(config, tasks):
46    for task in tasks:
47        dep = task["primary-dependency"]
48        task.pop("dependent-tasks", None)
49
50        # Fill out the dynamic fields in the task description
51        task["label"] = dep.label + "-upload-symbols"
52
53        # Skip tasks where we don't have the full crashsymbols enabled
54        if not dep.attributes.get("enable-full-crashsymbols") or dep.attributes.get(
55            "skip-upload-crashsymbols"
56        ):
57            logger.debug("Skipping upload symbols task for %s", task["label"])
58            continue
59
60        task["dependencies"] = {"build": dep.label}
61        task["worker"]["env"]["GECKO_HEAD_REPOSITORY"] = config.params[
62            "head_repository"
63        ]
64        task["worker"]["env"]["GECKO_HEAD_REV"] = config.params["head_rev"]
65        task["worker"]["env"]["SYMBOL_SECRET"] = task["worker"]["env"][
66            "SYMBOL_SECRET"
67        ].format(level=config.params["level"])
68
69        attributes = copy_attributes_from_dependent_job(dep)
70        attributes.update(task.get("attributes", {}))
71        task["attributes"] = attributes
72
73        treeherder = inherit_treeherder_from_dep(task, dep)
74        th = dep.task.get("extra")["treeherder"]
75        th_symbol = th.get("symbol")
76        th_groupsymbol = th.get("groupSymbol", "?")
77
78        # Disambiguate the treeherder symbol.
79        sym = "Sym" + (th_symbol[1:] if th_symbol.startswith("B") else th_symbol)
80        treeherder.setdefault("symbol", join_symbol(th_groupsymbol, sym))
81        task["treeherder"] = treeherder
82
83        # We only want to run these tasks if the build is run.
84        # XXX Better to run this on promote phase instead?
85        task["run-on-projects"] = dep.attributes.get("run_on_projects")
86        task["optimization"] = {"upload-symbols": None}
87        task["if-dependencies"] = ["build"]
88
89        # clear out the stuff that's not part of a task description
90        del task["primary-dependency"]
91
92        yield task
93