forked from packit/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.py
More file actions
936 lines (789 loc) · 32.8 KB
/
Copy pathschema.py
File metadata and controls
936 lines (789 loc) · 32.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT
import copy
import functools
import json
from collections.abc import Mapping
from logging import getLogger
from typing import Any, Optional, Union
from marshmallow import (
Schema,
ValidationError,
fields,
post_load,
pre_load,
validates_schema,
)
from marshmallow_enum import EnumField
from packit.actions import ActionName
from packit.config import (
CommonPackageConfig,
Config,
Deployment,
PackageConfig,
)
from packit.config.aliases import DEPRECATED_TARGET_MAP
from packit.config.commands import TestCommandConfig
from packit.config.common_package_config import MockBootstrapSetup
from packit.config.job_config import (
JobConfig,
JobConfigTriggerType,
JobConfigView,
JobType,
get_default_jobs,
)
from packit.config.notifications import (
FailureCommentNotificationsConfig,
FailureIssueNotificationsConfig,
NotificationsConfig,
PullRequestNotificationsConfig,
)
from packit.config.requirements import LabelRequirementsConfig, RequirementsConfig
from packit.config.sources import SourcesItem
from packit.constants import (
CHROOT_SPECIFIC_COPR_CONFIGURATION,
FAST_FORWARD_MERGE_INTO_KEY,
)
from packit.sync import SyncFilesItem
logger = getLogger(__name__)
class StringOrListOfStringsField(fields.Field):
"""Field type expecting a string or a list"""
def _serialize(self, value, attr, obj, **kwargs) -> list[str]:
return [str(item) for item in value]
def _deserialize(self, value, attr, data, **kwargs) -> list[str]:
if isinstance(value, list) and all(isinstance(v, str) for v in value):
return value
if isinstance(value, str):
return [value]
raise ValidationError(f"Expected 'list[str]' or 'str', got {type(value)!r}.")
class SyncFilesItemSchema(Schema):
"""Schema for SyncFilesItem"""
src = StringOrListOfStringsField()
dest = fields.String()
mkpath = fields.Boolean(default=False)
delete = fields.Boolean(default=False)
filters = fields.List(fields.String(), missing=None)
class FilesToSyncField(fields.Field):
"""
Field type representing SyncFilesItem
This is needed in order to handle entries which are strings, instead
of a dict matching SyncFilesItemSchema.
"""
def _serialize(self, value: Any, attr: str, obj: Any, **kwargs) -> list[dict]:
return SyncFilesItemSchema().dump(value)
def _deserialize(
self,
value: Any,
attr: Optional[str],
data: Optional[Mapping[str, Any]],
**kwargs,
) -> SyncFilesItem:
if isinstance(value, dict):
return SyncFilesItem(**SyncFilesItemSchema().load(value))
if isinstance(value, str):
return SyncFilesItem(src=[value], dest=value)
raise ValidationError(f"Expected 'dict' or 'str', got {type(value)!r}.")
class ActionField(fields.Field):
"""
Field class representing Action.
"""
def _serialize(self, value: Any, attr: str, obj: Any, **kwargs):
return {action_name.value: val for action_name, val in value.items()}
def _deserialize(
self,
value: Any,
attr: Optional[str],
data: Optional[Mapping[str, Any]],
**kwargs,
) -> dict:
if not isinstance(value, dict):
raise ValidationError(f"'dict' required, got {type(value)!r}.")
self.validate_all_actions(actions=list(value))
return {ActionName(key): val for key, val in value.items()}
def validate_all_actions(self, actions: list) -> None:
"""
Validates all keys and raises exception with list of all invalid keys
"""
invalid_actions = [
action for action in actions if not ActionName.is_valid_action(action)
]
if invalid_actions:
raise ValidationError(f"Unknown action(s) provided: {invalid_actions}")
class NotProcessedField(fields.Field):
"""
Field class to mark fields which will not be processed, only generates warning.
Can be passed additional message via additional_message parameter.
:param str additional_message: additional warning message to be displayed
"""
def _serialize(self, value: Any, attr: str, obj: Any, **kwargs):
raise NotImplementedError
def _deserialize(
self,
value: Any,
attr: Optional[str],
data: Optional[Mapping[str, Any]],
**kwargs,
):
logger.warning(f"{self.name!r} is no longer being processed.")
additional_message = self.metadata.get("additional_message")
if additional_message:
logger.warning(f"{additional_message}")
class SourceSchema(Schema):
"""
Schema for sources.
"""
path = fields.String(required=True)
url = fields.String(required=True)
@post_load
def make_instance(self, data, **_):
return SourcesItem(**data)
class PullRequestNotificationsSchema(Schema):
"""Configuration of commenting on pull requests."""
successful_build = fields.Bool(default=False)
@post_load
def make_instance(self, data, **kwargs):
return PullRequestNotificationsConfig(**data)
class FailureCommentNotificationsSchema(Schema):
"""Configuration of commenting on failures."""
message = fields.String(missing=None)
@post_load
def make_instance(self, data, **kwargs):
return FailureCommentNotificationsConfig(**data)
class FailureIssueNotificationsSchema(Schema):
"""Configuration of createing issues in upstream."""
create = fields.Bool(default=True)
@post_load
def make_instance(self, data, **kwargs):
return FailureIssueNotificationsConfig(**data)
class NotificationsSchema(Schema):
"""Configuration of notifications."""
pull_request = fields.Nested(PullRequestNotificationsSchema)
failure_comment = fields.Nested(FailureCommentNotificationsSchema)
failure_issue = fields.Nested(FailureIssueNotificationsSchema)
@post_load
def make_instance(self, data, **kwargs):
return NotificationsConfig(**data)
class TestCommandSchema(Schema):
"""Configuration of test command."""
default_labels = fields.List(fields.String, missing=None)
default_identifier = fields.String(missing=None)
@post_load
def make_instance(self, data, **kwargs):
return TestCommandConfig(**data)
class LabelRequirementsSchema(Schema):
present = fields.List(fields.String)
absent = fields.List(fields.String)
@post_load
def make_instance(self, data, **kwargs):
return LabelRequirementsConfig(**data)
class RequirementsSchema(Schema):
"""Configuration of test command."""
label = fields.Nested(LabelRequirementsSchema, missing=None)
@post_load
def make_instance(self, data, **kwargs):
return RequirementsConfig(**data)
class ListOrDict(fields.Field):
"""Field type expecting a List[str] or Dict[str, Dict[str, Any]]
Union is not supported by marshmallow so we have to validate manually :(
https://github.com/marshmallow-code/marshmallow/issues/1191
"""
def is_dict(self, value) -> bool:
return not (
not isinstance(value, dict)
or not all(isinstance(k, str) for k in value)
or not all(isinstance(v, dict) for v in value.values())
)
class DistGitBranches(ListOrDict):
ERR_MESSAGE = (
"Expected 'list[str]' or 'dict[str, dict[str, list]]', got {value!r} (type {type!r})."
'\nExample -> "dist_git_branches": '
'{{"fedora-rawhide": {{"fast_forward_merge_into": ["f40"]}}, epel9: {{}}}}}}'
)
def _deserialize(
self,
value,
attr,
data,
**kwargs,
) -> Union[list[str], dict[str, dict[str, list[str]]], None]:
if not (
(isinstance(value, list) and all(isinstance(v, str) for v in value))
or self.is_dict(value)
):
raise ValidationError(
self.ERR_MESSAGE.format(value=value, type=type(value)),
)
return value
def is_dict(self, value) -> bool:
if not super().is_dict(value):
return False
if isinstance(value, dict):
if any(
fast_forward_merge_into
for fast_forward_merge_into in value.values()
if not isinstance(fast_forward_merge_into, dict)
):
raise ValidationError(
self.ERR_MESSAGE.format(value=value, type=type(value)),
)
if any(
key
for fast_forward_merge_into in value.values()
for key in fast_forward_merge_into
if key != FAST_FORWARD_MERGE_INTO_KEY
):
raise ValidationError(
self.ERR_MESSAGE.format(value=value, type=type(value)),
)
return True
class Targets(ListOrDict):
def is_dict(self, value) -> bool:
if not super().is_dict(value):
return False
# check the 'attributes', e.g. {'distros': ['centos-7']} or
# {"additional_modules": "ruby:2.7,nodejs:12", "additional_packages": []}
for attr in value.values():
for key, value in attr.items():
# distros is a list of str
if key == "distros":
if isinstance(value, list) and all(
isinstance(distro, str) for distro in value
):
return True
raise ValidationError(
f"Expected list[str], got {value!r} (type {type(value)!r})",
)
# chroot-specific configuration:
if key in CHROOT_SPECIFIC_COPR_CONFIGURATION:
expected_type = CHROOT_SPECIFIC_COPR_CONFIGURATION[key].__class__
if isinstance(value, expected_type):
return True
raise ValidationError(
f"Expected {expected_type}, got {value!r} (type {type(value)!r})",
)
raise ValidationError(f"Unknown key {key!r} in {attr!r}")
return True
def _deserialize(self, value, attr, data, **kwargs) -> dict[str, dict[str, Any]]:
targets_dict: dict[str, dict[str, Any]]
if isinstance(value, list) and all(isinstance(v, str) for v in value):
targets_dict = {key: {} for key in value}
elif self.is_dict(value):
targets_dict = value
else:
raise ValidationError(
f"Expected 'list[str]' or 'dict[str,dict]', got {value!r} (type {type(value)!r}).",
)
for target in targets_dict:
if target in DEPRECATED_TARGET_MAP:
logger.warning(
f"Target '{target}' is deprecated. Please update your configuration "
f"file and use '{DEPRECATED_TARGET_MAP[target]}' instead.",
)
return targets_dict
class JobMetadataSchema(Schema):
"""Jobs metadata.
TODO: to be removed after deprecation period.
Will end also dist-git-branch and
dist_git_branch deprecation period.
"""
_targets = Targets(missing=None, data_key="targets")
timeout = fields.Integer()
owner = fields.String(missing=None)
project = fields.String(missing=None)
dist_git_branches = DistGitBranches(missing=None)
branch = fields.String(missing=None)
scratch = fields.Boolean()
list_on_homepage = fields.Boolean()
preserve_project = fields.Boolean()
use_internal_tf = fields.Boolean()
additional_packages = fields.List(fields.String(), missing=None)
additional_repos = fields.List(fields.String(), missing=None)
bootstrap = EnumField(MockBootstrapSetup, missing=None)
fmf_url = fields.String(missing=None)
fmf_ref = fields.String(missing=None)
fmf_path = fields.String(missing=None)
skip_build = fields.Boolean()
env = fields.Dict(keys=fields.String(), missing=None)
enable_net = fields.Boolean(missing=False)
tmt_plan = fields.String(missing=None)
tf_post_install_script = fields.String(missing=None)
tf_extra_params = fields.Dict(missing=None)
module_hotfixes = fields.Boolean()
@pre_load
def ordered_preprocess(self, data, **_):
for key in ("dist-git-branch", "dist_git_branch"):
if key in data:
logger.warning(
f"Job metadata key {key!r} has been renamed to 'dist_git_branches', "
f"please update your configuration file.",
)
data["dist_git_branches"] = data.pop(key)
for key in ("targets", "dist_git_branches"):
if isinstance(data.get(key), str):
# allow key value being specified as string, convert to list
data[key] = [data.pop(key)]
return data
def validate_repo_name(value):
"""
marshmallow validation for a repository name. Any
filename is acceptable: No slash, no zero char.
"""
if any(c in "/\0" for c in value):
raise ValidationError("Repository name must be a valid filename.")
return True
class CommonConfigSchema(Schema):
"""
Common configuration options and methods for a package.
"""
config_file_path = fields.String(missing=None)
specfile_path = fields.String(missing=None)
downstream_package_name = fields.String(missing=None)
upstream_project_url = fields.String(missing=None)
upstream_package_name = fields.String(missing=None, validate=validate_repo_name)
paths = fields.List(fields.String())
upstream_ref = fields.String(missing=None)
upstream_tag_template = fields.String()
archive_root_dir_template = fields.String()
dist_git_url = NotProcessedField(
additional_message="it is generated from dist_git_base_url and downstream_package_name",
load_only=True,
)
dist_git_base_url = fields.String(mising=None)
dist_git_namespace = fields.String(missing=None)
allowed_gpg_keys = fields.List(fields.String(), missing=None)
spec_source_id = fields.Method(
deserialize="spec_source_id_fm",
serialize="spec_source_id_serialize",
)
files_to_sync = fields.List(FilesToSyncField())
actions = ActionField(default={})
create_pr = fields.Bool(default=True)
sync_changelog = fields.Bool(default=False)
create_sync_note = fields.Bool(default=True)
patch_generation_ignore_paths = fields.List(fields.String(), missing=None)
patch_generation_patch_id_digits = fields.Integer(
missing=4,
default=4,
validate=lambda x: x >= 0,
)
notifications = fields.Nested(NotificationsSchema)
copy_upstream_release_description = fields.Bool(default=False)
sources = fields.List(fields.Nested(SourceSchema), missing=None)
merge_pr_in_ci = fields.Bool(default=True)
srpm_build_deps = fields.List(fields.String(), missing=None)
identifier = fields.String(missing=None)
packit_instances = fields.List(EnumField(Deployment), missing=[Deployment.prod])
issue_repository = fields.String(missing=None)
release_suffix = fields.String(missing=None)
update_release = fields.Bool(default=True)
upstream_tag_include = fields.String()
upstream_tag_exclude = fields.String()
prerelease_suffix_pattern = fields.String()
prerelease_suffix_macro = fields.String(missing=None)
upload_sources = fields.Bool(default=True)
test_command = fields.Nested(TestCommandSchema)
require = fields.Nested(RequirementsSchema)
status_name_template = fields.String(missing=None)
sync_test_job_statuses_with_builds = fields.Bool(default=True)
# Former 'metadata' keys
_targets = Targets(missing=None, data_key="targets")
timeout = fields.Integer()
owner = fields.String(missing=None)
project = fields.String(missing=None)
dist_git_branches = DistGitBranches(missing=None)
branch = fields.String(missing=None)
scratch = fields.Boolean()
list_on_homepage = fields.Boolean()
preserve_project = fields.Boolean()
use_internal_tf = fields.Boolean()
additional_packages = fields.List(fields.String(), missing=None)
additional_repos = fields.List(fields.String(), missing=None)
bootstrap = EnumField(MockBootstrapSetup, missing=None)
fmf_url = fields.String(missing=None)
fmf_ref = fields.String(missing=None)
fmf_path = fields.String(missing=None)
env = fields.Dict(keys=fields.String(), missing=None)
enable_net = fields.Boolean(missing=False)
allowed_pr_authors = fields.List(fields.String(), missing=None)
allowed_committers = fields.List(fields.String(), missing=None)
allowed_builders = fields.List(fields.String(), missing=None)
tmt_plan = fields.String(missing=None)
tf_post_install_script = fields.String(missing=None)
tf_extra_params = fields.Dict(missing=None)
module_hotfixes = fields.Boolean()
# Image Builder integration
image_distribution = fields.String(missing=None)
# these two are freeform so that users can immediately use IB's new features
image_request = fields.Dict(missing=None)
image_customizations = fields.Dict(missing=None)
copr_chroot = fields.String(missing=None)
# Packaging tool used for interaction with lookaside cache
pkg_tool = fields.String(missing=None)
sig = fields.String(missing=None)
version_update_mask = fields.String(missing=None)
parse_time_macros = fields.Dict(missing=None)
osh_diff_scan_after_copr_build = fields.Boolean(missing=True)
csmock_args = fields.String(missing=None)
use_target_repo_for_fmf_url = fields.Boolean(missing=False)
@staticmethod
def spec_source_id_serialize(value: CommonPackageConfig):
return value.spec_source_id
@staticmethod
def spec_source_id_fm(value: Union[str, int]):
"""
method used in spec_source_id field.Method
If value is int, it is transformed int -> "Source" + str(int)
ex.
1 -> "Source1"
:return str: prepends "Source" in case input value is int
"""
if value:
try:
value = int(value)
except ValueError:
# not a number
pass
else:
# is a number!
value = f"Source{value}"
return value
@post_load
def make_instance(self, data, **_):
return CommonPackageConfig(**data)
class JobConfigSchema(Schema):
"""
Schema for processing JobConfig config data.
"""
job = EnumField(JobType, required=True, attribute="type")
trigger = EnumField(JobConfigTriggerType, required=True)
skip_build = fields.Boolean()
manual_trigger = fields.Boolean()
labels = fields.List(fields.String(), missing=None)
packages = fields.Dict(
keys=fields.String(),
values=fields.Nested(CommonConfigSchema()),
)
package = fields.String(missing=None)
# sidetag group identifier for downstream Koji builds and Bodhi updates
sidetag_group = fields.String(missing=None)
# packages that depend on this downstream Koji build to be tagged into
# a particular sidetag group
dependents = fields.List(fields.String(), missing=None)
# packages whose downstream Koji builds are required to be tagged into
# a particular sidetag group by this downstream Koji build or Bodhi update
dependencies = fields.List(fields.String(), missing=None)
@pre_load
def ordered_preprocess(self, data, **_):
for package, config in data.get("packages", {}).items():
for key in ("targets", "dist_git_branches"):
if isinstance(config, dict) and isinstance(config.get(key), str):
# allow key value being specified as string, convert to list
data["packages"][package][key] = [config.pop(key)]
return data
@validates_schema
def specfile_path_defined(self, data: dict, **_):
"""Check if a 'specfile_path' is specified for each package
The only time 'specfile_path' is not required, is when the job is a
'test' job.
Args:
data: partially loaded configuration data.
Raises:
ValidationError, if 'specfile_path' is not specified when
it should be.
"""
# Note: At this point, 'data' is still a dict, but values are already
# loaded, this is why 'data["type"]' is already a JobType and not a string,
# and the package configs below are PackageConfig objects, not dictionaries.
if (data["type"] == JobType.tests and data.get("skip_build")) or data.get(
"specfile_path",
):
return
errors = {}
package: str
config: PackageConfig
for package, config in data.get("packages", {}).items():
if not config.specfile_path:
errors[package] = [
"'specfile_path' is not specified or "
"no specfile was found in the repo",
]
if errors:
raise ValidationError(errors)
@post_load
def make_instance(self, data, **_):
package = data.pop("package")
job_config = JobConfig(**data)
return JobConfigView(job_config, package) if package else job_config
class PackageConfigSchema(Schema):
"""
Schema for processing PackageConfig config data.
This class is intended to handle all the logic that is internal
to the configuration and it is possible to be done while loading
or dumping the configuration.
This includes, for example, setting default values which depend on
the value of other keys, or validating key values according to the
value of other keys.
It does not include setting the value of keys based on context
*external* to the config file (if there is a spec-file in the current
path, for example).
"""
jobs = fields.Nested(JobConfigSchema, many=True)
packages = fields.Dict(
keys=fields.String(),
values=fields.Nested(CommonConfigSchema()),
)
# list of deprecated keys and their replacement (new,old)
deprecated_keys: Union[None, tuple[tuple[str, str]]] = None
@pre_load
def ordered_preprocess(self, data: dict, **_) -> dict:
"""Rename deprecated keys, and set defaults for 'packages' and 'jobs'
Args:
data: configuration dictionary as loaded from packit.yaml
Returns:
Transformed configuration dictionary with defaults
for 'packages' and 'jobs' set.
"""
# Log the config before any pre-processing is done
logger.debug(
"Package config before pre-loading: %s",
json.dumps(data, separators=(",", ":")),
)
return functools.reduce(
lambda d, f: f(d),
[
self.rename_deprecated_keys,
self._convert_to_monorepo,
self._set_default_jobs,
# By this point, we expect both 'packages' and 'jobs' to be
# present in the config.
self.rearrange_packages,
self.rearrange_jobs,
self.process_job_triggers,
],
# Create a deepcopy(), so that loading doesn't modify the
# dictionary received.
copy.deepcopy(data),
)
def rename_deprecated_keys(self, data: dict) -> dict:
"""
Based on tuples stored in tuple cls.deprecated_keys, reassigns old keys values to new keys,
in case new key is None and logs warning
:param data: conf dictionary to process
:return: processed dictionary
"""
if not data: # data is None when .packit.yaml is empty
return data
if not self.deprecated_keys:
return data
for new_key_name, old_key_name in self.deprecated_keys:
old_key_value = data.get(old_key_name)
if old_key_value:
logger.warning(
f"{old_key_name!r} configuration key was renamed to {new_key_name!r},"
f" please update your configuration file.",
)
new_key_value = data.get(new_key_name)
if not new_key_value:
# prio: new > old
data[new_key_name] = old_key_value
del data[old_key_name]
return data
@staticmethod
def _convert_to_monorepo(data: dict) -> dict:
"""Converts the package config to the monorepo syntax, if it's
a single-package config.
Args:
data: package config of either single-repo or monorepo package.
Returns:
Dictionary that adheres to the monorepo syntax.
"""
# Don't use 'setdefault' in this case, as we should expect
# downstream_package_name only if there is no 'packages' key.
if "packages" not in data:
package_name = data.pop("downstream_package_name")
paths = data.pop("paths", ["./"])
data["packages"] = {
package_name: {
"downstream_package_name": package_name,
"paths": paths,
},
}
return data
@staticmethod
def _set_default_jobs(data: dict) -> dict:
"""Add default jobs, if none are configured.
Args:
data: package config that may or may not contain any jobs defined.
Returns:
Dictionary with package config that contains default jobs if none
were present.
"""
data.setdefault("jobs", get_default_jobs())
return data
@staticmethod
def rearrange_packages(data: dict) -> dict:
"""Update package objects with top-level configuration values
Top-level keys and values are copied to each package object if
the given key is not set in that object already.
Remove these keys from the top-level and return a dictionary
containing only a 'packages' and 'jobs' key.
Args:
data: configuration dictionary, before any of the leaves
having been loaded.
Returns:
A re-arranged configuration dictionary.
"""
# Pop 'packages' and 'jobs' in order for 'data'
# to contain only keys other then these when it comes
# to merging it bellow.
packages = data.pop("packages")
jobs = data.pop("jobs")
for k, v in packages.items():
# First set the defaults which are not inherited from
# the top-level, in case they are not set yet.
v.setdefault("downstream_package_name", k)
# Inherit default values from the top-level.
v.update(data | v)
return {"packages": packages, "jobs": jobs}
@staticmethod
def rearrange_jobs(data: dict) -> dict:
"""Set the selected package config objects in each job, and set defaults
according to the values specified on the level of job-objects (if any).
Args:
data: Configuration dict with 'packages' and 'jobs' already in place.
Returns:
Configuration dict where the package objects in jobs are correctly set.
"""
packages = data["packages"]
jobs = data["jobs"]
errors = {}
for i, job in enumerate(jobs):
# Validate the 'metadata' field if there is any, and merge its
# content with the job.
# Do this here in order to avoid complications further in the
# loading process.
if metadata := job.pop("metadata", {}):
logger.warning(
"The 'metadata' key in jobs is deprecated and can be removed. "
"Nest config options from 'metadata' directly under the job object.",
)
schema = JobMetadataSchema()
if errors := schema.validate(metadata):
raise ValidationError(errors)
if not_nested_metadata_keys := set(schema.fields).intersection(job):
raise ValidationError(
f"Keys: {not_nested_metadata_keys} are defined outside job metadata "
"dictionary. Mixing obsolete metadata dictionary and new job keys "
"is not possible. Remove obsolete nested job metadata dictionary.",
)
job.update(metadata)
top_keys = {}
for key in JobConfigSchema().fields:
if (value := job.pop(key, None)) is not None:
top_keys[key] = value
selected_packages = top_keys.pop("packages", None)
# Check that only packages which are defined on the top-level are selected.
# Do this here b/c the code further down requires this to be correct.
incorrect_packages = (
set(selected_packages).difference(packages)
if isinstance(selected_packages, (dict, list))
else None
)
if incorrect_packages:
errors[f"jobs[{i}].packages"] = (
f"Undefined package(s) referenced: {', '.join(incorrect_packages)}."
)
continue
# There is no 'packages' key in the job, so
# the job should handle all the top-level packages.
if not selected_packages:
jobs[i] = top_keys | {
"packages": {k: v | job for k, v in packages.items()},
}
# Some top-level packages are selected to be
# handled by the job.
elif isinstance(selected_packages, list):
jobs[i] = top_keys | {
"packages": {
k: v | job
for k, v in packages.items()
if k in selected_packages
},
}
# Some top-level packages are selected to be
# handled by the job AND have some custom config.
elif isinstance(selected_packages, dict):
jobs[i] = top_keys | {
"packages": {
k: packages[k] | job | v for k, v in selected_packages.items()
},
}
else:
errors[f"'jobs[{i}].packages'"] = [
f"Type is {type(selected_packages)} instead of 'list' or 'dict'.",
]
if errors:
# This will shadow all other possible errors in the configuration,
# as the process doesn't even get to the validation phase.
raise ValidationError(errors)
return data
@staticmethod
def process_job_triggers(data: dict) -> dict:
"""
Expands jobs with multiple triggers.
For example a job with `trigger: commit | koji_build` will be expanded into two jobs
with the same options except for `trigger`; the first one will have `trigger: commit`
and the second one will have `trigger: koji_build`.
"""
jobs = data["jobs"]
i = len(jobs) - 1
while i >= 0:
triggers = [t.strip() for t in jobs[i].get("trigger", "").split("|")]
if len(triggers) <= 1:
i -= 1
continue
original_job = jobs.pop(i)
for trigger in reversed(triggers):
job = copy.deepcopy(original_job)
job["trigger"] = trigger
jobs.insert(i, job)
i -= 1
return data
@post_load
def make_instance(self, data: dict, **_) -> PackageConfig:
return PackageConfig(**data)
class UserConfigSchema(Schema):
"""
Schema for processing Config config data.
"""
debug = fields.Bool()
fas_user = fields.String()
fas_password = fields.String()
keytab_path = fields.String()
redhat_api_refresh_token = fields.String()
upstream_git_remote = fields.String()
github_token = fields.String()
pagure_user_token = fields.String()
pagure_fork_token = fields.String()
github_app_installation_id = fields.String()
github_app_id = fields.String()
github_app_cert_path = fields.String()
authentication = fields.Dict()
command_handler = fields.String()
command_handler_work_dir = fields.String()
command_handler_pvc_env_var = fields.String()
command_handler_image_reference = fields.String()
command_handler_k8s_namespace = fields.String()
command_handler_pvc_volume_specs = fields.List(fields.Dict())
command_handler_storage_class = fields.String()
appcode = fields.String()
kerberos_realm = fields.String()
package_config_path = fields.String(default=None)
koji_build_command = fields.String()
pkg_tool = fields.String()
repository_cache = fields.String(default=None)
add_repositories_to_repository_cache = fields.Bool(default=True)
default_parse_time_macros = fields.Dict(missing=None)
@post_load
def make_instance(self, data, **kwargs):
return Config(**data)