| 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 |
|
| 6 |
import functools |
| 7 |
import re |
| 8 |
from dataclasses import dataclass, field |
| 9 |
from typing import Dict, List, Union |
| 10 |
|
| 11 |
from taskgraph.task import Task |
| 12 |
|
| 13 |
from ..config import GraphConfig |
| 14 |
from ..parameters import Parameters |
| 15 |
from ..util.schema import Schema, validate_schema |
| 16 |
|
| 17 |
|
| 18 |
@dataclass(frozen=True) |
| 19 |
class RepoConfig: |
| 20 |
prefix: str |
| 21 |
name: str |
| 22 |
base_repository: str |
| 23 |
head_repository: str |
| 24 |
head_ref: str |
| 25 |
type: str |
| 26 |
path: str = "" |
| 27 |
head_rev: Union[str, None] = None |
| 28 |
ssh_secret_name: Union[str, None] = None |
| 29 |
|
| 30 |
|
| 31 |
@dataclass(frozen=True, eq=False) |
| 32 |
class TransformConfig: |
| 33 |
""" |
| 34 |
A container for configuration affecting transforms. The `config` argument |
| 35 |
to transforms is an instance of this class. |
| 36 |
""" |
| 37 |
|
| 38 |
# the name of the current kind |
| 39 |
kind: str |
| 40 |
|
| 41 |
# the path to the kind configuration directory |
| 42 |
path: str |
| 43 |
|
| 44 |
# the parsed contents of kind.yml |
| 45 |
config: Dict |
| 46 |
|
| 47 |
# the parameters for this task-graph generation run |
| 48 |
params: Parameters |
| 49 |
|
| 50 |
# a dict of all the tasks associated with the kind dependencies of the |
| 51 |
# current kind |
| 52 |
kind_dependencies_tasks: Dict[str, Task] |
| 53 |
|
| 54 |
# Global configuration of the taskgraph |
| 55 |
graph_config: GraphConfig |
| 56 |
|
| 57 |
# whether to write out artifacts for the decision task |
| 58 |
write_artifacts: bool |
| 59 |
|
| 60 |
@property |
| 61 |
@functools.lru_cache(maxsize=None) |
| 62 |
def repo_configs(self): |
| 63 |
repositories = self.graph_config["taskgraph"]["repositories"] |
| 64 |
if len(repositories) == 1: |
| 65 |
current_prefix = list(repositories.keys())[0] |
| 66 |
else: |
| 67 |
project = self.params["project"] |
| 68 |
matching_repos = { |
| 69 |
repo_prefix: repo |
| 70 |
for (repo_prefix, repo) in repositories.items() |
| 71 |
if re.match(repo["project-regex"], project) |
| 72 |
} |
| 73 |
if len(matching_repos) != 1: |
| 74 |
raise Exception( |
| 75 |
f"Couldn't find repository matching project `{project}`" |
| 76 |
) |
| 77 |
current_prefix = list(matching_repos.keys())[0] |
| 78 |
|
| 79 |
repo_configs = { |
| 80 |
current_prefix: RepoConfig( |
| 81 |
prefix=current_prefix, |
| 82 |
name=repositories[current_prefix]["name"], |
| 83 |
base_repository=self.params["base_repository"], |
| 84 |
head_repository=self.params["head_repository"], |
| 85 |
head_ref=self.params["head_ref"], |
| 86 |
head_rev=self.params["head_rev"], |
| 87 |
type=self.params["repository_type"], |
| 88 |
ssh_secret_name=repositories[current_prefix].get("ssh-secret-name"), |
| 89 |
), |
| 90 |
} |
| 91 |
if len(repositories) != 1: |
| 92 |
repo_configs.update( |
| 93 |
{ |
| 94 |
repo_prefix: RepoConfig( |
| 95 |
prefix=repo_prefix, |
| 96 |
name=repo["name"], |
| 97 |
base_repository=repo["default-repository"], |
| 98 |
head_repository=repo["default-repository"], |
| 99 |
head_ref=repo["default-ref"], |
| 100 |
type=repo["type"], |
| 101 |
ssh_secret_name=repo.get("ssh-secret-name"), |
| 102 |
) |
| 103 |
for (repo_prefix, repo) in repositories.items() |
| 104 |
if repo_prefix != current_prefix |
| 105 |
} |
| 106 |
) |
| 107 |
return repo_configs |
| 108 |
|
| 109 |
|
| 110 |
@dataclass() |
| 111 |
class TransformSequence: |
| 112 |
""" |
| 113 |
Container for a sequence of transforms. Each transform is represented as a |
| 114 |
callable taking (config, items) and returning a generator which will yield |
| 115 |
transformed items. The resulting sequence has the same interface. |
| 116 |
|
| 117 |
This is convenient to use in a file full of transforms, as it provides a |
| 118 |
decorator, @transforms.add, that will add the decorated function to the |
| 119 |
sequence. |
| 120 |
""" |
| 121 |
|
| 122 |
_transforms: List = field(default_factory=list) |
| 123 |
|
| 124 |
def __call__(self, config, items): |
| 125 |
for xform in self._transforms: |
| 126 |
items = xform(config, items) |
| 127 |
if items is None: |
| 128 |
raise Exception(f"Transform {xform} is not a generator") |
| 129 |
return items |
| 130 |
|
| 131 |
def add(self, func): |
| 132 |
self._transforms.append(func) |
| 133 |
return func |
| 134 |
|
| 135 |
def add_validate(self, schema): |
| 136 |
self.add(ValidateSchema(schema)) |
| 137 |
|
| 138 |
|
| 139 |
@dataclass |
| 140 |
class ValidateSchema: |
| 141 |
schema: Schema |
| 142 |
|
| 143 |
def __call__(self, config, tasks): |
| 144 |
for task in tasks: |
| 145 |
if "name" in task: |
| 146 |
error = "In {kind} kind task {name!r}:".format( |
| 147 |
kind=config.kind, name=task["name"] |
| 148 |
) |
| 149 |
elif "label" in task: |
| 150 |
error = "In task {label!r}:".format(label=task["label"]) |
| 151 |
elif "primary-dependency" in task: |
| 152 |
error = "In {kind} kind task for {dependency!r}:".format( |
| 153 |
kind=config.kind, dependency=task["primary-dependency"].label |
| 154 |
) |
| 155 |
else: |
| 156 |
error = "In unknown task:" |
| 157 |
validate_schema(self.schema, task, error) |
| 158 |
yield task |