conflutora
Version:
Provider to convert and publish Antora documentation on Confluence
171 lines • 7.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfluenceClient = void 0;
const confluence_js_1 = require("confluence.js");
const Logger_js_1 = require("../utils/Logger.js");
const config_service_js_1 = require("../config/config.service.js");
const enum_js_1 = require("../common/enum.js");
class ConfluenceClient extends confluence_js_1.BaseClient {
htmlParserService;
uniqueProjectId;
logger = new Logger_js_1.Logger(this.constructor.name);
content = new confluence_js_1.Api.Content(this);
contentProperties = new confluence_js_1.Api.ContentProperties(this);
attachment = new confluence_js_1.Api.ContentAttachments(this);
spaceKey;
ancestorId;
publisherName;
constructor(htmlParserService, uniqueProjectId) {
super({
host: config_service_js_1.configService.get('confluence.url'),
authentication: {
basic: {
username: config_service_js_1.configService.get('confluence.username'),
password: config_service_js_1.configService.get('confluence.password')
}
}
});
this.htmlParserService = htmlParserService;
this.uniqueProjectId = uniqueProjectId;
this.spaceKey = config_service_js_1.configService.get('confluence.spaceKey');
this.ancestorId = config_service_js_1.configService.get('confluence.ancestorId');
this.publisherName = config_service_js_1.configService.get('application.name');
}
toUpdateContentPayload(page) {
const title = this.uniqueProjectId ? `${page.getName()} (${this.uniqueProjectId})` : page.getName();
return {
type: 'page',
id: page.id,
title,
body: {
storage: {
value: this.htmlParserService.convertToConfluenceXML(page.content),
representation: 'storage'
}
},
version: {
number: page.confluenceVersion + 1,
message: `Automatically updated by ${config_service_js_1.configService.get('application.name')}`
}
};
}
toCreateContentPayload(page) {
const ancestorId = page.confluenceParentId ?? this.ancestorId;
const title = this.uniqueProjectId ? `${page.getName()} (${this.uniqueProjectId})` : page.getName();
return {
type: 'page',
title,
ancestors: ancestorId ? [{ id: ancestorId }] : undefined,
space: { key: this.spaceKey },
body: {
storage: {
value: this.htmlParserService.convertToConfluenceXML(page.content),
representation: 'storage'
}
},
version: {
message: `Automatically created by ${config_service_js_1.configService.get('application.name')}`
}
};
}
async addPageProperty(pageId, propertyKey, propertyValue) {
try {
await this.contentProperties.createContentProperty({
id: pageId,
key: propertyKey,
value: propertyValue
});
this.logger.debug(`[AddProperty] Property ${propertyKey}:${propertyValue} added to page ID ${pageId}`);
}
catch (error) {
this.logger.error(`[AddProperty] Error adding property to page: ${pageId}`, error.stack);
}
}
async createPage(page) {
try {
const content = await this.content.createContent(this.toCreateContentPayload(page));
await this.addPageProperty(content.id, 'publisher', this.publisherName);
await this.addPageProperty(content.id, enum_js_1.PageIdentifier.LOCAL_HASH_TAG_ID, page.id);
return content;
}
catch (error) {
this.logger.error(`[CreatePage] Error creating page: ${page.title}`, {
data: error.config?.data,
response: error.response?.data,
stack: error.stack
});
}
}
async updatePage(page) {
try {
const currentPage = await this.getPageById(page.id);
page.setVersion(currentPage.version?.number);
await this.content.updateContent(this.toUpdateContentPayload(page));
this.logger.info(`Page ${page.title} updated to version ${page.confluenceVersion + 1}`);
}
catch (error) {
this.logger.error(`Error updating page: ${page.title}`, error.stack);
}
}
async deletePage(id) {
try {
await this.content.deleteContent({ id });
this.logger.info(`Page ${id} deleted`);
}
catch (error) {
this.logger.error(`Error deleting page: ${id}`, error.stack);
}
}
async getPageById(id) {
return await this.content.getContentById({ id });
}
async findPageByTitle(title) {
const result = await this.content.getContent({
type: 'page',
spaceKey: this.spaceKey,
title,
expand: [confluence_js_1.Parameters.GetContent.Expand.PropertiesMetadata]
});
const page = result.results.length > 0 ? result.results[0] : null;
if (!page)
return null;
// @ts-expect-error - This is a hack to get the properties from the metadata
const propertiesMeta = page.metadata.properties ?? {};
return propertiesMeta._expandable[enum_js_1.PageIdentifier.LOCAL_HASH_TAG_ID] ? page : null;
}
async getPageByProperty(id) {
const pages = await this.content.getContent({ type: 'page', spaceKey: this.spaceKey, expand: [confluence_js_1.Parameters.GetContent.Expand.PropertiesMetadata] });
return pages.results.find(page => {
// @ts-expect-error - This is a hack to get the properties from the metadata
const propertiesMeta = page.metadata.properties ?? {};
// TODO: Fix this, Confluence does not returns the values in the properties - Must use this.getPageProperties()
return propertiesMeta._expandable[enum_js_1.PageIdentifier.LOCAL_HASH_TAG_ID]?.value === id;
}) ?? null;
}
async getPageProperties(id) {
const properties = await this.contentProperties.getContentProperties({ id });
return properties.results.map(p => ({ key: p.key, value: p.value }));
}
async addAttachment(pageId, fileName, file, comment) {
try {
await this.attachment.createAttachments({
id: pageId,
attachments: [{
filename: fileName,
file,
comment,
minorEdit: true
}]
});
this.logger.info(`Attachment ${fileName} added to page ID ${pageId}`);
}
catch (error) {
this.logger.error(`Error adding attachment to page: ${pageId}`, error.stack);
}
}
async getAttachment(pageId, fileName) {
return await this.attachment.getAttachments({ id: pageId, filename: fileName });
}
}
exports.ConfluenceClient = ConfluenceClient;
//# sourceMappingURL=confluence.client.js.map