1
1
import type { Range , Uri } from 'vscode' ;
2
2
import type { AutolinkReference , DynamicAutolinkReference } from '../../autolinks/models/autolinks' ;
3
+ import type { Container } from '../../container' ;
4
+ import { HostingIntegration } from '../../plus/integrations/integration' ;
5
+ import { remoteProviderIdToIntegrationId } from '../../plus/integrations/integrationService' ;
3
6
import type { Brand , Unbrand } from '../../system/brand' ;
4
7
import type { Repository } from '../models/repository' ;
5
8
import type { GkProviderId } from '../models/repositoryIdentities' ;
@@ -17,7 +20,14 @@ const rangeRegex = /line=(\d+)(?:&lineEnd=(\d+))?/;
17
20
18
21
export class AzureDevOpsRemote extends RemoteProvider {
19
22
private readonly project : string | undefined ;
20
- constructor ( domain : string , path : string , protocol ?: string , name ?: string , legacy : boolean = false ) {
23
+ constructor (
24
+ private readonly container : Container ,
25
+ domain : string ,
26
+ path : string ,
27
+ protocol ?: string ,
28
+ name ?: string ,
29
+ legacy : boolean = false ,
30
+ ) {
21
31
let repoProject ;
22
32
if ( sshDomainRegex . test ( domain ) ) {
23
33
path = path . replace ( sshPathRegex , '' ) ;
@@ -186,16 +196,30 @@ export class AzureDevOpsRemote extends RemoteProvider {
186
196
return this . encodeUrl ( `${ this . baseUrl } /branchCompare?baseVersion=GB${ base } &targetVersion=GB${ head } ` ) ;
187
197
}
188
198
189
- protected override getUrlForCreatePullRequest (
199
+ protected override async getUrlForCreatePullRequest (
190
200
base : { branch ?: string ; remote : { path : string ; url : string } } ,
191
201
head : { branch : string ; remote : { path : string ; url : string } } ,
192
- ) : string | undefined {
193
- const query = new URLSearchParams ( { sourceRef : head . branch , targetRef : base . branch ?? '' } ) ;
194
- // TODO: figure this out
195
- // query.set('sourceRepositoryId', compare.repoId);
196
- // query.set('targetRepositoryId', base.repoId);
202
+ ) : Promise < string | undefined > {
203
+ const query = new URLSearchParams ( { sourceRef : head . branch , targetRef : base . branch ?? '' , title : 'OLOLO' } ) ;
204
+ if ( base . remote . url !== head . remote . url ) {
205
+ const { org : baseOrg , project : baseProject , repo : baseName } = parseAzureUrl ( base . remote . url ) ;
206
+ const targetDesc = { project : baseProject , name : baseName , owner : baseOrg } ;
207
+
208
+ const integrationId = remoteProviderIdToIntegrationId ( this . id ) ;
209
+ const integration = integrationId && ( await this . container . integrations . get ( integrationId ) ) ;
210
+ let targetRepoId = undefined ;
211
+ if ( integration ?. isConnected && integration instanceof HostingIntegration ) {
212
+ targetRepoId = ( await integration . getRepoInfo ( targetDesc ) ) ?. id ;
213
+ }
197
214
198
- return `${ this . encodeUrl ( `${ this . baseUrl } /pullrequestcreate` ) } ?${ query . toString ( ) } ` ;
215
+ if ( ! targetRepoId ) {
216
+ return undefined ; //?? or throw or trigger error showing
217
+ }
218
+ query . set ( 'targetRepositoryId' , targetRepoId ) ;
219
+ // query.set('sourceRepositoryId', compare.repoId); // ?? looks like not needed
220
+ }
221
+
222
+ return `${ this . encodeUrl ( `${ this . getRepoBaseUrl ( head . remote . path ) } /pullrequestcreate` ) } ?${ query . toString ( ) } ` ;
199
223
}
200
224
201
225
protected getUrlForFile ( fileName : string , branch ?: string , sha ?: string , range ?: Range ) : string {
@@ -219,3 +243,26 @@ export class AzureDevOpsRemote extends RemoteProvider {
219
243
return this . encodeUrl ( `${ this . baseUrl } ?path=/${ fileName } ${ line } ` ) ;
220
244
}
221
245
}
246
+
247
+ const azureSshUrlRegex = / ^ (?: [ ^ @ ] + @ ) ? ( [ ^ : ] + ) : v \d \/ / ;
248
+ function parseAzureUrl ( url : string ) : { org : string ; project : string ; repo : string } {
249
+ if ( azureSshUrlRegex . test ( url ) ) {
250
+ url = url . replace ( azureSshUrlRegex , '' ) ;
251
+ }
252
+ const match = orgAndProjectRegex . exec ( url ) ;
253
+ if ( match != null ) {
254
+ const [ , org , project , rest ] = match ;
255
+ return { org : org , project : project , repo : rest } ;
256
+ }
257
+
258
+ // Azure DevOps allows projects and repository names with spaces. In that situation,
259
+ // the `path` will be previously encoded during git clone
260
+ // revert that encoding to avoid double-encoding by gitlens during copy remote and open remote
261
+ //path = decodeURIComponent(path);
262
+
263
+ return {
264
+ org : '' ,
265
+ project : '' ,
266
+ repo : '' ,
267
+ } ;
268
+ }
0 commit comments