-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
selective_checks.py
1291 lines (1165 loc) · 51 KB
/
selective_checks.py
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import difflib
import json
import os
import re
import sys
from enum import Enum
from functools import cached_property, lru_cache
from pathlib import Path
from typing import Any, Dict, List, TypeVar
from airflow_breeze.branch_defaults import AIRFLOW_BRANCH, DEFAULT_AIRFLOW_CONSTRAINTS_BRANCH
from airflow_breeze.global_constants import (
ALL_PYTHON_MAJOR_MINOR_VERSIONS,
APACHE_AIRFLOW_GITHUB_REPOSITORY,
BASE_PROVIDERS_COMPATIBILITY_CHECKS,
CHICKEN_EGG_PROVIDERS,
COMMITTERS,
CURRENT_KUBERNETES_VERSIONS,
CURRENT_MYSQL_VERSIONS,
CURRENT_POSTGRES_VERSIONS,
CURRENT_PYTHON_MAJOR_MINOR_VERSIONS,
DEFAULT_KUBERNETES_VERSION,
DEFAULT_MYSQL_VERSION,
DEFAULT_POSTGRES_VERSION,
DEFAULT_PYTHON_MAJOR_MINOR_VERSION,
HELM_VERSION,
KIND_VERSION,
RUNS_ON_PUBLIC_RUNNER,
RUNS_ON_SELF_HOSTED_RUNNER,
TESTABLE_INTEGRATIONS,
GithubEvents,
SelectiveUnitTestTypes,
all_helm_test_packages,
all_selective_test_types,
)
from airflow_breeze.utils.console import get_console
from airflow_breeze.utils.exclude_from_matrix import excluded_combos
from airflow_breeze.utils.kubernetes_utils import get_kubernetes_python_combos
from airflow_breeze.utils.packages import get_available_packages
from airflow_breeze.utils.path_utils import (
AIRFLOW_PROVIDERS_ROOT,
AIRFLOW_SOURCES_ROOT,
DOCS_DIR,
SYSTEM_TESTS_PROVIDERS_ROOT,
TESTS_PROVIDERS_ROOT,
)
from airflow_breeze.utils.provider_dependencies import DEPENDENCIES, get_related_providers
from airflow_breeze.utils.run_utils import run_command
ALL_VERSIONS_LABEL = "all versions"
DEBUG_CI_RESOURCES_LABEL = "debug ci resources"
DEFAULT_VERSIONS_ONLY_LABEL = "default versions only"
DISABLE_IMAGE_CACHE_LABEL = "disable image cache"
FULL_TESTS_NEEDED_LABEL = "full tests needed"
INCLUDE_SUCCESS_OUTPUTS_LABEL = "include success outputs"
LATEST_VERSIONS_ONLY_LABEL = "latest versions only"
NON_COMMITTER_BUILD_LABEL = "non committer build"
UPGRADE_TO_NEWER_DEPENDENCIES_LABEL = "upgrade to newer dependencies"
USE_PUBLIC_RUNNERS_LABEL = "use public runners"
USE_SELF_HOSTED_RUNNERS_LABEL = "use self-hosted runners"
ALL_CI_SELECTIVE_TEST_TYPES = (
"API Always BranchExternalPython BranchPythonVenv "
"CLI Core ExternalPython Operators Other PlainAsserts "
"Providers[-amazon,google] Providers[amazon] Providers[google] "
"PythonVenv Serialization WWW"
)
ALL_PROVIDERS_SELECTIVE_TEST_TYPES = "Providers[-amazon,google] Providers[amazon] Providers[google]"
class FileGroupForCi(Enum):
ENVIRONMENT_FILES = "environment_files"
PYTHON_PRODUCTION_FILES = "python_scans"
JAVASCRIPT_PRODUCTION_FILES = "javascript_scans"
ALWAYS_TESTS_FILES = "always_test_files"
API_TEST_FILES = "api_test_files"
API_CODEGEN_FILES = "api_codegen_files"
HELM_FILES = "helm_files"
DEPENDENCY_FILES = "dependency_files"
DOC_FILES = "doc_files"
WWW_FILES = "www_files"
SYSTEM_TEST_FILES = "system_tests"
KUBERNETES_FILES = "kubernetes_files"
ALL_PYTHON_FILES = "all_python_files"
ALL_SOURCE_FILES = "all_sources_for_tests"
ALL_AIRFLOW_PYTHON_FILES = "all_airflow_python_files"
ALL_PROVIDERS_PYTHON_FILES = "all_provider_python_files"
ALL_DEV_PYTHON_FILES = "all_dev_python_files"
ALL_PROVIDER_YAML_FILES = "all_provider_yaml_files"
ALL_DOCS_PYTHON_FILES = "all_docs_python_files"
T = TypeVar("T", FileGroupForCi, SelectiveUnitTestTypes)
class HashableDict(Dict[T, List[str]]):
def __hash__(self):
return hash(frozenset(self))
CI_FILE_GROUP_MATCHES = HashableDict(
{
FileGroupForCi.ENVIRONMENT_FILES: [
r"^.github/workflows",
r"^dev/breeze",
r"^dev/.*\.py$",
r"^Dockerfile",
r"^scripts/ci/docker-compose",
r"^scripts/ci/kubernetes",
r"^scripts/docker",
r"^scripts/in_container",
r"^generated/provider_dependencies.json$",
],
FileGroupForCi.PYTHON_PRODUCTION_FILES: [
r"^airflow/.*\.py",
r"^pyproject.toml",
r"^hatch_build.py",
],
FileGroupForCi.JAVASCRIPT_PRODUCTION_FILES: [
r"^airflow/.*\.[jt]sx?",
r"^airflow/.*\.lock",
],
FileGroupForCi.API_TEST_FILES: [
r"^airflow/api/",
r"^airflow/api_connexion/",
],
FileGroupForCi.API_CODEGEN_FILES: [
r"^airflow/api_connexion/openapi/v1\.yaml",
r"^clients/gen",
],
FileGroupForCi.HELM_FILES: [
r"^chart",
r"^airflow/kubernetes",
r"^tests/kubernetes",
r"^helm_tests",
],
FileGroupForCi.DEPENDENCY_FILES: [
r"^generated/provider_dependencies.json$",
],
FileGroupForCi.DOC_FILES: [
r"^docs",
r"^\.github/SECURITY\.rst$",
r"^airflow/.*\.py$",
r"^chart",
r"^providers",
r"^tests/system",
r"^CHANGELOG\.txt",
r"^airflow/config_templates/config\.yml",
r"^chart/RELEASE_NOTES\.txt",
r"^chart/values\.schema\.json",
r"^chart/values\.json",
],
FileGroupForCi.WWW_FILES: [
r"^airflow/www/.*\.ts[x]?$",
r"^airflow/www/.*\.js[x]?$",
r"^airflow/www/[^/]+\.json$",
r"^airflow/www/.*\.lock$",
],
FileGroupForCi.KUBERNETES_FILES: [
r"^chart",
r"^kubernetes_tests",
r"^airflow/providers/cncf/kubernetes/",
r"^tests/providers/cncf/kubernetes/",
r"^tests/system/providers/cncf/kubernetes/",
],
FileGroupForCi.ALL_PYTHON_FILES: [
r".*\.py$",
],
FileGroupForCi.ALL_AIRFLOW_PYTHON_FILES: [
r".*\.py$",
],
FileGroupForCi.ALL_PROVIDERS_PYTHON_FILES: [
r"^airflow/providers/.*\.py$",
r"^tests/providers/.*\.py$",
r"^tests/system/providers/.*\.py$",
],
FileGroupForCi.ALL_DOCS_PYTHON_FILES: [
r"^docs/.*\.py$",
],
FileGroupForCi.ALL_DEV_PYTHON_FILES: [
r"^dev/.*\.py$",
],
FileGroupForCi.ALL_SOURCE_FILES: [
r"^.pre-commit-config.yaml$",
r"^airflow",
r"^chart",
r"^tests",
r"^kubernetes_tests",
],
FileGroupForCi.SYSTEM_TEST_FILES: [
r"^tests/system/",
],
FileGroupForCi.ALWAYS_TESTS_FILES: [
r"^tests/always/",
],
FileGroupForCi.ALL_PROVIDER_YAML_FILES: [
r".*/provider\.yaml$",
],
}
)
CI_FILE_GROUP_EXCLUDES = HashableDict(
{
FileGroupForCi.ALL_AIRFLOW_PYTHON_FILES: [
r"^.*/.*_vendor/.*",
r"^airflow/migrations/.*",
r"^airflow/providers/.*",
r"^dev/.*",
r"^docs/.*",
r"^provider_packages/.*",
r"^tests/providers/.*",
r"^tests/system/providers/.*",
r"^tests/dags/test_imports.py",
]
}
)
PYTHON_OPERATOR_FILES = [
r"^airflow/operators/python.py",
r"^tests/operators/test_python.py",
]
TEST_TYPE_MATCHES = HashableDict(
{
SelectiveUnitTestTypes.API: [
r"^airflow/api/",
r"^airflow/api_connexion/",
r"^airflow/api_internal/",
r"^tests/api/",
r"^tests/api_connexion/",
r"^tests/api_internal/",
],
SelectiveUnitTestTypes.CLI: [
r"^airflow/cli/",
r"^tests/cli/",
],
SelectiveUnitTestTypes.OPERATORS: [
r"^airflow/operators/",
r"^tests/operators/",
],
SelectiveUnitTestTypes.PROVIDERS: [
r"^airflow/providers/",
r"^tests/system/providers/",
r"^tests/providers/",
],
SelectiveUnitTestTypes.SERIALIZATION: [
r"^airflow/serialization/",
r"^tests/serialization/",
],
SelectiveUnitTestTypes.PYTHON_VENV: PYTHON_OPERATOR_FILES,
SelectiveUnitTestTypes.BRANCH_PYTHON_VENV: PYTHON_OPERATOR_FILES,
SelectiveUnitTestTypes.EXTERNAL_PYTHON: PYTHON_OPERATOR_FILES,
SelectiveUnitTestTypes.EXTERNAL_BRANCH_PYTHON: PYTHON_OPERATOR_FILES,
SelectiveUnitTestTypes.WWW: [r"^airflow/www", r"^tests/www"],
}
)
TEST_TYPE_EXCLUDES = HashableDict({})
def find_provider_affected(changed_file: str, include_docs: bool) -> str | None:
file_path = AIRFLOW_SOURCES_ROOT / changed_file
# is_relative_to is only available in Python 3.9 - we should simplify this check when we are Python 3.9+
for provider_root in (TESTS_PROVIDERS_ROOT, SYSTEM_TESTS_PROVIDERS_ROOT, AIRFLOW_PROVIDERS_ROOT):
try:
file_path.relative_to(provider_root)
relative_base_path = provider_root
break
except ValueError:
pass
else:
if include_docs:
try:
relative_path = file_path.relative_to(DOCS_DIR)
if relative_path.parts[0].startswith("apache-airflow-providers-"):
return relative_path.parts[0].replace("apache-airflow-providers-", "").replace("-", ".")
except ValueError:
pass
return None
for parent_dir_path in file_path.parents:
if parent_dir_path == relative_base_path:
break
relative_path = parent_dir_path.relative_to(relative_base_path)
if (AIRFLOW_PROVIDERS_ROOT / relative_path / "provider.yaml").exists():
return str(parent_dir_path.relative_to(relative_base_path)).replace(os.sep, ".")
# If we got here it means that some "common" files were modified. so we need to test all Providers
return "Providers"
def _match_files_with_regexps(files: tuple[str, ...], matched_files, matching_regexps):
for file in files:
if any(re.match(regexp, file) for regexp in matching_regexps):
matched_files.append(file)
def _exclude_files_with_regexps(files: tuple[str, ...], matched_files, exclude_regexps):
for file in files:
if any(re.match(regexp, file) for regexp in exclude_regexps):
if file in matched_files:
matched_files.remove(file)
@lru_cache(maxsize=None)
def _matching_files(
files: tuple[str, ...], match_group: FileGroupForCi, match_dict: HashableDict, exclude_dict: HashableDict
) -> list[str]:
matched_files: list[str] = []
match_regexps = match_dict[match_group]
excluded_regexps = exclude_dict.get(match_group)
_match_files_with_regexps(files, matched_files, match_regexps)
if excluded_regexps:
_exclude_files_with_regexps(files, matched_files, excluded_regexps)
count = len(matched_files)
if count > 0:
get_console().print(f"[warning]{match_group} matched {count} files.[/]")
get_console().print(matched_files)
else:
get_console().print(f"[warning]{match_group} did not match any file.[/]")
return matched_files
class SelectiveChecks:
__HASHABLE_FIELDS = {"_files", "_default_branch", "_commit_ref", "_pr_labels", "_github_event"}
def __init__(
self,
files: tuple[str, ...] = (),
default_branch=AIRFLOW_BRANCH,
default_constraints_branch=DEFAULT_AIRFLOW_CONSTRAINTS_BRANCH,
commit_ref: str | None = None,
pr_labels: tuple[str, ...] = (),
github_event: GithubEvents = GithubEvents.PULL_REQUEST,
github_repository: str = APACHE_AIRFLOW_GITHUB_REPOSITORY,
github_actor: str = "",
github_context_dict: dict[str, Any] | None = None,
):
self._files = files
self._default_branch = default_branch
self._default_constraints_branch = default_constraints_branch
self._commit_ref = commit_ref
self._pr_labels = pr_labels
self._github_event = github_event
self._github_repository = github_repository
self._github_actor = github_actor
self._github_context_dict = github_context_dict or {}
self._new_toml: dict[str, Any] = {}
self._old_toml: dict[str, Any] = {}
def __important_attributes(self) -> tuple[Any, ...]:
return tuple(getattr(self, f) for f in self.__HASHABLE_FIELDS)
def __hash__(self):
return hash(self.__important_attributes())
def __eq__(self, other):
return isinstance(other, SelectiveChecks) and all(
[getattr(other, f) == getattr(self, f) for f in self.__HASHABLE_FIELDS]
)
def __str__(self) -> str:
from airflow_breeze.utils.github import get_ga_output
output = []
for field_name in dir(self):
if not field_name.startswith("_"):
value = getattr(self, field_name)
if value is not None:
output.append(get_ga_output(field_name, value))
return "\n".join(output)
default_postgres_version = DEFAULT_POSTGRES_VERSION
default_mysql_version = DEFAULT_MYSQL_VERSION
default_kubernetes_version = DEFAULT_KUBERNETES_VERSION
default_kind_version = KIND_VERSION
default_helm_version = HELM_VERSION
@cached_property
def latest_versions_only(self) -> bool:
return LATEST_VERSIONS_ONLY_LABEL in self._pr_labels
@cached_property
def default_python_version(self) -> str:
return (
CURRENT_PYTHON_MAJOR_MINOR_VERSIONS[-1]
if LATEST_VERSIONS_ONLY_LABEL in self._pr_labels
else DEFAULT_PYTHON_MAJOR_MINOR_VERSION
)
@cached_property
def default_branch(self) -> str:
return self._default_branch
@cached_property
def default_constraints_branch(self) -> str:
return self._default_constraints_branch
def _should_run_all_tests_and_versions(self) -> bool:
if self._github_event in [GithubEvents.PUSH, GithubEvents.SCHEDULE, GithubEvents.WORKFLOW_DISPATCH]:
get_console().print(f"[warning]Running everything because event is {self._github_event}[/]")
return True
if not self._commit_ref:
get_console().print("[warning]Running everything in all versions as commit is missing[/]")
return True
if self.hatch_build_changed:
get_console().print("[warning]Running everything with all versions: hatch_build.py changed[/]")
return True
if self.pyproject_toml_changed and self.build_system_changed_in_pyproject_toml:
get_console().print(
"[warning]Running everything with all versions: build-system changed in pyproject.toml[/]"
)
return True
if self.generated_dependencies_changed:
get_console().print(
"[warning]Running everything with all versions: provider dependencies changed[/]"
)
return True
return False
@cached_property
def all_versions(self) -> bool:
if DEFAULT_VERSIONS_ONLY_LABEL in self._pr_labels:
return False
if LATEST_VERSIONS_ONLY_LABEL in self._pr_labels:
return False
if ALL_VERSIONS_LABEL in self._pr_labels:
return True
if self._should_run_all_tests_and_versions():
return True
return False
@cached_property
def full_tests_needed(self) -> bool:
if self._should_run_all_tests_and_versions():
return True
if self._matching_files(
FileGroupForCi.ENVIRONMENT_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
):
get_console().print("[warning]Running full set of tests because env files changed[/]")
return True
if FULL_TESTS_NEEDED_LABEL in self._pr_labels:
get_console().print(
"[warning]Full tests needed because "
f"label '{FULL_TESTS_NEEDED_LABEL}' is in {self._pr_labels}[/]"
)
return True
return False
@cached_property
def python_versions(self) -> list[str]:
if self.all_versions:
return CURRENT_PYTHON_MAJOR_MINOR_VERSIONS
if self.latest_versions_only:
return [CURRENT_PYTHON_MAJOR_MINOR_VERSIONS[-1]]
return [DEFAULT_PYTHON_MAJOR_MINOR_VERSION]
@cached_property
def python_versions_list_as_string(self) -> str:
return " ".join(self.python_versions)
@cached_property
def all_python_versions(self) -> list[str]:
"""
All python versions include all past python versions available in previous branches
Even if we remove them from the main version. This is needed to make sure we can cherry-pick
changes from main to the previous branch.
"""
if self.all_versions:
return ALL_PYTHON_MAJOR_MINOR_VERSIONS
if self.latest_versions_only:
return [CURRENT_PYTHON_MAJOR_MINOR_VERSIONS[-1]]
return [DEFAULT_PYTHON_MAJOR_MINOR_VERSION]
@cached_property
def all_python_versions_list_as_string(self) -> str:
return " ".join(self.all_python_versions)
@cached_property
def postgres_versions(self) -> list[str]:
if self.all_versions:
return CURRENT_POSTGRES_VERSIONS
if self.latest_versions_only:
return [CURRENT_POSTGRES_VERSIONS[-1]]
return [DEFAULT_POSTGRES_VERSION]
@cached_property
def mysql_versions(self) -> list[str]:
if self.all_versions:
return CURRENT_MYSQL_VERSIONS
if self.latest_versions_only:
return [CURRENT_MYSQL_VERSIONS[-1]]
return [DEFAULT_MYSQL_VERSION]
@cached_property
def kind_version(self) -> str:
return KIND_VERSION
@cached_property
def helm_version(self) -> str:
return HELM_VERSION
@cached_property
def postgres_exclude(self) -> list[dict[str, str]]:
if not self.all_versions:
# Only basic combination so we do not need to exclude anything
return []
return [
# Exclude all combinations that are repeating python/postgres versions
{"python-version": python_version, "backend-version": postgres_version}
for python_version, postgres_version in excluded_combos(
CURRENT_PYTHON_MAJOR_MINOR_VERSIONS, CURRENT_POSTGRES_VERSIONS
)
]
@cached_property
def mysql_exclude(self) -> list[dict[str, str]]:
if not self.all_versions:
# Only basic combination so we do not need to exclude anything
return []
return [
# Exclude all combinations that are repeating python/mysql versions
{"python-version": python_version, "backend-version": mysql_version}
for python_version, mysql_version in excluded_combos(
CURRENT_PYTHON_MAJOR_MINOR_VERSIONS, CURRENT_MYSQL_VERSIONS
)
]
@cached_property
def sqlite_exclude(self) -> list[dict[str, str]]:
return []
@cached_property
def kubernetes_versions(self) -> list[str]:
if self.all_versions:
return CURRENT_KUBERNETES_VERSIONS
if self.latest_versions_only:
return [CURRENT_KUBERNETES_VERSIONS[-1]]
return [DEFAULT_KUBERNETES_VERSION]
@cached_property
def kubernetes_versions_list_as_string(self) -> str:
return " ".join(self.kubernetes_versions)
@cached_property
def kubernetes_combos_list_as_string(self) -> str:
python_version_array: list[str] = self.python_versions_list_as_string.split(" ")
kubernetes_version_array: list[str] = self.kubernetes_versions_list_as_string.split(" ")
combo_titles, short_combo_titles, combos = get_kubernetes_python_combos(
kubernetes_version_array, python_version_array
)
return " ".join(short_combo_titles)
def _matching_files(
self, match_group: FileGroupForCi, match_dict: HashableDict, exclude_dict: HashableDict
) -> list[str]:
return _matching_files(self._files, match_group, match_dict, exclude_dict)
def _should_be_run(self, source_area: FileGroupForCi) -> bool:
if self.full_tests_needed:
get_console().print(f"[warning]{source_area} enabled because we are running everything[/]")
return True
matched_files = self._matching_files(source_area, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES)
if matched_files:
get_console().print(
f"[warning]{source_area} enabled because it matched {len(matched_files)} changed files[/]"
)
return True
else:
get_console().print(
f"[warning]{source_area} disabled because it did not match any changed files[/]"
)
return False
@cached_property
def mypy_folders(self) -> list[str]:
folders_to_check: list[str] = []
if (
self._matching_files(
FileGroupForCi.ALL_AIRFLOW_PYTHON_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
or self.full_tests_needed
):
folders_to_check.append("airflow")
if (
self._matching_files(
FileGroupForCi.ALL_PROVIDERS_PYTHON_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
or self._are_all_providers_affected()
) and self._default_branch == "main":
folders_to_check.append("providers")
if (
self._matching_files(
FileGroupForCi.ALL_DOCS_PYTHON_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
or self.full_tests_needed
):
folders_to_check.append("docs")
if (
self._matching_files(
FileGroupForCi.ALL_DEV_PYTHON_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
or self.full_tests_needed
):
folders_to_check.append("dev")
return folders_to_check
@cached_property
def needs_mypy(self) -> bool:
return self.mypy_folders != []
@cached_property
def needs_python_scans(self) -> bool:
return self._should_be_run(FileGroupForCi.PYTHON_PRODUCTION_FILES)
@cached_property
def needs_javascript_scans(self) -> bool:
return self._should_be_run(FileGroupForCi.JAVASCRIPT_PRODUCTION_FILES)
@cached_property
def needs_api_tests(self) -> bool:
return self._should_be_run(FileGroupForCi.API_TEST_FILES)
@cached_property
def needs_api_codegen(self) -> bool:
return self._should_be_run(FileGroupForCi.API_CODEGEN_FILES)
@cached_property
def run_www_tests(self) -> bool:
return self._should_be_run(FileGroupForCi.WWW_FILES)
@cached_property
def run_amazon_tests(self) -> bool:
if self.parallel_test_types_list_as_string is None:
return False
return (
"amazon" in self.parallel_test_types_list_as_string
or "Providers" in self.parallel_test_types_list_as_string.split(" ")
)
@cached_property
def run_kubernetes_tests(self) -> bool:
return self._should_be_run(FileGroupForCi.KUBERNETES_FILES)
@cached_property
def docs_build(self) -> bool:
return self._should_be_run(FileGroupForCi.DOC_FILES)
@cached_property
def needs_helm_tests(self) -> bool:
return self._should_be_run(FileGroupForCi.HELM_FILES) and self._default_branch == "main"
@cached_property
def run_tests(self) -> bool:
return self._should_be_run(FileGroupForCi.ALL_SOURCE_FILES)
@cached_property
def ci_image_build(self) -> bool:
return self.run_tests or self.docs_build or self.run_kubernetes_tests or self.needs_helm_tests
@cached_property
def prod_image_build(self) -> bool:
return self.run_kubernetes_tests or self.needs_helm_tests
def _select_test_type_if_matching(
self, test_types: set[str], test_type: SelectiveUnitTestTypes
) -> list[str]:
matched_files = self._matching_files(test_type, TEST_TYPE_MATCHES, TEST_TYPE_EXCLUDES)
count = len(matched_files)
if count > 0:
test_types.add(test_type.value)
get_console().print(f"[warning]{test_type} added because it matched {count} files[/]")
return matched_files
def _are_all_providers_affected(self) -> bool:
# if "Providers" test is present in the list of tests, it means that we should run all providers tests
# prepare all providers packages and build all providers documentation
return "Providers" in self._get_test_types_to_run()
def _fail_if_suspended_providers_affected(self) -> bool:
return "allow suspended provider changes" not in self._pr_labels
def _get_test_types_to_run(self, split_to_individual_providers: bool = False) -> list[str]:
if self.full_tests_needed:
return list(all_selective_test_types())
candidate_test_types: set[str] = {"Always"}
matched_files: set[str] = set()
for test_type in SelectiveUnitTestTypes:
if test_type not in [
SelectiveUnitTestTypes.ALWAYS,
SelectiveUnitTestTypes.CORE,
SelectiveUnitTestTypes.OTHER,
SelectiveUnitTestTypes.PLAIN_ASSERTS,
]:
matched_files.update(self._select_test_type_if_matching(candidate_test_types, test_type))
kubernetes_files = self._matching_files(
FileGroupForCi.KUBERNETES_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
system_test_files = self._matching_files(
FileGroupForCi.SYSTEM_TEST_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
all_source_files = self._matching_files(
FileGroupForCi.ALL_SOURCE_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
test_always_files = self._matching_files(
FileGroupForCi.ALWAYS_TESTS_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
remaining_files = (
set(all_source_files)
- set(matched_files)
- set(kubernetes_files)
- set(system_test_files)
- set(test_always_files)
)
get_console().print(f"[warning]Remaining non test/always files: {len(remaining_files)}[/]")
count_remaining_files = len(remaining_files)
for file in self._files:
if file.endswith("bash.py") and Path(file).parent.name == "operators":
candidate_test_types.add("Serialization")
candidate_test_types.add("Core")
break
if count_remaining_files > 0:
get_console().print(
f"[warning]We should run all tests. There are {count_remaining_files} changed "
"files that seems to fall into Core/Other category[/]"
)
get_console().print(remaining_files)
candidate_test_types.update(all_selective_test_types())
else:
if "Providers" in candidate_test_types or "API" in candidate_test_types:
affected_providers = self._find_all_providers_affected(
include_docs=False,
)
if affected_providers != "ALL_PROVIDERS" and affected_providers is not None:
try:
candidate_test_types.remove("Providers")
except KeyError:
# In case of API tests Providers could not be in the list originally so we can ignore
# Providers missing in the list.
pass
if split_to_individual_providers:
for provider in affected_providers:
candidate_test_types.add(f"Providers[{provider}]")
else:
candidate_test_types.add(f"Providers[{','.join(sorted(affected_providers))}]")
elif split_to_individual_providers:
candidate_test_types.remove("Providers")
for provider in get_available_packages():
candidate_test_types.add(f"Providers[{provider}]")
get_console().print(
"[warning]There are no core/other files. Only tests relevant to the changed files are run.[/]"
)
# sort according to predefined order
sorted_candidate_test_types = sorted(candidate_test_types)
get_console().print("[warning]Selected test type candidates to run:[/]")
get_console().print(sorted_candidate_test_types)
return sorted_candidate_test_types
@staticmethod
def _extract_long_provider_tests(current_test_types: set[str]):
"""
In case there are Provider tests in the list of test to run - either in the form of
Providers or Providers[...] we subtract them from the test type,
and add them to the list of tests to run individually.
In case of Providers, we need to replace it with Providers[-<list_of_long_tests>], but
in case of Providers[list_of_tests] we need to remove the long tests from the list.
"""
long_tests = ["amazon", "google"]
for original_test_type in tuple(current_test_types):
if original_test_type == "Providers":
current_test_types.remove(original_test_type)
for long_test in long_tests:
current_test_types.add(f"Providers[{long_test}]")
current_test_types.add(f"Providers[-{','.join(long_tests)}]")
elif original_test_type.startswith("Providers["):
provider_tests_to_run = (
original_test_type.replace("Providers[", "").replace("]", "").split(",")
)
if any(long_test in provider_tests_to_run for long_test in long_tests):
current_test_types.remove(original_test_type)
for long_test in long_tests:
if long_test in provider_tests_to_run:
current_test_types.add(f"Providers[{long_test}]")
provider_tests_to_run.remove(long_test)
current_test_types.add(f"Providers[{','.join(provider_tests_to_run)}]")
@cached_property
def parallel_test_types_list_as_string(self) -> str | None:
if not self.run_tests:
return None
current_test_types = set(self._get_test_types_to_run())
if self._default_branch != "main":
test_types_to_remove: set[str] = set()
for test_type in current_test_types:
if test_type.startswith("Providers"):
get_console().print(
f"[warning]Removing {test_type} because the target branch "
f"is {self._default_branch} and not main[/]"
)
test_types_to_remove.add(test_type)
current_test_types = current_test_types - test_types_to_remove
self._extract_long_provider_tests(current_test_types)
return " ".join(sorted(current_test_types))
@cached_property
def providers_test_types_list_as_string(self) -> str | None:
all_test_types = self.parallel_test_types_list_as_string
if all_test_types is None:
return None
return " ".join(
test_type for test_type in all_test_types.split(" ") if test_type.startswith("Providers")
)
@cached_property
def separate_test_types_list_as_string(self) -> str | None:
if not self.run_tests:
return None
current_test_types = set(self._get_test_types_to_run(split_to_individual_providers=True))
if "Providers" in current_test_types:
current_test_types.remove("Providers")
current_test_types.update({f"Providers[{provider}]" for provider in get_available_packages()})
return " ".join(sorted(current_test_types))
@cached_property
def include_success_outputs(
self,
) -> bool:
return INCLUDE_SUCCESS_OUTPUTS_LABEL in self._pr_labels
@cached_property
def basic_checks_only(self) -> bool:
return not self.ci_image_build
@staticmethod
def _print_diff(old_lines: list[str], new_lines: list[str]):
diff = "\n".join(line for line in difflib.ndiff(old_lines, new_lines) if line and line[0] in "+-?")
get_console().print(diff)
@cached_property
def generated_dependencies_changed(self) -> bool:
return "generated/provider_dependencies.json" in self._files
@cached_property
def hatch_build_changed(self) -> bool:
return "hatch_build.py" in self._files
@cached_property
def pyproject_toml_changed(self) -> bool:
if not self._commit_ref:
get_console().print("[warning]Cannot determine pyproject.toml changes as commit is missing[/]")
return False
new_result = run_command(
["git", "show", f"{self._commit_ref}:pyproject.toml"],
capture_output=True,
text=True,
cwd=AIRFLOW_SOURCES_ROOT,
check=False,
)
if new_result.returncode != 0:
get_console().print(
f"[warning]Cannot determine pyproject.toml changes. "
f"Could not get pyproject.toml from {self._commit_ref}[/]"
)
return False
old_result = run_command(
["git", "show", f"{self._commit_ref}^:pyproject.toml"],
capture_output=True,
text=True,
cwd=AIRFLOW_SOURCES_ROOT,
check=False,
)
if old_result.returncode != 0:
get_console().print(
f"[warning]Cannot determine pyproject.toml changes. "
f"Could not get pyproject.toml from {self._commit_ref}^[/]"
)
return False
try:
import tomllib
except ImportError:
import tomli as tomllib
self._new_toml = tomllib.loads(new_result.stdout)
self._old_toml = tomllib.loads(old_result.stdout)
return True
@cached_property
def build_system_changed_in_pyproject_toml(self) -> bool:
if not self.pyproject_toml_changed:
return False
new_build_backend = self._new_toml["build-system"]["build-backend"]
old_build_backend = self._old_toml["build-system"]["build-backend"]
if new_build_backend != old_build_backend:
get_console().print("[warning]Build backend changed in pyproject.toml [/]")
self._print_diff([old_build_backend], [new_build_backend])
return True
new_requires = self._new_toml["build-system"]["requires"]
old_requires = self._old_toml["build-system"]["requires"]
if new_requires != old_requires:
get_console().print("[warning]Build system changed in pyproject.toml [/]")
self._print_diff(old_requires, new_requires)
return True
return False
@cached_property
def upgrade_to_newer_dependencies(self) -> bool:
if (
len(
self._matching_files(
FileGroupForCi.DEPENDENCY_FILES, CI_FILE_GROUP_MATCHES, CI_FILE_GROUP_EXCLUDES
)
)
> 0
):
get_console().print("[warning]Upgrade to newer dependencies: Dependency files changed[/]")
return True
if self.hatch_build_changed:
get_console().print("[warning]Upgrade to newer dependencies: hatch_build.py changed[/]")
return True
if self.build_system_changed_in_pyproject_toml:
get_console().print(
"[warning]Upgrade to newer dependencies: Build system changed in pyproject.toml[/]"
)
return True
if self._github_event in [GithubEvents.PUSH, GithubEvents.SCHEDULE]:
get_console().print("[warning]Upgrade to newer dependencies: Push or Schedule event[/]")
return True
if UPGRADE_TO_NEWER_DEPENDENCIES_LABEL in self._pr_labels:
get_console().print(
f"[warning]Upgrade to newer dependencies: Label '{UPGRADE_TO_NEWER_DEPENDENCIES_LABEL}' "
f"in {self._pr_labels}[/]"
)
return True
return False
@cached_property
def docs_list_as_string(self) -> str | None:
_ALL_DOCS_LIST = ""
if not self.docs_build:
return None
if self._default_branch != "main":
return "apache-airflow docker-stack"
if self.full_tests_needed:
return _ALL_DOCS_LIST
providers_affected = self._find_all_providers_affected(
include_docs=True,
)
if (
providers_affected == "ALL_PROVIDERS"
or "docs/conf.py" in self._files
or "docs/build_docs.py" in self._files
or self._are_all_providers_affected()
):
return _ALL_DOCS_LIST
packages = []
if any(file.startswith(("airflow/", "docs/apache-airflow/")) for file in self._files):
packages.append("apache-airflow")
if any(file.startswith("docs/apache-airflow-providers/") for file in self._files):
packages.append("apache-airflow-providers")
if any(file.startswith(("chart/", "docs/helm-chart")) for file in self._files):
packages.append("helm-chart")
if any(file.startswith("docs/docker-stack/") for file in self._files):
packages.append("docker-stack")
if providers_affected:
for provider in providers_affected:
packages.append(provider.replace("-", "."))
return " ".join(packages)
@cached_property
def skip_pre_commits(self) -> str:
pre_commits_to_skip = set()
pre_commits_to_skip.add("identity")
# Skip all mypy "individual" file checks if we are running mypy checks in CI
# In the CI we always run mypy for the whole "package" rather than for `--all-files` because