Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import annotations

import json
import logging
import pathlib
import stat
import zipfile
Expand All @@ -36,8 +37,11 @@
from .GitManager import Git
from .SiemplifyApiClient import SiemplifyApiClient

LOGGER = logging.getLogger(__name__)

INTEGRATIONS_PATH = "Integrations"
PLAYBOOKS_PATH = "Playbooks"
BLOCKS_PATH = "Blocks"
CONNECTORS_PATH = "Connectors"
JOBS_PATH = "Jobs"
MAPPINGS_PATH = "Ontology/Mappings"
Expand Down Expand Up @@ -97,6 +101,8 @@ def get_integration(self, integration_name: str) -> Integration | None:
"""
try:
zip_buffer = BytesIO()
definition = None
is_def_file = False
with zipfile.ZipFile(
zip_buffer,
"a",
Expand All @@ -106,20 +112,49 @@ def get_integration(self, integration_name: str) -> Integration | None:
for file in self.git.get_file_objects_from_path(
f"Integrations/{integration_name}",
):
if file.path == f"Integration-{integration_name}.def":
if file.path in (
f"Integration-{integration_name}.def",
f"Integration-{integration_name}.json",
):
if file.path == f"Integration-{integration_name}.def":
is_def_file = True
definition = json.loads(file.content)
zip_file.writestr(file.path, file.content)
zip_buffer.seek(0)
return Integration(
{
"identifier": integration_name,
"isCustomIntegration": definition["IsCustom"],
},
zip_buffer,
)
except KeyError:
return None

if definition is None:
LOGGER.warning(
f"Definition file for integration {integration_name} not found."
)
return None

is_certified = bool(
definition.get("Certified")
or definition.get("isCertified")
or definition.get("certified")
)
if is_certified:
is_custom = False
else:
is_custom = None
for k in ("isCustomIntegration", "custom", "Custom", "IsCustom"):
if k in definition and definition.get(k) is not None:
is_custom = bool(definition[k])
break
if is_custom is None:
is_custom = not is_def_file

return Integration(
{
"identifier": integration_name,
"isCustomIntegration": is_custom,
"Staging": definition.get("Staging", "False"),
},
zip_buffer,
)

def get_integrations(self) -> list[Integration]:
try:
for integration in self.git.get_raw_object_from_path(
Expand All @@ -143,6 +178,9 @@ def get_playbook(self, playbook_name: str) -> Workflow | None:
for playbook in self.git.get_file_objects_from_path(PLAYBOOKS_PATH):
if pathlib.Path(playbook.path).name == f"{playbook_name}.json":
return Workflow(json.loads(playbook.content))
for block in self.git.get_file_objects_from_path(BLOCKS_PATH):
if pathlib.Path(block.path).name == f"{playbook_name}.json":
return Workflow(json.loads(block.content))
except KeyError:
return None

Expand All @@ -151,6 +189,9 @@ def get_playbooks(self) -> list[Workflow]:
for playbook in self.git.get_file_objects_from_path(PLAYBOOKS_PATH):
if pathlib.Path(playbook.path).suffix == ".json":
yield Workflow(json.loads(playbook.content))
for block in self.git.get_file_objects_from_path(BLOCKS_PATH):
if pathlib.Path(block.path).suffix == ".json":
yield Workflow(json.loads(block.content))
except KeyError:
return []

Expand Down Expand Up @@ -320,6 +361,32 @@ def push_integration(self, integration: Integration) -> None:
integration: An integration object

"""
if not integration.isCustom and not integration.integration_card.get("cards") and self.api:
try:
response = self.api.get_ide_cards(integration.identifier)
if isinstance(response, dict) and "cards" in response:
integration.integration_card["cards"] = response.get("cards", [])
elif isinstance(response, list):
if (
response
and isinstance(response[0], dict)
and "cards" in response[0]
):
matched = next(
(
x
for x in response
if x.get("identifier") == integration.identifier
),
response[0],
)
integration.integration_card["cards"] = matched.get("cards", [])
else:
integration.integration_card["cards"] = response
except Exception as e:
LOGGER.warning(
"Could not fetch ide cards for %s: %s", integration.identifier, e
)
integration.generate_readme(
self.metadata.get_readme_addon("Integration", integration.identifier),
)
Expand All @@ -342,6 +409,22 @@ def push_playbook(self, playbook: Workflow) -> None:
f"{PLAYBOOKS_PATH}/{playbook.category}/{playbook.name}",
)

def push_block(self, block: Workflow, category: str | None = None) -> None:
"""Writes a block to the repo

Args:
block: A block object
category: Optional category to override the block's own category

"""
cat = category or block.category
self._push_obj(
block,
block.name,
"Block",
f"{PLAYBOOKS_PATH}/{cat}/{block.name}",
)

def push_connector(self, connector: Connector) -> None:
"""Writes a connector instance to the repo

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@


PUSH_FAILURE_INDICATORS = (
"pre-receive hook declined",
"not allowed to push",
"push rejected",
"failed to push",
"error: failed to push",
"! [rejected]",
"! [remote rejected]",
)
"pre-receive hook declined",
"not allowed to push",
"push rejected",
"failed to push",
"error: failed to push",
"! [rejected]",
"! [remote rejected]",
)


class Git:
"""GitManager"""
Expand Down Expand Up @@ -805,4 +806,4 @@ def _verify_and_decide(self, client, hostname, key) -> None:
else:
self.vendor.siemplify_logger.error("Fingerprint verification failed.")
raise paramiko.ssh_exception.SSHException(
f"Host key verification failed for {hostname}")
f"Host key verification failed for {hostname}")
Loading
Loading