Skip to content

Commit

Permalink
fix(platform): compare target refs before updating (#23094)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Jul 4, 2023
1 parent 3b6ffa9 commit 1700467
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 49 deletions.
3 changes: 0 additions & 3 deletions lib/modules/platform/azure/__snapshots__/index.spec.ts.snap
Expand Up @@ -280,7 +280,6 @@ 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 @@ -303,7 +302,6 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOption
[
{
"description": "Hello world again",
"targetRefName": undefined,
"title": "The New Title",
},
"1",
Expand All @@ -330,7 +328,6 @@ exports[`modules/platform/azure/index updatePr(prNo, title, body, platformOption
[
[
{
"targetRefName": undefined,
"title": "The New Title - autoclose",
},
"1",
Expand Down
5 changes: 4 additions & 1 deletion lib/modules/platform/azure/index.ts
Expand Up @@ -521,9 +521,12 @@ export async function updatePr({
const azureApiGit = await azureApi.gitApi();
const objToUpdate: GitPullRequest = {
title,
targetRefName: getNewBranchName(targetBranch),
};

if (targetBranch) {
objToUpdate.targetRefName = getNewBranchName(targetBranch);
}

if (body) {
objToUpdate.description = max4000Chars(sanitize(body));
}
Expand Down
30 changes: 15 additions & 15 deletions lib/modules/platform/bitbucket-server/index.ts
Expand Up @@ -864,26 +864,26 @@ export async function updatePr({
throw Object.assign(new Error(REPOSITORY_NOT_FOUND), { statusCode: 404 });
}

const body: any = {
title,
description,
version: pr.version,
reviewers: pr.reviewers
?.filter((name: string) => !bitbucketInvalidReviewers?.includes(name))
.map((name: string) => ({ user: { name } })),
};
if (targetBranch) {
body.toRef = {
id: getNewBranchName(targetBranch),
};
}

const { body: updatedPr } = await bitbucketServerHttp.putJson<{
version: number;
state: string;
}>(
`./rest/api/1.0/projects/${config.projectKey}/repos/${config.repositorySlug}/pull-requests/${prNo}`,
{
body: {
title,
description,
version: pr.version,
reviewers: pr.reviewers
?.filter(
(name: string) => !bitbucketInvalidReviewers?.includes(name)
)
.map((name: string) => ({ user: { name } })),
toRef: {
id: getNewBranchName(targetBranch),
},
},
}
{ body }
);

updatePrVersion(prNo, updatedPr.version);
Expand Down
26 changes: 14 additions & 12 deletions lib/modules/platform/bitbucket/index.ts
Expand Up @@ -945,20 +945,22 @@ export async function updatePr({
).body;

try {
const body: any = {
title,
description: sanitize(description),
reviewers: pr.reviewers,
};
if (targetBranch) {
body.destination = {
branch: {
name: targetBranch,
},
};
}

await bitbucketHttp.putJson(
`/2.0/repositories/${config.repository}/pullrequests/${prNo}`,
{
body: {
title,
description: sanitize(description),
reviewers: pr.reviewers,
destination: {
branch: {
name: targetBranch,
},
},
},
}
{ body }
);
} catch (err) {
// Try sanitizing reviewers
Expand Down
11 changes: 8 additions & 3 deletions lib/modules/platform/gitea/index.ts
Expand Up @@ -43,6 +43,7 @@ import type {
Label,
PR,
PRMergeMethod,
PRUpdateParams,
Repo,
RepoSortMethod,
SortMethod,
Expand Down Expand Up @@ -629,12 +630,16 @@ const platform: Platform = {
title = DRAFT_PREFIX + title;
}

await helper.updatePR(config.repository, number, {
const prUpdateParams: PRUpdateParams = {
title,
base: targetBranch,
...(body && { body }),
...(state && { state }),
});
};
if (targetBranch) {
prUpdateParams.base = targetBranch;
}

await helper.updatePR(config.repository, number, prUpdateParams);
},

async mergePr({ id, strategy }: MergePRConfig): Promise<boolean> {
Expand Down
19 changes: 11 additions & 8 deletions lib/modules/platform/gitlab/index.ts
Expand Up @@ -712,16 +712,19 @@ export async function updatePr({
['open']: 'reopen',
// TODO: null check (#7154)
}[state!];

const body: any = {
title,
description: sanitize(description),
...(newState && { state_event: newState }),
};
if (targetBranch) {
body.target_branch = targetBranch;
}

await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${iid}`,
{
body: {
title,
description: sanitize(description),
...(newState && { state_event: newState }),
target_branch: targetBranch,
},
}
{ body }
);

await tryPrAutomerge(iid, platformOptions);
Expand Down
17 changes: 10 additions & 7 deletions lib/workers/repository/update/pr/index.ts
Expand Up @@ -12,6 +12,7 @@ import {
PlatformPrOptions,
Pr,
PrDebugData,
UpdatePrConfig,
platform,
} from '../../../../modules/platform';
import { ensureComment } from '../../../../modules/platform/comment';
Expand Down Expand Up @@ -357,6 +358,13 @@ export async function ensurePr(
);
return { type: 'with-pr', pr: existingPr };
}

const updatePrConfig: UpdatePrConfig = {
number: existingPr.number,
prTitle,
prBody,
platformOptions: getPlatformPrOptions(config),
};
// PR must need updating
if (existingPr?.targetBranch !== config.baseBranch) {
logger.debug(
Expand All @@ -367,6 +375,7 @@ export async function ensurePr(
},
'PR base branch has changed'
);
updatePrConfig.targetBranch = config.baseBranch;
}
if (existingPrTitle !== newPrTitle) {
logger.debug(
Expand All @@ -390,13 +399,7 @@ export async function ensurePr(
logger.info(`DRY-RUN: Would update PR #${existingPr.number}`);
return { type: 'with-pr', pr: existingPr };
} else {
await platform.updatePr({
number: existingPr.number,
prTitle,
prBody,
platformOptions: getPlatformPrOptions(config),
targetBranch: config.baseBranch,
});
await platform.updatePr(updatePrConfig);
logger.info({ pr: existingPr.number, prTitle }, `PR updated`);
setPrCache(branchName, prBodyFingerprint, true);
}
Expand Down

0 comments on commit 1700467

Please sign in to comment.