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
55 changes: 55 additions & 0 deletions apps/python-app/libs/cssc_graph/cssc_graph/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,61 @@ def show(database: Path, ref: str, output_format: str) -> None:
click.echo(f" referrer: {r.get('artifactType') or '-'} ({r['from']}){plat}")


def _format_provenance_text(subgraph: dict, family: str) -> str:
from . import queries

lines = [f"family: {family}", f"{len(subgraph['nodes'])} node(s), {len(subgraph['edges'])} edge(s)"]
labels: dict[str, str] = {}
for n in subgraph["nodes"]:
label = queries._node_label(n)
labels[n["key"]] = label
lines.append(f" [{n.get('nodeType', '?')}] {label} ({n.get('state', '')})")
for e in subgraph["edges"]:
date = str(e.get("date") or "")[:10]
frm = labels.get(e["from"], e["from"])
to = labels.get(e["to"], e["to"])
lines.append(f" {e['type']:<9} {frm} -> {to} {date}".rstrip())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve platform qualifiers in provenance text output

When a referrer targets a platform child, queries.provenance rolls the edge up to the index and records the original platform in e["platform"]; this formatter drops that field. Consequently, the default text output presents a per-platform attestation as though it directly attested the whole index and makes it indistinguishable from an actual index-level attestation. Include the platform qualifier in the rendered edge, as the existing Mermaid and other text renderers do.

Useful? React with 👍 / 👎.

return "\n".join(lines)


@cli.command()
@_database_option
@click.option("--family", required=True, help="Repository family (trailing image name, e.g. python).")
@click.option(
"--format",
"output_format",
type=click.Choice(["text", "json", "mermaid", "cytoscape"]),
default="text",
show_default=True,
)
@click.option("--output", "-o", type=click.Path(path_type=Path), default=None, help="Write to a file.")
def provenance(database: Path, family: str, output_format: str, output: Path | None) -> None:
"""Show a per-family provenance timeline (imported -> promoted -> built, with referrers)."""

from . import queries

store = _open_store(database)
try:
subgraph = queries.provenance(store, family)
finally:
store.close()

if output_format == "json":
text = json.dumps(subgraph, indent=2)
elif output_format == "mermaid":
text = queries.to_mermaid(subgraph)
elif output_format == "cytoscape":
text = json.dumps(queries.to_cytoscape(subgraph), indent=2)
else:
text = _format_provenance_text(subgraph, family)

if output is not None:
output.write_text(text + "\n", encoding="utf-8")
click.echo(f"Wrote {output_format} provenance to {output}.")
else:
click.echo(text)
Comment on lines +414 to +418


@cli.command()
@_database_option
@click.option("--digest", help="Seed by artifact digest.")
Expand Down
52 changes: 52 additions & 0 deletions apps/python-app/libs/cssc_graph/tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,55 @@ def test_non_provenance_subgraph_renders_without_classdefs(store):
assert "classDef" not in m


def _index_db(tmp_path: Path) -> Path:
db = tmp_path / "db"
with GraphStore(db) as gs:
gs.init_schema()
index_data(gs, EXAMPLES, SCHEMA_DIR)
return db


def test_cli_provenance_text(tmp_path: Path):
from click.testing import CliRunner

from cssc_graph.cli import cli

db = _index_db(tmp_path)
result = CliRunner().invoke(cli, ["provenance", "-d", str(db), "--family", "python"])
assert result.exit_code == 0, result.output
assert "family: python" in result.output
assert "[tag-root]" in result.output
assert "imported" in result.output


def test_cli_provenance_mermaid(tmp_path: Path):
from click.testing import CliRunner

from cssc_graph.cli import cli

db = _index_db(tmp_path)
result = CliRunner().invoke(
cli, ["provenance", "-d", str(db), "--family", "python", "--format", "mermaid"]
)
assert result.exit_code == 0, result.output
assert result.output.strip().startswith("flowchart")
assert "classDef" in result.output


def test_cli_provenance_json(tmp_path: Path):
import json

from click.testing import CliRunner

from cssc_graph.cli import cli

db = _index_db(tmp_path)
result = CliRunner().invoke(
cli, ["provenance", "-d", str(db), "--family", "python", "--format", "json"]
)
assert result.exit_code == 0, result.output
data = json.loads(result.output)
assert data["nodes"] and data["edges"]



Loading