-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathutils.py
793 lines (620 loc) · 27.3 KB
/
utils.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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import functools
import json
import logging
import os
import re
from dataclasses import dataclass
from enum import Enum
from glob import glob
from pathlib import Path
from typing import List, Optional, Set, Tuple, Union
import git
import requests
import yaml
from ci_credentials import SecretsManager
from pydash.collections import find
from pydash.objects import get
from rich.console import Console
from simpleeval import simple_eval
console = Console()
DIFFED_BRANCH = os.environ.get("DIFFED_BRANCH", "origin/master")
OSS_CATALOG_URL = "https://connectors.airbyte.com/files/registries/v0/oss_registry.json"
CLOUD_CATALOG_URL = "https://connectors.airbyte.com/files/registries/v0/cloud_registry.json"
BASE_AIRBYTE_DOCS_URL = "https://docs.airbyte.com"
CONNECTOR_PATH_PREFIX = "airbyte-integrations/connectors"
SOURCE_CONNECTOR_PATH_PREFIX = CONNECTOR_PATH_PREFIX + "/source-"
DESTINATION_CONNECTOR_PATH_PREFIX = CONNECTOR_PATH_PREFIX + "/destination-"
THIRD_PARTY_GLOB = "third-party"
THIRD_PARTY_CONNECTOR_PATH_PREFIX = CONNECTOR_PATH_PREFIX + f"/{THIRD_PARTY_GLOB}/"
SCAFFOLD_CONNECTOR_GLOB = "-scaffold-"
ACCEPTANCE_TEST_CONFIG_FILE_NAME = "acceptance-test-config.yml"
METADATA_FILE_NAME = "metadata.yaml"
AIRBYTE_DOCKER_REPO = "airbyte"
AIRBYTE_REPO_DIRECTORY_NAME = "airbyte"
GRADLE_PROJECT_RE_PATTERN = r"project\((['\"])(.+?)\1\)"
TEST_GRADLE_DEPENDENCIES = [
"testImplementation",
"testCompileOnly",
"integrationTestJavaImplementation",
"performanceTestJavaImplementation",
"testFixturesCompileOnly",
"testFixturesImplementation",
]
def download_catalog(catalog_url):
response = requests.get(catalog_url)
response.raise_for_status()
return response.json()
OSS_CATALOG = download_catalog(OSS_CATALOG_URL)
MANIFEST_FILE_NAME = "manifest.yaml"
COMPONENTS_FILE_NAME = "components.py"
DOCKERFILE_FILE_NAME = "Dockerfile"
PYPROJECT_FILE_NAME = "pyproject.toml"
ICON_FILE_NAME = "icon.svg"
POETRY_LOCK_FILE_NAME = "poetry.lock"
STRATEGIC_CONNECTOR_THRESHOLDS = {
"sl": 200,
"ql": 400,
}
ALLOWED_HOST_THRESHOLD = {
"ql": 300,
}
class ConnectorInvalidNameError(Exception):
pass
class ConnectorVersionNotFound(Exception):
pass
def get_connector_name_from_path(path):
return path.split("/")[2]
def get_changed_metadata(diff_regex: Optional[str] = None) -> Set[str]:
"""Retrieve the set of connectors for which the metadata file was changed in the current branch (compared to master).
Args:
diff_regex (str): Find the edited files that contain the following regex in their change.
Returns:
Set[Connector]: Set of connectors that were changed
"""
return get_changed_file(METADATA_FILE_NAME, diff_regex)
def get_changed_file(file_name: str, diff_regex: Optional[str] = None) -> Set[str]:
"""Retrieve the set of connectors for which the given file was changed in the current branch (compared to master).
Args:
diff_regex (str): Find the edited files that contain the following regex in their change.
Returns:
Set[Connector]: Set of connectors that were changed
"""
airbyte_repo = git.Repo(search_parent_directories=True)
if diff_regex is None:
diff_command_args = ("--name-only", DIFFED_BRANCH)
else:
diff_command_args = ("--name-only", f"-G{diff_regex}", DIFFED_BRANCH)
changed_acceptance_test_config_paths = {
file_path
for file_path in airbyte_repo.git.diff(*diff_command_args).split("\n")
if file_path.startswith(SOURCE_CONNECTOR_PATH_PREFIX) and file_path.endswith(file_name)
}
return {Connector(get_connector_name_from_path(changed_file)) for changed_file in changed_acceptance_test_config_paths}
def has_local_cdk_ref(build_file: Path) -> bool:
"""Return true if the build file uses the local CDK.
Args:
build_file (Path): Path to the build.gradle/build.gradle.kts file of the project.
Returns:
bool: True if using local CDK.
"""
contents = "\n".join(
[
# Return contents without inline code comments
line.split("//")[0]
for line in build_file.read_text().split("\n")
]
)
contents = contents.replace(" ", "")
return "useLocalCdk=true" in contents
def get_gradle_dependencies_block(build_file: Path) -> str:
"""Get the dependencies block of a Gradle file.
Args:
build_file (Path): Path to the build.gradle/build.gradle.kts file of the project.
Returns:
str: The dependencies block of the Gradle file.
"""
contents = build_file.read_text().split("\n")
dependency_block = []
in_dependencies_block = False
for line in contents:
if line.strip().startswith("dependencies"):
in_dependencies_block = True
continue
if in_dependencies_block:
if line.startswith("}"):
in_dependencies_block = False
break
else:
dependency_block.append(line)
dependencies_block = "\n".join(dependency_block)
return dependencies_block
def parse_gradle_dependencies(build_file: Path) -> Tuple[List[Path], List[Path]]:
"""Parse the dependencies block of a Gradle file and return the list of project dependencies and test dependencies.
Args:
build_file (Path): _description_
Returns:
Tuple[List[Tuple[str, Path]], List[Tuple[str, Path]]]: _description_
"""
dependencies_block = get_gradle_dependencies_block(build_file)
project_dependencies: List[Path] = []
test_dependencies: List[Path] = []
# Find all matches for test dependencies and regular dependencies
matches = re.findall(
r"(compileOnly|testCompileOnly|testFixturesCompileOnly|testFixturesImplementation|testImplementation|integrationTestJavaImplementation|performanceTestJavaImplementation|implementation|api).*?project\(['\"](.*?)['\"]\)",
dependencies_block,
)
if matches:
# Iterate through each match
for match in matches:
dependency_type, project_path = match
path_parts = project_path.split(":")
path = Path(*path_parts)
if dependency_type in TEST_GRADLE_DEPENDENCIES:
test_dependencies.append(path)
else:
project_dependencies.append(path)
# Dedupe dependencies:
project_dependencies = list(set(project_dependencies))
test_dependencies = list(set(test_dependencies))
return project_dependencies, test_dependencies
def get_all_gradle_dependencies(
build_file: Path, with_test_dependencies: bool = True, found_dependencies: Optional[List[Path]] = None
) -> List[Path]:
"""Recursively retrieve all transitive dependencies of a Gradle project.
Args:
build_file (Path): Path to the build.gradle/build.gradle.kts file of the project.
found_dependencies (List[Path]): List of dependencies that have already been found. Defaults to None.
Returns:
List[Path]: All dependencies of the project.
"""
if found_dependencies is None:
found_dependencies = []
project_dependencies, test_dependencies = parse_gradle_dependencies(build_file)
all_dependencies = project_dependencies + test_dependencies if with_test_dependencies else project_dependencies
valid_build_files = ["build.gradle", "build.gradle.kts"]
for dependency_path in all_dependencies:
for build_file in valid_build_files:
if dependency_path not in found_dependencies and Path(dependency_path / build_file).exists():
found_dependencies.append(dependency_path)
get_all_gradle_dependencies(dependency_path / build_file, with_test_dependencies, found_dependencies)
return found_dependencies
class ConnectorLanguage(str, Enum):
PYTHON = "python"
JAVA = "java"
LOW_CODE = "low-code"
MANIFEST_ONLY = "manifest-only"
class ConnectorLanguageError(Exception):
pass
@dataclass(frozen=True)
class Connector:
"""Utility class to gather metadata about a connector."""
# Path to the connector directory relative to the CONNECTOR_PATH_PREFIX
# e.g source-google-sheets or third-party/farosai/airbyte-pagerduty-source
relative_connector_path: str
def _get_type_and_name_from_technical_name(self) -> Tuple[str, str]:
if "-" not in self.technical_name:
raise ConnectorInvalidNameError(f"Connector type and name could not be inferred from {self.technical_name}")
_type = self.technical_name.split("-")[0]
name = self.technical_name[len(_type) + 1 :]
return _type, name
@property
def technical_name(self) -> str:
"""
Return the technical name of the connector from the given relative_connector_path
e.g. source-google-sheets -> source-google-sheets or third-party/farosai/airbyte-pagerduty-source -> airbyte-pagerduty-source
"""
return self.relative_connector_path.split("/")[-1]
@property
def name(self):
return self._get_type_and_name_from_technical_name()[1]
@property
def connector_type(self) -> str:
return self.metadata["connectorType"] if self.metadata else None
@property
def is_third_party(self) -> bool:
return THIRD_PARTY_GLOB in self.relative_connector_path
@property
def has_airbyte_docs(self) -> bool:
return (
self.metadata
and self.metadata.get("documentationUrl") is not None
and BASE_AIRBYTE_DOCS_URL in str(self.metadata.get("documentationUrl"))
)
@property
def local_connector_documentation_directory(self) -> Path:
return Path(f"./docs/integrations/{self.connector_type}s")
@property
def relative_documentation_path_str(self) -> str:
documentation_url = self.metadata["documentationUrl"]
relative_documentation_path = documentation_url.replace(BASE_AIRBYTE_DOCS_URL, "")
# strip leading and trailing slashes
relative_documentation_path = relative_documentation_path.strip("/")
return f"./docs/{relative_documentation_path}"
@property
def documentation_file_name(self) -> str:
return self.metadata.get("documentationUrl").split("/")[-1] + ".md"
@property
def documentation_file_path(self) -> Optional[Path]:
return Path(f"{self.relative_documentation_path_str}.md") if self.has_airbyte_docs else None
@property
def inapp_documentation_file_path(self) -> Path:
if not self.has_airbyte_docs:
return None
return Path(f"{self.relative_documentation_path_str}.inapp.md")
@property
def migration_guide_file_name(self) -> str:
return f"{self.name}-migrations.md"
@property
def migration_guide_file_path(self) -> Path:
return self.local_connector_documentation_directory / self.migration_guide_file_name
@property
def icon_path(self) -> Path:
file_path = self.code_directory / ICON_FILE_NAME
return file_path
@property
def code_directory(self) -> Path:
return Path(f"./{CONNECTOR_PATH_PREFIX}/{self.relative_connector_path}")
@property
def python_source_dir_path(self) -> Path:
return self.code_directory / self.technical_name.replace("-", "_")
@property
def _manifest_only_path(self) -> Path:
return self.code_directory / MANIFEST_FILE_NAME
@property
def _manifest_low_code_path(self) -> Path:
return self.python_source_dir_path / MANIFEST_FILE_NAME
@property
def manifest_path(self) -> Path:
if self._manifest_only_path.is_file():
return self._manifest_only_path
return self._manifest_low_code_path
@property
def manifest_only_components_path(self) -> Path:
"""Return the path to the components.py file of a manifest-only connector."""
return self.code_directory / COMPONENTS_FILE_NAME
@property
def has_dockerfile(self) -> bool:
return self.dockerfile_file_path.is_file()
@property
def dockerfile_file_path(self) -> Path:
return self.code_directory / DOCKERFILE_FILE_NAME
@property
def pyproject_file_path(self) -> Path:
return self.code_directory / PYPROJECT_FILE_NAME
@property
def metadata_file_path(self) -> Path:
return self.code_directory / METADATA_FILE_NAME
@property
def metadata(self) -> Optional[dict]:
file_path = self.metadata_file_path
if not file_path.is_file():
return None
return yaml.safe_load((self.code_directory / METADATA_FILE_NAME).read_text())["data"]
@property
def connector_spec_file_content(self) -> Optional[dict]:
"""
The spec source of truth is the actual output of the spec command, as connector can mutate their spec.
But this is the best effort approach at statically fetching a spec without running the command on the connector.
Which is "good enough" in some cases.
"""
yaml_spec = Path(self.python_source_dir_path / "spec.yaml")
json_spec = Path(self.python_source_dir_path / "spec.json")
if yaml_spec.exists():
return yaml.safe_load(yaml_spec.read_text())
elif json_spec.exists():
with open(json_spec) as f:
return json.load(f)
elif self.manifest_path.exists():
return yaml.safe_load(self.manifest_path.read_text())["spec"]
return None
@property
def language(self) -> ConnectorLanguage:
if Path(self.code_directory / "manifest.yaml").is_file():
return ConnectorLanguage.MANIFEST_ONLY
if Path(self.code_directory / self.technical_name.replace("-", "_") / "manifest.yaml").is_file():
return ConnectorLanguage.LOW_CODE
if Path(self.code_directory / "setup.py").is_file() or Path(self.code_directory / "pyproject.toml").is_file():
return ConnectorLanguage.PYTHON
if Path(self.code_directory / "src" / "main" / "java").exists() or Path(self.code_directory / "src" / "main" / "kotlin").exists():
return ConnectorLanguage.JAVA
return None
@property
def version(self) -> Optional[str]:
if self.metadata is None:
return self.version_in_dockerfile_label
return self.metadata["dockerImageTag"]
@property
def version_in_dockerfile_label(self) -> Optional[str]:
if not self.has_dockerfile:
return None
with open(self.code_directory / "Dockerfile") as f:
for line in f:
if "io.airbyte.version" in line:
return line.split("=")[1].strip()
raise ConnectorVersionNotFound(
"""
Could not find the connector version from its Dockerfile.
The io.airbyte.version tag is missing.
"""
)
@property
def name_from_metadata(self) -> Optional[str]:
return self.metadata.get("name") if self.metadata else None
@property
def support_level(self) -> Optional[str]:
return self.metadata.get("supportLevel") if self.metadata else None
def metadata_query_match(self, query_string: str) -> bool:
"""Evaluate a query string against the connector metadata.
Based on the simpleeval library:
https://github.com/danthedeckie/simpleeval
Examples
--------
>>> connector.metadata_query_match("'s3' in data.name")
True
>>> connector.metadata_query_match("data.supportLevel == 'certified'")
False
>>> connector.metadata_query_match("data.ab_internal.ql >= 100")
True
Args:
query_string (str): The query string to evaluate.
Returns:
bool: True if the query string matches the connector metadata, False otherwise.
"""
try:
matches = simple_eval(query_string, names={"data": self.metadata})
return bool(matches)
except Exception as e:
# Skip on error as we not all fields are present in all connectors.
logging.debug(f"Failed to evaluate query string {query_string} for connector {self.technical_name}, error: {e}")
return False
@property
def ab_internal_sl(self) -> int:
"""Airbyte Internal Field.
More info can be found here: https://www.notion.so/Internal-Metadata-Fields-32b02037e7b244b7934214019d0b7cc9
Returns:
int: The value
"""
default_value = 100
sl_value = get(self.metadata, "ab_internal.sl")
if sl_value is None:
logging.warning(
f"Connector {self.technical_name} does not have a `ab_internal.sl` defined in metadata.yaml. Defaulting to {default_value}"
)
return default_value
return sl_value
@property
def ab_internal_ql(self) -> int:
"""Airbyte Internal Field.
More info can be found here: https://www.notion.so/Internal-Metadata-Fields-32b02037e7b244b7934214019d0b7cc9
Returns:
int: The value
"""
default_value = 100
ql_value = get(self.metadata, "ab_internal.ql")
if ql_value is None:
logging.warning(
f"Connector {self.technical_name} does not have a `ab_internal.ql` defined in metadata.yaml. Defaulting to {default_value}"
)
return default_value
return ql_value
@property
def is_strategic_connector(self) -> bool:
"""Check if a connector qualifies as a strategic connector.
Returns:
bool: True if the connector is a high value connector, False otherwise.
"""
if self.ab_internal_sl >= STRATEGIC_CONNECTOR_THRESHOLDS["sl"]:
return True
if self.ab_internal_ql >= STRATEGIC_CONNECTOR_THRESHOLDS["ql"]:
return True
return False
@property
def requires_high_test_strictness_level(self) -> bool:
"""Check if a connector requires high strictness CAT tests.
Returns:
bool: True if the connector requires high test strictness level, False otherwise.
"""
return self.ab_internal_ql >= STRATEGIC_CONNECTOR_THRESHOLDS["ql"]
@property
def requires_allowed_hosts_check(self) -> bool:
"""Check if a connector requires allowed hosts.
Returns:
bool: True if the connector requires allowed hosts, False otherwise.
"""
return self.ab_internal_ql >= ALLOWED_HOST_THRESHOLD["ql"]
@property
def allowed_hosts(self) -> Optional[List[str]]:
return self.metadata.get("allowedHosts") if self.metadata else None
@property
def suggested_streams(self) -> Optional[List[str]]:
return self.metadata.get("suggestedStreams") if self.metadata else None
@property
def acceptance_test_config_path(self) -> Path:
return self.code_directory / ACCEPTANCE_TEST_CONFIG_FILE_NAME
@property
def acceptance_test_config(self) -> Optional[dict]:
try:
with open(self.acceptance_test_config_path) as acceptance_test_config_file:
return yaml.safe_load(acceptance_test_config_file)
except FileNotFoundError:
logging.warning(f"No {ACCEPTANCE_TEST_CONFIG_FILE_NAME} file found for {self.technical_name}")
return None
@property
def supports_normalization(self) -> bool:
return self.metadata and self.metadata.get("normalizationConfig") is not None
@property
def normalization_repository(self) -> Optional[str]:
if self.supports_normalization:
return f"{self.metadata['normalizationConfig']['normalizationRepository']}"
@property
def normalization_tag(self) -> Optional[str]:
if self.supports_normalization:
return f"{self.metadata['normalizationConfig']['normalizationTag']}"
@property
def is_using_poetry(self) -> bool:
return Path(self.code_directory / "pyproject.toml").exists()
@property
def registry_primary_key_field(self) -> str:
"""
The primary key field of the connector in the registry.
example:
- source -> sourceDefinitionId
- destination -> destinationDefinitionId
"""
return f"{self.connector_type}DefinitionId"
@property
def is_enabled_in_any_registry(self) -> bool:
"""Check if the connector is enabled in the registry.
Example:
- {registries: null} -> false
- {registries: {oss: {enabled: false }}} -> false
- {registries: {oss: {enabled: true }}} -> true
- {registries: {cloud: {enabled: true }}} -> true
Returns:
bool: True if the connector is enabled, False otherwise.
"""
registries = self.metadata.get("registryOverrides")
if not registries:
return False
for registry in registries.values():
if registry.get("enabled"):
return True
return False
@property
def is_released(self) -> bool:
"""Pull the the OSS registry and check if it the current definition ID and docker image tag are in the registry.
If there is a match it means the connector is released.
We use the OSS registry as the source of truth for released connectors as the cloud registry can be a subset of the OSS registry.
Returns:
bool: True if the connector is released, False otherwise.
"""
metadata = self.metadata
registry = download_catalog(OSS_CATALOG_URL)
for connector in registry[f"{self.connector_type}s"]:
if (
connector[self.registry_primary_key_field] == metadata["definitionId"]
and connector["dockerImageTag"] == metadata["dockerImageTag"]
):
return True
return False
@property
def cloud_usage(self) -> Optional[str]:
"""Pull the cloud registry, check if the connector is in the registry and return the usage metrics.
Returns:
Optional[str]: The usage metrics of the connector, could be one of ["low", "medium", "high"] or None if the connector is not in the registry.
"""
metadata = self.metadata
definition_id = metadata.get("definitionId")
cloud_registry = download_catalog(CLOUD_CATALOG_URL)
all_connectors_of_type = cloud_registry[f"{self.connector_type}s"]
connector_entry = find(all_connectors_of_type, {self.registry_primary_key_field: definition_id})
if not connector_entry:
return None
return get(connector_entry, "generated.metrics.cloud.usage")
@property
def sbom_url(self) -> Optional[str]:
"""
Fetches SBOM URL from the connector definition in the OSS registry, if it exists, None otherwise.
"""
metadata = self.metadata
definition_id = metadata.get("definitionId")
# We use the OSS registry as the source of truth for released connectors as the cloud registry can be a subset of the OSS registry.
oss_registry = download_catalog(OSS_CATALOG_URL)
all_connectors_of_type = oss_registry[f"{self.connector_type}s"]
connector_entry = find(all_connectors_of_type, {self.registry_primary_key_field: definition_id})
if not connector_entry:
return None
return get(connector_entry, "generated.sbomUrl")
@property
def image_address(self) -> str:
return f'{self.metadata["dockerRepository"]}:{self.metadata["dockerImageTag"]}'
@property
def cdk_name(self) -> str | None:
try:
return [tag.split(":")[-1] for tag in self.metadata["tags"] if tag.startswith("cdk:")][0]
except IndexError:
return None
@property
def base_image_address(self) -> str | None:
return self.metadata.get("connectorBuildOptions", {}).get("baseImage")
@property
def uses_base_image(self) -> bool:
return self.base_image_address is not None
@property
def base_image_version(self) -> str | None:
if not self.uses_base_image:
return None
return self.base_image_address.split(":")[1].split("@")[0]
def get_secret_manager(self, gsm_credentials: str):
return SecretsManager(connector_name=self.technical_name, gsm_credentials=gsm_credentials)
def __repr__(self) -> str:
return self.technical_name
@functools.lru_cache(maxsize=2)
def get_local_dependency_paths(self, with_test_dependencies: bool = True) -> Set[Path]:
dependencies_paths = []
build_script = "build.gradle"
if Path(self.code_directory / "build.gradle.kts").exists():
build_script = "build.gradle.kts"
if self.language == ConnectorLanguage.JAVA:
dependencies_paths += [Path("./airbyte-cdk/java/airbyte-cdk"), Path("./airbyte-cdk/bulk")]
dependencies_paths += get_all_gradle_dependencies(
self.code_directory / build_script, with_test_dependencies=with_test_dependencies
)
return sorted(list(set(dependencies_paths)))
def get_changed_connectors(
modified_files: Optional[Set[Union[str, Path]]] = None, source: bool = True, destination: bool = True, third_party: bool = True
) -> Set[Connector]:
"""Retrieve a set of Connectors that were changed in the current branch (compared to master)."""
if modified_files is None:
airbyte_repo = git.Repo(search_parent_directories=True)
modified_files = airbyte_repo.git.diff("--name-only", DIFFED_BRANCH).split("\n")
prefix_to_check = []
if source:
prefix_to_check.append(SOURCE_CONNECTOR_PATH_PREFIX)
if destination:
prefix_to_check.append(DESTINATION_CONNECTOR_PATH_PREFIX)
if third_party:
prefix_to_check.append(THIRD_PARTY_CONNECTOR_PATH_PREFIX)
changed_source_connector_files = {
file_path
for file_path in modified_files
if any(file_path.startswith(prefix) for prefix in prefix_to_check) and SCAFFOLD_CONNECTOR_GLOB not in file_path
}
return {Connector(get_connector_name_from_path(changed_file)) for changed_file in changed_source_connector_files}
def _get_relative_connector_folder_name_from_metadata_path(metadata_file_path: str) -> str:
"""Get the relative connector folder name from the metadata file path.
Args:
metadata_file_path (Path): Path to the metadata file.
Returns:
str: The relative connector folder name.
"""
# remove CONNECTOR_PATH_PREFIX and anything before
metadata_file_path = metadata_file_path.split(CONNECTOR_PATH_PREFIX)[-1]
# remove metadata.yaml
metadata_file_path = metadata_file_path.replace(METADATA_FILE_NAME, "")
# remove leading and trailing slashes
metadata_file_path = metadata_file_path.strip("/")
return metadata_file_path
def get_all_connectors_in_repo() -> Set[Connector]:
"""Retrieve a set of all Connectors in the repo.
We globe the connectors folder for metadata.yaml files and construct Connectors from the directory name.
Returns:
A set of Connectors.
"""
repo = git.Repo(search_parent_directories=True)
repo_path = repo.working_tree_dir
return {
Connector(_get_relative_connector_folder_name_from_metadata_path(metadata_file))
for metadata_file in glob(f"{repo_path}/{CONNECTOR_PATH_PREFIX}/**/metadata.yaml", recursive=True)
if SCAFFOLD_CONNECTOR_GLOB not in metadata_file
}
class ConnectorTypeEnum(str, Enum):
source = "source"
destination = "destination"
class SupportLevelEnum(str, Enum):
certified = "certified"
community = "community"
archived = "archived"