@repo-toolkit/confluence 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -12
- package/cli.js +556 -6
- package/index.d.ts +130 -1
- package/index.js +540 -4
- package/package.json +2 -2
package/cli.js
CHANGED
|
@@ -28,6 +28,7 @@ var DEFAULT_USER_AGENT = "repo-toolkit-confluence/1.0 (+node)";
|
|
|
28
28
|
var MAX_LIMIT = 250;
|
|
29
29
|
var MAX_ERROR_BODY_LENGTH = 8192;
|
|
30
30
|
var MAX_PAGES_PER_QUERY = 100;
|
|
31
|
+
var CONFLUENCE_MANAGED_LABEL = "repo-toolkit-confluence";
|
|
31
32
|
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
32
33
|
var DEFAULT_MAX_RETRIES = 3;
|
|
33
34
|
var DEFAULT_MAX_UPLOAD_BYTES = 50 * 1024 * 1024;
|
|
@@ -155,6 +156,44 @@ var ConfluenceClient = class {
|
|
|
155
156
|
body: JSON.stringify(body)
|
|
156
157
|
});
|
|
157
158
|
}
|
|
159
|
+
async getPageDescendants(pageId) {
|
|
160
|
+
const query = new URLSearchParams({ limit: String(MAX_LIMIT) });
|
|
161
|
+
return this.listAll(this.v2Url(`/pages/${encodeURIComponent(pageId)}/descendants?${query.toString()}`));
|
|
162
|
+
}
|
|
163
|
+
async getPageLabels(pageId) {
|
|
164
|
+
const query = new URLSearchParams({ limit: String(MAX_LIMIT) });
|
|
165
|
+
return this.listAll(this.v2Url(`/pages/${encodeURIComponent(pageId)}/labels?${query.toString()}`));
|
|
166
|
+
}
|
|
167
|
+
async addManagedLabel(pageId) {
|
|
168
|
+
await this.requestJson(this.v1Url(`/content/${encodeURIComponent(pageId)}/label`), {
|
|
169
|
+
method: "POST",
|
|
170
|
+
headers: { "Content-Type": "application/json" },
|
|
171
|
+
body: JSON.stringify([{ prefix: "global", name: CONFLUENCE_MANAGED_LABEL }])
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
async deletePage(pageId) {
|
|
175
|
+
await this.requestJson(this.v2Url(`/pages/${encodeURIComponent(pageId)}`), { method: "DELETE" });
|
|
176
|
+
}
|
|
177
|
+
async listAll(startUrl) {
|
|
178
|
+
const results = [];
|
|
179
|
+
const visited = /* @__PURE__ */ new Set();
|
|
180
|
+
let pageCount = 0;
|
|
181
|
+
let nextUrl = startUrl;
|
|
182
|
+
while (nextUrl) {
|
|
183
|
+
pageCount += 1;
|
|
184
|
+
if (pageCount > MAX_PAGES_PER_QUERY) {
|
|
185
|
+
throw new ConfluenceApiError(`Pagination limit (${MAX_PAGES_PER_QUERY}) exceeded`, 0, nextUrl, "");
|
|
186
|
+
}
|
|
187
|
+
if (visited.has(nextUrl)) {
|
|
188
|
+
throw new ConfluenceApiError("Confluence pagination loop detected", 0, nextUrl, "");
|
|
189
|
+
}
|
|
190
|
+
visited.add(nextUrl);
|
|
191
|
+
const data = await this.requestJson(nextUrl, { method: "GET" });
|
|
192
|
+
results.push(...data.results);
|
|
193
|
+
nextUrl = resolveNextUrl(this.baseUrl, this.baseUrlOrigin, data._links?.next);
|
|
194
|
+
}
|
|
195
|
+
return results;
|
|
196
|
+
}
|
|
158
197
|
async getAttachments(pageId) {
|
|
159
198
|
const results = [];
|
|
160
199
|
const visited = /* @__PURE__ */ new Set();
|
|
@@ -1661,6 +1700,133 @@ function runMmdc(cmdPath, source, outFile, timeoutMs, maxStreamBytes) {
|
|
|
1661
1700
|
});
|
|
1662
1701
|
}
|
|
1663
1702
|
|
|
1703
|
+
// src/parent-summary.ts
|
|
1704
|
+
var PARENT_SUMMARY_START_MARKER = "<!-- repo-toolkit-confluence:parent-summary:start -->";
|
|
1705
|
+
var PARENT_SUMMARY_END_MARKER = "<!-- repo-toolkit-confluence:parent-summary:end -->";
|
|
1706
|
+
function escapeCdata(text) {
|
|
1707
|
+
return text.replace(/]]>/g, "]]]]><![CDATA[>");
|
|
1708
|
+
}
|
|
1709
|
+
function pageLink(pageId, title) {
|
|
1710
|
+
const safeTitle = escapeCdata(title);
|
|
1711
|
+
return `<ac:link><ri:page ri:content-id="${escapeXmlAttribute(pageId)}" /><ac:plain-text-link-body><![CDATA[${safeTitle}]]></ac:plain-text-link-body></ac:link>`;
|
|
1712
|
+
}
|
|
1713
|
+
function countOccurrences(haystack, needle) {
|
|
1714
|
+
let count = 0;
|
|
1715
|
+
let idx = 0;
|
|
1716
|
+
while (true) {
|
|
1717
|
+
const found = haystack.indexOf(needle, idx);
|
|
1718
|
+
if (found === -1) {
|
|
1719
|
+
break;
|
|
1720
|
+
}
|
|
1721
|
+
count += 1;
|
|
1722
|
+
idx = found + needle.length;
|
|
1723
|
+
}
|
|
1724
|
+
return count;
|
|
1725
|
+
}
|
|
1726
|
+
function mergeParentSummaryBody(currentBody, generatedRegion) {
|
|
1727
|
+
const startCount = countOccurrences(currentBody, PARENT_SUMMARY_START_MARKER);
|
|
1728
|
+
const endCount = countOccurrences(currentBody, PARENT_SUMMARY_END_MARKER);
|
|
1729
|
+
if (startCount === 0 && endCount === 0) {
|
|
1730
|
+
if (currentBody === "") {
|
|
1731
|
+
return generatedRegion;
|
|
1732
|
+
}
|
|
1733
|
+
const sep2 = currentBody.endsWith("\n") ? "" : "\n";
|
|
1734
|
+
return currentBody + sep2 + generatedRegion;
|
|
1735
|
+
}
|
|
1736
|
+
if (startCount === 1 && endCount === 1) {
|
|
1737
|
+
const startIdx = currentBody.indexOf(PARENT_SUMMARY_START_MARKER);
|
|
1738
|
+
const endIdx = currentBody.indexOf(PARENT_SUMMARY_END_MARKER);
|
|
1739
|
+
if (startIdx === -1 || endIdx === -1) {
|
|
1740
|
+
throw new Error("malformed parent summary markers: missing marker");
|
|
1741
|
+
}
|
|
1742
|
+
if (startIdx > endIdx) {
|
|
1743
|
+
throw new Error("malformed parent summary markers: start after end");
|
|
1744
|
+
}
|
|
1745
|
+
const before = currentBody.slice(0, startIdx);
|
|
1746
|
+
const after = currentBody.slice(endIdx + PARENT_SUMMARY_END_MARKER.length);
|
|
1747
|
+
if (countOccurrences(before, PARENT_SUMMARY_START_MARKER) !== 0 || countOccurrences(before, PARENT_SUMMARY_END_MARKER) !== 0) {
|
|
1748
|
+
throw new Error("malformed parent summary markers: duplicate marker before region");
|
|
1749
|
+
}
|
|
1750
|
+
if (countOccurrences(after, PARENT_SUMMARY_START_MARKER) !== 0 || countOccurrences(after, PARENT_SUMMARY_END_MARKER) !== 0) {
|
|
1751
|
+
throw new Error("malformed parent summary markers: duplicate marker after region");
|
|
1752
|
+
}
|
|
1753
|
+
return before + generatedRegion + after;
|
|
1754
|
+
}
|
|
1755
|
+
throw new Error("malformed or duplicate parent summary markers: expected 0 or 1 managed region");
|
|
1756
|
+
}
|
|
1757
|
+
function renderParentSummary(input) {
|
|
1758
|
+
const lines = [];
|
|
1759
|
+
lines.push(PARENT_SUMMARY_START_MARKER);
|
|
1760
|
+
lines.push("<h2>Synced documentation</h2>");
|
|
1761
|
+
if (input.repositoryUrl) {
|
|
1762
|
+
const url = escapeXmlAttribute(input.repositoryUrl);
|
|
1763
|
+
const text = escapeHtml(input.repositoryUrl);
|
|
1764
|
+
lines.push(`<p><em>This documentation subtree is synced from <a href="${url}">${text}</a>.</em></p>`);
|
|
1765
|
+
}
|
|
1766
|
+
lines.push("<h3>Statistics</h3>");
|
|
1767
|
+
lines.push("<ul>");
|
|
1768
|
+
lines.push(`<li>Markdown pages: ${input.stats.markdownPages}</li>`);
|
|
1769
|
+
lines.push(`<li>Directory pages: ${input.stats.directoryPages}</li>`);
|
|
1770
|
+
lines.push(`<li>Total managed pages: ${input.stats.totalPages}</li>`);
|
|
1771
|
+
lines.push(`<li>Maximum depth: ${input.stats.maxDepth}</li>`);
|
|
1772
|
+
lines.push(`<li>Attachment references: ${input.stats.attachmentReferences}</li>`);
|
|
1773
|
+
lines.push(`<li>Mermaid blocks: ${input.stats.mermaidBlocks}</li>`);
|
|
1774
|
+
lines.push("</ul>");
|
|
1775
|
+
lines.push("<h3>Pages</h3>");
|
|
1776
|
+
if (input.pages.length === 0) {
|
|
1777
|
+
lines.push("<p>No managed child pages</p>");
|
|
1778
|
+
} else {
|
|
1779
|
+
lines.push(renderTree(input.pages));
|
|
1780
|
+
}
|
|
1781
|
+
lines.push(PARENT_SUMMARY_END_MARKER);
|
|
1782
|
+
return lines.join("\n");
|
|
1783
|
+
}
|
|
1784
|
+
function renderTree(pages) {
|
|
1785
|
+
const sorted = [...pages].sort((a, b) => a.relativePath.localeCompare(b.relativePath));
|
|
1786
|
+
const root = { children: /* @__PURE__ */ new Map(), key: "" };
|
|
1787
|
+
for (const page of sorted) {
|
|
1788
|
+
const parts = page.relativePath.split("/");
|
|
1789
|
+
let node = root;
|
|
1790
|
+
let currentPath = "";
|
|
1791
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
1792
|
+
const part = parts[i] ?? "";
|
|
1793
|
+
currentPath = currentPath ? currentPath + "/" + part : part;
|
|
1794
|
+
let child = node.children.get(part);
|
|
1795
|
+
if (!child) {
|
|
1796
|
+
child = { children: /* @__PURE__ */ new Map(), key: part };
|
|
1797
|
+
node.children.set(part, child);
|
|
1798
|
+
}
|
|
1799
|
+
if (i === parts.length - 1) {
|
|
1800
|
+
child.record = page;
|
|
1801
|
+
}
|
|
1802
|
+
node = child;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
const renderNode = (node) => {
|
|
1806
|
+
const entries = [...node.children.entries()].sort((a, b) => a[0].localeCompare(b[0]));
|
|
1807
|
+
if (entries.length === 0) {
|
|
1808
|
+
return "";
|
|
1809
|
+
}
|
|
1810
|
+
let html = "<ul>";
|
|
1811
|
+
for (const [, child] of entries) {
|
|
1812
|
+
const rec = child.record;
|
|
1813
|
+
if (rec) {
|
|
1814
|
+
const link = pageLink(rec.pageId, rec.title);
|
|
1815
|
+
const kindLabel = rec.kind === "directory" ? "directory" : "page";
|
|
1816
|
+
const pathCode = `<code>${escapeHtml(rec.relativePath)}</code>`;
|
|
1817
|
+
const childrenHtml = renderNode(child);
|
|
1818
|
+
html += `<li>${link} ${pathCode} <em>(${kindLabel})</em>${childrenHtml}</li>`;
|
|
1819
|
+
} else {
|
|
1820
|
+
const childrenHtml = renderNode(child);
|
|
1821
|
+
html += `<li>${escapeHtml(child.key)}${childrenHtml}</li>`;
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
html += "</ul>";
|
|
1825
|
+
return html;
|
|
1826
|
+
};
|
|
1827
|
+
return renderNode(root);
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1664
1830
|
// src/index.ts
|
|
1665
1831
|
var LocalSyncValidationAggregateError = class extends Error {
|
|
1666
1832
|
constructor(defects) {
|
|
@@ -1718,6 +1884,129 @@ var SyncMutationError = class extends Error {
|
|
|
1718
1884
|
this.unprocessed = input.unprocessed;
|
|
1719
1885
|
}
|
|
1720
1886
|
};
|
|
1887
|
+
var ReconciliationError = class extends Error {
|
|
1888
|
+
constructor(input) {
|
|
1889
|
+
super(input.failure.error.message);
|
|
1890
|
+
this.name = "ReconciliationError";
|
|
1891
|
+
this.phase = input.phase;
|
|
1892
|
+
this.completed = input.completed;
|
|
1893
|
+
this.failure = input.failure;
|
|
1894
|
+
this.unprocessed = input.unprocessed;
|
|
1895
|
+
}
|
|
1896
|
+
};
|
|
1897
|
+
var ParentSummaryError = class extends Error {
|
|
1898
|
+
constructor(input) {
|
|
1899
|
+
super(input.failure.error.message);
|
|
1900
|
+
this.name = "ParentSummaryError";
|
|
1901
|
+
this.phase = "parent-summary";
|
|
1902
|
+
this.changes = input.changes;
|
|
1903
|
+
this.labelsAdded = input.labelsAdded;
|
|
1904
|
+
this.cleanDeletions = input.cleanDeletions;
|
|
1905
|
+
this.pruneDeletions = input.pruneDeletions;
|
|
1906
|
+
this.blocked = input.blocked;
|
|
1907
|
+
this.failure = input.failure;
|
|
1908
|
+
}
|
|
1909
|
+
};
|
|
1910
|
+
function planStalePruning(input) {
|
|
1911
|
+
const nodes = buildInventoryNodes(input.parentPageId, input.inventory);
|
|
1912
|
+
const retained = /* @__PURE__ */ new Set();
|
|
1913
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1914
|
+
const isStale = (entry) => entry.type === "page" && entry.labeled && !input.expectedIds.has(entry.id) && entry.id !== input.parentPageId;
|
|
1915
|
+
const visit = (node) => {
|
|
1916
|
+
if (visited.has(node.entry.id)) {
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
visited.add(node.entry.id);
|
|
1920
|
+
let safe = isStale(node.entry);
|
|
1921
|
+
for (const child of node.children) {
|
|
1922
|
+
visit(child);
|
|
1923
|
+
if (retained.has(child.entry.id)) {
|
|
1924
|
+
safe = false;
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
if (!safe) {
|
|
1928
|
+
retained.add(node.entry.id);
|
|
1929
|
+
}
|
|
1930
|
+
};
|
|
1931
|
+
for (const node of nodes.values()) {
|
|
1932
|
+
visit(node);
|
|
1933
|
+
}
|
|
1934
|
+
const deletions = deepestFirst([...nodes.values()].filter((n) => !retained.has(n.entry.id)));
|
|
1935
|
+
const blocked = [...nodes.values()].filter((n) => retained.has(n.entry.id) && isStale(n.entry)).map((n) => n.entry.id).sort();
|
|
1936
|
+
return { deletions, blocked };
|
|
1937
|
+
}
|
|
1938
|
+
function planCleanDeletions(input) {
|
|
1939
|
+
const nodes = buildInventoryNodes(input.parentPageId, input.inventory);
|
|
1940
|
+
for (const node of nodes.values()) {
|
|
1941
|
+
if (node.entry.type !== "page") {
|
|
1942
|
+
throw new Error(
|
|
1943
|
+
`clean refused: descendant ${node.entry.id} has unsupported type "${node.entry.type}"; deleting its ancestors could remove content that cannot be restored by this tool`
|
|
1944
|
+
);
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
return { deletions: deepestFirst([...nodes.values()]), blocked: [] };
|
|
1948
|
+
}
|
|
1949
|
+
function buildInventoryNodes(parentPageId, inventory) {
|
|
1950
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
1951
|
+
for (const entry of inventory) {
|
|
1952
|
+
if (entry.id === parentPageId) {
|
|
1953
|
+
continue;
|
|
1954
|
+
}
|
|
1955
|
+
if (nodes.has(entry.id)) {
|
|
1956
|
+
throw new Error(`Incomplete descendant inventory: duplicate id ${entry.id}`);
|
|
1957
|
+
}
|
|
1958
|
+
nodes.set(entry.id, { entry, depth: -1, children: [] });
|
|
1959
|
+
}
|
|
1960
|
+
const depthOf = (node) => {
|
|
1961
|
+
if (node.depth >= 0) {
|
|
1962
|
+
return node.depth;
|
|
1963
|
+
}
|
|
1964
|
+
if (typeof node.entry.depth === "number") {
|
|
1965
|
+
node.depth = node.entry.depth;
|
|
1966
|
+
return node.depth;
|
|
1967
|
+
}
|
|
1968
|
+
let depth = 0;
|
|
1969
|
+
let current = node;
|
|
1970
|
+
const seen = /* @__PURE__ */ new Set([node.entry.id]);
|
|
1971
|
+
while (true) {
|
|
1972
|
+
const pid = current.entry.parentId;
|
|
1973
|
+
if (pid === parentPageId) {
|
|
1974
|
+
depth += 1;
|
|
1975
|
+
node.depth = depth;
|
|
1976
|
+
return depth;
|
|
1977
|
+
}
|
|
1978
|
+
if (!pid) {
|
|
1979
|
+
throw new Error(`Incomplete descendant inventory: missing parent for ${node.entry.id}`);
|
|
1980
|
+
}
|
|
1981
|
+
if (seen.has(pid)) {
|
|
1982
|
+
throw new Error(`Incomplete descendant inventory: parent cycle at ${pid}`);
|
|
1983
|
+
}
|
|
1984
|
+
seen.add(pid);
|
|
1985
|
+
const parent = nodes.get(pid);
|
|
1986
|
+
if (!parent) {
|
|
1987
|
+
throw new Error(
|
|
1988
|
+
`Incomplete descendant inventory: ancestor ${pid} of ${node.entry.id} is missing from the listing`
|
|
1989
|
+
);
|
|
1990
|
+
}
|
|
1991
|
+
depth += 1;
|
|
1992
|
+
current = parent;
|
|
1993
|
+
}
|
|
1994
|
+
};
|
|
1995
|
+
for (const node of nodes.values()) {
|
|
1996
|
+
depthOf(node);
|
|
1997
|
+
const pid = node.entry.parentId;
|
|
1998
|
+
if (pid !== void 0 && pid !== parentPageId) {
|
|
1999
|
+
nodes.get(pid)?.children.push(node);
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
return nodes;
|
|
2003
|
+
}
|
|
2004
|
+
function deepestFirst(nodes) {
|
|
2005
|
+
return [...nodes].sort((a, b) => b.depth - a.depth !== 0 ? b.depth - a.depth : a.entry.id.localeCompare(b.entry.id)).map((n) => n.entry.id);
|
|
2006
|
+
}
|
|
2007
|
+
function hasManagedMarker(labels) {
|
|
2008
|
+
return labels.some((label) => label.name === CONFLUENCE_MANAGED_LABEL && label.prefix === "global");
|
|
2009
|
+
}
|
|
1721
2010
|
function resolveConfluenceSyncPlan(options = {}) {
|
|
1722
2011
|
const pageTitleStrategy = resolvePageTitleStrategy(options.pageTitleStrategy);
|
|
1723
2012
|
const cwd = resolve2(options.cwd ?? process.cwd());
|
|
@@ -1767,6 +2056,8 @@ function resolveConfluenceSyncPlan(options = {}) {
|
|
|
1767
2056
|
dryRun: options.dryRun ?? false,
|
|
1768
2057
|
renderHtmlBlocks: options.renderHtmlBlocks === true,
|
|
1769
2058
|
repositoryUrl,
|
|
2059
|
+
clean: options.clean ?? false,
|
|
2060
|
+
updateParentPage: options.updateParentPage ?? true,
|
|
1770
2061
|
pageTitleStrategy
|
|
1771
2062
|
};
|
|
1772
2063
|
}
|
|
@@ -1776,7 +2067,6 @@ async function syncConfluenceToDocs(options = {}) {
|
|
|
1776
2067
|
const tree = await readDocTree(plan.folder);
|
|
1777
2068
|
if (tree.entries.length === 0) {
|
|
1778
2069
|
log(`No markdown files found under ${plan.folder}`);
|
|
1779
|
-
return;
|
|
1780
2070
|
}
|
|
1781
2071
|
const localPlan = validateLocalSync(tree.entries, plan);
|
|
1782
2072
|
validateLocalHierarchy(localPlan.entries, plan.pageTitleStrategy);
|
|
@@ -1789,6 +2079,32 @@ async function syncConfluenceToDocs(options = {}) {
|
|
|
1789
2079
|
`[dry-run] would sync ${entryPlan.entry.segments.join("/")} as "${entryPlan.title}"` + (attCount > 0 ? ` (${attCount} attachment${attCount === 1 ? "" : "s"} validated)` : "") + (mermaidCount > 0 ? ` (${mermaidCount} mermaid block${mermaidCount === 1 ? "" : "s"})` : "")
|
|
1790
2080
|
);
|
|
1791
2081
|
}
|
|
2082
|
+
if (plan.clean) {
|
|
2083
|
+
log(
|
|
2084
|
+
"[dry-run] clean requested: a real sync would move every page descendant of the target page to trash before recreating the local hierarchy."
|
|
2085
|
+
);
|
|
2086
|
+
}
|
|
2087
|
+
log(
|
|
2088
|
+
"[dry-run] a real sync would label every mapped page with the ownership marker and prune stale labeled descendants."
|
|
2089
|
+
);
|
|
2090
|
+
if (plan.updateParentPage) {
|
|
2091
|
+
const stats = computeDryRunStats(localPlan);
|
|
2092
|
+
log(
|
|
2093
|
+
`[dry-run] parent summary: Markdown pages: ${stats.markdownPages}, Directory pages: ${stats.directoryPages}, Total managed pages: ${stats.totalPages}, Maximum depth: ${stats.maxDepth}, Attachment references: ${stats.attachmentReferences}, Mermaid blocks: ${stats.mermaidBlocks}`
|
|
2094
|
+
);
|
|
2095
|
+
if (localPlan.entries.length === 0) {
|
|
2096
|
+
log("[dry-run] parent tree: No managed child pages");
|
|
2097
|
+
} else {
|
|
2098
|
+
for (const entryPlan of localPlan.entries) {
|
|
2099
|
+
const dirParts = entryPlan.entry.segments.slice(0, -1);
|
|
2100
|
+
for (let i = 0; i < dirParts.length; i += 1) {
|
|
2101
|
+
const dirPath = dirParts.slice(0, i + 1).join("/");
|
|
2102
|
+
log(`[dry-run] parent tree: ${dirPath} (directory) => "${dirParts[i] ?? ""}"`);
|
|
2103
|
+
}
|
|
2104
|
+
log(`[dry-run] parent tree: ${entryPlan.entry.segments.join("/")} (page) => "${entryPlan.title}"`);
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
1792
2108
|
return;
|
|
1793
2109
|
}
|
|
1794
2110
|
const client = options.client ?? new ConfluenceClient({
|
|
@@ -1796,13 +2112,29 @@ async function syncConfluenceToDocs(options = {}) {
|
|
|
1796
2112
|
username: plan.username,
|
|
1797
2113
|
apiToken: plan.apiToken
|
|
1798
2114
|
});
|
|
2115
|
+
const labelsAdded = [];
|
|
2116
|
+
const cleanDeletions = [];
|
|
2117
|
+
const pruneDeletions = [];
|
|
2118
|
+
const blocked = [];
|
|
2119
|
+
if (plan.clean) {
|
|
2120
|
+
const descendants = await client.getPageDescendants(plan.parentPageId);
|
|
2121
|
+
const inventory = descendants.filter((d) => d.id !== plan.parentPageId).map((d) => toInventoryEntry(d, false));
|
|
2122
|
+
const cleanPlan = planCleanDeletions({ parentPageId: plan.parentPageId, inventory });
|
|
2123
|
+
await executeDeletions(cleanPlan.deletions, "clean", client, log, cleanDeletions);
|
|
2124
|
+
}
|
|
1799
2125
|
const spaceId = await client.getSpaceIdByKey(plan.spaceKey);
|
|
1800
2126
|
const cache = new PageTitleCache(spaceId, client);
|
|
2127
|
+
const syncState = {
|
|
2128
|
+
mappedIds: /* @__PURE__ */ new Set(),
|
|
2129
|
+
ensuredLabels: /* @__PURE__ */ new Set(),
|
|
2130
|
+
labelsAdded,
|
|
2131
|
+
mappedRecords: /* @__PURE__ */ new Map()
|
|
2132
|
+
};
|
|
1801
2133
|
const changes = [];
|
|
1802
2134
|
for (let i = 0; i < localPlan.entries.length; i += 1) {
|
|
1803
2135
|
const entryPlan = localPlan.entries[i];
|
|
1804
2136
|
try {
|
|
1805
|
-
await syncEntry(entryPlan, plan, client, cache, log, changes);
|
|
2137
|
+
await syncEntry(entryPlan, plan, client, cache, log, changes, syncState);
|
|
1806
2138
|
} catch (error) {
|
|
1807
2139
|
throw new SyncMutationError({
|
|
1808
2140
|
changes,
|
|
@@ -1811,15 +2143,202 @@ async function syncConfluenceToDocs(options = {}) {
|
|
|
1811
2143
|
});
|
|
1812
2144
|
}
|
|
1813
2145
|
}
|
|
1814
|
-
|
|
2146
|
+
if (!plan.clean) {
|
|
2147
|
+
const descendants = await client.getPageDescendants(plan.parentPageId);
|
|
2148
|
+
const inventory = [];
|
|
2149
|
+
for (const d of descendants) {
|
|
2150
|
+
if (d.id === plan.parentPageId) {
|
|
2151
|
+
continue;
|
|
2152
|
+
}
|
|
2153
|
+
if (d.type !== "page") {
|
|
2154
|
+
inventory.push(toInventoryEntry(d, false));
|
|
2155
|
+
continue;
|
|
2156
|
+
}
|
|
2157
|
+
if (syncState.mappedIds.has(d.id)) {
|
|
2158
|
+
inventory.push(toInventoryEntry(d, true));
|
|
2159
|
+
continue;
|
|
2160
|
+
}
|
|
2161
|
+
const labels = await client.getPageLabels(d.id);
|
|
2162
|
+
inventory.push(toInventoryEntry(d, hasManagedMarker(labels)));
|
|
2163
|
+
}
|
|
2164
|
+
const prunePlan = planStalePruning({
|
|
2165
|
+
parentPageId: plan.parentPageId,
|
|
2166
|
+
expectedIds: syncState.mappedIds,
|
|
2167
|
+
inventory
|
|
2168
|
+
});
|
|
2169
|
+
await executeDeletions(prunePlan.deletions, "prune", client, log, pruneDeletions);
|
|
2170
|
+
for (const pageId of prunePlan.blocked) {
|
|
2171
|
+
blocked.push(pageId);
|
|
2172
|
+
log(`blocked: stale page ${pageId} retained because it has unlabeled, non-page, or expected descendants`);
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
let parentStatus = "skipped";
|
|
2176
|
+
if (plan.updateParentPage) {
|
|
2177
|
+
try {
|
|
2178
|
+
const parentPage = await client.getPage(plan.parentPageId);
|
|
2179
|
+
const stats = computeParentStats(localPlan, syncState);
|
|
2180
|
+
const pages = [...syncState.mappedRecords.values()].sort((a, b) => a.relativePath.localeCompare(b.relativePath));
|
|
2181
|
+
const region = renderParentSummary({ repositoryUrl: plan.repositoryUrl, stats, pages });
|
|
2182
|
+
const currentBody = parentPage.body?.storage?.value ?? "";
|
|
2183
|
+
const merged = mergeParentSummaryBody(currentBody, region);
|
|
2184
|
+
if (merged === currentBody) {
|
|
2185
|
+
log(`parent-unchanged: page ${plan.parentPageId}`);
|
|
2186
|
+
parentStatus = "unchanged";
|
|
2187
|
+
} else {
|
|
2188
|
+
await client.updatePage({
|
|
2189
|
+
id: plan.parentPageId,
|
|
2190
|
+
title: parentPage.title,
|
|
2191
|
+
body: { representation: "storage", value: merged },
|
|
2192
|
+
version: { number: (parentPage.version?.number ?? 0) + 1, message: plan.versionMessage }
|
|
2193
|
+
});
|
|
2194
|
+
log(`parent-updated: page ${plan.parentPageId}`);
|
|
2195
|
+
parentStatus = "updated";
|
|
2196
|
+
}
|
|
2197
|
+
} catch (error) {
|
|
2198
|
+
throw new ParentSummaryError({
|
|
2199
|
+
changes,
|
|
2200
|
+
labelsAdded: [...labelsAdded],
|
|
2201
|
+
cleanDeletions: [...cleanDeletions],
|
|
2202
|
+
pruneDeletions: [...pruneDeletions],
|
|
2203
|
+
blocked: [...blocked],
|
|
2204
|
+
failure: { pageId: plan.parentPageId, error: error instanceof Error ? error : new Error(String(error)) }
|
|
2205
|
+
});
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
return { changes, labelsAdded, cleanDeletions, pruneDeletions, blocked, parentStatus };
|
|
2209
|
+
}
|
|
2210
|
+
function computeParentStats(localPlan, state) {
|
|
2211
|
+
const dirSet = /* @__PURE__ */ new Set();
|
|
2212
|
+
let maxDepth = 0;
|
|
2213
|
+
let attachmentReferences = 0;
|
|
2214
|
+
let mermaidBlocks = 0;
|
|
2215
|
+
for (const entryPlan of localPlan.entries) {
|
|
2216
|
+
const segs = entryPlan.entry.segments;
|
|
2217
|
+
if (segs.length > maxDepth) {
|
|
2218
|
+
maxDepth = segs.length;
|
|
2219
|
+
}
|
|
2220
|
+
for (let i = 0; i < segs.length - 1; i += 1) {
|
|
2221
|
+
dirSet.add(segs.slice(0, i + 1).join("/"));
|
|
2222
|
+
}
|
|
2223
|
+
attachmentReferences += entryPlan.attachments.length;
|
|
2224
|
+
mermaidBlocks += entryPlan.mermaidBlocks.length;
|
|
2225
|
+
}
|
|
2226
|
+
const markdownPages = localPlan.entries.length;
|
|
2227
|
+
const directoryPages = dirSet.size;
|
|
2228
|
+
const totalPages = state.mappedRecords.size;
|
|
2229
|
+
const effectiveMaxDepth = localPlan.entries.length === 0 ? 0 : maxDepth;
|
|
2230
|
+
return {
|
|
2231
|
+
markdownPages,
|
|
2232
|
+
directoryPages,
|
|
2233
|
+
totalPages: totalPages > 0 ? totalPages : directoryPages + markdownPages,
|
|
2234
|
+
maxDepth: effectiveMaxDepth,
|
|
2235
|
+
attachmentReferences,
|
|
2236
|
+
mermaidBlocks
|
|
2237
|
+
};
|
|
1815
2238
|
}
|
|
1816
|
-
|
|
2239
|
+
function computeDryRunStats(localPlan) {
|
|
2240
|
+
const dirSet = /* @__PURE__ */ new Set();
|
|
2241
|
+
let maxDepth = 0;
|
|
2242
|
+
let attachmentReferences = 0;
|
|
2243
|
+
let mermaidBlocks = 0;
|
|
2244
|
+
for (const entryPlan of localPlan.entries) {
|
|
2245
|
+
const segs = entryPlan.entry.segments;
|
|
2246
|
+
if (segs.length > maxDepth) {
|
|
2247
|
+
maxDepth = segs.length;
|
|
2248
|
+
}
|
|
2249
|
+
for (let i = 0; i < segs.length - 1; i += 1) {
|
|
2250
|
+
dirSet.add(segs.slice(0, i + 1).join("/"));
|
|
2251
|
+
}
|
|
2252
|
+
attachmentReferences += entryPlan.attachments.length;
|
|
2253
|
+
mermaidBlocks += entryPlan.mermaidBlocks.length;
|
|
2254
|
+
}
|
|
2255
|
+
const markdownPages = localPlan.entries.length;
|
|
2256
|
+
const directoryPages = dirSet.size;
|
|
2257
|
+
return {
|
|
2258
|
+
markdownPages,
|
|
2259
|
+
directoryPages,
|
|
2260
|
+
totalPages: directoryPages + markdownPages,
|
|
2261
|
+
maxDepth: markdownPages === 0 ? 0 : maxDepth,
|
|
2262
|
+
attachmentReferences,
|
|
2263
|
+
mermaidBlocks
|
|
2264
|
+
};
|
|
2265
|
+
}
|
|
2266
|
+
function toInventoryEntry(d, labeled) {
|
|
2267
|
+
const entry = { id: d.id, type: d.type, labeled };
|
|
2268
|
+
if (d.parentId !== void 0) {
|
|
2269
|
+
entry.parentId = d.parentId;
|
|
2270
|
+
}
|
|
2271
|
+
if (d.depth !== void 0) {
|
|
2272
|
+
entry.depth = d.depth;
|
|
2273
|
+
}
|
|
2274
|
+
if (d.title !== void 0) {
|
|
2275
|
+
entry.title = d.title;
|
|
2276
|
+
}
|
|
2277
|
+
return entry;
|
|
2278
|
+
}
|
|
2279
|
+
async function ensureManagedLabel(pageId, client, state, log) {
|
|
2280
|
+
if (state.ensuredLabels.has(pageId)) {
|
|
2281
|
+
return;
|
|
2282
|
+
}
|
|
2283
|
+
const labels = await client.getPageLabels(pageId);
|
|
2284
|
+
if (!hasManagedMarker(labels)) {
|
|
2285
|
+
await client.addManagedLabel(pageId);
|
|
2286
|
+
state.labelsAdded.push(pageId);
|
|
2287
|
+
log(`labeled: page ${pageId}`);
|
|
2288
|
+
}
|
|
2289
|
+
state.ensuredLabels.add(pageId);
|
|
2290
|
+
}
|
|
2291
|
+
async function executeDeletions(ids, phase, client, log, evidence) {
|
|
2292
|
+
for (let i = 0; i < ids.length; i += 1) {
|
|
2293
|
+
const pageId = ids[i];
|
|
2294
|
+
if (pageId === void 0) {
|
|
2295
|
+
continue;
|
|
2296
|
+
}
|
|
2297
|
+
try {
|
|
2298
|
+
await client.deletePage(pageId);
|
|
2299
|
+
evidence.push(pageId);
|
|
2300
|
+
log(`${phase === "clean" ? "clean" : "pruned"}: trashed page ${pageId}`);
|
|
2301
|
+
} catch (error) {
|
|
2302
|
+
throw new ReconciliationError({
|
|
2303
|
+
phase,
|
|
2304
|
+
completed: [...evidence],
|
|
2305
|
+
failure: { pageId, error: error instanceof Error ? error : new Error(String(error)) },
|
|
2306
|
+
unprocessed: ids.slice(i + 1)
|
|
2307
|
+
});
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
async function syncEntry(entryPlan, plan, client, cache, log, changes, state) {
|
|
1817
2312
|
const { entry, html: precomputedHtml, mermaidBlocks, markdownDir, hasLocalImages, hasMermaidBlocks } = entryPlan;
|
|
1818
2313
|
const segments = entry.segments;
|
|
1819
2314
|
if (segments.length === 0) {
|
|
1820
2315
|
return;
|
|
1821
2316
|
}
|
|
1822
2317
|
let currentParentId = plan.parentPageId;
|
|
2318
|
+
const recordDirectory = (relativePath, title, pageId, depth) => {
|
|
2319
|
+
if (!state.mappedRecords.has(relativePath)) {
|
|
2320
|
+
state.mappedRecords.set(relativePath, {
|
|
2321
|
+
relativePath,
|
|
2322
|
+
kind: "directory",
|
|
2323
|
+
title,
|
|
2324
|
+
pageId,
|
|
2325
|
+
depth,
|
|
2326
|
+
attachmentCount: 0,
|
|
2327
|
+
mermaidCount: 0
|
|
2328
|
+
});
|
|
2329
|
+
}
|
|
2330
|
+
};
|
|
2331
|
+
const recordLeaf = (relativePath, title, pageId, depth) => {
|
|
2332
|
+
state.mappedRecords.set(relativePath, {
|
|
2333
|
+
relativePath,
|
|
2334
|
+
kind: "leaf",
|
|
2335
|
+
title,
|
|
2336
|
+
pageId,
|
|
2337
|
+
depth,
|
|
2338
|
+
attachmentCount: entryPlan.attachments.length,
|
|
2339
|
+
mermaidCount: entryPlan.mermaidBlocks.length
|
|
2340
|
+
});
|
|
2341
|
+
};
|
|
1823
2342
|
for (let idx = 0; idx < segments.length; idx += 1) {
|
|
1824
2343
|
const isLast = idx === segments.length - 1;
|
|
1825
2344
|
const segment = segments[idx] ?? "";
|
|
@@ -1833,12 +2352,18 @@ async function syncEntry(entryPlan, plan, client, cache, log, changes) {
|
|
|
1833
2352
|
parentId: currentParentId,
|
|
1834
2353
|
body: { representation: "storage", value: precomputedHtml }
|
|
1835
2354
|
});
|
|
2355
|
+
state.mappedIds.add(pageId2);
|
|
2356
|
+
await ensureManagedLabel(pageId2, client, state, log);
|
|
2357
|
+
recordLeaf(segments.join("/"), title, pageId2, segments.length);
|
|
1836
2358
|
log(`created: ${segments.join("/")} (page ${pageId2})`);
|
|
1837
2359
|
changes.push({ entry, pageId: pageId2, kind: "created" });
|
|
1838
2360
|
return;
|
|
1839
2361
|
}
|
|
1840
2362
|
const existingPage = existing ?? await cache.findOrCreate(title, currentParentId);
|
|
1841
2363
|
const pageId = existingPage.id;
|
|
2364
|
+
state.mappedIds.add(pageId);
|
|
2365
|
+
await ensureManagedLabel(pageId, client, state, log);
|
|
2366
|
+
recordLeaf(segments.join("/"), title, pageId, segments.length);
|
|
1842
2367
|
const current = await client.getPage(pageId);
|
|
1843
2368
|
const currentBody = current.body?.storage?.value ?? "";
|
|
1844
2369
|
let body = precomputedHtml;
|
|
@@ -1885,10 +2410,16 @@ async function syncEntry(entryPlan, plan, client, cache, log, changes) {
|
|
|
1885
2410
|
if (isMarkdownName(segment)) {
|
|
1886
2411
|
const title = pageTitleFromSegments(segments.slice(0, idx + 1), plan.pageTitleStrategy);
|
|
1887
2412
|
const page2 = await cache.findOrCreate(title, currentParentId);
|
|
2413
|
+
state.mappedIds.add(page2.id);
|
|
2414
|
+
await ensureManagedLabel(page2.id, client, state, log);
|
|
2415
|
+
recordDirectory(segments.slice(0, idx + 1).join("/"), title, page2.id, idx + 1);
|
|
1888
2416
|
currentParentId = page2.id;
|
|
1889
2417
|
continue;
|
|
1890
2418
|
}
|
|
1891
2419
|
const page = await cache.findOrCreate(segment, currentParentId);
|
|
2420
|
+
state.mappedIds.add(page.id);
|
|
2421
|
+
await ensureManagedLabel(page.id, client, state, log);
|
|
2422
|
+
recordDirectory(segments.slice(0, idx + 1).join("/"), segment, page.id, idx + 1);
|
|
1892
2423
|
currentParentId = page.id;
|
|
1893
2424
|
}
|
|
1894
2425
|
}
|
|
@@ -2052,11 +2583,13 @@ var SPECS = [
|
|
|
2052
2583
|
{ name: "skip-unchanged", boolean: true, negatable: true },
|
|
2053
2584
|
{ name: "dry-run", boolean: true },
|
|
2054
2585
|
{ name: "render-html-blocks", boolean: true },
|
|
2586
|
+
{ name: "clean", boolean: true },
|
|
2587
|
+
{ name: "update-parent-page", boolean: true, negatable: true },
|
|
2055
2588
|
INTERACTIVE_FLAG
|
|
2056
2589
|
];
|
|
2057
2590
|
var ENV_TRUTHY = /* @__PURE__ */ new Set(["true", "1", "yes", "on"]);
|
|
2058
2591
|
var ENV_FALSY = /* @__PURE__ */ new Set(["false", "0", "no", "off", ""]);
|
|
2059
|
-
var BOOLEAN_ENV_KEYS = /* @__PURE__ */ new Set(["skipUnchanged", "dryRun", "renderHtmlBlocks"]);
|
|
2592
|
+
var BOOLEAN_ENV_KEYS = /* @__PURE__ */ new Set(["skipUnchanged", "dryRun", "renderHtmlBlocks", "clean", "updateParentPage"]);
|
|
2060
2593
|
function isBooleanOption(key) {
|
|
2061
2594
|
return BOOLEAN_ENV_KEYS.has(key);
|
|
2062
2595
|
}
|
|
@@ -2098,6 +2631,8 @@ Environment variables (CLI form; GitHub Action INPUT_* form is also read):
|
|
|
2098
2631
|
CONFLUENCE_SKIP_UNCHANGED true|false (default: true)
|
|
2099
2632
|
CONFLUENCE_DRY_RUN true|false (default: false)
|
|
2100
2633
|
CONFLUENCE_RENDER_HTML_BLOCKS true|false (default: false)
|
|
2634
|
+
CONFLUENCE_CLEAN true|false (default: false)
|
|
2635
|
+
CONFLUENCE_UPDATE_PARENT_PAGE true|false (default: true)
|
|
2101
2636
|
INPUT_<UPPER-FLAG> GitHub Actions input form (lower precedence)
|
|
2102
2637
|
|
|
2103
2638
|
Note: prefer CONFLUENCE_API_TOKEN_FILE or CONFLUENCE_API_TOKEN over
|
|
@@ -2122,6 +2657,11 @@ Options:
|
|
|
2122
2657
|
--dry-run Walk the doc tree and print the plan without API calls
|
|
2123
2658
|
--render-html-blocks Render \`\`\`html fenced blocks as inline HTML via the
|
|
2124
2659
|
Confluence html macro (default: false; emits as code box)
|
|
2660
|
+
--clean Move all page descendants to trash before recreation (default: false).
|
|
2661
|
+
WARNING: destructive \u2014 all page descendants, including manual/unlabeled
|
|
2662
|
+
pages, are moved to trash; parentPageId is retained.
|
|
2663
|
+
--update-parent-page Update parent page summary region (default: true)
|
|
2664
|
+
--no-update-parent-page Do not update parent page summary
|
|
2125
2665
|
-i, --interactive Prompt interactively for missing non-secret required values
|
|
2126
2666
|
-h, --help Show this help message
|
|
2127
2667
|
`);
|
|
@@ -2162,6 +2702,8 @@ var ENV_BINDINGS = [
|
|
|
2162
2702
|
{ envName: "INPUT_DRY-RUN", key: "dryRun", kind: "boolean" },
|
|
2163
2703
|
{ envName: "INPUT_SKIP-UNCHANGED", key: "skipUnchanged", kind: "boolean" },
|
|
2164
2704
|
{ envName: "INPUT_RENDER-HTML-BLOCKS", key: "renderHtmlBlocks", kind: "boolean" },
|
|
2705
|
+
{ envName: "INPUT_CLEAN", key: "clean", kind: "boolean" },
|
|
2706
|
+
{ envName: "INPUT_UPDATE-PARENT-PAGE", key: "updateParentPage", kind: "boolean" },
|
|
2165
2707
|
{ envName: "CONFLUENCE_FOLDER", key: "folder", kind: "string" },
|
|
2166
2708
|
{ envName: "CONFLUENCE_USERNAME", key: "username", kind: "string" },
|
|
2167
2709
|
{ envName: "CONFLUENCE_API_TOKEN", key: "apiToken", kind: "string" },
|
|
@@ -2174,7 +2716,9 @@ var ENV_BINDINGS = [
|
|
|
2174
2716
|
{ envName: "CONFLUENCE_PAGE_TITLE_STRATEGY", key: "pageTitleStrategy", kind: "string" },
|
|
2175
2717
|
{ envName: "CONFLUENCE_DRY_RUN", key: "dryRun", kind: "boolean" },
|
|
2176
2718
|
{ envName: "CONFLUENCE_SKIP_UNCHANGED", key: "skipUnchanged", kind: "boolean" },
|
|
2177
|
-
{ envName: "CONFLUENCE_RENDER_HTML_BLOCKS", key: "renderHtmlBlocks", kind: "boolean" }
|
|
2719
|
+
{ envName: "CONFLUENCE_RENDER_HTML_BLOCKS", key: "renderHtmlBlocks", kind: "boolean" },
|
|
2720
|
+
{ envName: "CONFLUENCE_CLEAN", key: "clean", kind: "boolean" },
|
|
2721
|
+
{ envName: "CONFLUENCE_UPDATE_PARENT_PAGE", key: "updateParentPage", kind: "boolean" }
|
|
2178
2722
|
];
|
|
2179
2723
|
function optionsFromEnv(env = process.env) {
|
|
2180
2724
|
const options = {};
|
|
@@ -2240,6 +2784,12 @@ function buildOptions(result) {
|
|
|
2240
2784
|
if (values["render-html-blocks"] !== void 0) {
|
|
2241
2785
|
options.renderHtmlBlocks = true;
|
|
2242
2786
|
}
|
|
2787
|
+
if (values["clean"] !== void 0) {
|
|
2788
|
+
options.clean = values["clean"] === "true";
|
|
2789
|
+
}
|
|
2790
|
+
if (values["update-parent-page"] !== void 0) {
|
|
2791
|
+
options.updateParentPage = values["update-parent-page"] === "true";
|
|
2792
|
+
}
|
|
2243
2793
|
return options;
|
|
2244
2794
|
}
|
|
2245
2795
|
async function resolveConfluenceOptions(args) {
|