@@ -118,6 +118,37 @@ export class PullRequestManager implements IPullRequestManager {
118118 } ) ;
119119 }
120120
121+ async deleteLocalPullRequest ( pullRequest : PullRequestModel ) : Promise < void > {
122+ const remoteName = await this . _repository . getConfig ( `branch.${ pullRequest . localBranchName } .remote` ) ;
123+ if ( ! remoteName ) {
124+ throw new Error ( 'Unable to find remote for branch' ) ;
125+ }
126+
127+ const result = await this . _repository . run ( [ 'branch' , '-D' , pullRequest . localBranchName ] ) ;
128+ if ( result . stderr ) {
129+ throw new Error ( result . stderr ) ;
130+ }
131+
132+ // If the extension created a remote for the branch, remove it if there are no other branches associated with it
133+ const isPRRemote = await PullRequestGitHelper . isRemoteCreatedForPullRequest ( this . _repository , remoteName ) ;
134+ if ( isPRRemote ) {
135+ const configKeyValues = await this . _repository . run ( [ 'config' , '--local' , '-l' ] ) ;
136+ if ( configKeyValues . stderr ) {
137+ throw new Error ( configKeyValues . stderr ) ;
138+ }
139+
140+ const result = configKeyValues . stdout . trim ( ) ;
141+ const hasOtherAssociatedBranches = new RegExp ( `^branch.*\.remote=${ remoteName } $` , 'm' ) . test ( result ) ;
142+
143+ if ( ! hasOtherAssociatedBranches ) {
144+ const remoteResult = await this . _repository . run ( [ 'remote' , 'remove' , remoteName ] ) ;
145+ if ( remoteResult . stderr ) {
146+ throw new Error ( remoteResult . stderr ) ;
147+ }
148+ }
149+ }
150+ }
151+
121152 async getPullRequests ( type : PRType , options : IPullRequestsPagingOptions = { fetchNextPage : false } ) : Promise < [ IPullRequestModel [ ] , boolean ] > {
122153 let githubRepositories = this . _githubRepositories ;
123154
0 commit comments