Skip to content

Commit

Permalink
Associates 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 13, 2025
1 parent 9ea6153 commit d96fa7f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/env/node/git/sub-providers/remotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase implements Git
let remotesPromise = this.cache.remotes?.get(repoPath);
if (remotesPromise == null) {
async function load(this: RemotesGitSubProvider): 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 @@ -48,7 +51,7 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase implements Git
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
39 changes: 35 additions & 4 deletions src/git/remotes/remoteProviders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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 { AzureDevOpsRemote } from './azure-devops';
Expand Down Expand Up @@ -73,7 +76,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 +103,29 @@ export function loadRemoteProviders(cfg: RemotesConfig[] | null | undefined): Re
}
}

if (connectedIntegrations?.length) {
for (const ci of connectedIntegrations) {
if (toIntegrationId[ci.provider] === SelfHostedIntegrationId.CloudGitHubEnterprise) {
const matcher = new URL(ci.domain).host.toLocaleLowerCase();
const providerCreator = (_container: Container, domain: string, path: string) =>
new GitHubRemote(domain, path);
const provider = {
custom: false,
matcher: matcher,
creator: providerCreator,
};

const indexOfCustomDuplication: number = providers.findIndex(p => p.matcher === matcher);

if (indexOfCustomDuplication !== -1) {
providers[indexOfCustomDuplication] = provider;
} else {
providers.push(provider);
}
}
}
}

providers.push(...builtInProviders);

return providers;
Expand Down Expand Up @@ -136,12 +165,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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase {
): 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 @@ -31,7 +31,7 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase {
'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 d96fa7f

Please sign in to comment.