Skip to content

Commit

Permalink
feat(platform): update PR target branch if baseBranch changed (#23010)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Jul 1, 2023
1 parent 21f984f commit 4180370
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 18 deletions.
32 changes: 18 additions & 14 deletions lib/modules/platform/azure/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,49 @@ exports[`modules/platform/azure/index createPr() should create and return a PR o
}
`;

exports[`modules/platform/azure/index createPr() when usePlatformAutomerge is set should create and return a PR object with auto-complete set 1`] = `
exports[`modules/platform/azure/index createPr() should create and return an approved PR object 1`] = `
{
"autoCompleteSetBy": {
"id": "123",
},
"bodyStruct": {
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
"completionOptions": {
"deleteSourceBranch": true,
"mergeCommitMessage": "The Title",
"squashMerge": true,
},
"createdAt": undefined,
"createdBy": {
"id": "123",
"id": 123,
"url": "user-url",
},
"number": 456,
"pullRequestId": 456,
"sourceBranch": undefined,
"sourceRefName": undefined,
"state": "open",
"targetBranch": undefined,
"title": "The Title",
}
`;

exports[`modules/platform/azure/index createPr() should create and return an approved PR object 1`] = `
exports[`modules/platform/azure/index createPr() when usePlatformAutomerge is set should create and return a PR object with auto-complete set 1`] = `
{
"autoCompleteSetBy": {
"id": "123",
},
"bodyStruct": {
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
"completionOptions": {
"deleteSourceBranch": true,
"mergeCommitMessage": "The Title",
"squashMerge": true,
},
"createdAt": undefined,
"createdBy": {
"id": 123,
"url": "user-url",
"id": "123",
},
"number": 456,
"pullRequestId": 456,
"sourceBranch": undefined,
"sourceRefName": undefined,
"state": "open",
"targetBranch": undefined,
"title": "The Title",
}
`;

Expand Down Expand Up @@ -280,6 +280,7 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOption
{
"description": "Hello world again",
"status": 2,
"targetRefName": undefined,
"title": "The New Title",
},
"1",
Expand All @@ -302,6 +303,7 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOption
[
{
"description": "Hello world again",
"targetRefName": undefined,
"title": "The New Title",
},
"1",
Expand All @@ -315,6 +317,7 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOption
[
{
"description": "Hello world again",
"targetRefName": "refs/heads/new_base",
"title": "The New Title",
},
"1",
Expand All @@ -327,6 +330,7 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOption
[
[
{
"targetRefName": undefined,
"title": "The New Title - autoclose",
},
"1",
Expand Down
1 change: 1 addition & 0 deletions lib/modules/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ describe('modules/platform/azure/index', () => {
number: 1234,
prTitle: 'The New Title',
prBody: 'Hello world again',
targetBranch: 'new_base',
});
expect(updatePullRequest.mock.calls).toMatchSnapshot();
});
Expand Down
6 changes: 5 additions & 1 deletion lib/modules/platform/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,14 @@ export async function updatePr({
prBody: body,
state,
platformOptions,
targetBranch,
}: UpdatePrConfig): Promise<void> {
logger.debug(`updatePr(${prNo}, ${title}, body)`);

const azureApiGit = await azureApi.gitApi();
const objToUpdate: GitPullRequest = {
title,
targetRefName: getNewBranchName(targetBranch),
};

if (body) {
Expand All @@ -528,7 +530,9 @@ export async function updatePr({

if (state === 'open') {
await azureApiGit.updatePullRequest(
{ status: PullRequestStatus.Active },
{
status: PullRequestStatus.Active,
},
config.repoId,
prNo
);
Expand Down
1 change: 1 addition & 0 deletions lib/modules/platform/bitbucket-server/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,7 @@ describe('modules/platform/bitbucket-server/index', () => {
number: 5,
prTitle: 'title',
prBody: 'body',
targetBranch: 'new_base',
})
).toResolve();
});
Expand Down
6 changes: 5 additions & 1 deletion lib/modules/platform/bitbucket-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type {
RepoResult,
UpdatePrConfig,
} from '../types';
import { repoFingerprint } from '../util';
import { getNewBranchName, repoFingerprint } from '../util';
import { smartTruncate } from '../utils/pr-body';
import type {
BbsConfig,
Expand Down Expand Up @@ -851,6 +851,7 @@ export async function updatePr({
prBody: rawDescription,
state,
bitbucketInvalidReviewers,
targetBranch,
}: UpdatePrConfig & {
bitbucketInvalidReviewers: string[] | undefined;
}): Promise<void> {
Expand Down Expand Up @@ -878,6 +879,9 @@ export async function updatePr({
(name: string) => !bitbucketInvalidReviewers?.includes(name)
)
.map((name: string) => ({ user: { name } })),
toRef: {
id: getNewBranchName(targetBranch),
},
},
}
);
Expand Down
7 changes: 6 additions & 1 deletion lib/modules/platform/bitbucket/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,12 @@ describe('modules/platform/bitbucket/index', () => {
.put('/2.0/repositories/some/repo/pullrequests/5')
.reply(200);
await expect(
bitbucket.updatePr({ number: 5, prTitle: 'title', prBody: 'body' })
bitbucket.updatePr({
number: 5,
prTitle: 'title',
prBody: 'body',
targetBranch: 'new_base',
})
).toResolve();
});

Expand Down
6 changes: 6 additions & 0 deletions lib/modules/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ export async function updatePr({
prTitle: title,
prBody: description,
state,
targetBranch,
}: UpdatePrConfig): Promise<void> {
logger.debug(`updatePr(${prNo}, ${title}, body)`);
// Updating a PR in Bitbucket will clear the reviewers if reviewers is not present
Expand All @@ -922,6 +923,11 @@ export async function updatePr({
title,
description: sanitize(description),
reviewers: pr.reviewers,
destination: {
branch: {
name: targetBranch,
},
},
},
}
);
Expand Down
16 changes: 16 additions & 0 deletions lib/modules/platform/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,22 @@ describe('modules/platform/gitea/index', () => {
});
});

it('should update pull target branch', async () => {
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
await gitea.updatePr({
number: 1,
prTitle: 'New Title',
targetBranch: 'New Base',
});

expect(helper.updatePR).toHaveBeenCalledTimes(1);
expect(helper.updatePR).toHaveBeenCalledWith(mockRepo.full_name, 1, {
title: 'New Title',
base: 'New Base',
});
});

it('should update pull request with title and body', async () => {
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/platform/gitea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ const platform: Platform = {
prTitle,
prBody: body,
state,
targetBranch,
}: UpdatePrConfig): Promise<void> {
let title = prTitle;
if ((await getPrList()).find((pr) => pr.number === number)?.isDraft) {
Expand All @@ -630,6 +631,7 @@ const platform: Platform = {

await helper.updatePR(config.repository, number, {
title,
base: targetBranch,
...(body && { body }),
...(state && { state }),
});
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/gitea/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ export interface IssueSearchParams {
}

export interface PRCreateParams extends PRUpdateParams {
base?: string;
head?: string;
}

Expand All @@ -179,6 +178,7 @@ export interface PRUpdateParams {
assignees?: string[];
labels?: number[];
state?: PRState;
base?: string;
}

export interface PRSearchParams {
Expand Down
29 changes: 29 additions & 0 deletions lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2190,6 +2190,35 @@ describe('modules/platform/gitlab/index', () => {
).toResolve();
});

it('updates target branch of the PR', async () => {
await initPlatform('13.3.6-ee');
httpMock
.scope(gitlabApiHost)
.get(
'/api/v4/projects/undefined/merge_requests?per_page=100&scope=created_by_me'
)
.reply(200, [
{
iid: 1,
source_branch: 'branch-a',
title: 'branch a pr',
state: 'open',
target_branch: 'branch-b',
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
await expect(
gitlab.updatePr({
number: 1,
prTitle: 'title',
prBody: 'body',
state: 'closed',
targetBranch: 'branch-b',
})
).toResolve();
});

it('closes the PR', async () => {
await initPlatform('13.3.6-ee');
httpMock
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ export async function updatePr({
prBody: description,
state,
platformOptions,
targetBranch,
}: UpdatePrConfig): Promise<void> {
let title = prTitle;
if ((await getPrList()).find((pr) => pr.number === iid)?.isDraft) {
Expand All @@ -718,6 +719,7 @@ export async function updatePr({
title,
description: sanitize(description),
...(newState && { state_event: newState }),
target_branch: targetBranch,
},
}
);
Expand Down

0 comments on commit 4180370

Please sign in to comment.