Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 4 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ jobs:
printf '{"components": []}\n' > "$ANALYSIS_DIR/analysis.json"
scripts/action/install-sync.sh
cmp "$ANALYSIS_DIR/analysis.json" "$CHECKOUT_DIR/.codeboarding/analysis.json"
- name: Render with the installed CodeBoarding release
run: >-
python scripts/diff_to_mermaid.py
--base .codeboarding/analysis.json
--head .codeboarding/analysis.json
--out "${RUNNER_TEMP}/diagram.md"
--direction LR
--render-depth 1
# Not the committed .codeboarding/analysis.json: after a pin moves, that can still be in the
# format of the release before, which the new one refuses.
- name: Read and diff analyses with the installed CodeBoarding release
run: python -m unittest tests.test_diff_to_mermaid.InstalledCoreTests -v

lint:
runs-on: ubuntu-latest
Expand Down
56 changes: 40 additions & 16 deletions scripts/diff_to_mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
analysed code changed, whatever the component diff says: a component can still read as
changed when the analysis grouped the same code differently.

Self-contained stdlib.
Stdlib only, apart from the installed CodeBoarding release: both analyses are read through its parser
and never as a document, so whatever format that release writes is read. The review analyses both
commits with it, so both sides are always in that format.
"""

from __future__ import annotations
Expand Down Expand Up @@ -63,23 +65,46 @@ def _relation_dict(relation) -> dict:
}


def _project_analysis(data: dict, root_analysis, sub_analyses: dict, id_to_name: dict, projector) -> dict:
"""Attach Core's global-relation projection to every nested JSON level."""
def _diff_view(root_analysis, sub_analyses: dict, id_to_name: dict, projector) -> dict:
"""The dict the diff reads, built from Core's parsed analysis rather than the document: the component
tree with each component's methods by file, Core's projection of the global relations at every level,
and the content hash of every analysed file and method."""
global_relations = list(root_analysis.components_relations)

def project_level(container: dict, analysis) -> None:
def level(analysis) -> dict:
level_ids = {component.component_id for component in analysis.components}
container["components_relations"] = [
_relation_dict(relation) for relation in projector(global_relations, level_ids, id_to_name)
]
for component in container.get("components") or []:
component_id = component.get("component_id", "")
sub_analysis = sub_analyses.get(component_id)
components = []
for component in analysis.components:
view = {
"name": component.name,
"component_id": component.component_id,
"file_methods": [
{"file_path": group.file_path, "methods": [method.qualified_name for method in group.methods]}
for group in component.file_methods
],
}
sub_analysis = sub_analyses.get(component.component_id)
if sub_analysis is not None:
project_level(component, sub_analysis)

project_level(data, root_analysis)
return data
view.update(level(sub_analysis))
components.append(view)
return {
"components": components,
"components_relations": [
_relation_dict(relation) for relation in projector(global_relations, level_ids, id_to_name)
],
}

files = root_analysis.files
return {
**level(root_analysis),
# files[""] only holds pathless methods; it is no file.
"files": {path: {"content_hash": entry.content_hash} for path, entry in files.items() if path},
"methods_index": {
f"{path}|{method.qualified_name}": {"file_path": path, "content_hash": method.content_hash}
for path, entry in files.items()
for method in entry.methods
},
}


def read_analysis_json(path: Path) -> dict:
Expand All @@ -101,8 +126,7 @@ def load_analysis(path: Path) -> dict:
root_analysis, sub_analyses = parse_unified_analysis(data)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Parse each stored analysis format independently

When the engine pin moves to the v1-writing release, a review can still use a legacy committed or stored baseline while producing a v1 head analysis. Both files are passed through the newly installed release's parse_unified_analysis, but that parser rejects the other release's format, so rendering exits before computing the diff. The compatibility test masks this by accepting success on any one fixture rather than requiring legacy/v1 mixtures; the action needs its own dual-format reader or another normalization path that can read both sides independently.

Useful? React with 👍 / 👎.

