From ac81c9813fd766d596068b8684f358aa12e1246d Mon Sep 17 00:00:00 2001 From: Fuglen Date: Wed, 2 Jul 2025 10:42:02 +0200 Subject: [PATCH 1/5] Improve createConfluenceVariables with support for spaces links Signed-off-by: Fuglen --- .../src/actions/confluence/helpers.ts | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts index bbc722b1d5..59c3f00cf3 100644 --- a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts +++ b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts @@ -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'; @@ -184,30 +184,28 @@ export const getAndWriteAttachments = async ( }; export const createConfluenceVariables = (url: string) => { - let spacekey: string | undefined = undefined; - let title: string | undefined = undefined; - let titleWithSpaces: string | undefined = ''; + let spacekey; + let title; + let titleWithSpaces = ''; 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// + 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 }; }; From 642282debe548f89280704454d9ff8bab3a125da Mon Sep 17 00:00:00 2001 From: Fuglen <fuglenbusiness@gmail.com> Date: Wed, 2 Jul 2025 10:50:41 +0200 Subject: [PATCH 2/5] Add changeset Signed-off-by: Fuglen <fuglenbusiness@gmail.com> --- .changeset/giant-taxes-shine.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/giant-taxes-shine.md diff --git a/.changeset/giant-taxes-shine.md b/.changeset/giant-taxes-shine.md new file mode 100644 index 0000000000..8724b658c1 --- /dev/null +++ b/.changeset/giant-taxes-shine.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-confluence-to-markdown': patch +--- + +Added support for new link format for on-prem Confluence From afd1a63fbb304c9684ff55fd486a61aa77878873 Mon Sep 17 00:00:00 2001 From: Fuglen <fuglenbusiness@gmail.com> Date: Wed, 2 Jul 2025 10:55:46 +0200 Subject: [PATCH 3/5] Update description as there is no difference between cloud and non-cloud Signed-off-by: Fuglen <fuglenbusiness@gmail.com> --- .../scaffolder-backend-module-confluence-to-markdown/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend-module-confluence-to-markdown/README.md b/plugins/scaffolder-backend-module-confluence-to-markdown/README.md index d3722f1227..7ea832b80b 100644 --- a/plugins/scaffolder-backend-module-confluence-to-markdown/README.md +++ b/plugins/scaffolder-backend-module-confluence-to-markdown/README.md @@ -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: From ba7de6052c439f1b53c8d0ffee0db8cb5221cd1c Mon Sep 17 00:00:00 2001 From: Fuglen <fuglenbusiness@gmail.com> Date: Mon, 7 Jul 2025 09:51:57 +0200 Subject: [PATCH 4/5] Adding test for non-cloud using spaces Signed-off-by: Fuglen <fuglenbusiness@gmail.com> --- .../src/actions/confluence/helpers.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.test.ts b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.test.ts index 42d83c18a8..9acf40d6c2 100644 --- a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.test.ts +++ b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.test.ts @@ -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', () => { From dc444fab366d8c526a6432b84ce5334c978c9f43 Mon Sep 17 00:00:00 2001 From: Fuglen <fuglenbusiness@gmail.com> Date: Mon, 7 Jul 2025 09:56:58 +0200 Subject: [PATCH 5/5] Re-add string and undefined on variables as I didn't intend to remove them Signed-off-by: Fuglen <fuglenbusiness@gmail.com> --- .../src/actions/confluence/helpers.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts index 59c3f00cf3..04d475677c 100644 --- a/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts +++ b/plugins/scaffolder-backend-module-confluence-to-markdown/src/actions/confluence/helpers.ts @@ -184,9 +184,9 @@ export const getAndWriteAttachments = async ( }; export const createConfluenceVariables = (url: string) => { - let spacekey; - let title; - let titleWithSpaces = ''; + let spacekey: string | undefined = undefined; + let title: string | undefined = undefined; + let titleWithSpaces: string | undefined = ''; const params = new URL(url); const pathParts = params.pathname.split('/').filter(Boolean);