Skip to content

Commit a27e8de

Browse files
committed
Associates a Cloud GitHub Enterprise provider with remote by its domain
(#3901, #3922)
1 parent b8dd1b0 commit a27e8de

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

src/env/node/git/sub-providers/remotes.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase implements Git
3838
let remotesPromise = this.cache.remotes?.get(repoPath);
3939
if (remotesPromise == null) {
4040
async function load(this: RemotesGitSubProvider): Promise<GitRemote[]> {
41+
const ci = await this.container.cloudIntegrations;
42+
const connections = await ci?.getConnections();
4143
const providers = loadRemoteProviders(
4244
configuration.get('remotes', this.container.git.getRepository(repoPath!)?.folder?.uri ?? null),
45+
connections,
4346
);
4447

4548
try {
@@ -48,7 +51,7 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase implements Git
4851
this.container,
4952
data,
5053
repoPath!,
51-
getRemoteProviderMatcher(this.container, providers),
54+
await getRemoteProviderMatcher(this.container, providers),
5255
);
5356
return remotes;
5457
} catch (ex) {

src/git/parsers/remoteParser.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import type { getRemoteProviderMatcher } from '../remotes/remoteProviders';
66

77
const remoteRegex = /^(.*)\t(.*)\s\((.*)\)$/gm;
88

9+
type PromiseType<T> = T extends Promise<infer U> ? U : T;
10+
911
export function parseGitRemotes(
1012
container: Container,
1113
data: string,
1214
repoPath: string,
13-
remoteProviderMatcher: ReturnType<typeof getRemoteProviderMatcher>,
15+
remoteProviderMatcher: PromiseType<ReturnType<typeof getRemoteProviderMatcher>>,
1416
): GitRemote[] {
1517
using sw = maybeStopWatch(`Git.parseRemotes(${repoPath})`, { log: false, logLevel: 'debug' });
1618
if (!data) return [];

src/git/remotes/remoteProviders.ts

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { RemotesConfig } from '../../config';
2+
import { SelfHostedIntegrationId } from '../../constants.integrations';
23
import type { Container } from '../../container';
4+
import type { CloudIntegrationConnection } from '../../plus/integrations/authentication/models';
5+
import { toIntegrationId } from '../../plus/integrations/authentication/models';
36
import { Logger } from '../../system/logger';
47
import { configuration } from '../../system/vscode/configuration';
58
import { AzureDevOpsRemote } from './azure-devops';
@@ -73,7 +76,10 @@ const builtInProviders: RemoteProviders = [
7376
},
7477
];
7578

76-
export function loadRemoteProviders(cfg: RemotesConfig[] | null | undefined): RemoteProviders {
79+
export function loadRemoteProviders(
80+
cfg: RemotesConfig[] | null | undefined,
81+
connectedIntegrations: CloudIntegrationConnection[] | undefined,
82+
): RemoteProviders {
7783
const providers: RemoteProviders = [];
7884

7985
if (cfg?.length) {
@@ -97,6 +103,29 @@ export function loadRemoteProviders(cfg: RemotesConfig[] | null | undefined): Re
97103
}
98104
}
99105

106+
if (connectedIntegrations?.length) {
107+
for (const ci of connectedIntegrations) {
108+
if (toIntegrationId[ci.provider] === SelfHostedIntegrationId.CloudGitHubEnterprise) {
109+
const matcher = new URL(ci.domain).host.toLocaleLowerCase();
110+
const providerCreator = (_container: Container, domain: string, path: string) =>
111+
new GitHubRemote(domain, path);
112+
const provider = {
113+
custom: false,
114+
matcher: matcher,
115+
creator: providerCreator,
116+
};
117+
118+
const indexOfCustomDuplication: number = providers.findIndex(p => p.matcher === matcher);
119+
120+
if (indexOfCustomDuplication !== -1) {
121+
providers[indexOfCustomDuplication] = provider;
122+
} else {
123+
providers.push(provider);
124+
}
125+
}
126+
}
127+
}
128+
100129
providers.push(...builtInProviders);
101130

102131
return providers;
@@ -136,12 +165,14 @@ function getCustomProviderCreator(cfg: RemotesConfig) {
136165
}
137166
}
138167

139-
export function getRemoteProviderMatcher(
168+
export async function getRemoteProviderMatcher(
140169
container: Container,
141170
providers?: RemoteProviders,
142-
): (url: string, domain: string, path: string) => RemoteProvider | undefined {
171+
): Promise<(url: string, domain: string, path: string) => RemoteProvider | undefined> {
143172
if (providers == null) {
144-
providers = loadRemoteProviders(configuration.get('remotes', null));
173+
const ci = await container.cloudIntegrations;
174+
const c = await ci?.getConnections();
175+
providers = loadRemoteProviders(configuration.get('remotes', null), c);
145176
}
146177

147178
return (url: string, domain: string, path: string) =>

src/plus/drafts/draftsService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ export class DraftService implements Disposable {
729729
} else if (data.provider?.repoName != null) {
730730
name = data.provider.repoName;
731731
} else if (data.remote?.url != null && data.remote?.domain != null && data.remote?.path != null) {
732-
const matcher = getRemoteProviderMatcher(this.container);
732+
const matcher = await getRemoteProviderMatcher(this.container);
733733
const provider = matcher(data.remote.url, data.remote.domain, data.remote.path);
734734
name = provider?.repoName ?? data.remote.path;
735735
} else {

src/plus/integrations/providers/github/sub-providers/remotes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase {
1414
): Promise<GitRemote[]> {
1515
if (repoPath == null) return [];
1616

17-
const providers = loadRemoteProviders(configuration.get('remotes', null));
17+
const providers = loadRemoteProviders(configuration.get('remotes', null), undefined);
1818

1919
const uri = Uri.parse(repoPath, true);
2020
const [, owner, repo] = uri.path.split('/', 3);
@@ -31,7 +31,7 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase {
3131
'https',
3232
domain,
3333
path,
34-
getRemoteProviderMatcher(this.container, providers)(url, domain, path),
34+
(await getRemoteProviderMatcher(this.container, providers))(url, domain, path),
3535
[
3636
{ type: 'fetch', url: url },
3737
{ type: 'push', url: url },

0 commit comments

Comments
 (0)