-
Notifications
You must be signed in to change notification settings - Fork 31
/
TreeConfluence.ts
273 lines (249 loc) · 6.51 KB
/
TreeConfluence.ts
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import {
ConfluenceAdfFile,
ConfluenceNode,
ConfluenceTreeNode,
LocalAdfFile,
LocalAdfFileTreeNode,
} from "./Publisher";
import { doc, p } from "@atlaskit/adf-utils/builders";
import { RequiredConfluenceClient, LoaderAdaptor } from "./adaptors";
import { JSONDocNode } from "@atlaskit/editor-json-transformer";
import { prepareAdfToUpload } from "./AdfProcessing";
import { ConfluenceSettings } from "./Settings";
const blankPageAdf: string = JSON.stringify(doc(p("Page not published yet")));
function flattenTree(
node: ConfluenceTreeNode,
ancestors: string[] = []
): ConfluenceNode[] {
const nodes: ConfluenceNode[] = [];
const { file, version, lastUpdatedBy, existingPageData, children } = node;
if (ancestors.length > 0) {
nodes.push({
file,
version,
lastUpdatedBy,
existingPageData,
ancestors,
});
}
if (children) {
children.forEach((child) => {
nodes.push(...flattenTree(child, [...ancestors, file.pageId]));
});
}
return nodes;
}
export async function ensureAllFilesExistInConfluence(
confluenceClient: RequiredConfluenceClient,
adaptor: LoaderAdaptor,
node: LocalAdfFileTreeNode,
spaceKey: string,
parentPageId: string,
topPageId: string,
settings: ConfluenceSettings
): Promise<ConfluenceNode[]> {
const confluenceNode = await createFileStructureInConfluence(
settings,
confluenceClient,
adaptor,
node,
spaceKey,
parentPageId,
topPageId,
false
);
const pages = flattenTree(confluenceNode);
prepareAdfToUpload(pages, settings);
return pages;
}
async function createFileStructureInConfluence(
settings: ConfluenceSettings,
confluenceClient: RequiredConfluenceClient,
adaptor: LoaderAdaptor,
node: LocalAdfFileTreeNode,
spaceKey: string,
parentPageId: string,
topPageId: string,
createPage: boolean
): Promise<ConfluenceTreeNode> {
if (!node.file) {
throw new Error("Missing file on node");
}
let version: number;
let adfContent: JSONDocNode | undefined;
let pageTitle = "";
let contentType = "page";
let ancestors: { id: string }[] = [];
let lastUpdatedBy: string | undefined;
const file: ConfluenceAdfFile = {
...node.file,
pageId: parentPageId,
spaceKey,
pageUrl: "",
};
if (createPage) {
const pageDetails = await ensurePageExists(
confluenceClient,
adaptor,
node.file,
spaceKey,
parentPageId,
topPageId
);
file.pageId = pageDetails.id;
file.spaceKey = pageDetails.spaceKey;
version = pageDetails.version;
adfContent = JSON.parse(pageDetails.existingAdf ?? "{}") as JSONDocNode;
pageTitle = pageDetails.pageTitle;
ancestors = pageDetails.ancestors;
lastUpdatedBy = pageDetails.lastUpdatedBy;
contentType = pageDetails.contentType;
} else {
version = 0;
adfContent = doc(p());
pageTitle = "";
ancestors = [];
contentType = "page";
}
const childDetailsTasks = node.children.map((childNode) => {
return createFileStructureInConfluence(
settings,
confluenceClient,
adaptor,
childNode,
spaceKey,
file.pageId,
topPageId,
true
);
});
const childDetails = await Promise.all(childDetailsTasks);
const pageUrl = `${settings.confluenceBaseUrl}/wiki/spaces/${spaceKey}/pages/${file.pageId}/`;
return {
file: { ...file, pageUrl },
version,
lastUpdatedBy: lastUpdatedBy ?? "",
children: childDetails,
existingPageData: {
adfContent,
pageTitle,
ancestors,
contentType,
},
};
}
async function ensurePageExists(
confluenceClient: RequiredConfluenceClient,
adaptor: LoaderAdaptor,
file: LocalAdfFile,
spaceKey: string,
parentPageId: string,
topPageId: string
) {
if (file.pageId) {
const contentById = await confluenceClient.content.getContentById({
id: file.pageId,
expand: ["version", "body.atlas_doc_format", "ancestors", "space"],
});
if (!contentById.space?.key) {
throw new Error("Missing Space Key");
}
await adaptor.updateMarkdownValues(file.absoluteFilePath, {
publish: true,
pageId: contentById.id,
});
return {
id: contentById.id,
title: file.pageTitle,
version: contentById?.version?.number ?? 1,
lastUpdatedBy:
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 = {
type: file.contentType,
spaceKey,
title: file.pageTitle,
expand: ["version", "body.atlas_doc_format", "ancestors"],
};
const contentByTitle = await confluenceClient.content.getContent(
searchParams
);
const currentPage = contentByTitle.results[0];
if (currentPage) {
if (
file.contentType === "page" &&
!currentPage.ancestors?.some((ancestor) => ancestor.id == topPageId)
) {
throw new Error(
`${file.pageTitle} is trying to overwrite a page outside the page tree from the selected top page`
);
}
await adaptor.updateMarkdownValues(file.absoluteFilePath, {
publish: true,
pageId: currentPage.id,
});
return {
id: currentPage.id,
title: file.pageTitle,
version: currentPage.version?.number ?? 1,
lastUpdatedBy:
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 },
...(file.contentType === "page"
? { ancestors: [{ id: parentPageId }] }
: {}),
title: file.pageTitle,
type: file.contentType,
body: {
// eslint-disable-next-line @typescript-eslint/naming-convention
atlas_doc_format: {
value: blankPageAdf,
representation: "atlas_doc_format",
},
},
expand: ["version", "body.atlas_doc_format", "ancestors"],
};
const pageDetails = await confluenceClient.content.createContent(
creatingBlankPageRequest
);
await adaptor.updateMarkdownValues(file.absoluteFilePath, {
publish: true,
pageId: pageDetails.id,
});
return {
id: pageDetails.id,
title: file.pageTitle,
version: pageDetails.version?.number ?? 1,
lastUpdatedBy:
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;
}
}