Skip to content

Commit

Permalink
merge: Merge pull request #95 from DSD-DBS/fail-on-failed-requests
Browse files Browse the repository at this point in the history
Render Documents Enhancements and fail on failed Polarion Requests
  • Loading branch information
micha91 authored Aug 18, 2024
2 parents 65a312c + 4282892 commit 7406073
Show file tree
Hide file tree
Showing 9 changed files with 414 additions and 126 deletions.
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
44 changes: 19 additions & 25 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,13 @@ 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."""
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."""
self.client.project_client.documents.update(documents)

def get_document(
self, space: str, name: str
Expand All @@ -443,24 +446,15 @@ 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)

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

0 comments on commit 7406073

Please sign in to comment.