Skip to content

Commit

Permalink
[Core] Fix failure on initialization of actions in case of existence (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tankilevitch authored Sep 12, 2024
1 parent 52e7b91 commit dc82a4f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 58 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

<!-- towncrier release notes start -->

## 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
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ lint:
$(ACTIVATE) && \
$(call run_checks,.)

lint/fix:
$(ACTIVATE) && \
black .

# Development commands
build:
$(ACTIVATE) && poetry build
Expand Down
15 changes: 8 additions & 7 deletions port_ocean/clients/port/mixins/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -82,20 +81,23 @@ 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",
json=action,
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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down
76 changes: 32 additions & 44 deletions port_ocean/core/defaults/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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)


Expand Down
2 changes: 0 additions & 2 deletions port_ocean/exceptions/port_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit dc82a4f

Please sign in to comment.