Skip to content

Commit

Permalink
improve bump__version
Browse files Browse the repository at this point in the history
better CDK dev workflow
  • Loading branch information
stephane-airbyte committed Mar 21, 2024
1 parent 9f50f6c commit e81ff44
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@


@click.command(cls=DaggerPipelineCommand, short_help="Bump a connector version: update metadata.yaml and changelog.")
@click.argument("bump-type", type=click.Choice(["patch", "minor", "major"]))
@click.argument("pull-request-number", type=str)
@click.argument("changelog-entry", type=str)
@click.argument("bump-type", type=click.Choice(["patch", "minor", "major"]), required=False, default=None)
@click.argument("pull-request-number", type=str, required=False, default=None)
@click.argument("changelog-entry", type=str, required=False, default=None)
@click.option("--from-changelog-entry-files", help="bump version according to changelog_entry files.", default=False, type=bool)
@click.pass_context
async def bump_version(
ctx: click.Context,
bump_type: str,
pull_request_number: str,
changelog_entry: str,
from_changelog_entry_files: bool,
) -> bool:
"""Bump a connector version: update metadata.yaml and changelog."""

if from_changelog_entry_files:
if bump_type is not None or pull_request_number is not None or changelog_entry is not None:
raise click.UsageError(f"--from-changelog-entry-files cannot be used with other bump_version options")
else:
if bump_type is None or pull_request_number is None or changelog_entry is None:
missing_parameters = []
if bump_type is None:
missing_parameters += "{patch|minor|major}"
if pull_request_number is None:
missing_parameters += "PULL_REQUEST_NUMBER"
if changelog_entry is None:
missing_parameters += "CHANGELOG_ENTRY"
raise click.UsageError(f"missing bump_version parameters: {', '.join(missing_parameters)}")

