Skip to content

Commit 1a72c3d

Browse files
author
Al Manning
committed
fix issue with nested parent creation
1 parent 1492e1f commit 1a72c3d

File tree

3 files changed

+103
-94
lines changed

3 files changed

+103
-94
lines changed

src/publish/confluence/confluence-helper.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,12 @@ export const buildSpaceChanges = (
300300
accumulatedChanges: ConfluenceSpaceChange[],
301301
fileMetadata: SiteFileMetadata
302302
): ConfluenceSpaceChange[] => {
303-
const existingPage = existingSite.find(
304-
(page: SitePage) => page?.metadata?.fileName === fileMetadata.fileName
305-
);
303+
const findPageInExistingSite = (fileName: string) =>
304+
existingSite.find(
305+
(page: SitePage) => page?.metadata?.fileName === fileName
306+
);
307+
308+
const existingPage = findPageInExistingSite(fileMetadata.fileName);
306309

307310
let spaceChangeList: ConfluenceSpaceChange[] = [];
308311

@@ -314,16 +317,13 @@ export const buildSpaceChanges = (
314317
: parent?.parent;
315318

316319
const checkCreateParents = (): SitePage | null => {
317-
console.log("checkCreateParents");
318-
console.log("pathList", pathList);
319320
if (pathList.length < 2) {
320321
return null;
321322
}
322323

323324
let existingSiteParent = null;
324325

325326
const parentsList = pathList.slice(0, pathList.length - 1);
326-
console.log("parentsList", parentsList);
327327

328328
parentsList.forEach((parentFileName, index) => {
329329
const ancestorFilePath = parentsList.slice(0, index).join("/");
@@ -351,10 +351,11 @@ export const buildSpaceChanges = (
351351
return false;
352352
});
353353

354-
console.log("existingSite", existingSite);
355-
console.log("existingSiteParent", existingSiteParent);
356-
357354
if (!existingParentCreateChange && !existingSiteParent) {
355+
// Create a new parent page
356+
357+
const existingAncestor = findPageInExistingSite(ancestor ?? "");
358+
358359
spaceChangeList = [
359360
...spaceChangeList,
360361
buildContentCreate(
@@ -367,7 +368,7 @@ export const buildSpaceChanges = (
367368
},
368369
},
369370
fileName,
370-
ancestor,
371+
existingAncestor ? existingAncestor.id : ancestor,
371372
ContentStatusEnum.current
372373
),
373374
];
@@ -378,7 +379,6 @@ export const buildSpaceChanges = (
378379
};
379380

380381
const existingParent: SitePage | null = checkCreateParents();
381-
console.log("existingParent", existingParent);
382382

383383
pageParent = existingParent ? existingParent.id : pageParent;
384384

@@ -421,8 +421,6 @@ export const buildSpaceChanges = (
421421
existingSite
422422
);
423423

424-
console.log("pagesToDelete", pagesToDelete);
425-
426424
// TODO prompt as a sanity check and limiter to prevent any major run-away deletes
427425
const deleteChanges: ContentDelete[] = pagesToDelete.map(
428426
(toDelete: SitePage) => {

src/publish/confluence/confluence.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,11 @@ async function publish(
247247
trace("publish", { parent, server, space });
248248

249249
const uniquifyTitle = async (title: string) => {
250-
console.log("uniquifyTitle", title);
251250
const titleAlreadyExistsInSpace: boolean = await client.isTitleInSpace(
252251
title,
253252
space
254253
);
255254

256-
console.log("titleAlreadyExistsInSpace", titleAlreadyExistsInSpace);
257-
258255
const uuid = globalThis.crypto.randomUUID();
259256
const shortUUID = uuid.split("-")[0] ?? uuid;
260257
const createTitle = titleAlreadyExistsInSpace
@@ -469,11 +466,8 @@ async function publish(
469466
fileName: string = ""
470467
): Promise<Content> => {
471468
//TODO check why files are always being uniquified
472-
console.log("create Content");
473-
console.log("titleToCreate");
474-
const createTitle = await uniquifyTitle(titleToCreate);
475469

476-
console.log("createTitle", createTitle);
470+
const createTitle = await uniquifyTitle(titleToCreate);
477471

478472
const attachmentsToUpload: string[] = findAttachments(
479473
body.storage.value,
@@ -619,9 +613,6 @@ async function publish(
619613

620614
trace("changelist", changeList);
621615

622-
console.log("existingSite", existingSite);
623-
console.log("changeList", changeList);
624-
625616
changeList = updateLinks(
626617
metadataByFilename,
627618
changeList,
@@ -633,12 +624,9 @@ async function publish(
633624

634625
const doChange = async (change: ConfluenceSpaceChange) => {
635626
if (isContentCreate(change)) {
636-
console.log("DO CREATE", change);
637-
638627
let ancestorId =
639628
(change?.ancestors && change?.ancestors[0]?.id) ?? null;
640-
console.log("ancestorId", ancestorId);
641-
console.log("pathsToId", pathsToId);
629+
642630
if (ancestorId && pathsToId[ancestorId]) {
643631
ancestorId = pathsToId[ancestorId];
644632
}
@@ -658,7 +646,6 @@ async function publish(
658646

659647
if (change.fileName) {
660648
pathsToId[change.fileName] = result.id ?? "";
661-
console.log("pathsToId added", pathsToId);
662649
}
663650

664651
const contentPropertyResult: Content =
@@ -669,7 +656,6 @@ async function publish(
669656

670657
return result;
671658
} else if (isContentUpdate(change)) {
672-
console.log("DO UPDATE");
673659
const update = change as ContentUpdate;
674660
return await updateContent(
675661
publishFiles,
@@ -678,7 +664,6 @@ async function publish(
678664
update.title ?? ""
679665
);
680666
} else if (isContentDelete(change)) {
681-
console.log("DO DELETE", change);
682667
if (DELETE_DISABLED) {
683668
console.warn("DELETE DISABELD");
684669
return null;

tests/unit/confluence.test.ts

Lines changed: 90 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,10 +2160,9 @@ const runSpaceUpdatesWithNestedMoves = () => {
21602160
};
21612161

21622162
const fakeMultiNestedFile: SiteFileMetadata = {
2163-
fileName:
2164-
"fake-great-grand-parent/fake-grand-parent/fake-parent/fake-file-name.xml",
2165-
title: "fake-title",
2166-
originalTitle: "fake-title-original",
2163+
fileName: "fake-parent/fake-inner-parent/fake-file-name.xml",
2164+
title: "fake-multi-nested-title",
2165+
originalTitle: "fake-multi-nested-title-original",
21672166
matchingPages: [],
21682167
contentBody: {
21692168
storage: {
@@ -2379,69 +2378,96 @@ const runSpaceUpdatesWithNestedMoves = () => {
23792378
fakeSpace,
23802379
existingSite
23812380
);
2382-
console.log("actual", actual);
23832381
assertEquals(expected, actual);
23842382
});
23852383

2386-
// TODO Delete orphaned parents
2387-
// unitTest(suiteLabel("parent_to_root_del_parent"), async () => {
2388-
// const fileMetadataList: SiteFileMetadata[] = [fakeRootFile];
2389-
// const existingSite = [
2390-
// {
2391-
// id: "fake-title-id",
2392-
// title: "fake-title",
2393-
// metadata: { fileName: "fake-parent-id/fake-file-name.xml" },
2394-
// ancestors: [{ id: "fake-grand-parent-id" }, { id: "fake-parent-id" }],
2395-
// },
2396-
// ];
2397-
//
2398-
// const expected: ConfluenceSpaceChange[] = [
2399-
// {
2400-
// contentChangeType: ContentChangeType.delete,
2401-
// id: "fake-title-id",
2402-
// },
2403-
// {
2404-
// contentChangeType: ContentChangeType.delete,
2405-
// id: "fake-parent-id",
2406-
// },
2407-
// {
2408-
// contentChangeType: ContentChangeType.create,
2409-
// ancestors: [
2410-
// {
2411-
// id: "8781825",
2412-
// },
2413-
// ],
2414-
// body: {
2415-
// storage: {
2416-
// representation: "storage",
2417-
// value: "fake-value",
2418-
// },
2419-
// },
2420-
// fileName: "fake-file-name.xml",
2421-
// space: {
2422-
// key: "fake-space-key",
2423-
// id: "fake-space-id",
2424-
// homepage: buildFakeContent(),
2425-
// },
2426-
// status: "current",
2427-
// title: "fake-title",
2428-
// type: "page",
2429-
// },
2430-
// ];
2431-
// const pagesToDelete = findPagesToDelete(fileMetadataList, existingSite);
2432-
//
2433-
// console.log("pagesToDelete", pagesToDelete);
2434-
//
2435-
// const actual: ConfluenceSpaceChange[] = buildSpaceChanges(
2436-
// fileMetadataList,
2437-
// FAKE_PARENT,
2438-
// fakeSpace,
2439-
// existingSite
2440-
// );
2441-
//
2442-
// console.log("actual", actual);
2443-
// // assertEquals(expected, actual);
2444-
// });
2384+
unitTest(suiteLabel("create_in_nested_parent"), async () => {
2385+
const fileMetadataList: SiteFileMetadata[] = [
2386+
fakeNestedFile,
2387+
fakeMultiNestedFile,
2388+
];
2389+
const existingSite = [
2390+
{
2391+
id: "fake-title-id",
2392+
title: "fake-title",
2393+
metadata: { fileName: "fake-parent/fake-file-name.xml" },
2394+
ancestors: [{ id: "fake-grand-parent-id" }, { id: "fake-parent-id" }],
2395+
},
2396+
{
2397+
title: "Fake Parent",
2398+
id: "fake-parent-id",
2399+
metadata: { editor: "v2", fileName: "fake-parent" },
2400+
ancestors: [{ id: "fake-grand-parent-id" }],
2401+
},
2402+
];
2403+
2404+
const expected: ConfluenceSpaceChange[] = [
2405+
{
2406+
contentChangeType: ContentChangeType.update,
2407+
id: "fake-title-id",
2408+
version: null,
2409+
title: "fake-title",
2410+
type: "page",
2411+
status: "current",
2412+
ancestors: [{ id: "fake-parent-id" }],
2413+
body: { storage: { value: "fake-value", representation: "storage" } },
2414+
fileName: "fake-parent/fake-file-name.xml",
2415+
},
2416+
{
2417+
contentChangeType: ContentChangeType.create,
2418+
title: "Fake-inner-parent",
2419+
type: "page",
2420+
ancestors: [
2421+
{
2422+
id: "fake-parent-id",
2423+
},
2424+
],
2425+
body: {
2426+
storage: {
2427+
representation: "storage",
2428+
value: "",
2429+
},
2430+
},
2431+
fileName: "fake-parent/fake-inner-parent",
2432+
space: {
2433+
key: "fake-space-key",
2434+
id: "fake-space-id",
2435+
homepage: buildFakeContent(),
2436+
},
2437+
status: "current",
2438+
},
2439+
{
2440+
contentChangeType: ContentChangeType.create,
2441+
title: "fake-multi-nested-title",
2442+
type: "page",
2443+
ancestors: [
2444+
{
2445+
id: "fake-parent/fake-inner-parent",
2446+
},
2447+
],
2448+
body: {
2449+
storage: {
2450+
representation: "storage",
2451+
value: "fake-value",
2452+
},
2453+
},
2454+
fileName: "fake-parent/fake-inner-parent/fake-file-name.xml",
2455+
space: {
2456+
key: "fake-space-key",
2457+
id: "fake-space-id",
2458+
homepage: buildFakeContent(),
2459+
},
2460+
status: "current",
2461+
},
2462+
];
2463+
const actual: ConfluenceSpaceChange[] = buildSpaceChanges(
2464+
fileMetadataList,
2465+
FAKE_PARENT,
2466+
fakeSpace,
2467+
existingSite
2468+
);
2469+
assertEquals(expected, actual);
2470+
});
24452471
};
24462472

24472473
const runBuildFileToMetaTable = () => {

0 commit comments

Comments
 (0)