@@ -44,6 +44,7 @@ import {
4444 TimelineEventsResponse ,
4545 UnresolveReviewThreadResponse ,
4646 UpdateIssueResponse ,
47+ UpdatePullRequestBranchResponse ,
4748} from './graphql' ;
4849import {
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
0 commit comments