From c4b460a47dc2c75af6dd9b644acb233e2742d75c Mon Sep 17 00:00:00 2001 From: Namco Date: Fri, 22 Jul 2022 14:22:12 +0800 Subject: [PATCH] Avoid double encoding in bitbucket integration Signed-off-by: Namco --- .changeset/pretty-gifts-do.md | 5 +++++ packages/integration/src/bitbucket/core.test.ts | 14 ++++++++++++++ packages/integration/src/bitbucket/core.ts | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changeset/pretty-gifts-do.md diff --git a/.changeset/pretty-gifts-do.md b/.changeset/pretty-gifts-do.md new file mode 100644 index 0000000000..8d939db6b6 --- /dev/null +++ b/.changeset/pretty-gifts-do.md @@ -0,0 +1,5 @@ +--- +'@backstage/integration': patch +--- + +Avoid double encoding of the file path in `getBitbucketDownloadUrl` diff --git a/packages/integration/src/bitbucket/core.test.ts b/packages/integration/src/bitbucket/core.test.ts index 2b16be82d4..23a193e6fe 100644 --- a/packages/integration/src/bitbucket/core.test.ts +++ b/packages/integration/src/bitbucket/core.test.ts @@ -139,6 +139,20 @@ describe('bitbucket core', () => { ); }); + it('does not double encode the filepath', async () => { + const config: BitbucketIntegrationConfig = { + host: 'bitbucket.mycompany.net', + apiBaseUrl: 'https://api.bitbucket.mycompany.net/rest/api/1.0', + }; + const result = await getBitbucketDownloadUrl( + 'https://bitbucket.mycompany.net/projects/backstage/repos/mock/browse/%2Fdocs?at=some-branch', + config, + ); + expect(result).toEqual( + 'https://api.bitbucket.mycompany.net/rest/api/1.0/projects/backstage/repos/mock/archive?format=tgz&at=some-branch&prefix=backstage-mock&path=%2Fdocs', + ); + }); + it('do not add path param if no path is specified for Bitbucket Server', async () => { const defaultBranchResponse = { displayId: 'main', diff --git a/packages/integration/src/bitbucket/core.ts b/packages/integration/src/bitbucket/core.ts index f6018ad27a..5c5533155d 100644 --- a/packages/integration/src/bitbucket/core.ts +++ b/packages/integration/src/bitbucket/core.ts @@ -100,7 +100,9 @@ export async function getBitbucketDownloadUrl( // path will limit the downloaded content // /docs will only download the docs folder and everything below it // /docs/index.md will download the docs folder and everything below it - const path = filepath ? `&path=${encodeURIComponent(filepath)}` : ''; + const path = filepath + ? `&path=${encodeURIComponent(decodeURIComponent(filepath))}` + : ''; const archiveUrl = isHosted ? `${protocol}://${resource}/${project}/${repoName}/get/${branch}.tar.gz` : `${config.apiBaseUrl}/projects/${project}/repos/${repoName}/archive?format=tgz&at=${branch}&prefix=${project}-${repoName}${path}`;