| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from dataclasses import dataclass |
| 14 |
|
| 15 |
from .errors import JenkinsJobsException |
| 16 |
from .expander import Expander |
| 17 |
from .root_base import RootBase, NonTemplateRootMixin, TemplateRootMixin, Group |
| 18 |
from .defaults import split_contents_params, job_contents_keys |
| 19 |
|
| 20 |
|
| 21 |
@dataclass |
| 22 |
class JobBase(RootBase): |
| 23 |
project_type: str |
| 24 |
folder: str |
| 25 |
|
| 26 |
@classmethod |
| 27 |
def from_dict(cls, config, roots, data, pos): |
| 28 |
keep_descriptions = config.yamlparser["keep_descriptions"] |
| 29 |
d = data.copy() |
| 30 |
name = d.pop_required_loc_string("name") |
| 31 |
id = d.pop_loc_string("id", None) |
| 32 |
description = d.pop_loc_string("description", None) |
| 33 |
defaults = d.pop_loc_string("defaults", "global") |
| 34 |
project_type = d.pop_loc_string("project-type", None) |
| 35 |
folder = d.pop_loc_string("folder", None) |
| 36 |
contents, params = split_contents_params(d, job_contents_keys) |
| 37 |
return cls( |
| 38 |
_defaults=roots.defaults, |
| 39 |
_expander=Expander(config), |
| 40 |
_keep_descriptions=keep_descriptions, |
| 41 |
_id=id, |
| 42 |
name=name, |
| 43 |
pos=pos, |
| 44 |
description=description, |
| 45 |
defaults_name=defaults, |
| 46 |
params=params, |
| 47 |
_contents=contents, |
| 48 |
project_type=project_type, |
| 49 |
folder=folder, |
| 50 |
) |
| 51 |
|
| 52 |
@property |
| 53 |
def contents(self): |
| 54 |
contents = super().contents |
| 55 |
contents["name"] = self.name |
| 56 |
if self.project_type: |
| 57 |
contents["project-type"] = self.project_type |
| 58 |
if self.folder: |
| 59 |
contents["folder"] = self.folder |
| 60 |
return contents |
| 61 |
|
| 62 |
def _expand_contents(self, contents, params): |
| 63 |
expanded_contents = super()._expand_contents(contents, params) |
| 64 |
try: |
| 65 |
folder = expanded_contents["folder"] |
| 66 |
except KeyError: |
| 67 |
pass |
| 68 |
else: |
| 69 |
name = expanded_contents["name"] |
| 70 |
expanded_contents["name"] = f"{folder}/{name}" |
| 71 |
return expanded_contents |
| 72 |
|
| 73 |
|
| 74 |
class Job(JobBase, NonTemplateRootMixin): |
| 75 |
@classmethod |
| 76 |
def add(cls, config, roots, data, pos): |
| 77 |
job = cls.from_dict(config, roots, data, pos) |
| 78 |
roots.assign(roots.jobs, job.id, job, "job") |
| 79 |
|
| 80 |
def __str__(self): |
| 81 |
return f"job {self.name!r}" |
| 82 |
|
| 83 |
|
| 84 |
class JobTemplate(JobBase, TemplateRootMixin): |
| 85 |
@classmethod |
| 86 |
def add(cls, config, roots, data, pos): |
| 87 |
template = cls.from_dict(config, roots, data, pos) |
| 88 |
roots.assign(roots.job_templates, template.id, template, "job template") |
| 89 |
|
| 90 |
def __str__(self): |
| 91 |
return f"job template {self.name!r}" |
| 92 |
|
| 93 |
|
| 94 |
@dataclass |
| 95 |
class JobGroup(Group): |
| 96 |
_jobs: dict |
| 97 |
_job_templates: dict |
| 98 |
|
| 99 |
@classmethod |
| 100 |
def add(cls, config, roots, data, pos): |
| 101 |
d = data.copy() |
| 102 |
name = d.pop_required_loc_string("name") |
| 103 |
try: |
| 104 |
job_specs = cls._specs_from_list(d.pop("jobs", None)) |
| 105 |
except JenkinsJobsException as x: |
| 106 |
raise x.with_context(f"In job {name!r}", pos=pos) |
| 107 |
group = cls( |
| 108 |
name, |
| 109 |
pos, |
| 110 |
job_specs, |
| 111 |
d, |
| 112 |
roots.jobs, |
| 113 |
roots.job_templates, |
| 114 |
) |
| 115 |
roots.assign(roots.job_groups, group.name, group, "job group") |
| 116 |
|
| 117 |
def __str__(self): |
| 118 |
return f"job group {self.name!r}" |
| 119 |
|
| 120 |
@property |
| 121 |
def _root_dicts(self): |
| 122 |
return [self._jobs, self._job_templates] |