From dc82a4fbb1e1e09d3b7abaa728e6a688a608c7a9 Mon Sep 17 00:00:00 2001 From: Tom Tankilevitch <59158507+Tankilevitch@users.noreply.github.com> Date: Thu, 12 Sep 2024 19:01:07 +0300 Subject: [PATCH] [Core] Fix failure on initialization of actions in case of existence (#1012) --- .pre-commit-config.yaml | 8 +-- CHANGELOG.md | 11 +++ Makefile | 4 ++ port_ocean/clients/port/mixins/blueprints.py | 15 ++-- port_ocean/core/defaults/initialize.py | 76 +++++++++----------- port_ocean/exceptions/port_defaults.py | 2 - pyproject.toml | 2 +- 7 files changed, 60 insertions(+), 58 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bd6afe1c27..e1c02b2b3a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,10 +13,10 @@ repos: - id: detect-aws-credentials - repo: local hooks: - - id: lint - name: Run linter - description: This hooks runs our linters - entry: make lint + - id: fix lint + name: Fix linter + description: This hooks fixes our linters + entry: make lint/fix language: system types: - python diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aded69b41..311fe1f8e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm +## 0.10.10 (2024-09-12) + +### Bug Fixes + +- Fixed failing on initialization of the integration when one of the actions exists in port + +### Improvements + +- Added fix lint command to the makefile as well as the pre-commit hook + + ## 0.10.9 (2024-09-05) ### Bug Fixes diff --git a/Makefile b/Makefile index c2d374fa18..1154a15301 100644 --- a/Makefile +++ b/Makefile @@ -99,6 +99,10 @@ lint: $(ACTIVATE) && \ $(call run_checks,.) +lint/fix: + $(ACTIVATE) && \ + black . + # Development commands build: $(ACTIVATE) && poetry build diff --git a/port_ocean/clients/port/mixins/blueprints.py b/port_ocean/clients/port/mixins/blueprints.py index 2f77b6b04f..c4fe4704b0 100644 --- a/port_ocean/clients/port/mixins/blueprints.py +++ b/port_ocean/clients/port/mixins/blueprints.py @@ -64,7 +64,6 @@ async def delete_blueprint( f"Deleting blueprint with id: {identifier} with all entities: {delete_entities}" ) headers = await self.auth.headers(user_agent_type) - response = None if not delete_entities: response = await self.client.delete( @@ -82,7 +81,9 @@ async def delete_blueprint( handle_status_code(response, should_raise) return response.json().get("migrationId", "") - async def create_action(self, action: dict[str, Any]) -> None: + async def create_action( + self, action: dict[str, Any], should_log: bool = True + ) -> None: logger.info(f"Creating action: {action}") response = await self.client.post( f"{self.auth.api_url}/actions", @@ -90,12 +91,13 @@ async def create_action(self, action: dict[str, Any]) -> None: headers=await self.auth.headers(), ) - handle_status_code(response) + handle_status_code(response, should_log=should_log) async def create_scorecard( self, blueprint_identifier: str, scorecard: dict[str, Any], + should_log: bool = True, ) -> None: logger.info(f"Creating scorecard: {scorecard}") response = await self.client.post( @@ -104,11 +106,10 @@ async def create_scorecard( headers=await self.auth.headers(), ) - handle_status_code(response) + handle_status_code(response, should_log=should_log) async def create_page( - self, - page: dict[str, Any], + self, page: dict[str, Any], should_log: bool = True ) -> dict[str, Any]: logger.info(f"Creating page: {page}") response = await self.client.post( @@ -117,7 +118,7 @@ async def create_page( headers=await self.auth.headers(), ) - handle_status_code(response) + handle_status_code(response, should_log=should_log) return page async def delete_page( diff --git a/port_ocean/core/defaults/initialize.py b/port_ocean/core/defaults/initialize.py index e490bb0231..66aa9efae3 100644 --- a/port_ocean/core/defaults/initialize.py +++ b/port_ocean/core/defaults/initialize.py @@ -120,7 +120,7 @@ async def _create_resources( ) return - created_blueprints, errors = await gather_and_split_errors_from_results( + created_blueprints, blueprint_errors = await gather_and_split_errors_from_results( ( port_client.create_blueprint( blueprint, user_agent_type=UserAgentType.exporter @@ -131,15 +131,17 @@ async def _create_resources( created_blueprints_identifiers = [bp["identifier"] for bp in created_blueprints] - if errors: - for error in errors: + if blueprint_errors: + for error in blueprint_errors: if isinstance(error, httpx.HTTPStatusError): logger.warning( f"Failed to create resources: {error.response.text}. Rolling back changes..." ) - raise AbortDefaultCreationError(created_blueprints_identifiers, errors) - created_pages_identifiers = [] + raise AbortDefaultCreationError( + created_blueprints_identifiers, blueprint_errors + ) + try: for patch_stage in blueprint_patches: await asyncio.gather( @@ -153,44 +155,43 @@ async def _create_resources( ) ) - await asyncio.gather( - *(port_client.create_action(action) for action in defaults.actions) + except httpx.HTTPStatusError as err: + logger.error(f"Failed to create resources: {err.response.text}. continuing...") + raise AbortDefaultCreationError(created_blueprints_identifiers, [err]) + try: + created_actions, actions_errors = await gather_and_split_errors_from_results( + ( + port_client.create_action(action, should_log=False) + for action in defaults.actions + ) ) - await asyncio.gather( - *( - port_client.create_scorecard(blueprint_scorecards["blueprint"], action) - for blueprint_scorecards in defaults.scorecards - for action in blueprint_scorecards["data"] + created_scorecards, scorecards_errors = ( + await gather_and_split_errors_from_results( + ( + port_client.create_scorecard( + blueprint_scorecards["blueprint"], action, should_log=False + ) + for blueprint_scorecards in defaults.scorecards + for action in blueprint_scorecards["data"] + ) ) ) created_pages, pages_errors = await gather_and_split_errors_from_results( - (port_client.create_page(page) for page in defaults.pages) + (port_client.create_page(page, should_log=False) for page in defaults.pages) ) - created_pages_identifiers = [ - page.get("identifier", "") for page in created_pages - ] - if pages_errors: - for error in pages_errors: + errors = actions_errors + scorecards_errors + pages_errors + if errors: + for error in errors: if isinstance(error, httpx.HTTPStatusError): logger.warning( - f"Failed to create resources: {error.response.text}. Rolling back changes..." + f"Failed to create resource: {error.response.text}. continuing..." ) - raise AbortDefaultCreationError( - created_blueprints_identifiers, - pages_errors, - created_pages_identifiers, - ) - except httpx.HTTPStatusError as err: - logger.error( - f"Failed to create resources: {err.response.text}. Rolling back changes..." - ) - raise AbortDefaultCreationError( - created_blueprints_identifiers, [err], created_pages_identifiers - ) + except Exception as err: + logger.error(f"Failed to create resources: {err}. continuing...") async def _initialize_defaults( @@ -227,19 +228,6 @@ async def _initialize_defaults( for identifier in e.blueprints_to_rollback ) ) - if e.pages_to_rollback: - logger.warning( - f"Failed to create resources. Rolling back pages : {e.pages_to_rollback}" - ) - await asyncio.gather( - *( - port_client.delete_page( - identifier, - ) - for identifier in e.pages_to_rollback - ) - ) - raise ExceptionGroup(str(e), e.errors) diff --git a/port_ocean/exceptions/port_defaults.py b/port_ocean/exceptions/port_defaults.py index 177e79d11b..38ebe3c612 100644 --- a/port_ocean/exceptions/port_defaults.py +++ b/port_ocean/exceptions/port_defaults.py @@ -6,10 +6,8 @@ def __init__( self, blueprints_to_rollback: list[str], errors: list[Exception], - pages_to_rollback: list[str] | None = None, ): self.blueprints_to_rollback = blueprints_to_rollback - self.pages_to_rollback = pages_to_rollback self.errors = errors super().__init__("Aborting defaults creation") diff --git a/pyproject.toml b/pyproject.toml index 92feb96bd1..76dd9a04a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "port-ocean" -version = "0.10.9" +version = "0.10.10" description = "Port Ocean is a CLI tool for managing your Port projects." readme = "README.md" homepage = "https://app.getport.io"