From ac81c9813fd766d596068b8684f358aa12e1246d Mon Sep 17 00:00:00 2001 From: Fuglen Date: Wed, 2 Jul 2025 10:42:02 +0200 Subject: [PATCH] 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 }; };