Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mixed authority mode #132

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 20 additions & 7 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 @@ -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(
micha91 marked this conversation as resolved.
Show resolved Hide resolved
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
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) }}
70 changes: 70 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,73 @@ 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]):
links: list[str] = []
for el in content:
if el.tag == "p":
for e in el.getchildren():
if (
e.tag == "span"
and e.get("class") == "polarion-rte-link"
):
links.append(e)

return links
micha91 marked this conversation as resolved.
Show resolved Hide resolved

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
)
wis = list(
filter(
lambda el: el.tag == "div"
and el.get("id") == "polarion_wiki macro name=module-workitem;"
"params=id=ATSY-1234|layout=0|external=true",
content,
)
)
micha91 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading