7
7
import logging
8
8
from pathlib import Path
9
9
import subprocess
10
- from typing import Iterable , Optional
10
+ from typing import Optional
11
11
12
12
import git
13
- from github import Github
14
13
15
14
from .models import ConnectorQAReport
15
+ from .constants import (
16
+ AIRBYTE_CLOUD_GITHUB_REPO_URL ,
17
+ AIRBYTE_CLOUD_MAIN_BRANCH_NAME
18
+ )
16
19
17
- logging .basicConfig (level = "INFO" )
20
+ logger = logging .getLogger (__name__ )
21
+ logger .setLevel (logging .INFO )
18
22
19
- AIRBYTE_CLOUD_REPO_PATH = Path (os .environ ["AIRBYTE_CLOUD_REPO_PATH" ])
20
- GITHUB_ACCESS_TOKEN = os .environ ["GITHUB_ACCESS_TOKEN" ]
21
- AIRBYTE_CLOUD_REPO = git .Repo (AIRBYTE_CLOUD_REPO_PATH )
22
- PATH_TO_DEFINITIONS_MASKS = {
23
- "source" : AIRBYTE_CLOUD_REPO_PATH / "cloud-config/cloud-config-seed/src/main/resources/seed/source_definitions_mask.yaml" ,
24
- "destination" : AIRBYTE_CLOUD_REPO_PATH / "cloud-config/cloud-config-seed/src/main/resources/seed/destination_definitions_mask.yaml"
25
- }
26
23
27
- def checkout_new_branch (connector : ConnectorQAReport ) -> git .Head :
28
- new_branch_name = f"cloud-availability-updater/{ connector .connector_technical_name } -to-cloud"
29
- new_branch = AIRBYTE_CLOUD_REPO .create_head (new_branch_name )
24
+ def clone_airbyte_cloud_repo (local_repo_path : Path ) -> git .Repo :
25
+ logging .info (f"Cloning { AIRBYTE_CLOUD_GITHUB_REPO_URL } to { local_repo_path } " )
26
+ return git .Repo .clone_from (AIRBYTE_CLOUD_GITHUB_REPO_URL , local_repo_path , branch = AIRBYTE_CLOUD_MAIN_BRANCH_NAME )
27
+
28
+ def get_definitions_mask_path (local_repo_path , definition_type : str ) -> Path :
29
+ definitions_mask_path = local_repo_path / f"cloud-config/cloud-config-seed/src/main/resources/seed/{ definition_type } _definitions_mask.yaml"
30
+ if not definitions_mask_path .exists ():
31
+ raise FileNotFoundError (f"Can't find the { definition_type } definitions mask" )
32
+ return definitions_mask_path
33
+
34
+ def checkout_new_branch (airbyte_cloud_repo : git .Repo , new_branch_name : str ) -> git .Head :
35
+ new_branch = airbyte_cloud_repo .create_head (new_branch_name )
30
36
new_branch .checkout ()
31
37
logging .info (f"Checked out branch { new_branch_name } ." )
32
38
return new_branch
33
39
34
- def update_definition_mask (connector : ConnectorQAReport ) -> Optional [Path ]:
35
- definition_mask_path = PATH_TO_DEFINITIONS_MASKS [connector .connector_type ]
36
- with open (definition_mask_path , "r" ) as definition_mask :
40
+ def update_definitions_mask (connector : ConnectorQAReport , definitions_mask_path : Path ) -> Optional [Path ]:
41
+ with open (definitions_mask_path , "r" ) as definition_mask :
37
42
connector_already_in_mask = connector .connector_definition_id in definition_mask .read ()
38
43
if connector_already_in_mask :
39
- logging .warning (f"{ connector .connector_name } 's definition id is already in { definition_mask_path } ." )
44
+ logging .warning (f"{ connector .connector_name } 's definition id is already in { definitions_mask_path } ." )
40
45
return None
41
46
42
47
to_append = f"""# { connector .connector_name } (from cloud availability updater)
43
48
- { connector .connector_type } DefinitionId: { connector .connector_definition_id }
44
49
"""
45
50
46
- with open (definition_mask_path , "a" ) as f :
51
+ with open (definitions_mask_path , "a" ) as f :
47
52
f .write (to_append )
48
- logging .info (f"Updated { definition_mask_path } with { connector .connector_name } 's definition id." )
49
- return definition_mask_path
53
+ logging .info (f"Updated { definitions_mask_path } with { connector .connector_name } 's definition id." )
54
+ return definitions_mask_path
50
55
51
- def run_generate_cloud_connector_catalog () -> str :
56
+ def run_generate_cloud_connector_catalog (airbyte_cloud_repo_path : Path ) -> str :
52
57
result = subprocess .check_output (
53
- f"cd { AIRBYTE_CLOUD_REPO_PATH } && ./gradlew :cloud-config:cloud-config-seed:generateCloudConnectorCatalog" ,
58
+ f"cd { airbyte_cloud_repo_path } && ./gradlew :cloud-config:cloud-config-seed:generateCloudConnectorCatalog" ,
54
59
shell = True
55
60
)
56
61
logging .info ("Ran generateCloudConnectorCatalog Gradle Task" )
57
62
return result .decode ()
58
63
59
- def commit_files ( connector : ConnectorQAReport ):
60
- AIRBYTE_CLOUD_REPO .git .add ('--all' )
61
- AIRBYTE_CLOUD_REPO .git .commit (m = f"🤖 Add { connector . connector_technical_name } to cloud" )
64
+ def commit_all_files ( airbyte_cloud_repo : git . Repo , commit_message : str ):
65
+ airbyte_cloud_repo .git .add ('--all' )
66
+ airbyte_cloud_repo .git .commit (m = commit_message )
62
67
logging .info (f"Committed file changes." )
63
68
64
- def push_branch (branch , dry_run = True ):
65
- if not dry_run :
66
- AIRBYTE_CLOUD_REPO .git .push ("--set-upstream" , "origin" , branch )
67
- logging .info (f"Pushed branch { branch } to origin" )
68
-
69
- def create_pr (connector : ConnectorQAReport , branch : str , dry_run = True ):
70
- g = Github (GITHUB_ACCESS_TOKEN )
71
-
72
- repo = g .get_repo ("airbytehq/airbyte-cloud" )
73
- body = f"""
74
- The Cloud Availability Updater decided that it's the right time to make { connector .connector_name } available on Cloud!
75
- ```
76
- { connector }
77
- ```
69
+ def push_branch (airbyte_cloud_repo : git .Repo , branch :str ):
70
+ airbyte_cloud_repo .git .push ("--set-upstream" , "origin" , branch )
71
+ logging .info (f"Pushed branch { branch } to origin" )
72
+
73
+ def deploy_new_connector_to_cloud_repo (
74
+ airbyte_cloud_repo_path : Path ,
75
+ airbyte_cloud_repo : git .Repo ,
76
+ connector : ConnectorQAReport
77
+ ):
78
+ """Updates the local definitions mask on Airbyte cloud repo.
79
+ Calls the generateCloudConnectorCatalog gradle task.
80
+ Commits these changes on a new branch.
81
+ Pushes these new branch to the origin.
82
+
83
+ Args:
84
+ airbyte_cloud_repo_path (Path): The local path to Airbyte Cloud repository.
85
+ airbyte_cloud_repo (git.Repo): The Airbyte Cloud repo instance.
86
+ connector (ConnectorQAReport): The connector to add to a definitions mask.
78
87
"""
79
- if not dry_run :
80
- return repo .create_pull (title = f"🤖 Add { connector .connector_technical_name } to cloud" , body = body , head = branch , base = "master" )
81
-
82
- def deploy_eligible_connector (connector : ConnectorQAReport , dry_run = True ):
83
- AIRBYTE_CLOUD_REPO .heads .master .checkout ()
84
- new_branch = checkout_new_branch (connector )
85
- update_definition_mask (connector )
86
- run_generate_cloud_connector_catalog ()
87
- commit_files (connector )
88
- push_branch (new_branch , dry_run = dry_run )
89
- create_pr (connector , new_branch , dry_run = dry_run )
90
-
91
- def deploy_eligible_connectors (connectors_eligible_for_cloud : Iterable [ConnectorQAReport ]):
92
- for connector in connectors_eligible_for_cloud :
93
- deploy_eligible_connector (connector )
94
- AIRBYTE_CLOUD_REPO .heads .master .checkout ()
88
+ airbyte_cloud_repo .git .checkout (AIRBYTE_CLOUD_MAIN_BRANCH_NAME )
89
+ new_branch_name = f"cloud-availability-updater/deploy-{ connector .connector_technical_name } "
90
+ checkout_new_branch (airbyte_cloud_repo , new_branch_name )
91
+ definitions_mask_path = get_definitions_mask_path (airbyte_cloud_repo_path , connector .connector_type )
92
+ update_definitions_mask (connector , definitions_mask_path )
93
+ run_generate_cloud_connector_catalog (airbyte_cloud_repo_path )
94
+ commit_all_files (
95
+ airbyte_cloud_repo ,
96
+ f"🤖 Add { connector .connector_name } connector to cloud"
97
+ )
98
+ push_branch (airbyte_cloud_repo , new_branch_name )
99
+ airbyte_cloud_repo .git .checkout (AIRBYTE_CLOUD_MAIN_BRANCH_NAME )
0 commit comments