Skip to content

Commit

Permalink
feat: Diff page details to see if they have changed. If so then publi…
Browse files Browse the repository at this point in the history
…sh page.

Fixes: #238
  • Loading branch information
andymac4182 committed May 3, 2023
1 parent b801d67 commit 7e30ca8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/lib/src/AdfProcessing.ts
Expand Up @@ -57,7 +57,7 @@ export function prepareAdfToUpload(
result = mergeTextNodes(result);

const pageInlineComments = extractInlineComments(
confluenceNode.existingAdf
confluenceNode.existingPageData.adfContent
);

result = applyInlineComments(result, pageInlineComments);
Expand Down
65 changes: 50 additions & 15 deletions packages/lib/src/Publisher.ts
Expand Up @@ -14,6 +14,7 @@ import {
import { PageContentType } from "./ConniePageConfig";
import { p } from "@atlaskit/adf-utils/builders";
import { ADFEntity } from "@atlaskit/adf-utils/dist/types/types";
import { isEqual } from "./isEqual";

export interface FailedFile {
fileName: string;
Expand Down Expand Up @@ -74,18 +75,26 @@ export interface ConfluenceAdfFile {
blogPostDate: string | undefined;
}

interface ConfluencePageExistingData {
adfContent: JSONDocNode;
pageTitle: string;
ancestors: { id: string }[];
contentType: string;
}

export interface ConfluenceNode {
file: ConfluenceAdfFile;
version: number;
lastUpdatedBy: string;
existingAdf: JSONDocNode;
parentPageId: string;
existingPageData: ConfluencePageExistingData;
ancestors: string[];
}

export interface ConfluenceTreeNode {
file: ConfluenceAdfFile;
version: number;
lastUpdatedBy: string;
existingAdf: JSONDocNode;
existingPageData: ConfluencePageExistingData;
children: ConfluenceTreeNode[];
}

Expand Down Expand Up @@ -192,9 +201,9 @@ export class Publisher {
): Promise<FilePublishResult> {
try {
const successfulUploadResult = await this.updatePageContent(
node.parentPageId,
node.ancestors,
node.version,
node.existingAdf,
node.existingPageData,
node.file,
node.lastUpdatedBy
);
Expand All @@ -219,9 +228,9 @@ export class Publisher {
}

private async updatePageContent(
parentPageId: string,
ancestors: string[],
pageVersionNumber: number,
currentContents: JSONDocNode,
existingPageData: ConfluencePageExistingData,
adfFile: ConfluenceAdfFile,
lastUpdatedBy: string
): Promise<UploadAdfFileResult> {
Expand All @@ -230,6 +239,11 @@ export class Publisher {
`Page last updated by another user. Won't publish over their changes. MyAccountId: ${this.myAccountId}, Last Updated By: ${lastUpdatedBy}`
);
}
if (existingPageData.contentType !== adfFile.contentType) {
throw new Error(
`Cannot convert between content types. From ${existingPageData.contentType} to ${adfFile.contentType}`
);
}

const result: UploadAdfFileResult = {
adfFile,
Expand Down Expand Up @@ -266,25 +280,46 @@ export class Publisher {
(imageResult["uploaded"] ?? 0) > 0 ? "updated" : "same";
}

if (!adfEqual(currentContents, imageUploadResult.adf)) {
const existingPageDetails = {
title: existingPageData.pageTitle,
type: existingPageData.contentType,
...(adfFile.contentType === "blogpost" ||
adfFile.dontChangeParentPageId
? {}
: { ancestors: existingPageData.ancestors }),
};

const newPageDetails = {
title: adfFile.pageTitle,
type: adfFile.contentType,
...(adfFile.contentType === "blogpost" ||
adfFile.dontChangeParentPageId
? {}
: {
ancestors: ancestors.map((ancestor) => ({
id: ancestor,
})),
}),
};

console.log({ existingPageDetails, newPageDetails });
if (
!adfEqual(existingPageData.adfContent, imageUploadResult.adf) ||
!isEqual(existingPageDetails, newPageDetails)
) {
result.contentResult = "updated";
console.log(`TESTING DIFF - ${adfFile.absoluteFilePath}`);

const replacer = (_key: unknown, value: unknown) =>
typeof value === "undefined" ? null : value;

console.log(JSON.stringify(currentContents, replacer));
console.log(JSON.stringify(existingPageData.adfContent, replacer));
console.log(JSON.stringify(imageUploadResult.adf, replacer));

const updateContentDetails = {
...newPageDetails,
id: adfFile.pageId,
...(adfFile.contentType === "blogpost" ||
adfFile.dontChangeParentPageId
? {}
: { ancestors: [{ id: parentPageId }] }),
version: { number: pageVersionNumber + 1 },
title: adfFile.pageTitle,
type: adfFile.contentType,
body: {
// eslint-disable-next-line @typescript-eslint/naming-convention
atlas_doc_format: {
Expand Down
72 changes: 51 additions & 21 deletions packages/lib/src/TreeConfluence.ts
Expand Up @@ -15,24 +15,24 @@ const blankPageAdf: string = JSON.stringify(doc(p("Page not published yet")));

function flattenTree(
node: ConfluenceTreeNode,
parentPageId?: string
ancestors: string[] = []
): ConfluenceNode[] {
const nodes: ConfluenceNode[] = [];
const { file, version, lastUpdatedBy, existingAdf, children } = node;
const { file, version, lastUpdatedBy, existingPageData, children } = node;

if (parentPageId) {
if (ancestors.length > 0) {
nodes.push({
file,
version,
lastUpdatedBy,
existingAdf,
parentPageId: parentPageId,
existingPageData,
ancestors,
});
}

if (children) {
children.forEach((child) => {
nodes.push(...flattenTree(child, file.pageId));
nodes.push(...flattenTree(child, [...ancestors, file.pageId]));
});
}

Expand Down Expand Up @@ -81,7 +81,10 @@ async function createFileStructureInConfluence(
}

let version: number;
let existingAdf: JSONDocNode | undefined;
let adfContent: JSONDocNode | undefined;
let pageTitle = "";
let contentType = "page";
let ancestors: { id: string }[] = [];
let lastUpdatedBy: string | undefined;
const file: ConfluenceAdfFile = {
...node.file,
Expand All @@ -102,13 +105,17 @@ async function createFileStructureInConfluence(
file.pageId = pageDetails.id;
file.spaceKey = pageDetails.spaceKey;
version = pageDetails.version;
existingAdf = JSON.parse(
pageDetails.existingAdf ?? "{}"
) as JSONDocNode;
adfContent = JSON.parse(pageDetails.existingAdf ?? "{}") as JSONDocNode;
pageTitle = pageDetails.pageTitle;
ancestors = pageDetails.ancestors;
lastUpdatedBy = pageDetails.lastUpdatedBy;
contentType = pageDetails.contentType;
} else {
version = 0;
existingAdf = doc(p());
adfContent = doc(p());
pageTitle = "";
ancestors = [];
contentType = "page";
}

const childDetailsTasks = node.children.map((childNode) => {
Expand All @@ -131,8 +138,13 @@ async function createFileStructureInConfluence(
file: { ...file, pageUrl },
version,
lastUpdatedBy: lastUpdatedBy ?? "",
existingAdf,
children: childDetails,
existingPageData: {
adfContent,
pageTitle,
ancestors,
contentType,
},
};
}

Expand Down Expand Up @@ -167,7 +179,13 @@ async function ensurePageExists(
contentById?.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: contentById?.body?.atlas_doc_format?.value,
spaceKey: contentById.space.key,
};
pageTitle: contentById.title,
ancestors:
contentById.ancestors?.map((ancestor) => ({
id: ancestor.id,
})) ?? [],
contentType: contentById.type,
} as const;
}

const searchParams = {
Expand Down Expand Up @@ -199,12 +217,18 @@ async function ensurePageExists(
return {
id: currentPage.id,
title: file.pageTitle,
version: currentPage?.version?.number ?? 1,
version: currentPage.version?.number ?? 1,
lastUpdatedBy:
currentPage?.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: currentPage?.body?.atlas_doc_format?.value,
currentPage.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: currentPage.body?.atlas_doc_format?.value,
pageTitle: currentPage.title,
spaceKey,
};
ancestors:
currentPage.ancestors?.map((ancestor) => ({
id: ancestor.id,
})) ?? [],
contentType: currentPage.type,
} as const;
} else {
const creatingBlankPageRequest = {
space: { key: spaceKey },
Expand Down Expand Up @@ -233,11 +257,17 @@ async function ensurePageExists(
return {
id: pageDetails.id,
title: file.pageTitle,
version: pageDetails?.version?.number ?? 1,
version: pageDetails.version?.number ?? 1,
lastUpdatedBy:
pageDetails?.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: pageDetails?.body?.atlas_doc_format?.value,
pageDetails.version?.by?.accountId ?? "NO ACCOUNT ID",
existingAdf: pageDetails.body?.atlas_doc_format?.value,
pageTitle: pageDetails.title,
ancestors:
pageDetails.ancestors?.map((ancestor) => ({
id: ancestor.id,
})) ?? [],
spaceKey,
};
contentType: pageDetails.type,
} as const;
}
}

0 comments on commit 7e30ca8

Please sign in to comment.