Skip to content

Commit

Permalink
merge: Merge pull request #132 from DSD-DBS/fix-mixed-authority-mode
Browse files Browse the repository at this point in the history
Fix mixed authority mode
  • Loading branch information
micha91 authored Nov 21, 2024
2 parents 9c80b3d + f4fbde4 commit 74dea46
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 13 deletions.
29 changes: 21 additions & 8 deletions capella2polarion/converters/document_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ class RenderingSession:
rendering_layouts: list[polarion_api.RenderingLayout] = dataclasses.field(
default_factory=list
)
inserted_work_items: list[polarion_api.WorkItem] = dataclasses.field(
default_factory=list
)
inserted_work_item_ids: list[str] = dataclasses.field(default_factory=list)
text_work_items: dict[str, polarion_api.WorkItem] = dataclasses.field(
default_factory=dict
)
Expand Down Expand Up @@ -109,7 +107,8 @@ def __insert_work_item(
if wi := self.polarion_repository.get_work_item_by_capella_uuid(
obj.uuid
):
if wi in session.inserted_work_items:
assert wi.id
if wi.id in session.inserted_work_item_ids:
logger.info(
"WorkItem %s is already in the document."
"A link will be added instead of inserting it.",
Expand All @@ -126,7 +125,7 @@ def __insert_work_item(
if level is not None:
custom_info = f"level={level}|"

session.inserted_work_items.append(wi)
session.inserted_work_item_ids.append(wi.id)
if self._is_external_document(session):
# pylint: disable-next=line-too-long
return polarion_html_helper.POLARION_WORK_ITEM_DOCUMENT_PROJECT.format(
Expand Down Expand Up @@ -168,7 +167,7 @@ def __heading(self, level: int, text: str, session: RenderingSession):
session.headings.append(polarion_api.WorkItem(id=hid, title=text))
return (
f"<h{level} "
f'id="{polarion_html_helper.wi_id_prefix}{hid}">'
f'id="{polarion_html_helper.WI_ID_PREFIX}{hid}">'
f"</h{level}>"
)
return f"<h{level}>{text}</h{level}>"
Expand Down Expand Up @@ -303,12 +302,12 @@ def update_mixed_authority_document(
html_elements = lxmlhtml.fragments_fromstring(
document.home_page_content.value
)
section_areas = self._extract_section_areas(html_elements)

session = RenderingSession(
rendering_layouts=document.rendering_layouts or [],
document_project_id=document_project_id,
)
section_areas = self._extract_section_areas(html_elements, session)
env = self._get_jinja_env(template_folder)

new_content = []
Expand Down Expand Up @@ -576,11 +575,25 @@ def _render_full_authority_documents(

project_data.new_docs.append(document_data)

def _extract_section_areas(self, html_elements: list[etree._Element]):
def _extract_section_areas(
self, html_elements: list[etree._Element], session: RenderingSession
):
section_areas = {}
current_area_id = None
current_area_start = None
for element_index, element in enumerate(html_elements):
if (
current_area_id is None
and element.tag == "div"
and (
matches := polarion_html_helper.WI_ID_REGEX.match(
element.get("id", "")
)
)
):
session.inserted_work_item_ids.append(matches.group(1))
continue

if (
element.tag != "div"
or element.get("class") != "polarion-dle-wiki-block"
Expand Down
10 changes: 5 additions & 5 deletions capella2polarion/converters/polarion_html_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from capellambse import model as m
from lxml import html

wi_id_prefix = "polarion_wiki macro name=module-workitem;params=id="
h_regex = re.compile("h[0-9]")
wi_id_regex = re.compile(f"{wi_id_prefix}([A-Z|a-z|0-9]*-[0-9]+)")
WI_ID_PREFIX = "polarion_wiki macro name=module-workitem;params=id="
H_REGEX = re.compile("h[0-9]")
WI_ID_REGEX = re.compile(f"{WI_ID_PREFIX}([A-Z|a-z|0-9]*-[0-9]+)")

TEXT_WORK_ITEM_ID_FIELD = "__C2P__id"
TEXT_WORK_ITEM_TYPE = "text"
Expand Down Expand Up @@ -148,7 +148,7 @@ def extract_headings(
html_content: str | list[html.HtmlElement | str],
) -> list[str]:
"""Return a list of work item IDs for all headings in the given content."""
return extract_work_items(html_content, h_regex)
return extract_work_items(html_content, H_REGEX)


def extract_work_items(
Expand All @@ -165,7 +165,7 @@ def extract_work_items(
if (tag_regex is not None and tag_regex.fullmatch(element.tag)) or (
tag_regex is None and element.tag == "div"
):
if matches := wi_id_regex.match(element.get("id")):
if matches := WI_ID_REGEX.match(element.get("id")):
work_item_ids.append(matches.group(1))
return work_item_ids

Expand Down
29 changes: 29 additions & 0 deletions tests/data/documents/mixed_authority_doc_workitem_inserted.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
~ Copyright DB InfraGO AG and contributors
~ SPDX-License-Identifier: Apache-2.0
-->

<h1 id="polarion_wiki macro name=module-workitem;params=id=ATSY-18306"></h1>
<p id="polarion_1">Some intro</p>
<p id="polarion_3">Additional Content -&gt; This is kept</p>
<div id="polarion_editor_html_block2" class="polarion-dle-wiki-block">
<div id="polarion_editor_html_block_content" contenteditable="false"></div>
<pre id="polarion_editor_html_block_source" class="polarion-dle-wiki-block-source" contenteditable="false">&lt;div class="c2pAreaStart" id="section1"&gt;#if ($document.getStatus().id == "draft")&lt;span style="font-weight: bold;background-color: #FFFF00;"&gt;&#8595;&#8595;&#8595;Below this point all content is autogenerated and will be overwritten &#8595;&#8595;&#8595;&lt;/span&gt;#end&lt;/div&gt;</pre>
</div>
<div id="polarion_editor_html_block4" class="polarion-dle-wiki-block">
<div id="polarion_editor_html_block_content" contenteditable="false"></div>
<pre id="polarion_editor_html_block_source" class="polarion-dle-wiki-block-source" contenteditable="false">&lt;div class="c2pAreaEnd" id="section1"&gt;#if ($document.getStatus().id == "draft")&lt;span style="font-weight: bold;background-color: #FFFF00;"&gt;&#8593;&#8593;&#8593;Above this point all content is autogenerated and will be overwritten &#8593;&#8593;&#8593;&lt;/span&gt;#end&lt;/div&gt;</pre>
</div>
<p id="polarion_5">This will be kept.</p>
<div id="polarion_editor_html_block6" class="polarion-dle-wiki-block">
<div id="polarion_editor_html_block_content" contenteditable="false"></div>
<pre id="polarion_editor_html_block_source" class="polarion-dle-wiki-block-source" contenteditable="false">&lt;div class="c2pAreaStart" id="section2"&gt;#if ($document.getStatus().id == "draft")&lt;span style="font-weight: bold;background-color: #FFFF00;"&gt;&#8595;&#8595;&#8595;Below this point all content is autogenerated and will be overwritten &#8595;&#8595;&#8595;&lt;/span&gt;#end&lt;/div&gt;</pre>
</div>
<h2 id="polarion_wiki macro name=module-workitem;params=id=ATSY-18305"></h2>
<p>This will be removed</p>
<div id="polarion_editor_html_block8" class="polarion-dle-wiki-block">
<div id="polarion_editor_html_block_content" contenteditable="false"></div>
<pre id="polarion_editor_html_block_source" class="polarion-dle-wiki-block-source" contenteditable="false">&lt;div class="c2pAreaEnd" id="section2"&gt;#if ($document.getStatus().id == "draft")&lt;span style="font-weight: bold;background-color: #FFFF00;"&gt;&#8593;&#8593;&#8593;Above this point all content is autogenerated and will be overwritten &#8593;&#8593;&#8593;&lt;/span&gt;#end&lt;/div&gt;</pre>
</div>
<p id="polarion_9">Some postfix stuff</p>
<div id="polarion_wiki macro name=module-workitem;params=id=ATSY-1234|layout=0|external=true"></div>
7 changes: 7 additions & 0 deletions tests/data/documents/sections/section_with_work_items.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{#
Copyright DB InfraGO AG and contributors
SPDX-License-Identifier: Apache-2.0
#}

{% set element = model.by_uuid(element) %}
{{ insert_work_item(element, session) }}
68 changes: 68 additions & 0 deletions tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
FULL_AUTHORITY_CONFIG = TEST_DOCUMENT_ROOT / "full_authority_config.yaml"
DOCUMENTS_CONFIG_JINJA = TEST_DOCUMENT_ROOT / "config.yaml.j2"
MIXED_AUTHORITY_DOCUMENT = TEST_DOCUMENT_ROOT / "mixed_authority_doc.html"
MIXED_AUTHORITY_DOCUMENT_WI = (
TEST_DOCUMENT_ROOT / "mixed_authority_doc_workitem_inserted.html"
)
PROJECT_EXTERNAL_WORKITEM_SRC = (
'<div id="polarion_wiki macro name=module-workitem;params=id=ATSY-1234'
f'|layout=0|external=true|project={TEST_PROJECT_ID}"></div>'
Expand Down Expand Up @@ -288,6 +291,71 @@ def test_mixed_authority_document(
assert document_data.headings[0].title == "Keep Heading"


def test_mixed_authority_with_work_item(
empty_polarion_worker: polarion_worker.CapellaPolarionWorker,
model: capellambse.MelodyModel,
):
empty_polarion_worker.polarion_data_repo.update_work_items(
[
dm.CapellaWorkItem(
"ATSY-1234",
uuid_capella="d8655737-39ab-4482-a934-ee847c7ff6bd",
type="componentExchange",
)
]
)

renderer = document_renderer.DocumentRenderer(
empty_polarion_worker.polarion_data_repo, model, TEST_PROJECT_ID
)
old_doc = polarion_api.Document(
module_folder="_default",
module_name="TEST-DOC",
home_page_content=polarion_api.TextContent(
type="text/html",
value=MIXED_AUTHORITY_DOCUMENT_WI.read_text("utf-8"),
),
)

def _find_links(content: list[etree._Element]) -> list[etree._Element]:
links: list[etree._Element] = []
for el in content:
if el.tag != "p":
continue

for e in el:
if e.tag == "span" and e.get("class") == "polarion-rte-link":
links.append(e)

return links

document_data = renderer.update_mixed_authority_document(
old_doc,
DOCUMENT_SECTIONS,
{
"section1": "section_with_work_items.html.j2",
"section2": "section_with_work_items.html.j2",
},
{"element": "d8655737-39ab-4482-a934-ee847c7ff6bd"},
{},
)

content: list[etree._Element] = html.fromstring(
document_data.document.home_page_content.value
)
target_id = (
"polarion_wiki macro name=module-workitem;"
"params=id=ATSY-1234|layout=0|external=true"
)
wis = [
el for el in content if el.tag == "div" and el.get("id") == target_id
]
wi_links = _find_links(content)

assert len(wis) == 1
assert len(wi_links) == 2


def test_create_full_authority_document_text_work_items(
empty_polarion_worker: polarion_worker.CapellaPolarionWorker,
model: capellambse.MelodyModel,
Expand Down

0 comments on commit 74dea46

Please sign in to comment.