Merge pull request #30431 from Fuglen/fuglen/confluence-to-markdown

Improve createConfluenceVariables with support for spaces links
This commit is contained in:
Ben Lambert
2025-07-08 12:30:38 +02:00
committed by GitHub
4 changed files with 37 additions and 23 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-confluence-to-markdown': patch
---
Added support for new link format for on-prem Confluence
@@ -106,7 +106,7 @@ spec:
properties:
confluenceUrls:
type: array
description: Urls for Confluence doc to be converted to markdown. In format <CONFLUENCE_BASE_URL>/display/<SPACEKEY>/<PAGE+TITLE> or <CONFLUENCE_BASE_URL>/spaces/<SPACEKEY>/pages/<PAGEID>/<PAGE+TITLE> for Confluence cloud
description: Urls for Confluence doc to be converted to markdown. In format <CONFLUENCE_BASE_URL>/display/<SPACEKEY>/<PAGE+TITLE> or <CONFLUENCE_BASE_URL>/spaces/<SPACEKEY>/pages/<PAGEID>/<PAGE+TITLE>
items:
type: string
ui:options:
@@ -50,6 +50,17 @@ describe('createConfluenceVariables', () => {
expect(title).toEqual('Cloud+Page+Title');
expect(titleWithSpaces).toEqual('Cloud Page Title');
});
it('should return values for Confluence Url using spaces (non-cloud)', () => {
const url =
'https://confluence.example.com/spaces/SPACEKEY/pages/1234567/Confluence+Page+Title';
const { spacekey, title, titleWithSpaces } = createConfluenceVariables(url);
expect(spacekey).toEqual('SPACEKEY');
expect(title).toEqual('Confluence+Page+Title');
expect(titleWithSpaces).toEqual('Confluence Page Title');
});
});
describe('getConfluenceConfig', () => {
@@ -15,7 +15,7 @@
*/
import { Config } from '@backstage/config';
import { ResponseError, ConflictError, InputError } from '@backstage/errors';
import { ResponseError, ConflictError } from '@backstage/errors';
import fs from 'fs-extra';
import { Readable } from 'stream';
@@ -188,26 +188,24 @@ export const createConfluenceVariables = (url: string) => {
let title: string | undefined = undefined;
let titleWithSpaces: string | undefined = '';
const params = new URL(url);
if (params.pathname.split('/')[1] === 'display') {
// https://confluence.example.com/display/SPACEKEY/Page+Title
spacekey = params.pathname.split('/')[2];
title = params.pathname.split('/')[3];
titleWithSpaces = title?.replace(/\+/g, ' ');
return { spacekey, title, titleWithSpaces };
} else if (params.pathname.split('/')[2] === 'display') {
// https://confluence.example.com/prefix/display/SPACEKEY/Page+Title
spacekey = params.pathname.split('/')[3];
title = params.pathname.split('/')[4];
titleWithSpaces = title?.replace(/\+/g, ' ');
return { spacekey, title, titleWithSpaces };
} else if (params.pathname.split('/')[2] === 'spaces') {
// https://example.atlassian.net/wiki/spaces/SPACEKEY/pages/1234567/Page+Title
spacekey = params.pathname.split('/')[3];
title = params.pathname.split('/')[6];
titleWithSpaces = title?.replace(/\+/g, ' ');
return { spacekey, title, titleWithSpaces };
const pathParts = params.pathname.split('/').filter(Boolean);
if (pathParts.includes('display')) {
// /display/<SPACEKEY>/<TITLE>
const idx = pathParts.indexOf('display');
spacekey = pathParts[idx + 1];
title = pathParts[idx + 2];
} else if (pathParts.includes('spaces')) {
// /spaces/<SPACEKEY>/pages/<PAGEID>/<TITLE>
const idx = pathParts.indexOf('spaces');
spacekey = pathParts[idx + 1];
title = pathParts[pathParts.length - 1];
} else {
throw new Error(
'The Url format for Confluence is incorrect. Acceptable format is `<CONFLUENCE_BASE_URL>/display/<SPACEKEY>/<PAGE+TITLE>` or `<CONFLUENCE_BASE_URL>/spaces/<SPACEKEY>/pages/<PAGEID>/<PAGE+TITLE>`',
);
}
throw new InputError(
'The Url format for Confluence is incorrect. Acceptable format is `<CONFLUENCE_BASE_URL>/display/<SPACEKEY>/<PAGE+TITLE>` or `<CONFLUENCE_BASE_URL>/spaces/<SPACEKEY>/pages/<PAGEID>/<PAGE+TITLE>` for Confluence cloud',
);
titleWithSpaces = title?.replace(/\+/g, ' ');
return { spacekey, title, titleWithSpaces };
};