connectors_contexts = [
ConnectorContext(
pipeline_name=f"Upgrade base image versions of connector {connector.technical_name}",
Expand Down Expand Up @@ -58,6 +74,7 @@ async def bump_version(
bump_type,
changelog_entry,
pull_request_number,
from_changelog_entry_files,
)

return True
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,40 @@
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import datetime
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List, Set

import semver
from dagger import Container, Directory
from semver import Version

from pipelines.airbyte_ci.connectors.context import ConnectorContext
from pipelines.airbyte_ci.connectors.reports import ConnectorReport, Report
from pipelines.airbyte_ci.metadata.pipeline import MetadataValidation
from pipelines.helpers import git
from pipelines.helpers.changelog import Changelog
from pipelines.helpers.changelog import Changelog, ChangelogEntry
from pipelines.helpers.connectors import metadata_change_helpers
from pipelines.models.steps import Step, StepResult, StepStatus

import tomli

if TYPE_CHECKING:
from anyio import Semaphore


def get_bumped_version(version: str, bump_type: str) -> str:
current_version = semver.VersionInfo.parse(version)
if bump_type == "patch":
new_version = current_version.bump_patch()
elif bump_type == "minor":
new_version = current_version.bump_minor()
elif bump_type == "major":
new_version = current_version.bump_major()
else:
raise ValueError(f"Unknown bump type: {bump_type}")
return str(new_version)


class AddChangelogEntry(Step):
class AddChangelogEntries(Step):
context: ConnectorContext
title = "Add changelog entry"

def __init__(
self,
context: ConnectorContext,
repo_dir: Container,
new_version: str,
comment: str,
pull_request_number: str,
entries: Set[ChangelogEntry],
export_docs: bool = False,
) -> None:
super().__init__(context)
self.repo_dir = repo_dir
self.new_version = semver.VersionInfo.parse(new_version)
self.comment = comment
self.pull_request_number = int(pull_request_number)
self.entries = entries
self.export_docs = export_docs

async def _run(self) -> StepResult:
Expand All @@ -63,7 +50,8 @@ async def _run(self) -> StepResult:
try:
original_markdown = doc_path.read_text()
changelog = Changelog(original_markdown)
changelog.add_entry(self.new_version, datetime.date.today(), self.pull_request_number, self.comment)
for entry in self.entries:
changelog.add_entry(entry)
updated_doc = changelog.to_markdown()
except Exception as e:
return StepResult(
Expand All @@ -88,7 +76,7 @@ def __init__(
self,
context: ConnectorContext,
repo_dir: Directory,
new_version: str,
new_version: Version,
export_metadata: bool = False,
) -> None:
super().__init__(context)
Expand All @@ -97,8 +85,8 @@ def __init__(
self.export_metadata = export_metadata

@staticmethod
def get_metadata_with_bumped_version(previous_version: str, new_version: str, metadata_str: str) -> str:
return metadata_str.replace("dockerImageTag: " + previous_version, "dockerImageTag: " + new_version)
def get_metadata_with_bumped_version(previous_version: str, new_version: Version, metadata_str: str) -> str:
return metadata_str.replace("dockerImageTag: " + previous_version, "dockerImageTag: " + str(new_version))

async def _run(self) -> StepResult:
metadata_path = self.context.connector.metadata_file_path
Expand Down Expand Up @@ -137,6 +125,7 @@ async def run_connector_version_bump_pipeline(
bump_type: str,
changelog_entry: str,
pull_request_number: str,
from_changelog_entry_files: bool,
) -> Report:
"""Run a pipeline to upgrade for a single connector.
Expand All @@ -150,7 +139,13 @@ async def run_connector_version_bump_pipeline(
steps_results = []
async with context:
og_repo_dir = await context.get_repo_dir()
new_version = get_bumped_version(context.connector.version, bump_type)
changelog_entries: Set[ChangelogEntry]
new_version: Version
if from_changelog_entry_files:
new_version = Changelog.get_bumped_version(context.connector.version, bump_type)
changelog_entries = {ChangelogEntry(version=new_version, pr_number=int(pull_request_number), comment=changelog_entry, date=datetime.date.today())}
else:
changelog_entries, new_version = Changelog.entries_from_files(context.connector.version, {tomli .load(open(f, 'rb')) for f in context.connector.changelog_entry_files})
update_docker_image_tag_in_metadata = BumpDockerImageTagInMetadata(
context,
og_repo_dir,
Expand All @@ -160,12 +155,10 @@ async def run_connector_version_bump_pipeline(
repo_dir_with_updated_metadata = update_docker_image_tag_in_metadata_result.output
steps_results.append(update_docker_image_tag_in_metadata_result)

add_changelog_entry = AddChangelogEntry(
add_changelog_entry = AddChangelogEntries(
context,
repo_dir_with_updated_metadata,
new_version,
changelog_entry,
pull_request_number,
changelog_entries,
)
add_changelog_entry_result = await add_changelog_entry.run()
steps_results.append(add_changelog_entry_result)
Expand Down
26 changes: 23 additions & 3 deletions airbyte-ci/connectors/pipelines/pipelines/helpers/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import re
from dataclasses import dataclass
from operator import attrgetter
from typing import Set, Tuple
from os import path
from typing import Set, Tuple, List

import semver
from semver import Version

from pipelines.helpers.github import AIRBYTE_GITHUB_REPO


Expand Down Expand Up @@ -99,8 +102,8 @@ def __init__(self, markdown: str, github_repo: str = AIRBYTE_GITHUB_REPO) -> Non
self.new_entries: Set[ChangelogEntry] = set()
self.github_repo = github_repo

def add_entry(self, version: semver.Version, date: datetime.date, pull_request_number: int, comment: str) -> None:
self.new_entries.add(ChangelogEntry(date, version, pull_request_number, comment))
def add_entry(self, entry: ChangelogEntry) -> None:
self.new_entries.add(entry)

def to_markdown(self) -> str:
all_entries = set(self.original_entries.union(self.new_entries))
Expand All @@ -119,3 +122,20 @@ def to_markdown(self) -> str:
+ self.original_markdown_lines[(self.changelog_entries_start_line_index + len(self.original_entries)) :]
)
return "\n".join(new_lines) + "\n"

@staticmethod
def entries_from_files(old_version: str, tomls: set[dict]) -> [List[ChangelogEntry], Version]:
return []

@staticmethod
def get_bumped_version(version: str, bump_type: str, ) -> Version:
current_version = semver.VersionInfo.parse(version)
if bump_type == "patch":
new_version = current_version.bump_patch()
elif bump_type == "minor":
new_version = current_version.bump_minor()
elif bump_type == "major":
new_version = current_version.bump_major()
else:
raise ValueError(f"Unknown bump type: {bump_type}")
return new_version

0 comments on commit e81ff44

Please sign in to comment.