Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(bitbucket-cloud): Preserve Bitbucket Cloud reviewers when updatin…
…g PRs (#6560)
  • Loading branch information
daekene committed Jul 6, 2020
1 parent ed13e22 commit e015875
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
44 changes: 42 additions & 2 deletions lib/platform/bitbucket/__snapshots__/index.spec.ts.snap
Expand Up @@ -1344,12 +1344,23 @@ Array [
"url": "https://api.bitbucket.org/2.0/repositories/some/repo",
},
Object {
"body": "{\\"title\\":\\"title\\",\\"description\\":\\"body\\"}",
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"content-length": 38,
"host": "api.bitbucket.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.bitbucket.org/2.0/repositories/some/repo/pullrequests/5",
},
Object {
"body": "{\\"title\\":\\"title\\",\\"description\\":\\"body\\",\\"reviewers\\":[{\\"display_name\\":\\"Jane Smith\\",\\"uuid\\":\\"{90b6646d-1724-4a64-9fd9-539515fe94e9}\\"}]}",
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"content-length": 130,
"content-type": "application/json",
"host": "api.bitbucket.org",
"user-agent": "https://github.com/renovatebot/renovate",
Expand All @@ -1359,3 +1370,32 @@ Array [
},
]
`;

exports[`platform/bitbucket updatePr() throws an error on failure to get current list of reviewers 1`] = `"Response code 500 (Internal Server Error)"`;

exports[`platform/bitbucket updatePr() throws an error on failure to get current list of reviewers 2`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"authorization": "Basic YWJjOjEyMw==",
"host": "api.bitbucket.org",
"user-agent": "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",
"authorization": "Basic YWJjOjEyMw==",
"host": "api.bitbucket.org",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://api.bitbucket.org/2.0/repositories/some/repo/pullrequests/5",
},
]
`;
20 changes: 19 additions & 1 deletion lib/platform/bitbucket/index.spec.ts
Expand Up @@ -773,11 +773,29 @@ describe('platform/bitbucket', () => {

describe('updatePr()', () => {
it('puts PR', async () => {
const reviewer = {
display_name: 'Jane Smith',
uuid: '{90b6646d-1724-4a64-9fd9-539515fe94e9}',
};
const scope = await initRepoMock();
scope.put('/2.0/repositories/some/repo/pullrequests/5').reply(200);
scope
.get('/2.0/repositories/some/repo/pullrequests/5')
.reply(200, { reviewers: [reviewer] })
.put('/2.0/repositories/some/repo/pullrequests/5')
.reply(200);
await bitbucket.updatePr(5, 'title', 'body');
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws an error on failure to get current list of reviewers', async () => {
const scope = await initRepoMock();
scope
.get('/2.0/repositories/some/repo/pullrequests/5')
.reply(500, undefined);
await expect(() =>
bitbucket.updatePr(5, 'title', 'body')
).rejects.toThrowErrorMatchingSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});

describe('mergePr()', () => {
Expand Down
14 changes: 13 additions & 1 deletion lib/platform/bitbucket/index.ts
Expand Up @@ -281,6 +281,7 @@ interface PrResponse {
name: string;
};
};
reviewers: Array<any>;
}

// Gets details for a PR
Expand Down Expand Up @@ -785,10 +786,21 @@ export async function updatePr(
description: string
): Promise<void> {
logger.debug(`updatePr(${prNo}, ${title}, body)`);
// Updating a PR in Bitbucket will clear the reviewers if reviewers is not present
const pr = (
await bitbucketHttp.getJson<PrResponse>(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`
)
).body;

await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`,
{
body: { title, description: sanitize(description) },
body: {
title,
description: sanitize(description),
reviewers: pr.reviewers,
},
}
);
}
Expand Down

0 comments on commit e015875

Please sign in to comment.