Skip to content

Commit afd930d

Browse files
committed
drop abilitly to parse werk v3
this branch should not be able to parse v3 werks. with the previous implementation it was possible to create a v3 werk, specify the v2 edition enumeration and still the validation would not complain. the new implementation only allows parsing v3 werks when picking a werk v3 werk. this fix was verified by: 1) picking it on top of a8003a4 and executing `./scripts/run-uvenv python -m cmk.werks.validate` which then shows "cmk.werks.error.WerkError: Markdown formatted werks need to start with header: '[//]: # (werk v2)\n'" this makes sure that the change validation of said commit would have failed. 2) reverting commit `a8003a4ce195c9dd3efe8863b803b1a462d01328` and executing `werk pick 8687b20` (the same commit on the master branch) to make sure picking werks from master still works, and the edition and werk version is adapted correctly. CMK-31445 Change-Id: I60dd37e6040eba45a70b9a2d3a7211709e083f22
1 parent 72c0318 commit afd930d

File tree

3 files changed

+14
-26
lines changed

3 files changed

+14
-26
lines changed

packages/cmk-werks/cmk/werks/__init__.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77
from .convert import werkv1_to_werkv2
88
from .load import load_werk_v2
99
from .models import Werk
10-
from .parse import (
11-
parse_werk_v2,
12-
parse_werk_v3,
13-
WerkV2ParseResult,
14-
WerkV3ParseResult,
15-
WERK_V2_START,
16-
)
10+
from .parse import parse_werk_v2, WerkV2ParseResult
1711

1812

19-
def parse_werk(file_content: str, file_name: str) -> WerkV2ParseResult | WerkV3ParseResult:
13+
def parse_werk(file_content: str, file_name: str) -> WerkV2ParseResult:
2014
if file_name.endswith(".md"):
21-
if file_content.startswith(WERK_V2_START):
22-
return parse_werk_v2(file_content, file_name.removesuffix(".md"))
23-
return parse_werk_v3(file_content, file_name.removesuffix(".md"))
15+
return parse_werk_v2(file_content, file_name.removesuffix(".md"))
2416
file_content, werk_id = werkv1_to_werkv2(file_content, int(file_name))
2517
return parse_werk_v2(file_content, str(werk_id)) # TODO: str does not make sense!
2618

packages/cmk-werks/cmk/werks/cli.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .convert import werkv1_metadata_to_werkv2_metadata
3131
from .format import format_as_werk_v1, format_as_werk_v2
3232
from .models import EditionV3
33-
from .parse import WerkV2ParseResult, WerkV3ParseResult
33+
from .parse import parse_werk_v3, WERK_V3_START, WerkV2ParseResult, WerkV3ParseResult
3434
from .utils import edition_v3_to_v2
3535

3636
T = TypeVar("T", bound="Stash")
@@ -478,24 +478,20 @@ def change_werk_version(werk_path: Path, new_version: str, werk_version: WerkVer
478478
git_add(werk)
479479

480480

481-
def adapt_werk_for_target_branch(werk_path: Path, werk_version: WerkVersion) -> None:
481+
def adapt_werk_for_target_branch(werk_path: Path) -> None:
482482
"""Adapt a V3 werk to V2 format if the target branch uses V2 editions."""
483-
werk = load_werk(werk_path)
484-
485-
if not isinstance(werk.content, WerkV3ParseResult):
486-
return
487483

488-
# Check if target branch uses V2 editions
489-
editions = get_config().editions
490-
if editions and editions[0][0] in {e.value for e in EditionV3}:
484+
if not werk_path.read_text().startswith(WERK_V3_START):
485+
# not sure what werk this is, but it's not a werk v3
491486
return
492487

493-
werk.content.metadata["edition"] = edition_v3_to_v2(
494-
EditionV3(werk.content.metadata["edition"])
488+
parsed_werk = parse_werk_v3(werk_path.read_text(), werk_path.name)
489+
parsed_werk.metadata["edition"] = edition_v3_to_v2(
490+
EditionV3(parsed_werk.metadata["edition"])
495491
).value
496492

497-
save_werk(werk, werk_version)
498-
git_add(werk)
493+
werk_path.write_text(format_as_werk_v2(parsed_werk))
494+
git_add(load_werk(werk_path))
499495
sys.stdout.write(f"Adapted Werk {werk_path} from v3 to v2 format (converted edition names).\n")
500496

501497

@@ -1038,7 +1034,7 @@ def werk_cherry_pick(commit_id: str, no_commit: bool, werk_version: WerkVersion)
10381034

10391035
if found_werk_path is not None:
10401036
# Adapt V3 werks to V2 format if necessary (e.g., when picking from master to 2.4)
1041-
adapt_werk_for_target_branch(found_werk_path.source, werk_version)
1037+
adapt_werk_for_target_branch(found_werk_path.source)
10421038

10431039
# Find werks that have been cherry-picked and change their version
10441040
# to our current checkmk version.

packages/cmk-werks/cmk/werks/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def parse_werk_v3(content: str, werk_id: str) -> WerkV3ParseResult:
103103
raise WerkError(
104104
"Markdown formatted werks need to start with header: '[//]: # (werk v3)\\n'"
105105
)
106-
# V3 has the same format as V2, just different header
106+
# V3 has the same layout as V2, just different header
107107
v2_content = content.replace(WERK_V3_START, WERK_V2_START, 1)
108108
result = parse_werk_v2(v2_content, werk_id)
109109
return WerkV3ParseResult(result.metadata, result.description)

0 commit comments

Comments
 (0)