Skip to content

Commit 96c06e5

Browse files
authored
feat(platform/bitbucket): handle author is also default reviewer (#22978)
1 parent f12576e commit 96c06e5

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

lib/modules/platform/bitbucket/index.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,56 @@ describe('modules/platform/bitbucket/index', () => {
10151015
).rejects.toThrow(new Error('Response code 401 (Unauthorized)'));
10161016
});
10171017

1018+
it('removes reviewer if they are also the author of the pr', async () => {
1019+
const reviewers = [
1020+
{
1021+
user: {
1022+
display_name: 'Bob Smith',
1023+
uuid: '{d2238482-2e9f-48b3-8630-de22ccb9e42f}',
1024+
account_id: '123',
1025+
},
1026+
},
1027+
{
1028+
user: {
1029+
display_name: 'Jane Smith',
1030+
uuid: '{90b6646d-1724-4a64-9fd9-539515fe94e9}',
1031+
account_id: '456',
1032+
},
1033+
},
1034+
];
1035+
const scope = await initRepoMock();
1036+
scope
1037+
.get(
1038+
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100'
1039+
)
1040+
.reply(200, {
1041+
values: reviewers,
1042+
})
1043+
.post('/2.0/repositories/some/repo/pullrequests')
1044+
.reply(400, {
1045+
type: 'error',
1046+
error: {
1047+
fields: {
1048+
reviewers: [
1049+
'Jane Smith is the author and cannot be included as a reviewer.',
1050+
],
1051+
},
1052+
},
1053+
})
1054+
.post('/2.0/repositories/some/repo/pullrequests')
1055+
.reply(200, { id: 5 });
1056+
const pr = await bitbucket.createPr({
1057+
sourceBranch: 'branch',
1058+
targetBranch: 'master',
1059+
prTitle: 'title',
1060+
prBody: 'body',
1061+
platformOptions: {
1062+
bbUseDefaultReviewers: true,
1063+
},
1064+
});
1065+
expect(pr?.number).toBe(5);
1066+
});
1067+
10181068
it('rethrows exception when PR create error due to unknown reviewers error', async () => {
10191069
const reviewer = {
10201070
user: {

lib/modules/platform/bitbucket/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,15 @@ async function sanitizeReviewers(
719719
if (err.statusCode === 400 && err.body?.error?.fields?.reviewers) {
720720
const sanitizedReviewers: Account[] = [];
721721

722+
const MSG_AUTHOR_AND_REVIEWER =
723+
'is the author and cannot be included as a reviewer.';
724+
const MSG_MALFORMED_REVIEWERS_LIST = 'Malformed reviewers list';
725+
const MSG_NOT_WORKSPACE_MEMBER =
726+
'is not a member of this workspace and cannot be added to this pull request';
727+
722728
for (const msg of err.body.error.fields.reviewers) {
723729
// Bitbucket returns a 400 if any of the PR reviewer accounts are now inactive (ie: disabled/suspended)
724-
if (msg === 'Malformed reviewers list') {
730+
if (msg === MSG_MALFORMED_REVIEWERS_LIST) {
725731
logger.debug(
726732
{ err },
727733
'PR contains reviewers that may be either inactive or no longer a member of this workspace. Will try setting only active reviewers'
@@ -741,11 +747,7 @@ async function sanitizeReviewers(
741747
}
742748
}
743749
// Bitbucket returns a 400 if any of the PR reviewer accounts are no longer members of this workspace
744-
} else if (
745-
msg.endsWith(
746-
'is not a member of this workspace and cannot be added to this pull request'
747-
)
748-
) {
750+
} else if (msg.endsWith(MSG_NOT_WORKSPACE_MEMBER)) {
749751
logger.debug(
750752
{ err },
751753
'PR contains reviewer accounts which are no longer member of this workspace. Will try setting only member reviewers'
@@ -757,6 +759,17 @@ async function sanitizeReviewers(
757759
sanitizedReviewers.push(reviewer);
758760
}
759761
}
762+
} else if (msg.endsWith(MSG_AUTHOR_AND_REVIEWER)) {
763+
logger.debug(
764+
{ err },
765+
'PR contains reviewer accounts which are also the author. Will try setting only non-author reviewers'
766+
);
767+
const author = msg.replace(MSG_AUTHOR_AND_REVIEWER, '').trim();
768+
for (const reviewer of reviewers) {
769+
if (reviewer.display_name !== author) {
770+
sanitizedReviewers.push(reviewer);
771+
}
772+
}
760773
} else {
761774
return undefined;
762775
}
@@ -822,6 +835,7 @@ export async function createPr({
822835
).body;
823836
reviewers = reviewersResponse.values.map((reviewer: EffectiveReviewer) => ({
824837
uuid: reviewer.user.uuid,
838+
display_name: reviewer.user.display_name,
825839
}));
826840
}
827841

0 commit comments

Comments
 (0)