Skip to content

Commit

Permalink
fix(platform/bitbucket): ensure getPrList() runtime integrity (#32970)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Dec 18, 2024
1 parent 6857f95 commit 3ee4857
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 18 deletions.
137 changes: 132 additions & 5 deletions lib/modules/platform/bitbucket/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,12 @@ describe('modules/platform/bitbucket/index', () => {
.get('/2.0/repositories/some/repo/pullrequests/5')
.reply(200, { reviewers: [reviewer] })
.put('/2.0/repositories/some/repo/pullrequests/5')
.reply(200);
.reply(200, { id: 5 })
.get(`/2.0/repositories/some/repo/pullrequests`)
.query(true)
.reply(200, {
values: [{ id: 5 }],
});
await expect(
bitbucket.updatePr({
number: 5,
Expand Down Expand Up @@ -1736,7 +1741,12 @@ describe('modules/platform/bitbucket/index', () => {
account_status: 'inactive',
})
.put('/2.0/repositories/some/repo/pullrequests/5')
.reply(200);
.reply(200, { id: 5 })
.get(`/2.0/repositories/some/repo/pullrequests`)
.query(true)
.reply(200, {
values: [{ id: 5 }],
});
await expect(
bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' }),
).toResolve();
Expand Down Expand Up @@ -1779,7 +1789,12 @@ describe('modules/platform/bitbucket/index', () => {
)
.reply(200)
.put('/2.0/repositories/some/repo/pullrequests/5')
.reply(200);
.reply(200, { id: 5 })
.get(`/2.0/repositories/some/repo/pullrequests`)
.query(true)
.reply(200, {
values: [{ id: 5 }],
});

await expect(
bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' }),
Expand Down Expand Up @@ -1884,9 +1899,14 @@ describe('modules/platform/bitbucket/index', () => {
.get('/2.0/repositories/some/repo/pullrequests/5')
.reply(200, { values: [pr] })
.put('/2.0/repositories/some/repo/pullrequests/5')
.reply(200)
.reply(200, { id: 5 })
.post('/2.0/repositories/some/repo/pullrequests/5/decline')
.reply(200);
.reply(200)
.get(`/2.0/repositories/some/repo/pullrequests`)
.query(true)
.reply(200, {
values: [{ id: 5 }],
});

expect(
await bitbucket.updatePr({
Expand All @@ -1898,6 +1918,113 @@ describe('modules/platform/bitbucket/index', () => {
});
});

describe('maintains pr cache integrity at runtime', () => {
it('pr cache gets updated after a pr is created', async () => {
const projectReviewer = {
type: 'default_reviewer',
reviewer_type: 'project',
user: {
display_name: 'Bob Smith',
uuid: '{d2238482-2e9f-48b3-8630-de22ccb9e42f}',
account_id: '123',
},
};
const repoReviewer = {
type: 'default_reviewer',
reviewer_type: 'repository',
user: {
display_name: 'Jane Smith',
uuid: '{90b6646d-1724-4a64-9fd9-539515fe94e9}',
account_id: '456',
},
};

const scope = httpMock.scope(baseUrl);
scope.get('/2.0/user').reply(200, { uuid: '12345' });
await bitbucket.initPlatform({ username: 'renovate', password: 'pass' });
await initRepoMock(undefined, null, scope);
scope
.get(`/2.0/repositories/some/repo/pullrequests`)
.query(true)
.reply(200, {
values: [
{
id: 1,
author: { uuid: '12345' },
source: { branch: { name: 'branch-a' } },
destination: { branch: { name: 'branch-b' } },
state: 'OPEN',
},
],
})
.get(
'/2.0/repositories/some/repo/effective-default-reviewers?pagelen=100',
)
.reply(200, {
values: [projectReviewer, repoReviewer],
})
.post('/2.0/repositories/some/repo/pullrequests')
.reply(200, { id: 5 });

await bitbucket.getPrList(); // cache is now initialized

await bitbucket.createPr({
sourceBranch: 'branch',
targetBranch: 'master',
prTitle: 'title',
prBody: 'body',
platformPrOptions: {
bbUseDefaultReviewers: true,
},
});

const newPrList = await bitbucket.getPrList();
expect(newPrList).toHaveLength(2);
});

it('pr cache gets updated after a pr is updated', async () => {
const reviewer = {
display_name: 'Jane Smith',
uuid: '{90b6646d-1724-4a64-9fd9-539515fe94e9}',
};
const scope = httpMock.scope(baseUrl);
scope.get('/2.0/user').reply(200, { uuid: '12345' });
await bitbucket.initPlatform({ username: 'renovate', password: 'pass' });
await initRepoMock(undefined, null, scope);
scope
.get(`/2.0/repositories/some/repo/pullrequests`)
.query(true)
.reply(200, {
values: [
{
id: 5,
author: { uuid: '12345' },
source: { branch: { name: 'branch-a' } },
destination: { branch: { name: 'branch-b' } },
state: 'OPEN',
title: 'title',
},
],
})
.get('/2.0/repositories/some/repo/pullrequests/5')
.reply(200, { reviewers: [reviewer] })
.put('/2.0/repositories/some/repo/pullrequests/5')
.reply(200, { id: 5, title: 'newTitle' });

const oldPrList = await bitbucket.getPrList(); // cache is now initialized
expect(oldPrList.find((pr) => pr.title === 'title')).toBeDefined();
await bitbucket.updatePr({
number: 5,
prTitle: 'newTitle',
prBody: 'body',
targetBranch: 'new_base',
});

const newPrList = await bitbucket.getPrList();
expect(newPrList.find((pr) => pr.title === 'newTitle')).toBeDefined();
});
});

describe('mergePr()', () => {
it('posts Merge with optional merge strategy', async () => {
const scope = await initRepoMock();
Expand Down
39 changes: 26 additions & 13 deletions lib/modules/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ export async function updatePr({
)
).body;

let updatedPrRes: PrResponse;
try {
const body: any = {
title,
Expand All @@ -1036,27 +1037,31 @@ export async function updatePr({
};
}

await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`,
{ body },
);
updatedPrRes = (
await bitbucketHttp.putJson<PrResponse>(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`,
{ body },
)
).body;
} catch (err) {
// Try sanitizing reviewers
const sanitizedReviewers = await sanitizeReviewers(pr.reviewers, err);

if (sanitizedReviewers === undefined) {
throw err;
} else {
await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`,
{
body: {
title,
description: sanitize(description),
reviewers: sanitizedReviewers,
updatedPrRes = (
await bitbucketHttp.putJson<PrResponse>(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`,
{
body: {
title,
description: sanitize(description),
reviewers: sanitizedReviewers,
},
},
},
);
)
).body;
}
}

Expand All @@ -1065,6 +1070,14 @@ export async function updatePr({
`/2.0/repositories/${config.repository}/pullrequests/${prNo}/decline`,
);
}

// update pr cache
await BitbucketPrCache.setPr(
bitbucketHttp,
config.repository,
renovateUserUuid,
utils.prInfo({ ...updatedPrRes, ...(state && { state }) }),
);
}

export async function mergePr({
Expand Down

0 comments on commit 3ee4857

Please sign in to comment.