Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bitbucket-cloud): Preserve Bitbucket Cloud reviewers when updating PRs #6560

Merged
merged 1 commit into from Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
rarkins marked this conversation as resolved.
Show resolved Hide resolved
.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>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we be more specific here? Looks like reviewers: {username: string}[] might be more appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type returned here is a bit more complicated than that. It can return different fields, some that are populated and some that are not. I felt that a simple solution would be to put any here since we don't really care what the values are, we just immediately return them to Bitbucket in the very next call.

Here's are links to the relevant API docs:

reviewers0..∞

The list of users that were added as reviewers on this pull request when it was created. For performance reasons, the API only includes this list on a pull request's self URL.
all of
object

Base type for most resource objects. It defines the common type element that identifies an object's type. It also identifies the element as Swagger's discriminator.
typestring

An account object.
...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't care, we should use unknown instead of 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