Skip to content

Commit

Permalink
feat(github): update pr baseBranch (#22663)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
RahulGautamSingh and rarkins committed Jun 17, 2023
1 parent 086b9a7 commit 4665f1d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,22 @@ describe('modules/platform/github/index', () => {

await expect(github.updatePr(pr)).toResolve();
});

it('should update target branch', async () => {
const pr: UpdatePrConfig = {
number: 1234,
prTitle: 'The New Title',
prBody: 'Hello world again',
state: 'closed',
targetBranch: 'new_base',
};
const scope = httpMock.scope(githubApiHost);
initRepoMock(scope, 'some/repo');
await github.initRepo({ repository: 'some/repo' });
scope.patch('/repos/some/repo/pulls/1234').reply(200, pr);

await expect(github.updatePr(pr)).toResolve();
});
});

describe('mergePr(prNo)', () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,13 +1602,17 @@ export async function updatePr({
prTitle: title,
prBody: rawBody,
state,
targetBranch,
}: UpdatePrConfig): Promise<void> {
logger.debug(`updatePr(${prNo}, ${title}, body)`);
const body = sanitize(rawBody);
const patchBody: any = { title };
if (body) {
patchBody.body = body;
}
if (targetBranch) {
patchBody.base = targetBranch;
}
if (state) {
patchBody.state = state;
}
Expand Down
1 change: 1 addition & 0 deletions lib/modules/platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export interface UpdatePrConfig {
prTitle: string;
prBody?: string;
state?: 'open' | 'closed';
targetBranch?: string;
}
export interface EnsureIssueConfig {
title: string;
Expand Down
28 changes: 28 additions & 0 deletions lib/workers/repository/update/pr/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('workers/repository/update/pr/index', () => {
title: prTitle,
bodyStruct,
state: 'open',
targetBranch: 'base',
};

const config: BranchConfig = {
Expand Down Expand Up @@ -304,6 +305,32 @@ describe('workers/repository/update/pr/index', () => {
);
});

it('updates PR target branch if base branch changed in config', async () => {
platform.getBranchPr.mockResolvedValueOnce(pr);

const res = await ensurePr({ ...config, baseBranch: 'new_base' }); // user changed base branch in config

expect(platform.updatePr).toHaveBeenCalled();
expect(platform.createPr).not.toHaveBeenCalled();
expect(prCache.setPrCache).toHaveBeenCalled();
expect(logger.logger.info).toHaveBeenCalledWith(
{ pr: pr.number, prTitle },
`PR updated`
);
expect(logger.logger.debug).toHaveBeenCalledWith(
{
branchName: 'renovate-branch',
oldBaseBranch: 'base',
newBaseBranch: 'new_base',
},
'PR base branch has changed'
);
expect(res).toEqual({
type: 'with-pr',
pr: { ...pr, targetBranch: 'new_base' }, // updated target branch of pr
});
});

it('ignores reviewable content ', async () => {
// See: https://reviewable.io/

Expand Down Expand Up @@ -892,6 +919,7 @@ describe('workers/repository/update/pr/index', () => {
title: prTitle,
bodyStruct,
state: 'open',
targetBranch: 'base',
},
});
expect(logger.logger.debug).toHaveBeenCalledWith(
Expand Down
16 changes: 15 additions & 1 deletion lib/workers/repository/update/pr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export async function ensurePr(
const newPrTitle = stripEmojis(prTitle);
const newPrBodyHash = hashBody(prBody);
if (
existingPr?.targetBranch === config.baseBranch &&
existingPrTitle === newPrTitle &&
existingPrBodyHash === newPrBodyHash
) {
Expand All @@ -353,6 +354,16 @@ export async function ensurePr(
return { type: 'with-pr', pr: existingPr };
}
// PR must need updating
if (existingPr?.targetBranch !== config.baseBranch) {
logger.debug(
{
branchName,
oldBaseBranch: existingPr?.targetBranch,
newBaseBranch: config.baseBranch,
},
'PR base branch has changed'
);
}
if (existingPrTitle !== newPrTitle) {
logger.debug(
{
Expand All @@ -370,6 +381,7 @@ export async function ensurePr(
'PR body changed'
);
}

if (GlobalConfig.get('dryRun')) {
logger.info(`DRY-RUN: Would update PR #${existingPr.number}`);
return { type: 'with-pr', pr: existingPr };
Expand All @@ -379,6 +391,7 @@ export async function ensurePr(
prTitle,
prBody,
platformOptions: getPlatformPrOptions(config),
targetBranch: config.baseBranch,
});
logger.info({ pr: existingPr.number, prTitle }, `PR updated`);
setPrCache(branchName, prBodyFingerprint, true);
Expand All @@ -389,6 +402,7 @@ export async function ensurePr(
...existingPr,
bodyStruct: getPrBodyStruct(prBody),
title: prTitle,
targetBranch: config.baseBranch,
},
};
}
Expand All @@ -412,7 +426,7 @@ export async function ensurePr(
}
pr = await platform.createPr({
sourceBranch: branchName,
targetBranch: config.baseBranch ?? '',
targetBranch: config.baseBranch,
prTitle,
prBody,
labels: prepareLabels(config),
Expand Down
2 changes: 2 additions & 0 deletions lib/workers/repository/update/pr/pr-fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface FilteredBranchUpgradeConfig {
export interface PrBodyFingerprintConfig {
// BranchConfig - filtered
automerge?: boolean;
baseBranch?: string;
automergeSchedule?: string[];
hasReleaseNotes?: boolean;
isPin?: boolean;
Expand Down Expand Up @@ -63,6 +64,7 @@ export function generatePrBodyFingerprintConfig(
return {
automerge: config.automerge,
automergeSchedule: config.automergeSchedule,
baseBranch: config.baseBranch,
filteredUpgrades,
hasReleaseNotes: config.hasReleaseNotes,
isPin: config.isPin,
Expand Down

0 comments on commit 4665f1d

Please sign in to comment.