Skip to content

Commit

Permalink
chore: Update code base for polarion-rest-api-client v1.2.0 (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
micha91 authored Oct 21, 2024
1 parent c097061 commit 9e10ae7
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ repos:
- capellambse==0.6.6
- click
- jinja2
- polarion-rest-api-client==1.1.3
- polarion-rest-api-client==1.2.0
- pydantic
- types-requests
- types-PyYAML
Expand Down
19 changes: 8 additions & 11 deletions capella2polarion/connectors/polarion_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def __init__(
delete_status=(
"deleted" if self.polarion_params.delete_work_items else None
),
add_work_item_checksum=True,
)
self._additional_clients: dict[str, polarion_api.ProjectClient] = {}
self.check_client()
Expand All @@ -104,7 +103,6 @@ def _get_client(
delete_status=(
"deleted" if self.polarion_params.delete_work_items else None
),
add_work_item_checksum=True,
)
if not client.exists():
raise KeyError(f"Miss Polarion project with id {project_id}")
Expand Down Expand Up @@ -174,6 +172,7 @@ def create_missing_work_items(
if work_item.uuid_capella in self.polarion_data_repo:
continue

work_item.calculate_checksum()
missing_work_items.append(work_item)
logger.info("Create work item for %r...", work_item.title)
if missing_work_items:
Expand All @@ -196,7 +195,7 @@ def compare_and_update_work_item(
assert old.id is not None

new.calculate_checksum()
if not self.force_update and new == old:
if not self.force_update and new.checksum == old.checksum:
return

log_args = (old.id, new.type, new.title)
Expand All @@ -205,13 +204,12 @@ def compare_and_update_work_item(
)

try:
old_checksums = json.loads(old.get_current_checksum() or "")
old_checksums = json.loads(old.checksum or "")
except json.JSONDecodeError:
old_checksums = {"__C2P__WORK_ITEM": ""}

new_checksum = new.get_current_checksum()
assert new_checksum is not None
new_checksums = json.loads(new_checksum)
assert new.checksum is not None
new_checksums = json.loads(new.checksum)

new_work_item_check_sum = new_checksums.pop("__C2P__WORK_ITEM")
old_work_item_check_sum = old_checksums.pop("__C2P__WORK_ITEM")
Expand Down Expand Up @@ -289,11 +287,10 @@ def compare_and_update_work_item(
new.linked_work_items, old.linked_work_items
)
else:
new.additional_attributes = {}
new.clear_attributes()
new.type = None
new.status = None
new.description = None
new.description_type = None
new.title = None

try:
Expand Down Expand Up @@ -348,8 +345,8 @@ def set_attachment_id(node: etree._Element) -> None:
)

if new.description:
new.description = chelpers.process_html_fragments(
new.description, set_attachment_id
new.description.value = chelpers.process_html_fragments(
new.description.value, set_attachment_id
)
for _, attributes in new.additional_attributes.items():
if (
Expand Down
38 changes: 18 additions & 20 deletions capella2polarion/converters/element_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ def _format(texts: list[str]) -> dict[str, str]:
return requirement_types


def _condition(
html: bool, value: str
) -> data_models.CapellaWorkItem.Condition:
_type = "text/html" if html else "text/plain"
return {
"type": _type,
"value": f'<div style="text-align: center;">{value}</div>',
}


class CapellaWorkItemSerializer(polarion_html_helper.JinjaRendererMixin):
"""The general serializer class for CapellaWorkItems."""

Expand Down Expand Up @@ -432,8 +422,7 @@ def __generic_work_item(
type=converter_data.type_config.p_type,
title=obj.name,
uuid_capella=obj.uuid,
description_type="text/html",
description=value,
description=polarion_api.HtmlContent(value),
status="open",
**requirement_types, # type:ignore[arg-type]
)
Expand Down Expand Up @@ -463,8 +452,7 @@ def _diagram(
type=converter_data.type_config.p_type,
title=diagram.name,
uuid_capella=diagram.uuid,
description_type="text/html",
description=diagram_html,
description=polarion_api.HtmlContent(diagram_html),
status="open",
)
if attachment:
Expand All @@ -491,9 +479,11 @@ def get_condition(cap: m.ModelElement, name: str) -> str:
post_condition = get_condition(obj, "postcondition")

assert converter_data.work_item, "No work item set yet"
converter_data.work_item.preCondition = _condition(True, pre_condition)
converter_data.work_item.postCondition = _condition(
True, post_condition
converter_data.work_item.preCondition = polarion_api.HtmlContent(
pre_condition
)
converter_data.work_item.postCondition = polarion_api.HtmlContent(
post_condition
)
return converter_data.work_item

Expand All @@ -502,9 +492,12 @@ def _linked_text_as_description(
) -> data_models.CapellaWorkItem:
"""Return attributes for a ``Constraint``."""
assert converter_data.work_item, "No work item set yet"
assert (
converter_data.work_item.description
), "Description should already be defined"
(
uuids,
converter_data.work_item.description,
converter_data.work_item.description.value,
attachments,
) = self._sanitize_linked_text(converter_data.capella_element)
if uuids:
Expand Down Expand Up @@ -584,7 +577,12 @@ def _jinja_as_description(
) -> data_models.CapellaWorkItem:
"""Use a Jinja template to render the description content."""
assert converter_data.work_item, "No work item set yet"
converter_data.work_item.description = self._render_jinja_template(
template_folder, template_path, converter_data
assert (
converter_data.work_item.description
), "Description should already be defined"
converter_data.work_item.description.value = (
self._render_jinja_template(
template_folder, template_path, converter_data
)
)
return converter_data.work_item
3 changes: 1 addition & 2 deletions capella2polarion/converters/text_work_item_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def generate_text_work_items(
},
)

work_item.description_type = "text/html"
inner_content = "".join(
[
(
Expand All @@ -78,7 +77,7 @@ def generate_text_work_items(
if element.text:
inner_content = element.text + inner_content

work_item.description = inner_content
work_item.description = polarion_api.HtmlContent(inner_content)
self.new_text_work_items[text_id] = work_item

def insert_text_work_items(
Expand Down
26 changes: 13 additions & 13 deletions capella2polarion/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dataclasses
import hashlib
import json
import typing as t

import polarion_rest_api_client as polarion_api

Expand All @@ -17,23 +16,25 @@
class CapellaWorkItem(polarion_api.WorkItem):
"""A WorkItem class with additional Capella related attributes."""

class Condition(t.TypedDict):
"""A class to describe a pre or post condition."""

type: str
value: str

uuid_capella: str
preCondition: Condition | None
postCondition: Condition | None
checksum: str | None
preCondition: polarion_api.HtmlContent | None
postCondition: polarion_api.HtmlContent | None

def clear_attributes(self):
"""Clear all additional attributes except the checksum."""
# pylint: disable=attribute-defined-outside-init
self.additional_attributes = {"checksum": self.checksum}
# pylint: enable=attribute-defined-outside-init

def calculate_checksum(self) -> str:
"""Calculate and return a checksum for this WorkItem.
In addition, the checksum will be written to self._checksum.
"""
data = self.to_dict()
del data["checksum"]
if "checksum" in data["additional_attributes"]:
del data["additional_attributes"]["checksum"]
del data["id"]

attachments = data.pop("attachments")
Expand All @@ -57,12 +58,11 @@ def calculate_checksum(self) -> str:
data = dict(sorted(data.items()))

converted = json.dumps(data).encode("utf8")
# pylint: disable=attribute-defined-outside-init
self._checksum = json.dumps(
self.checksum = json.dumps(
{"__C2P__WORK_ITEM": hashlib.sha256(converted).hexdigest()}
| dict(sorted(attachment_checksums.items()))
)
return self._checksum
return self.checksum


@dataclasses.dataclass
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
"capellambse_context_diagrams>=0.4.0",
"click",
"PyYAML",
"polarion-rest-api-client==1.1.3",
"polarion-rest-api-client==1.2.0",
"bidict",
"cairosvg",
"jinja2",
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def dummy_work_items() -> dict[str, data_models.CapellaWorkItem]:
uuid_capella=f"uuid{i}",
title=f"Fake {i}",
type="fakeModelObject",
description_type="text/html",
description=markupsafe.Markup(""),
description=polarion_api.HtmlContent(markupsafe.Markup("")),
linked_work_items=[
polarion_api.WorkItemLink(
f"Obj-{i}", f"Obj-{j}", "attribute", True, TEST_PROJECT_ID
Expand Down
Loading

0 comments on commit 9e10ae7

Please sign in to comment.