@@ -2,6 +2,8 @@ import type { Range, Uri } from 'vscode';
22import type { AutolinkReference , DynamicAutolinkReference } from '../../autolinks/models/autolinks' ;
33import type { Source } from '../../constants.telemetry' ;
44import type { Container } from '../../container' ;
5+ import { HostingIntegration } from '../../plus/integrations/integration' ;
6+ import { remoteProviderIdToIntegrationId } from '../../plus/integrations/integrationService' ;
57import type { Brand , Unbrand } from '../../system/brand' ;
68import type { CreatePullRequestRemoteResource } from '../models/remoteResource' ;
79import type { Repository } from '../models/repository' ;
@@ -62,18 +64,16 @@ export class BitbucketServerRemote extends RemoteProvider {
6264 }
6365
6466 protected override get baseUrl ( ) : string {
65- const [ project , repo ] = this . splitPath ( ) ;
67+ const [ project , repo ] = this . splitPath ( this . path ) ;
6668 return `${ this . protocol } ://${ this . domain } /projects/${ project } /repos/${ repo } ` ;
6769 }
6870
69- protected override splitPath ( ) : [ string , string ] {
70- if ( this . path . startsWith ( 'scm/' ) && this . path . indexOf ( '/' ) !== this . path . lastIndexOf ( '/' ) ) {
71- const path = this . path . replace ( 'scm/' , '' ) ;
72- const index = path . indexOf ( '/' ) ;
73- return [ path . substring ( 0 , index ) , path . substring ( index + 1 ) ] ;
71+ protected override splitPath ( path : string ) : [ string , string ] {
72+ if ( path . startsWith ( 'scm/' ) && path . indexOf ( '/' ) !== path . lastIndexOf ( '/' ) ) {
73+ return super . splitPath ( path . replace ( 'scm/' , '' ) ) ;
7474 }
7575
76- return super . splitPath ( ) ;
76+ return super . splitPath ( path ) ;
7777 }
7878
7979 override get icon ( ) : string {
@@ -191,7 +191,13 @@ export class BitbucketServerRemote extends RemoteProvider {
191191 }
192192
193193 protected override getUrlForComparison ( base : string , head : string , _notation : GitRevisionRangeNotation ) : string {
194- return this . encodeUrl ( `${ this . baseUrl } /branches/compare/${ base } %0D${ head } ` ) . replaceAll ( '%250D' , '%0D' ) ;
194+ return this . encodeUrl ( `${ this . baseUrl } /branches/compare/${ head } \r${ base } ` ) ;
195+ }
196+
197+ override async isReadyForForCrossForkPullRequestUrls ( ) : Promise < boolean > {
198+ const integrationId = remoteProviderIdToIntegrationId ( this . id ) ;
199+ const integration = integrationId && ( await this . container . integrations . get ( integrationId ) ) ;
200+ return integration ?. maybeConnected ?? integration ?. isConnected ( ) ?? false ;
195201 }
196202
197203 protected override async getUrlForCreatePullRequest (
@@ -210,17 +216,33 @@ export class BitbucketServerRemote extends RemoteProvider {
210216 }
211217
212218 const query = new URLSearchParams ( { sourceBranch : head . branch , targetBranch : base . branch ?? '' } ) ;
213- // TODO: figure this out
214- // query.set('targetRepoId', base.repoId);
215-
219+ const [ baseOwner , baseName ] = this . splitPath ( base . remote . path ) ;
220+ if ( base . remote . url !== head . remote . url ) {
221+ const targetDesc = {
222+ owner : baseOwner ,
223+ name : baseName ,
224+ } ;
225+ const integrationId = remoteProviderIdToIntegrationId ( this . id ) ;
226+ const integration = integrationId && ( await this . container . integrations . get ( integrationId ) ) ;
227+ let targetRepoId = undefined ;
228+ if ( integration ?. isConnected && integration instanceof HostingIntegration ) {
229+ targetRepoId = ( await integration . getRepoInfo ?.( targetDesc ) ) ?. id ;
230+ }
231+ if ( ! targetRepoId ) {
232+ return undefined ;
233+ }
234+ query . set ( 'targetRepoId' , targetRepoId ) ;
235+ }
216236 if ( details ?. title ) {
217237 query . set ( 'title' , details . title ) ;
218238 }
219239 if ( details ?. description ) {
220240 query . set ( 'description' , details . description ) ;
221241 }
222-
223- return `${ this . encodeUrl ( `${ this . baseUrl } /pull-requests?create` ) } &${ query . toString ( ) } ` ;
242+ const [ headOwner , headName ] = this . splitPath ( head . remote . path ) ;
243+ return `${ this . encodeUrl (
244+ `${ this . protocol } ://${ this . domain } /projects/${ headOwner } /repos/${ headName } /pull-requests?create` ,
245+ ) } &${ query . toString ( ) } `;
224246 }
225247
226248 protected getUrlForFile ( fileName : string , branch ?: string , sha ?: string , range ?: Range ) : string {
0 commit comments