fix(import): a paper ending with its scan is podpisany, and the scan reads back

From review: the old engine ended a paper signature as approved, so those
runs came in as zatwierdzony although this module has podpisany for exactly
that; and the imported scan lacked to_dict's kind, so from_dict refused it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 12:41:31 +01:00
co-authored by Claude Opus 5.5
parent 656da736ef
commit a27701ac85
2 changed files with 37 additions and 4 deletions
+16 -4
View File
@@ -25,7 +25,7 @@ import uuid
from typing import Any
from dredd_core import workflow_contract
from dredd_core.document_ref import DocumentRef
from dredd_core.document_ref import DocumentRef, to_dict
from sqlalchemy import select
from sqlalchemy.orm import Session as DbSession
@@ -38,6 +38,15 @@ FORMAT = "galena-workflows/1"
_NS = uuid.UUID("5f0c1a52-9d36-4f4e-9b0a-6a3f2e3d4c11") # fixed: ids must be the same on every run
_HEX40, _HEX64 = re.compile(r"^[0-9a-f]{40}$"), re.compile(r"^[0-9a-f]{64}$")
_FINISHED = {RunStatus.approved.value, RunStatus.rejected.value, RunStatus.cancelled.value}
def state_of(state: str, scan: dict | None, stages: list[dict]) -> str:
"""The old engine had no "signed": an approval that ended with the scan of a paper signature was
`zatwierdzony`. Here that is `podpisany`, so the lists, the archive filter and the CSV say so."""
if (state == RunStatus.approved.value and scan is not None and stages
and stages[-1].get("type") == rules.PAPER):
return RunStatus.signed.value
return state
_KINDS = {"start": EventKind.start, "zatwierdzenie": EventKind.approval,
"odrzucenie": EventKind.rejection, "anulowanie": EventKind.cancellation,
"zakonczenie": EventKind.finished}
@@ -140,8 +149,10 @@ def _run(db: DbSession, r: dict[str, Any], template_ids: set[uuid.UUID], report:
stages = stages_of(r["stages"])
scan = None
if r.get("scan") and r["scan"].get("commit_sha") and r["scan"].get("sha256"):
scan = {"module": "galena", "document_id": s["document_id"], "path": r["scan"]["path"],
"commit_sha": r["scan"]["commit_sha"], "sha256": r["scan"]["sha256"]}
# The shape `runs.act` writes (`to_dict`, its `kind` included), so `from_dict` reads it.
scan = to_dict(DocumentRef(document_id=s["document_id"], path=r["scan"]["path"],
commit_sha=r["scan"]["commit_sha"],
sha256=r["scan"]["sha256"]))
except (AppError, KeyError, TypeError, ValueError) as exc:
report.skipped.append(f"{label}: {exc}")
return
@@ -154,7 +165,8 @@ def _run(db: DbSession, r: dict[str, Any], template_ids: set[uuid.UUID], report:
settings=rules.merge_settings({
"initiator_may_approve": bool(r["initiator_may_approve"]),
"on_reject_by_owner": "end", "on_reject_by_approver": "end"}),
current=None, state=r["state"], started_by=uuid.UUID(r["started_by"]), round=1, scan=scan,
current=None, state=state_of(r["state"], scan, stages),
started_by=uuid.UUID(r["started_by"]), round=1, scan=scan,
created_at=_when(r.get("created_at")), updated_at=_when(r.get("updated_at")))
run.set_ref(subject)
db.add(run)
+21
View File
@@ -142,3 +142,24 @@ def test_nothing_is_emitted_or_notified():
import inspect
source = inspect.getsource(li)
assert "events.emit" not in source and "notify." not in source
def test_a_paper_ending_with_its_scan_is_signed_not_approved():
"""The old engine had no "signed": its approved run ending with a paper scan is `podpisany` here."""
paper = [{"type": "zatwierdzenie"}, {"type": "podpis_papierowy"}]
scan = {"kind": "document"}
assert li.state_of("zatwierdzony", scan, paper) == "podpisany"
assert li.state_of("zatwierdzony", None, paper) == "zatwierdzony"
assert li.state_of("zatwierdzony", scan, [{"type": "zatwierdzenie"}]) == "zatwierdzony"
assert li.state_of("odrzucony", scan, paper) == "odrzucony"
def test_an_imported_scan_reads_back_as_a_document_ref():
from dredd_core.document_ref import DocumentRef, from_dict
db = _Db()
li.import_export(db, _export(scan={"path": "podpisane/umowa.pdf", "commit_sha": "c" * 40,
"sha256": "d" * 64}))
run = next(o for o in db.added if isinstance(o, WorkflowRun))
ref = from_dict(run.scan)
assert isinstance(ref, DocumentRef) and ref.path == "podpisane/umowa.pdf"