Skip to content

Commit c216fdf

Browse files
committed
Searches by a text query using API
(#3543, #3684)
1 parent e301436 commit c216fdf

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/constants.telemetry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ export type TelemetryEvents = {
296296
timeout: number;
297297
operation:
298298
| 'getPullRequest'
299+
| 'searchPullRequests'
299300
| 'getMyPullRequests'
300301
| 'getCodeSuggestions'
301302
| 'getEnrichedItems'

src/plus/launchpad/launchpad.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ export class LaunchpadCommand extends QuickCommand<State> {
518518
};
519519
}
520520

521+
const mergeQuickpickItems = <T extends { item: { id: string } } | object>(arr: readonly T[], items: T[]) => {
522+
const ids: Set<string> = new Set(
523+
arr.map(i => 'item' in i && i.item?.id).filter(id => typeof id === 'string'),
524+
);
525+
const result = [...arr];
526+
for (const item of items) {
527+
if ('item' in item && item.item?.id && !ids.has(item.item.id)) {
528+
result.push(item);
529+
}
530+
}
531+
return result;
532+
};
533+
521534
const updateItems = async (
522535
quickpick: QuickPick<LaunchpadItemQuickPickItem | DirectiveQuickPickItem | ConnectMoreIntegrationsItem>,
523536
) => {
@@ -536,7 +549,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
536549
}
537550
const { items, placeholder } = getItemsAndPlaceholder(Boolean(search));
538551
quickpick.placeholder = placeholder;
539-
quickpick.items = items;
552+
quickpick.items = search ? mergeQuickpickItems(quickpick.items, items) : items;
540553
});
541554
} finally {
542555
quickpick.busy = false;
@@ -577,11 +590,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
577590
quickpick.items =
578591
updated && quickpick.items === consideredItems ? [...consideredItems] : consideredItems;
579592

580-
const activeLaunchpadItems = quickpick.activeItems.filter(
581-
(i): i is LaunchpadItemQuickPickItem => 'item' in i,
582-
);
583-
584-
if (!value?.length || activeLaunchpadItems.length) {
593+
if (!value?.length) {
585594
// Nothing to search
586595
this.updateItemsDebouncer.cancel();
587596
return true;
@@ -593,7 +602,9 @@ export class LaunchpadCommand extends QuickCommand<State> {
593602
// Then when we iterate local items we can check them to corresponding identitie according to the item's repo type.
594603
// Same with API: we iterate connected integrations and search in each of them with the corresponding identity.
595604
const prUrlIdentity = getPullRequestIdentityValuesFromSearch(value);
605+
596606
if (prUrlIdentity.prNumber != null) {
607+
// We can identify the PR number, so let's try to find it locally:
597608
const launchpadItems = quickpick.items.filter((i): i is LaunchpadItemQuickPickItem => 'item' in i);
598609
let item = launchpadItems.find(i =>
599610
// perform strict match first
@@ -608,16 +619,15 @@ export class LaunchpadCommand extends QuickCommand<State> {
608619
item.alwaysShow = true;
609620
// Force quickpick to update by changing the items object:
610621
quickpick.items = [...quickpick.items];
611-
// We have found an item that matches to the URL.
612-
// Now it will be displayed as the found item and we exit this function now without sending any requests to API:
613-
this.updateItemsDebouncer.cancel();
614-
return true;
615622
}
616-
// Nothing is found above, so let's perform search in the API:
617-
await updateItems(quickpick);
623+
// We have found an item that matches to the URL.
624+
// Now it will be displayed as the found item and we exit this function now without sending any requests to API:
625+
this.updateItemsDebouncer.cancel();
626+
return true;
618627
}
619628
}
620-
this.updateItemsDebouncer.cancel();
629+
630+
await updateItems(quickpick);
621631
return true;
622632
},
623633
onDidClickButton: async (quickpick, button) => {

src/plus/launchpad/launchpadProvider.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export class LaunchpadProvider implements Disposable {
320320
return { prs: prs, suggestionCounts: suggestionCounts };
321321
}
322322

323-
private async getSearchedPullRequests(search: string) {
323+
private async getSearchedPullRequests(search: string, cancellation?: CancellationToken) {
324324
// TODO: This needs to be generalized to work outside of GitHub,
325325
// The current idea is that we should iterate the connected integrations and apply their parsing.
326326
// Probably we even want to build a map like this: { integrationId: identity }
@@ -348,6 +348,17 @@ export class LaunchpadProvider implements Disposable {
348348
return { prs: result, suggestionCounts: undefined };
349349
}
350350
}
351+
} else {
352+
const integration = await this.container.integrations.get(HostingIntegrationId.GitHub);
353+
const prs = await withDurationAndSlowEventOnTimeout(
354+
integration?.searchPullRequests(search, undefined, cancellation),
355+
'searchPullRequests',
356+
this.container,
357+
);
358+
if (prs != null) {
359+
result = { value: prs.value?.map(pr => ({ pullRequest: pr, reasons: [] })), duration: prs.duration };
360+
return { prs: result, suggestionCounts: undefined };
361+
}
351362
}
352363
return { prs: undefined, suggestionCounts: undefined };
353364
}
@@ -673,7 +684,7 @@ export class LaunchpadProvider implements Disposable {
673684
this.container.git.isDiscoveringRepositories,
674685
this.getEnrichedItems({ force: options?.force, cancellation: cancellation }),
675686
options?.search
676-
? this.getSearchedPullRequests(options.search)
687+
? this.getSearchedPullRequests(options.search, cancellation)
677688
: this.getPullRequestsWithSuggestionCounts({ force: options?.force, cancellation: cancellation }),
678689
]);
679690

@@ -1086,6 +1097,7 @@ function withDurationAndSlowEventOnTimeout<T>(
10861097
promise: Promise<T>,
10871098
name:
10881099
| 'getPullRequest'
1100+
| 'searchPullRequests'
10891101
| 'getMyPullRequests'
10901102
| 'getCodeSuggestionCounts'
10911103
| 'getCodeSuggestions'

0 commit comments

Comments
 (0)