-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract.js
More file actions
164 lines (151 loc) · 6.15 KB
/
Copy pathcontract.js
File metadata and controls
164 lines (151 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Live coverage contract. Reads the populace-us input-coverage manifest for the
// latest release and renders the required / reviewed-exclusion counts, the gate,
// and the full exclusion list with reasons. Like releases.js, this renders real
// registry data or nothing: the live region stays hidden unless the manifest
// loads, so the page cannot go stale.
//
// Fetch strategy, in order:
// 1) raw.githubusercontent.com at the build sha embedded in the release id
// (e.g. ...-75d5add-<timestamp>), where the manifest is a source file at
// packages/populace-build/src/populace/build/us/release_input_coverage_manifest.json.
// It is present at every build's commit, so this renders today and the counts
// update automatically as each new release pins a newer sha — no code change.
// 2) fall back to an in-release copy, if a future build publishes
// release_input_coverage_manifest.json next to the other manifests.
// (1) is primary because it always exists at the build sha; the release-directory
// copy is only a safety net, so no request 404s on a normal load.
(async () => {
const HF =
"https://huggingface.co/datasets/policyengine/populace-us/resolve/main/";
const RAW = "https://raw.githubusercontent.com/PolicyEngine/populace/";
const GH_BLOB = "https://github.com/PolicyEngine/populace/blob/";
const COV_PATH =
"packages/populace-build/src/populace/build/us/release_input_coverage_manifest.json";
const COV_FILE = "release_input_coverage_manifest.json";
const live = document.getElementById("contract-live");
if (!live) return;
const slot = (name) => live.querySelector(`[data-slot="${name}"]`);
const getJSON = async (url) => {
const res = await fetch(url, { cache: "no-store" });
if (!res.ok) throw new Error(String(res.status));
return res.json();
};
// The build sha is the last 7–40 char hex segment of the release id, sitting
// just before the trailing ...T......Z timestamp segment.
const shaFromReleaseId = (rid) => {
const segs = String(rid || "").split("-");
for (let i = segs.length - 1; i >= 0; i--) {
if (/^[0-9a-f]{7,40}$/.test(segs[i])) return segs[i];
}
return null;
};
const issueUrl = (ref) => {
// "PolicyEngine/populace#38" -> repo issue URL + "#38" label
const m = String(ref || "").match(/^([\w.-]+\/[\w.-]+)#(\d+)$/);
if (!m) return null;
return { href: `https://github.com/${m[1]}/issues/${m[2]}`, label: `#${m[2]}` };
};
let manifest = null;
let releaseId = "";
let sourceHref = "";
try {
const latest = await getJSON(HF + "latest.json");
releaseId = latest.release_id || "";
// (1) source tree at the release's build sha — present at every build's commit
const sha = shaFromReleaseId(releaseId);
if (sha) {
try {
manifest = await getJSON(RAW + sha + "/" + COV_PATH);
sourceHref = GH_BLOB + sha + "/" + COV_PATH;
} catch (_) {
manifest = null;
}
}
// (2) safety net: an in-release copy, if a future build publishes one
if (!manifest) {
const relDir = String(
(latest.paths && latest.paths.build_manifest) || ""
).replace(/build_manifest\.json$/, "");
if (!relDir) return;
const url = HF + relDir + COV_FILE;
manifest = await getJSON(url);
sourceHref = url;
}
} catch (_) {
return; // best-effort, like releases.js
}
if (!manifest || !manifest.columns) return;
const cols = manifest.columns;
const names = Object.keys(cols);
// counts: trust the manifest's own block, else derive from the columns
const c = manifest.counts || {};
const total = c.total != null ? c.total : names.length;
const required =
c.required != null
? c.required
: names.filter((n) => cols[n].status === "required").length;
const excluded =
c.reviewed_exclusion != null
? c.reviewed_exclusion
: names.filter((n) => cols[n].status === "reviewed_exclusion").length;
const setText = (name, v) => {
const el = slot(name);
if (el) el.textContent = v;
};
setText("total", total.toLocaleString("en-US"));
setText("required", required.toLocaleString("en-US"));
setText("excluded", excluded.toLocaleString("en-US"));
// gate line — name the gate (pulled live from the manifest description) and the
// count it checks. The mechanism, stated once more next to the live number.
let gateName = "release_input_coverage";
const gm = String(manifest.description || "").match(
/release gate \(([^)]+)\)/
);
if (gm) gateName = gm[1].split(".").pop();
const gateEl = slot("gate");
if (gateEl) {
gateEl.textContent =
`gate ${gateName} · checks all ${total.toLocaleString("en-US")} ` +
`declared input columns at build time · a required column missing or ` +
`all-default fails the release`;
}
// exclusion list — every reviewed exclusion, its reason, and its tracked issue
const excl = names
.filter((n) => cols[n].status === "reviewed_exclusion")
.sort();
const table = slot("table");
if (table && excl.length) {
const rows = excl.map((n) => {
const e = cols[n];
const iss = issueUrl(e.issue);
const link = iss
? `<a class="contract-excl-issue mono" href="${iss.href}" target="_blank" rel="noopener">${iss.label}</a>`
: "";
const reason = (e.reason || e.note || "").replace(
/[<>&]/g,
(ch) => ({ "<": "<", ">": ">", "&": "&" }[ch])
);
return (
`<div class="contract-excl-row">` +
`<div class="contract-excl-head"><span class="contract-excl-col mono">${n}</span>${link}</div>` +
`<p class="contract-excl-reason">${reason}</p>` +
`</div>`
);
});
table.innerHTML = rows.join("");
}
const summary = slot("summary");
if (summary)
summary.textContent = `Show all ${excluded.toLocaleString(
"en-US"
)} reviewed exclusions and their reasons`;
// provenance
const source = slot("source");
if (source) {
const label = releaseId ? ` · ${releaseId}` : "";
source.innerHTML =
`Coverage contract read live from ` +
`<a href="${sourceHref}" target="_blank" rel="noopener">${COV_FILE}</a>${label}`;
}
live.hidden = false;
})();