except (KeyError, TypeError, ValueError) as exc:
sys.exit(f"::error::Could not parse analysis JSON at {path}: {exc}")
return _project_analysis(
data,
return _diff_view(
root_analysis,
sub_analyses,
build_id_to_name_map(root_analysis, sub_analyses),
Expand Down
289 changes: 289 additions & 0 deletions tests/fixtures/analysis/core-0.15.5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
{
"metadata": {
"generated_at": "2026-11-12T00:00:00Z",
"source_tree_hash": "",
"repo_name": "pkg",
"depth_level": 2,
"depth_cap": 2,
"file_coverage_summary": {
"total_files": 3,
"analyzed": 3,
"not_analyzed": 0,
"not_analyzed_by_reason": {}
},
"tree_spec": {}
},
"description": "fixture",
"files": {
"pkg/a.py": {
"method_keys": [
"pkg/a.py|pkg.a.run",
"pkg/a.py|pkg.a.helper"
],
"content_hash": "",
"module_hash": ""
},
"pkg/b.py": {
"method_keys": [
"pkg/b.py|pkg.b.save",
"pkg/b.py|~Weird"
],
"content_hash": "",
"module_hash": ""
},
"pkg/c.py": {
"method_keys": [
"pkg/c.py|pkg.c.load"
],
"content_hash": "",
"module_hash": ""
}
},
"methods_index": {
"pkg/a.py|pkg.a.run": {
"file_path": "pkg/a.py",
"qualified_name": "pkg.a.run",
"start_line": 1,
"end_line": 5,
"type": "FUNCTION",
"content_hash": "h1"
},
"pkg/a.py|pkg.a.helper": {
"file_path": "pkg/a.py",
"qualified_name": "pkg.a.helper",
"start_line": 6,
"end_line": 8,
"type": "FUNCTION",
"content_hash": "h2"
},
"pkg/b.py|pkg.b.save": {
"file_path": "pkg/b.py",
"qualified_name": "pkg.b.save",
"start_line": 1,
"end_line": 4,
"type": "FUNCTION",
"content_hash": "h3"
},
"pkg/b.py|~Weird": {
"file_path": "pkg/b.py",
"qualified_name": "~Weird",
"start_line": 10,
"end_line": 12,
"type": "FUNCTION",
"content_hash": "h4"
},
"pkg/c.py|pkg.c.load": {
"file_path": "pkg/c.py",
"qualified_name": "pkg.c.load",
"start_line": 1,
"end_line": 3,
"type": "FUNCTION",
"content_hash": "h5"
}
},
"components": [
{
"name": "Core",
"description": "d1",
"key_entities": [],
"source_cluster_ids": [],
"file_methods": [
{
"file_path": "pkg/a.py",
"methods": [
"pkg.a.run",
"pkg.a.helper"
]
},
{
"file_path": "pkg/b.py",
"methods": [
"pkg.b.save",
"~Weird"
]
}
],
"component_id": "1",
"can_expand": true,
"components": [
{
"name": "Run",
"description": "",
"key_entities": [],
"source_cluster_ids": [],
"file_methods": [
{
"file_path": "pkg/a.py",
"methods": [
"pkg.a.run",
"pkg.a.helper"
]
}
],
"component_id": "1.1",
"can_expand": false,
"components": [],
"components_relations": []
},
{
"name": "Store",
"description": "",
"key_entities": [],
"source_cluster_ids": [],
"file_methods": [
{
"file_path": "pkg/b.py",
"methods": [
"pkg.b.save",
"~Weird"
]
}
],
"component_id": "1.2",
"can_expand": false,
"components": [],
"components_relations": []
}
],
"components_relations": [
{
"relation": "saves",
"src_name": "Run",
"dst_name": "Store",
"evidence": "",
"key_edges": [],
"src_id": "1.1",
"dst_id": "1.2",
"is_static": false,
"all_edges": [
{
"source": "pkg/a.py|pkg.a.run",
"target": "pkg/b.py|pkg.b.save",
"call_sites": [
{
"line": 3,
"column": 8
}
],
"description": ""
},
{
"source": "pkg/a.py|pkg.a.helper",
"target": "pkg/b.py|pkg.b.save",
"call_sites": [],
"description": ""
}
]
}
]
},
{
"name": "Also",
"description": "",
"key_entities": [],
"source_cluster_ids": [],
"file_methods": [
{
"file_path": "pkg/c.py",
"methods": [
"pkg.c.load"
]
}
],
"component_id": "2",
"can_expand": false,
"components": [],
"components_relations": []
}
],
"components_relations": [
{
"relation": "saves",
"src_name": "Run",
"dst_name": "Store",
"evidence": "",
"key_edges": [],
"src_id": "1.1",
"dst_id": "1.2",
"is_static": false,
"all_edges": [
{
"source": "pkg/a.py|pkg.a.run",
"target": "pkg/b.py|pkg.b.save",
"call_sites": [
{
"line": 3,
"column": 8
}
],
"description": ""
},
{
"source": "pkg/a.py|pkg.a.helper",
"target": "pkg/b.py|pkg.b.save",
"call_sites": [],
"description": ""
}
]
},
{
"relation": "uses",
"src_name": "Run",
"dst_name": "Also",
"evidence": "",
"key_edges": [],
"src_id": "1.1",
"dst_id": "2",
"is_static": false,
"all_edges": [
{
"source": "pkg/a.py|pkg.a.run",
"target": "pkg/c.py|pkg.c.load",
"call_sites": [
{
"line": 4,
"column": 4
}
],
"description": ""
}
]
},
{
"relation": "loads",
"src_name": "Store",
"dst_name": "Also",
"evidence": "",
"key_edges": [],
"src_id": "1.2",
"dst_id": "2",
"is_static": false,
"all_edges": [
{
"source": "pkg/b.py|~Weird",
"target": "pkg/c.py|pkg.c.load",
"call_sites": [],
"description": ""
}
]
},
{
"relation": "reads",
"src_name": "Also",
"dst_name": "Store",
"evidence": "",
"key_edges": [],
"src_id": "2",
"dst_id": "1.2",
"is_static": false,
"all_edges": [
{
"source": "pkg/c.py|pkg.c.load",
"target": "pkg/b.py|pkg.b.save",
"call_sites": [],
"description": ""
}
]
}
]
}
Loading
Loading