Skip to content

Commit

Permalink
Associate a Cloud GitHub Enterprise provider with remote by its domain
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Jan 7, 2025
1 parent 0958984 commit 377483f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/env/node/git/localGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5039,8 +5039,12 @@ export class LocalGitProvider implements GitProvider, Disposable {
let remotesPromise = this.useCaching ? this._remotesCache.get(repoPath) : undefined;
if (remotesPromise == null) {
async function load(this: LocalGitProvider): Promise<GitRemote[]> {
const ci = await this.container.cloudIntegrations;
const connections = await ci?.getConnections();

const providers = loadRemoteProviders(
configuration.get('remotes', this.container.git.getRepository(repoPath!)?.folder?.uri ?? null),
connections,
);

try {
Expand All @@ -5049,7 +5053,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
this.container,
data,
repoPath!,
getRemoteProviderMatcher(this.container, providers),
await getRemoteProviderMatcher(this.container, providers),
);
return remotes;
} catch (ex) {
Expand Down
4 changes: 3 additions & 1 deletion src/git/parsers/remoteParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import type { getRemoteProviderMatcher } from '../remotes/remoteProviders';

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

type PromiseType<T> = T extends Promise<infer U> ? U : T;

export function parseGitRemotes(
container: Container,
data: string,
repoPath: string,
remoteProviderMatcher: ReturnType<typeof getRemoteProviderMatcher>,
remoteProviderMatcher: PromiseType<ReturnType<typeof getRemoteProviderMatcher>>,
): GitRemote[] {
using sw = maybeStopWatch(`Git.parseRemotes(${repoPath})`, { log: false, logLevel: 'debug' });
if (!data) return [];
Expand Down
48 changes: 44 additions & 4 deletions src/git/remotes/remoteProviders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { RemotesConfig } from '../../config';
import { SelfHostedIntegrationId } from '../../constants.integrations';
import type { Container } from '../../container';
import type { CloudIntegrationConnection } from '../../plus/integrations/authentication/models';
import { toIntegrationId } from '../../plus/integrations/authentication/models';
import { Logger } from '../../system/logger';
import { configuration } from '../../system/vscode/configuration';
import { parseGitRemoteUrl } from '../parsers/remoteParser';
import { AzureDevOpsRemote } from './azure-devops';
import { BitbucketRemote } from './bitbucket';
import { BitbucketServerRemote } from './bitbucket-server';
Expand Down Expand Up @@ -73,7 +77,10 @@ const builtInProviders: RemoteProviders = [
},
];

export function loadRemoteProviders(cfg: RemotesConfig[] | null | undefined): RemoteProviders {
export function loadRemoteProviders(
cfg: RemotesConfig[] | null | undefined,
connectedIntegrations: CloudIntegrationConnection[] | undefined,
): RemoteProviders {
const providers: RemoteProviders = [];

if (cfg?.length) {
Expand All @@ -97,6 +104,37 @@ export function loadRemoteProviders(cfg: RemotesConfig[] | null | undefined): Re
}
}

if (connectedIntegrations?.length) {
for (const ci of connectedIntegrations) {
if (toIntegrationId[ci.provider] === SelfHostedIntegrationId.CloudGitHubEnterprise) {
const host = new URL(ci.domain).host;
const rc: RemotesConfig = {
domain: host,
regex: null,
name: ci.domain,
protocol: 'https',
type: 'GitHub',
};
const providerCreator = getCustomProviderCreator(rc);
if (providerCreator == null) continue;

let matcher: string | RegExp | undefined;
try {
matcher = rc.domain?.toLowerCase();
if (matcher == null) throw new Error('No matcher found');
} catch (ex) {
Logger.error(ex, `Loading remote provider '${rc.name ?? ''}' failed`);
}

providers.push({
custom: true,
matcher: matcher!,
creator: providerCreator,
});
}
}
}

providers.push(...builtInProviders);

return providers;
Expand Down Expand Up @@ -136,12 +174,14 @@ function getCustomProviderCreator(cfg: RemotesConfig) {
}
}

export function getRemoteProviderMatcher(
export async function getRemoteProviderMatcher(
container: Container,
providers?: RemoteProviders,
): (url: string, domain: string, path: string) => RemoteProvider | undefined {
): Promise<(url: string, domain: string, path: string) => RemoteProvider | undefined> {
if (providers == null) {
providers = loadRemoteProviders(configuration.get('remotes', null));
const ci = await container.cloudIntegrations;
const c = await ci?.getConnections();
providers = loadRemoteProviders(configuration.get('remotes', null), c);
}

return (url: string, domain: string, path: string) =>
Expand Down
2 changes: 1 addition & 1 deletion src/plus/drafts/draftsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ export class DraftService implements Disposable {
} else if (data.provider?.repoName != null) {
name = data.provider.repoName;
} else if (data.remote?.url != null && data.remote?.domain != null && data.remote?.path != null) {
const matcher = getRemoteProviderMatcher(this.container);
const matcher = await getRemoteProviderMatcher(this.container);
const provider = matcher(data.remote.url, data.remote.domain, data.remote.path);
name = provider?.repoName ?? data.remote.path;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/plus/integrations/providers/github/githubGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,7 @@ export class GitHubGitProvider implements GitProvider, Disposable {
): Promise<GitRemote[]> {
if (repoPath == null) return [];

const providers = loadRemoteProviders(configuration.get('remotes', null));
const providers = loadRemoteProviders(configuration.get('remotes', null), undefined);

const uri = Uri.parse(repoPath, true);
const [, owner, repo] = uri.path.split('/', 3);
Expand All @@ -2798,7 +2798,7 @@ export class GitHubGitProvider implements GitProvider, Disposable {
'https',
domain,
path,
getRemoteProviderMatcher(this.container, providers)(url, domain, path),
(await getRemoteProviderMatcher(this.container, providers))(url, domain, path),
[
{ type: 'fetch', url: url },
{ type: 'push', url: url },
Expand Down

0 comments on commit 377483f

Please sign in to comment.