@@ -6,6 +6,9 @@ import type {
66 MaybeEnrichedAutolink ,
77} from '../../autolinks/models/autolinks' ;
88import { GlyphChars } from '../../constants' ;
9+ import type { Container } from '../../container' ;
10+ import { HostingIntegration } from '../../plus/integrations/integration' ;
11+ import { remoteProviderIdToIntegrationId } from '../../plus/integrations/integrationService' ;
912import type { GitLabRepositoryDescriptor } from '../../plus/integrations/providers/gitlab' ;
1013import type { Brand , Unbrand } from '../../system/brand' ;
1114import { fromNow } from '../../system/date' ;
@@ -30,7 +33,14 @@ function isGitLabDotCom(domain: string): boolean {
3033}
3134
3235export class GitLabRemote extends RemoteProvider < GitLabRepositoryDescriptor > {
33- constructor ( domain : string , path : string , protocol ?: string , name ?: string , custom : boolean = false ) {
36+ constructor (
37+ private readonly container : Container ,
38+ domain : string ,
39+ path : string ,
40+ protocol ?: string ,
41+ name ?: string ,
42+ custom : boolean = false ,
43+ ) {
3444 super ( domain , path , protocol , name , custom ) ;
3545 }
3646
@@ -372,8 +382,51 @@ export class GitLabRemote extends RemoteProvider<GitLabRepositoryDescriptor> {
372382 return this . encodeUrl ( `${ this . baseUrl } /-/commit/${ sha } ` ) ;
373383 }
374384
375- protected override getUrlForComparison ( base : string , compare : string , notation : '..' | '...' ) : string {
376- return this . encodeUrl ( `${ this . baseUrl } /-/compare/${ base } ${ notation } ${ compare } ` ) ;
385+ protected override getUrlForComparison ( base : string , head : string , notation : '..' | '...' ) : string {
386+ return this . encodeUrl ( `${ this . baseUrl } /-/compare/${ base } ${ notation } ${ head } ` ) ;
387+ }
388+
389+ override async isReadyForForCrossForkPullRequestUrls ( ) : Promise < boolean > {
390+ const integrationId = remoteProviderIdToIntegrationId ( this . id ) ;
391+ const integration = integrationId && ( await this . container . integrations . get ( integrationId ) ) ;
392+ return integration ?. maybeConnected ?? integration ?. isConnected ( ) ?? false ;
393+ }
394+
395+ protected override async getUrlForCreatePullRequest (
396+ base : { branch ?: string ; remote : { path : string ; url : string } } ,
397+ head : { branch : string ; remote : { path : string ; url : string } } ,
398+ options ?: { title ?: string ; description ?: string } ,
399+ ) : Promise < string | undefined > {
400+ const query = new URLSearchParams ( {
401+ utf8 : '✓' ,
402+ 'merge_request[source_branch]' : head . branch ,
403+ 'merge_request[target_branch]' : base . branch ?? '' ,
404+ } ) ;
405+ if ( base . remote . url !== head . remote . url ) {
406+ const targetDesc = {
407+ owner : base . remote . path . split ( '/' ) [ 0 ] ,
408+ name : base . remote . path . split ( '/' ) [ 1 ] ,
409+ } ;
410+ const integrationId = remoteProviderIdToIntegrationId ( this . id ) ;
411+ const integration = integrationId && ( await this . container . integrations . get ( integrationId ) ) ;
412+ let targetRepoId = undefined ;
413+ if ( integration ?. isConnected && integration instanceof HostingIntegration ) {
414+ targetRepoId = ( await integration . getRepoInfo ?.( targetDesc ) ) ?. id ;
415+ }
416+ if ( ! targetRepoId ) {
417+ return undefined ;
418+ }
419+ query . set ( 'merge_request[target_project_id]' , targetRepoId ) ;
420+ // 'merge_request["source_project_id"]': this.path, // ?? seems we don't need it
421+ }
422+ if ( options ?. title ) {
423+ query . set ( 'merge_request[title]' , options . title ) ;
424+ }
425+ if ( options ?. description ) {
426+ query . set ( 'merge_request[description]' , options . description ) ;
427+ }
428+
429+ return `${ this . encodeUrl ( `${ this . getRepoBaseUrl ( head . remote . path ) } /-/merge_requests/new` ) } ?${ query . toString ( ) } ` ;
377430 }
378431
379432 protected getUrlForFile ( fileName : string , branch ?: string , sha ?: string , range ?: Range ) : string {
0 commit comments