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

Render Documents Enhancements and fail on failed Polarion Requests #95

Merged
merged 7 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
76 changes: 16 additions & 60 deletions capella2polarion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,73 +153,29 @@ def render_documents(
capella_to_polarion_cli.force_update,
)

polarion_worker.load_polarion_work_item_map()

configs = document_config.read_config_file(
document_rendering_config, capella_to_polarion_cli.capella_model
)

polarion_worker.load_polarion_work_item_map()
documents = polarion_worker.load_polarion_documents(
configs.iterate_documents()
)

renderer = document_renderer.DocumentRenderer(
polarion_worker.polarion_data_repo,
capella_to_polarion_cli.capella_model,
overwrite_numbering,
overwrite_layouts,
)

new_documents, updated_documents, work_items = renderer.render_documents(
configs, documents
)
for config in configs.full_authority:
rendering_layouts = document_config.generate_work_item_layouts(
config.work_item_layouts
)
for instance in config.instances:
if old_doc := polarion_worker.get_and_customize_document(
instance.polarion_space,
instance.polarion_name,
instance.polarion_title,
rendering_layouts if overwrite_layouts else None,
config.heading_numbering if overwrite_numbering else None,
):
new_doc, work_items = renderer.render_document(
config.template_directory,
config.template,
document=old_doc,
**instance.params,
)
polarion_worker.update_document(new_doc)
polarion_worker.update_work_items(work_items)
else:
new_doc, _ = renderer.render_document(
config.template_directory,
config.template,
instance.polarion_space,
instance.polarion_name,
instance.polarion_title,
config.heading_numbering,
rendering_layouts,
**instance.params,
)
polarion_worker.post_document(new_doc)

for config in configs.mixed_authority:
rendering_layouts = document_config.generate_work_item_layouts(
config.work_item_layouts
)
for instance in config.instances:
old_doc = polarion_worker.get_and_customize_document(
instance.polarion_space,
instance.polarion_name,
instance.polarion_title,
rendering_layouts if overwrite_layouts else None,
config.heading_numbering if overwrite_numbering else None,
)
assert old_doc is not None, (
"Did not find document "
f"{instance.polarion_space}/{instance.polarion_name}"
)
new_doc, work_items = renderer.update_mixed_authority_document(
old_doc,
config.template_directory,
config.sections,
instance.params,
instance.section_params,
)
polarion_worker.update_document(new_doc)
polarion_worker.update_work_items(work_items)

polarion_worker.post_documents(new_documents)
polarion_worker.update_documents(updated_documents)
polarion_worker.update_work_items(work_items)


if __name__ == "__main__":
Expand Down
49 changes: 23 additions & 26 deletions capella2polarion/connectors/polarion_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def serialize_for_delete(uuid: str) -> str:
)
except polarion_api.PolarionApiException as error:
logger.error("Deleting work items failed. %s", error.args[0])
raise error

def create_missing_work_items(
self, converter_session: data_session.ConverterSession
Expand All @@ -149,6 +150,7 @@ def create_missing_work_items(
self.polarion_data_repo.update_work_items(missing_work_items)
except polarion_api.PolarionApiException as error:
logger.error("Creating work items failed. %s", error.args[0])
raise error

def compare_and_update_work_item(
self, converter_data: data_session.ConverterData
Expand Down Expand Up @@ -205,7 +207,7 @@ def compare_and_update_work_item(
*log_args,
error.args[0],
)
return
raise error

assert new.id is not None
delete_links = None
Expand Down Expand Up @@ -281,6 +283,7 @@ def compare_and_update_work_item(
*log_args,
error.args[0],
)
raise error

def _refactor_attached_images(self, new: data_models.CapellaWorkItem):
def set_attachment_id(node: etree._Element) -> None:
Expand Down Expand Up @@ -422,13 +425,15 @@ def compare_and_update_work_items(
if uuid in self.polarion_data_repo and data.work_item is not None:
self.compare_and_update_work_item(data)

def post_document(self, document: polarion_api.Document):
"""Create a new document."""
self.client.project_client.documents.create(document)
def post_documents(self, documents: list[polarion_api.Document]):
"""Create new documents."""
if documents:
self.client.project_client.documents.create(documents)

def update_document(self, document: polarion_api.Document):
"""Update an existing document."""
self.client.project_client.documents.update(document)
def update_documents(self, documents: list[polarion_api.Document]):
"""Update existing documents."""
if documents:
self.client.project_client.documents.update(documents)

def get_document(
self, space: str, name: str
Expand All @@ -443,24 +448,16 @@ def get_document(
return None
raise e

def get_and_customize_document(
self,
space: str,
name: str,
new_title: str | None,
rendering_layouts: list[polarion_api.RenderingLayout] | None,
heading_numbering: bool | None,
) -> polarion_api.Document | None:
"""Get a document from polarion and return None if not found."""
if document := self.get_document(space, name):
document.title = new_title
if rendering_layouts is not None:
document.rendering_layouts = rendering_layouts
if heading_numbering is not None:
document.outline_numbering = heading_numbering

return document

def update_work_items(self, work_items: list[polarion_api.WorkItem]):
"""Update the given workitems without any additional checks."""
self.client.project_client.work_items.update(work_items)
if work_items:
micha91 marked this conversation as resolved.
Show resolved Hide resolved
self.client.project_client.work_items.update(work_items)

def load_polarion_documents(
self, document_paths: t.Iterable[tuple[str, str]]
) -> dict[tuple[str, str], polarion_api.Document | None]:
"""Load the given document references from Polarion."""
return {
(space, name): self.get_document(space, name)
for space, name in document_paths
}
6 changes: 6 additions & 0 deletions capella2polarion/converters/document_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ class DocumentConfigs(pydantic.BaseModel):
pydantic.Field(default_factory=list)
)

def iterate_documents(self) -> t.Iterator[tuple[str, str]]:
"""Yield all document paths of the config as tuples."""
for conf in self.full_authority + self.mixed_authority:
for inst in conf.instances:
yield inst.polarion_space, inst.polarion_name


def read_config_file(
config: t.TextIO, model: capellambse.MelodyModel | None = None
Expand Down
Loading
Loading