Skip to content

Commit

Permalink
Enhances PR search with multiple integrations instead of only GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Dec 17, 2024
1 parent defeae4 commit 76d1801
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions src/plus/launchpad/launchpadProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getOrOpenPullRequestRepository,
getRepositoryIdentityForPullRequest,
} from '../../git/models/pullRequest';
import type { PullRequestUrlIdentity } from '../../git/models/pullRequest.utils';
import type { GitRemote } from '../../git/models/remote';
import type { Repository } from '../../git/models/repository';
import type { CodeSuggestionCounts, Draft } from '../../gk/models/drafts';
Expand All @@ -39,7 +40,7 @@ import type { UriTypes } from '../../uris/deepLinks/deepLink';
import { DeepLinkActionType, DeepLinkType } from '../../uris/deepLinks/deepLink';
import { showInspectView } from '../../webviews/commitDetails/actions';
import type { ShowWipArgs } from '../../webviews/commitDetails/protocol';
import type { IntegrationResult } from '../integrations/integration';
import type { HostingIntegration, IntegrationResult } from '../integrations/integration';
import type { ConnectionStateChangeEvent } from '../integrations/integrationService';
import type { GitHubRepositoryDescriptor } from '../integrations/providers/github';
import type { GitLabRepositoryDescriptor } from '../integrations/providers/gitlab';
Expand Down Expand Up @@ -327,9 +328,48 @@ export class LaunchpadProvider implements Disposable {
// The current idea is that we should iterate the connected integrations and apply their parsing.
// Probably we even want to build a map like this: { integrationId: identity }
// Then we iterate connected integrations and search in each of them with the corresponding identity.
const { ownerAndRepo, prNumber, provider } = getPullRequestIdentityFromMaybeUrl(search);
let result: TimedResult<SearchedPullRequest[] | undefined> | undefined;
const prUrlIdentity = getPullRequestIdentityFromMaybeUrl(search);
const result: { readonly value: SearchedPullRequest[]; duration: number } = {
value: [],
duration: 0,
};

const connectedIntegrations = await this.getConnectedIntegrations();

await Promise.allSettled(
[...connectedIntegrations.keys()]
.filter(
(id: IntegrationId): id is SupportedLaunchpadIntegrationIds =>
(connectedIntegrations.get(id) && isSupportedLaunchpadIntegrationId(id)) ?? false,
)
.map(async (id: HostingIntegrationId) => {
const integration = await this.container.integrations.get(id);
const searchResult = await this.searchIntegrationPRs(
search,
prUrlIdentity,
integration,
cancellation,
);
const prs = searchResult?.value;
if (prs) {
result.value?.push(...prs);
result.duration = Math.max(result.duration, searchResult.duration);
}
}),
);
return {
prs: result,
suggestionCounts: undefined,
};
}

private async searchIntegrationPRs(
search: string,
{ ownerAndRepo, prNumber, provider }: PullRequestUrlIdentity,
integration: HostingIntegration,
cancellation: CancellationToken | undefined,
): Promise<undefined | TimedResult<SearchedPullRequest[] | undefined>> {
let result: TimedResult<SearchedPullRequest[] | undefined> | undefined;
if (provider != null && prNumber != null && ownerAndRepo != null) {
// TODO: This needs to be generalized to work outside of GitHub/GitLab
const integration = await this.container.integrations.get(provider);
Expand All @@ -346,21 +386,20 @@ export class LaunchpadProvider implements Disposable {
);
if (pr?.value != null) {
result = { value: [{ pullRequest: pr.value, reasons: [] }], duration: pr.duration };
return { prs: result, suggestionCounts: undefined };
return result;
}
} else {
const integration = await this.container.integrations.get(HostingIntegrationId.GitHub);
const prs = await withDurationAndSlowEventOnTimeout(
integration?.searchPullRequests(search, undefined, cancellation),
integration?.searchPullRequests(search, undefined, cancellation), //
'searchPullRequests',
this.container,
);
if (prs != null) {
result = { value: prs.value?.map(pr => ({ pullRequest: pr, reasons: [] })), duration: prs.duration };
return { prs: result, suggestionCounts: undefined };
return result;
}
}
return { prs: undefined, suggestionCounts: undefined };
return undefined;
}

private _enrichedItems: CachedLaunchpadPromise<TimedResult<EnrichedItem[]>> | undefined;
Expand Down

0 comments on commit 76d1801

Please sign in to comment.