Skip to content

Commit 5e2072c

Browse files
Copilotalexr00
andcommitted
Use GraphQL UpdatePullRequestBranch mutation when there are no conflicts
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 8859fc8 commit 5e2072c

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

src/github/graphql.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,15 @@ export interface EnqueuePullRequestResponse {
545545
}
546546
}
547547

548+
export interface UpdatePullRequestBranchResponse {
549+
updatePullRequestBranch: {
550+
pullRequest: {
551+
id: string;
552+
headRefOid: string;
553+
}
554+
}
555+
}
556+
548557
export interface SubmittedReview extends Review {
549558
comments: {
550559
nodes: ReviewComment[];

src/github/pullRequestModel.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
TimelineEventsResponse,
4545
UnresolveReviewThreadResponse,
4646
UpdateIssueResponse,
47+
UpdatePullRequestBranchResponse,
4748
} from './graphql';
4849
import {
4950
AccountType,
@@ -1206,6 +1207,50 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
12061207
}
12071208

12081209
Logger.debug(`Updating branch ${model.prHeadBranchName} to ${model.prBaseBranchName} - enter`, GitHubRepository.ID);
1210+
1211+
// When there are no conflicts, use the GitHub GraphQL API's UpdatePullRequestBranch mutation
1212+
// This is simpler and more efficient than manually creating trees and commits
1213+
if (this.item.mergeable !== PullRequestMergeability.Conflict) {
1214+
return this.updateBranchWithGraphQL();
1215+
}
1216+
1217+
// When there are conflicts, use the REST API approach with conflict resolution
1218+
return this.updateBranchWithConflictResolution(model);
1219+
}
1220+
1221+
/**
1222+
* Update the branch using the GitHub GraphQL API's UpdatePullRequestBranch mutation.
1223+
* This is used when there are no conflicts between the head and base branches.
1224+
*/
1225+
private async updateBranchWithGraphQL(): Promise<boolean> {
1226+
Logger.debug(`Updating branch using GraphQL UpdatePullRequestBranch mutation - enter`, GitHubRepository.ID);
1227+
try {
1228+
const { mutate, schema } = await this.githubRepository.ensure();
1229+
1230+
await mutate<UpdatePullRequestBranchResponse>({
1231+
mutation: schema.UpdatePullRequestBranch,
1232+
variables: {
1233+
input: {
1234+
pullRequestId: this.graphNodeId,
1235+
expectedHeadOid: this.head?.sha
1236+
}
1237+
}
1238+
});
1239+
1240+
Logger.debug(`Updating branch using GraphQL UpdatePullRequestBranch mutation - done`, GitHubRepository.ID);
1241+
return true;
1242+
} catch (e) {
1243+
Logger.error(`Updating branch using GraphQL UpdatePullRequestBranch mutation failed: ${e}`, GitHubRepository.ID);
1244+
return false;
1245+
}
1246+
}
1247+
1248+
/**
1249+
* Update the branch with conflict resolution using the REST API.
1250+
* This is used when there are conflicts between the head and base branches.
1251+
*/
1252+
private async updateBranchWithConflictResolution(model: ConflictResolutionModel): Promise<boolean> {
1253+
Logger.debug(`Updating branch ${model.prHeadBranchName} with conflict resolution - enter`, GitHubRepository.ID);
12091254
try {
12101255
const { octokit } = await this.githubRepository.ensure();
12111256

@@ -1225,10 +1270,10 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
12251270
await octokit.call(octokit.api.git.updateRef, { owner: model.prHeadOwner, repo: model.repositoryName, ref: `heads/${model.prHeadBranchName}`, sha: newCommitSha });
12261271

12271272
} catch (e) {
1228-
Logger.error(`Updating branch ${model.prHeadBranchName} to ${model.prBaseBranchName} failed: ${e}`, GitHubRepository.ID);
1273+
Logger.error(`Updating branch ${model.prHeadBranchName} with conflict resolution failed: ${e}`, GitHubRepository.ID);
12291274
return false;
12301275
}
1231-
Logger.debug(`Updating branch ${model.prHeadBranchName} to ${model.prBaseBranchName} - done`, GitHubRepository.ID);
1276+
Logger.debug(`Updating branch ${model.prHeadBranchName} with conflict resolution - done`, GitHubRepository.ID);
12321277
return true;
12331278
}
12341279

src/github/queries.gql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,4 +763,13 @@ mutation ReplaceActorsForAssignable($input: ReplaceActorsForAssignableInput!) {
763763
}
764764
}
765765
}
766+
}
767+
768+
mutation UpdatePullRequestBranch($input: UpdatePullRequestBranchInput!) {
769+
updatePullRequestBranch(input: $input) {
770+
pullRequest {
771+
id
772+
headRefOid
773+
}
774+
}
766775
}

src/github/queriesExtra.gql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,4 +806,13 @@ mutation EnqueuePullRequest($input: EnqueuePullRequestInput!) {
806806
...MergeQueueEntryFragment
807807
}
808808
}
809+
}
810+
811+
mutation UpdatePullRequestBranch($input: UpdatePullRequestBranchInput!) {
812+
updatePullRequestBranch(input: $input) {
813+
pullRequest {
814+
id
815+
headRefOid
816+
}
817+
}
809818
}

0 commit comments

Comments
 (0)