Skip to content

Commit

Permalink
merge: Merge pull request #13 from DSD-DBS/fix-system-actor-duplication
Browse files Browse the repository at this point in the history
Fix SystemActor duplication (endless duplication)
  • Loading branch information
ewuerger authored Sep 1, 2023
2 parents 1c0dd17 + b09ce59 commit 506e312
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
42 changes: 29 additions & 13 deletions capella2polarion/elements/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
import polarion_rest_api_client as polarion_api
from capellambse.model import common

from capella2polarion.elements import POL2CAPELLA_TYPES, api_helper, serialize
from capella2polarion.elements import (
POL2CAPELLA_TYPES,
api_helper,
helpers,
serialize,
)

logger = logging.getLogger(__name__)

Expand All @@ -23,22 +28,33 @@

def create_work_items(ctx: dict[str, t.Any]) -> None:
"""Create a set of work items in Polarion."""

def serialize_for_create(
obj: common.GenericElement,
) -> serialize.CapellaWorkItem | None:
logger.debug(
"Create work item for model element %r...", obj._short_repr_()
)
return serialize.element(obj, ctx, serialize.generic_work_item)

objects = chain.from_iterable(ctx["ELEMENTS"].values())
work_items = [
serialize_for_create(obj)
_work_items = [
serialize.element(obj, ctx, serialize.generic_work_item)
for obj in objects
if obj.uuid not in ctx["POLARION_ID_MAP"]
]
work_items = list(filter(None.__ne__, work_items))
_work_items = list(filter(None.__ne__, _work_items))
valid_types = set(map(helpers.resolve_element_type, set(ctx["ELEMENTS"])))
work_items: list[polarion_api.CapellaWorkItem] = []
missing_types: set[str] = set()
for work_item in _work_items:
assert work_item is not None
if work_item.type in valid_types:
work_items.append(work_item)
else:
missing_types.add(work_item.type)
logger.debug(
"%r are missing in the capella2polarion configuration",
", ".join(missing_types),
)

for work_item in work_items:
assert work_item is not None
obj = ctx["MODEL"].by_uuid(work_item.uuid_capella)
logger.debug(
"Create work item for model element %r...", obj._short_repr_()
)
if work_items:
try:
ctx["API"].create_work_items(work_items)
Expand Down
10 changes: 7 additions & 3 deletions capella2polarion/elements/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _generic_work_item(
) -> CapellaWorkItem:
xtype = ctx["POLARION_TYPE_MAP"].get(obj.uuid, type(obj).__name__)
raw_description = getattr(obj, "description", markupsafe.Markup(""))
uuids, value = _sanitize_description(raw_description, ctx)
uuids, value = _sanitize_description(obj, raw_description, ctx)
ctx.setdefault("DESCR_REFERENCES", {})[obj.uuid] = uuids
return CapellaWorkItem(
type=helpers.resolve_element_type(xtype),
Expand All @@ -128,7 +128,7 @@ def _generic_work_item(


def _sanitize_description(
descr: markupsafe.Markup, ctx: dict[str, t.Any]
obj: common.GenericElement, descr: markupsafe.Markup, ctx: dict[str, t.Any]
) -> tuple[list[str], markupsafe.Markup]:
referenced_uuids: list[str] = []
replaced_markup = RE_DESCR_LINK_PATTERN.sub(
Expand All @@ -151,7 +151,11 @@ def repair_images(node: etree._Element) -> None:
b64_img = base64.b64encode(img.read()).decode("utf8")
node.attrib["src"] = f"data:{mime_type};base64,{b64_img}"
except FileNotFoundError:
logger.error("Inline image can't be found from %r", file_path)
logger.error(
"Inline image can't be found from %r for %r",
file_path,
obj._short_repr_(),
)

repaired_markup = chelpers.process_html_fragments(
replaced_markup, repair_images
Expand Down
2 changes: 2 additions & 0 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def context() -> dict[str, t.Any]:
def test_create_work_items(
monkeypatch: pytest.MonkeyPatch, context: dict[str, t.Any]
):
context["MODEL"] = model = mock.MagicMock()
model.by_uuid.side_effect = context["ELEMENTS"]["FakeModelObject"]
monkeypatch.setattr(
serialize,
"generic_work_item",
Expand Down

0 comments on commit 506e312

Please sign in to comment.