Skip to content

Commit

Permalink
merge: Merge pull request #108 from DSD-DBS/fix-project-external-work…
Browse files Browse the repository at this point in the history
…-items

fix: cross project work item references
  • Loading branch information
micha91 authored Sep 5, 2024
2 parents b5a368f + 8a2cdcc commit 9519a51
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 23 deletions.
1 change: 1 addition & 0 deletions capella2polarion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def render_documents(
renderer = document_renderer.DocumentRenderer(
polarion_worker.polarion_data_repo,
capella_to_polarion_cli.capella_model,
capella_to_polarion_cli.polarion_params.project_id,
overwrite_numbering,
overwrite_layouts,
)
Expand Down
40 changes: 32 additions & 8 deletions capella2polarion/converters/document_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class RenderingSession:
text_work_items: dict[str, polarion_api.WorkItem] = dataclasses.field(
default_factory=dict
)
document_project_id: str | None = None


@dataclasses.dataclass
Expand All @@ -61,6 +62,7 @@ def __init__(
self,
polarion_repository: polarion_repo.PolarionDataRepository,
model: capellambse.MelodyModel,
model_work_item_project_id: str,
overwrite_heading_numbering: bool = False,
overwrite_layouts: bool = False,
):
Expand All @@ -71,6 +73,7 @@ def __init__(
self.overwrite_layouts = overwrite_layouts
self.projects: dict[str | None, ProjectData] = {}
self.existing_documents: polarion_repo.DocumentRepository = {}
self.model_work_item_project_id = model_work_item_project_id

def setup_env(self, env: jinja2.Environment):
"""Add globals and filters to the environment."""
Expand All @@ -79,6 +82,12 @@ def setup_env(self, env: jinja2.Environment):
env.globals["work_item_field"] = self.__work_item_field
env.filters["link_work_item"] = self.__link_work_item

def _is_external_document(self, session: RenderingSession) -> bool:
"""Check if the document is in a different project than the model."""
if session.document_project_id is None:
return False
return session.document_project_id != self.model_work_item_project_id

def __insert_work_item(
self, obj: object, session: RenderingSession, level: int | None = None
) -> str:
Expand Down Expand Up @@ -108,10 +117,17 @@ def __insert_work_item(
custom_info = f"level={level}|"

session.inserted_work_items.append(wi)

return polarion_html_helper.POLARION_WORK_ITEM_DOCUMENT.format(
pid=wi.id, lid=layout_index, custom_info=custom_info
)
if self._is_external_document(session):
return polarion_html_helper.POLARION_WORK_ITEM_DOCUMENT_PROJECT.format(
pid=wi.id,
lid=layout_index,
custom_info=custom_info,
project=self.model_work_item_project_id,
)
else:
return polarion_html_helper.POLARION_WORK_ITEM_DOCUMENT.format(
pid=wi.id, lid=layout_index, custom_info=custom_info
)

return polarion_html_helper.RED_TEXT.format(
text=f"Missing WorkItem for UUID {obj.uuid}"
Expand All @@ -124,8 +140,8 @@ def __link_work_item(self, obj: object) -> str:
if wi := self.polarion_repository.get_work_item_by_capella_uuid(
obj.uuid
):
return polarion_html_helper.POLARION_WORK_ITEM_URL.format(
pid=wi.id
return polarion_html_helper.POLARION_WORK_ITEM_URL_PROJECT.format(
pid=wi.id, project=self.model_work_item_project_id
)

return polarion_html_helper.RED_TEXT.format(
Expand Down Expand Up @@ -168,6 +184,7 @@ def render_document(
rendering_layouts: list[polarion_api.RenderingLayout] | None = None,
*,
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
**kwargs: t.Any,
) -> data_models.DocumentData:
"""Render a new Polarion document."""
Expand All @@ -180,6 +197,7 @@ def render_document(
*,
document: polarion_api.Document,
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
**kwargs: t.Any,
) -> data_models.DocumentData:
"""Update an existing Polarion document."""
Expand All @@ -195,6 +213,7 @@ def render_document(
rendering_layouts: list[polarion_api.RenderingLayout] | None = None,
document: polarion_api.Document | None = None,
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
**kwargs: t.Any,
) -> data_models.DocumentData:
"""Render a Polarion document."""
Expand All @@ -213,7 +232,7 @@ def render_document(
env = self._get_jinja_env(template_folder)
template = env.get_template(template_name)

session = RenderingSession()
session = RenderingSession(document_project_id=document_project_id)
if document is not None:
session.rendering_layouts = document.rendering_layouts or []
if document.home_page_content and document.home_page_content.value:
Expand Down Expand Up @@ -255,6 +274,7 @@ def update_mixed_authority_document(
global_parameters: dict[str, t.Any],
section_parameters: dict[str, dict[str, t.Any]],
text_work_item_provider: twi.TextWorkItemProvider | None = None,
document_project_id: str | None = None,
) -> data_models.DocumentData:
"""Update a mixed authority document."""
text_work_item_provider = (
Expand All @@ -269,7 +289,8 @@ def update_mixed_authority_document(
section_areas = self._extract_section_areas(html_elements)

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

Expand Down Expand Up @@ -433,6 +454,7 @@ def _render_mixed_authority_documents(
instance.params,
instance.section_params,
text_work_item_provider,
config.project_id,
)
except Exception as e:
logger.error(
Expand Down Expand Up @@ -481,6 +503,7 @@ def _render_full_authority_documents(
config.template,
document=old_doc,
text_work_item_provider=text_work_item_provider,
document_project_id=config.project_id,
**instance.params,
)
except Exception as e:
Expand All @@ -505,6 +528,7 @@ def _render_full_authority_documents(
config.heading_numbering,
rendering_layouts,
text_work_item_provider=text_work_item_provider,
document_project_id=config.project_id,
**instance.params,
)
except Exception as e:
Expand Down
10 changes: 10 additions & 0 deletions capella2polarion/converters/polarion_html_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@
'id="fake" data-item-id="{pid}" data-option-id="long">'
"</span>"
)
POLARION_WORK_ITEM_URL_PROJECT = (
'<span class="polarion-rte-link" data-type="workItem" '
'id="fake" data-scope="{project}" data-item-id="{pid}" '
'data-option-id="long"></span>'
)
POLARION_WORK_ITEM_DOCUMENT = (
'<div id="polarion_wiki macro name=module-workitem;'
'params=id={pid}|layout={lid}|{custom_info}external=true"></div>'
)
POLARION_WORK_ITEM_DOCUMENT_PROJECT = (
'<div id="polarion_wiki macro name=module-workitem;'
"params=id={pid}|layout={lid}|{custom_info}external=true"
'|project={project}"></div>'
)
RE_DESCR_DELETED_PATTERN = re.compile(
f"&lt;deleted element ({chelpers.RE_VALID_UUID.pattern})&gt;"
)
Expand Down
1 change: 1 addition & 0 deletions jupyter-notebooks/document_templates/test-icd.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ At the moment physical interface definition is not in scope for this document.
The scope of the document covers Interface Requirements and Definition
</p>
{{ heading(2, "Interface Partners", session)}}
{{ insert_work_item(interface, session) }}
<p>The figure below provides an overview of the interface partners:</p>
<p>There may be a diagram with 2 boxes and a blue line</p>
<p>
Expand Down
14 changes: 7 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"diagram_cache": str(TEST_DIAGRAM_CACHE),
}
TEST_HOST = "https://api.example.com"
TEST_PROJECT_ID = "project_id"
DOCUMENT_TEMPLATES = TEST_DOCUMENT_ROOT / "templates"
DOCUMENT_TEXT_WORK_ITEMS = "document_work_items.html.j2"
DOCUMENT_WORK_ITEMS_CROSS_PROJECT = "work_items_cross_project.html.j2"


@pytest.fixture
Expand Down Expand Up @@ -59,7 +63,7 @@ def dummy_work_items() -> dict[str, data_models.CapellaWorkItem]:
description=markupsafe.Markup(""),
linked_work_items=[
polarion_api.WorkItemLink(
f"Obj-{i}", f"Obj-{j}", "attribute", True, "project_id"
f"Obj-{i}", f"Obj-{j}", "attribute", True, TEST_PROJECT_ID
)
for j in range(3)
if (i not in (j, 2))
Expand Down Expand Up @@ -120,7 +124,7 @@ def base_object(
)
c2p_cli = cli.Capella2PolarionCli(
debug=True,
polarion_project_id="project_id",
polarion_project_id=TEST_PROJECT_ID,
polarion_url=TEST_HOST,
polarion_pat="PrivateAccessToken",
polarion_delete_work_items=True,
Expand Down Expand Up @@ -180,13 +184,9 @@ def empty_polarion_worker(monkeypatch: pytest.MonkeyPatch):
mock_project_client = mock.MagicMock(spec=polarion_api.ProjectClient)
monkeypatch.setattr(polarion_api, "ProjectClient", mock_project_client)
polarion_params = polarion_worker.PolarionWorkerParams(
project_id="project_id",
project_id=TEST_PROJECT_ID,
url=TEST_HOST,
pat="PrivateAccessToken",
delete_work_items=True,
)
yield polarion_worker.CapellaPolarionWorker(polarion_params)


DOCUMENT_TEMPLATES = TEST_DOCUMENT_ROOT / "templates"
DOCUMENT_TEXT_WORK_ITEMS = "document_work_items.html.j2"
2 changes: 1 addition & 1 deletion tests/data/documents/combined_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ full_authority:
- polarion_space: _default
polarion_name: id1240
params:
interface: 2681f26a-e492-4e5d-8b33-92fb00a48622
interface: d8655737-39ab-4482-a934-ee847c7ff6bd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{#
Copyright DB InfraGO AG and contributors
SPDX-License-Identifier: Apache-2.0
#}

{% set element = model.by_uuid(element) %}
{{ insert_work_item(element, session) }}
{{ element | link_work_item }}
Loading

0 comments on commit 9519a51

Please sign in to comment.