From b3252ae86cd4d1d219afcec9787cf19fd2af6a31 Mon Sep 17 00:00:00 2001 From: Oleg Krivtsov <olegkrivtsov@gmail.com> Date: Mon, 29 Nov 2021 14:55:53 +0700 Subject: [PATCH] feat(platform/bitbucket): modify getJsonFile to use branchOrTag on Bitbucket (#12825) Co-authored-by: Rhys Arkins <rhys@arkins.net> Co-authored-by: Michael Kriese <michael.kriese@visualon.de> --- .../__snapshots__/index.spec.ts.snap | 43 +++++++++++++++++-- lib/platform/bitbucket/index.spec.ts | 22 +++++++++- lib/platform/bitbucket/index.ts | 14 +++++- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/lib/platform/bitbucket/__snapshots__/index.spec.ts.snap b/lib/platform/bitbucket/__snapshots__/index.spec.ts.snap index 5a3567ff52..3d6b461911 100644 --- a/lib/platform/bitbucket/__snapshots__/index.spec.ts.snap +++ b/lib/platform/bitbucket/__snapshots__/index.spec.ts.snap @@ -912,7 +912,7 @@ Array [ ] `; -exports[`platform/bitbucket/index getJsonFile() ignores branchOrTag 1`] = ` +exports[`platform/bitbucket/index getJsonFile() returns file content 1`] = ` Array [ Object { "headers": Object { @@ -938,7 +938,7 @@ Array [ ] `; -exports[`platform/bitbucket/index getJsonFile() returns file content 1`] = ` +exports[`platform/bitbucket/index getJsonFile() returns file content from branch or tag 1`] = ` Array [ Object { "headers": Object { @@ -959,7 +959,44 @@ Array [ "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", }, "method": "GET", - "url": "https://api.bitbucket.org/2.0/repositories/some/repo/src/HEAD/file.json", + "url": "https://api.bitbucket.org/2.0/repositories/some/repo/src/dev/file.json", + }, +] +`; + +exports[`platform/bitbucket/index getJsonFile() returns file content from branch with a slash in its name 1`] = ` +Array [ + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Basic YWJjOjEyMw==", + "host": "api.bitbucket.org", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://api.bitbucket.org/2.0/repositories/some/repo", + }, + Object { + "headers": Object { + "accept": "application/json", + "accept-encoding": "gzip, deflate, br", + "authorization": "Basic YWJjOjEyMw==", + "host": "api.bitbucket.org", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://api.bitbucket.org/2.0/repositories/some/repo/refs/branches/feat/123-test", + }, + Object { + "headers": Object { + "accept-encoding": "gzip, deflate, br", + "authorization": "Basic YWJjOjEyMw==", + "host": "api.bitbucket.org", + "user-agent": "RenovateBot/0.0.0-semantic-release (https://github.com/renovatebot/renovate)", + }, + "method": "GET", + "url": "https://api.bitbucket.org/2.0/repositories/some/repo/src/1234567890/file.json", }, ] `; diff --git a/lib/platform/bitbucket/index.spec.ts b/lib/platform/bitbucket/index.spec.ts index 05cc557e71..2027b0fdcc 100644 --- a/lib/platform/bitbucket/index.spec.ts +++ b/lib/platform/bitbucket/index.spec.ts @@ -966,17 +966,35 @@ describe('platform/bitbucket/index', () => { expect(httpMock.getTrace()).toMatchSnapshot(); }); - it('ignores branchOrTag', async () => { + it('returns file content from branch or tag', async () => { const data = { foo: 'bar' }; const scope = await initRepoMock(); scope - .get('/2.0/repositories/some/repo/src/HEAD/file.json') + .get('/2.0/repositories/some/repo/src/dev/file.json') .reply(200, JSON.stringify(data)); const res = await bitbucket.getJsonFile('file.json', 'some/repo', 'dev'); expect(res).toEqual(data); expect(httpMock.getTrace()).toMatchSnapshot(); }); + it('returns file content from branch with a slash in its name', async () => { + const data = { foo: 'bar' }; + const scope = await initRepoMock(); + scope + .get('/2.0/repositories/some/repo/refs/branches/feat/123-test') + .reply(200, JSON.stringify({ target: { hash: '1234567890' } })); + scope + .get('/2.0/repositories/some/repo/src/1234567890/file.json') + .reply(200, JSON.stringify(data)); + const res = await bitbucket.getJsonFile( + 'file.json', + 'some/repo', + 'feat/123-test' + ); + expect(res).toEqual(data); + expect(httpMock.getTrace()).toMatchSnapshot(); + }); + it('throws on malformed JSON', async () => { const scope = await initRepoMock(); scope diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts index 31b36a17ff..1b9803120d 100644 --- a/lib/platform/bitbucket/index.ts +++ b/lib/platform/bitbucket/index.ts @@ -48,6 +48,8 @@ let config: utils.Config = {} as any; const defaults = { endpoint: BITBUCKET_PROD_ENDPOINT }; +const pathSeparator = '/'; + let renovateUserUuid: string; export async function initPlatform({ @@ -116,7 +118,17 @@ export async function getRawFile( // See: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/src/%7Bcommit%7D/%7Bpath%7D const repo = repoName ?? config.repository; const path = fileName; - const url = `/2.0/repositories/${repo}/src/HEAD/${path}`; + + let finalBranchOrTag = branchOrTag; + if (branchOrTag?.includes(pathSeparator)) { + // Branch name contans slash, so we have to replace branch name with SHA1 of the head commit; otherwise the API will not work. + finalBranchOrTag = await getBranchCommit(branchOrTag); + } + + const url = + `/2.0/repositories/${repo}/src/` + + (finalBranchOrTag || `HEAD`) + + `/${path}`; const res = await bitbucketHttp.get(url); return res.body; } -- GitLab