Skip to content

Commit

Permalink
add the ability to upgrade CDK for java connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-airbyte committed Jan 18, 2024
1 parent 0063382 commit 6ad2ade
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,8 @@
from pipelines.cli.dagger_pipeline_command import DaggerPipelineCommand


def latest_cdk_version() -> str:
"""
Get the latest version of airbyte-cdk from pypi
"""
cdk_pypi_url = "https://pypi.org/pypi/airbyte-cdk/json"
response = requests.get(cdk_pypi_url)
response.raise_for_status()
package_info = response.json()
return package_info["info"]["version"]


@click.command(cls=DaggerPipelineCommand, short_help="Upgrade CDK version")
@click.argument("target-cdk-version", type=str, default=latest_cdk_version)
@click.argument("target-cdk-version", type=str, default="latest")
@click.pass_context
async def bump_version(
ctx: click.Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
from typing import TYPE_CHECKING

from connector_ops.utils import ConnectorLanguage
from pipelines.airbyte_ci.connectors.context import ConnectorContext
from pipelines.airbyte_ci.connectors.reports import ConnectorReport, Report
from pipelines.helpers import git
Expand All @@ -30,14 +31,18 @@ def __init__(

async def _run(self) -> StepResult:
context = self.context
og_connector_dir = await context.get_connector_dir()
if "setup.py" not in await og_connector_dir.entries():
return self.skip("Connector does not have a setup.py file.")
setup_py = og_connector_dir.file("setup.py")
setup_py_content = await setup_py.contents()

try:
updated_setup_py = self.update_cdk_version(setup_py_content)
updated_connector_dir = og_connector_dir.with_new_file("setup.py", updated_setup_py)
og_connector_dir = await context.get_connector_dir()
if self.context.connector.language == ConnectorLanguage.PYTHON:
updated_connector_dir = await self.upgrade_cdk_version_for_python_connector(og_connector_dir)
elif self.context.connector.language == ConnectorLanguage.JAVA:
updated_connector_dir = await self.upgrade_cdk_version_for_java_connector(og_connector_dir)
else:
return self.skip(
"Can't upgrade the CDK for connector {self.context.connector.technical_name} of written in {self.context.connector.language}"
)

diff = og_connector_dir.diff(updated_connector_dir)
exported_successfully = await diff.export(os.path.join(git.get_git_repo_path(), context.connector.code_directory))
if not exported_successfully:
Expand All @@ -55,17 +60,77 @@ async def _run(self) -> StepResult:
exc_info=e,
)

def update_cdk_version(self, og_setup_py_content: str) -> str:
async def upgrade_cdk_version_for_java_connector(self, og_connector_dir) -> StepResult:
context = self.context

if "build.gradle" not in await og_connector_dir.entries():
return StepResult(
self,
StepStatus.FAILURE,
stdout=f"Java connector {self.context.connector.technical_name} does not have a build.gradle file.",
)

build_gradle = og_connector_dir.file("build.gradle")
build_gradle_content = await build_gradle.contents()

old_cdk_version_required = re.search(r"cdkVersionRequired *= *'(?P<version>[0-9]*\.[0-9]*\.[0-9]*)?'", build_gradle_content)
# If there is no airbyte-cdk dependency, add the version
if old_cdk_version_required is None:
raise ValueError("Could not find airbyte-cdk dependency in build.gradle")

if self.new_version == "latest":
version_file_content = await self.context.get_repo_file(
"airbyte-cdk/java/airbyte-cdk/core/src/main/resources/version.properties"
).contents()
new_version = version_file_content.replace("version=", "")
else:
new_version = self.new_version

updated_build_gradle = build_gradle_content.replace(old_cdk_version_required.group(), f"cdkVersionRequired = '{new_version}'")

use_local_cdk = re.search(r"useLocalCdk *=.*", updated_build_gradle)
if use_local_cdk is not None:
updated_build_gradle = updated_build_gradle.replace(use_local_cdk.group(), "useLocalCdk = false")

return og_connector_dir.with_new_file("build.gradle", updated_build_gradle)

async def upgrade_cdk_version_for_python_connector(self, og_connector_dir) -> StepResult:
context = self.context

og_connector_dir = await context.get_connector_dir()
if "setup.py" not in await og_connector_dir.entries():
return StepResult(
self,
StepStatus.FAILURE,
stdout=f"Python connector {self.context.connector.technical_name} does not have a setup.py file.",
)
setup_py = og_connector_dir.file("setup.py")
setup_py_content = await setup_py.contents()

airbyte_cdk_dependency = re.search(
r"airbyte-cdk(?P<extra>\[[a-zA-Z0-9-]*\])?(?P<version>[<>=!~]+[0-9]*\.[0-9]*\.[0-9]*)?", og_setup_py_content
r"airbyte-cdk(?P<extra>\[[a-zA-Z0-9-]*\])?(?P<version>[<>=!~]+[0-9]*\.[0-9]*\.[0-9]*)?", setup_py_content
)
# If there is no airbyte-cdk dependency, add the version
if airbyte_cdk_dependency is not None:
new_version = f"airbyte-cdk{airbyte_cdk_dependency.group('extra') or ''}>={self.new_version}"
return og_setup_py_content.replace(airbyte_cdk_dependency.group(), new_version)
else:
if airbyte_cdk_dependency is None:
raise ValueError("Could not find airbyte-cdk dependency in setup.py")

if self.new_version == "latest":
"""
Get the latest version of airbyte-cdk from pypi
"""
cdk_pypi_url = "https://pypi.org/pypi/airbyte-cdk/json"
response = requests.get(cdk_pypi_url)
response.raise_for_status()
package_info = response.json()
new_version = package_info["info"]["version"]
else:
new_version = self.new_version

new_version_str = f"airbyte-cdk{airbyte_cdk_dependency.group('extra') or ''}>={new_version}"
updated_setup_py = setup_py_content.replace(airbyte_cdk_dependency.group(), new_version_str)

return og_connector_dir.with_new_file("setup.py", updated_setup_py)


async def run_connector_cdk_upgrade_pipeline(
context: ConnectorContext,
Expand Down

0 comments on commit 6ad2ade

Please sign in to comment.