Skip to content

Commit 99c86e1

Browse files
committed
Moves PR URL util functions to pullRequest.ts from github.ts and rename,
because they are not GitHub specific. (#3543)
1 parent d44f344 commit 99c86e1

File tree

5 files changed

+59
-59
lines changed

5 files changed

+59
-59
lines changed

src/plus/integrations/providers/__tests__/github.test.ts renamed to src/git/models/__tests__/pullRequest.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22
import { suite, test } from 'mocha';
3-
import { getPullRequestIdentityValuesFromSearch } from '../github';
3+
import { getPullRequestIdentityValuesFromSearch } from '../pullRequest'
44

55
suite('Test GitHub PR URL parsing to identity: getPullRequestIdentityValuesFromSearch()', () => {
66
function t(message: string, query: string, prNumber: string | undefined, ownerAndRepo?: string) {

src/git/models/pullRequest.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Uri, window } from 'vscode';
22
import { Schemes } from '../../constants';
33
import { Container } from '../../container';
44
import type { RepositoryIdentityDescriptor } from '../../gk/models/repositoryIdentities';
5+
import type { EnrichablePullRequest } from '../../plus/integrations/providers/models';
56
import { formatDate, fromNow } from '../../system/date';
67
import { memoize } from '../../system/decorators/memoize';
78
import type { LeftRightCommitCountResult } from '../gitProvider';
@@ -415,3 +416,53 @@ export async function getOpenedPullRequestRepo(
415416
const repo = await getOrOpenPullRequestRepository(container, pr, { promptIfNeeded: true });
416417
return repo;
417418
}
419+
420+
export type PullRequestURLIdentity = {
421+
ownerAndRepo?: string;
422+
prNumber?: string;
423+
};
424+
425+
export function getPullRequestIdentityValuesFromSearch(search: string): PullRequestURLIdentity {
426+
let ownerAndRepo: string | undefined = undefined;
427+
let prNumber: string | undefined = undefined;
428+
429+
let match = search.match(/([^/]+\/[^/]+)\/pull\/(\d+)/); // with org and rep name
430+
if (match != null) {
431+
ownerAndRepo = match[1];
432+
prNumber = match[2];
433+
}
434+
435+
if (prNumber == null) {
436+
match = search.match(/(?:\/|^)pull\/(\d+)/); // without repo name
437+
if (match != null) {
438+
prNumber = match[1];
439+
}
440+
}
441+
442+
if (prNumber == null) {
443+
match = search.match(/(?:#|\/)(\d+)/); // any number starting with # or /
444+
if (match != null) {
445+
prNumber = match[1];
446+
}
447+
}
448+
449+
return { ownerAndRepo: ownerAndRepo, prNumber: prNumber };
450+
}
451+
452+
export function doesPullRequestSatisfyRepositoryURLIdentity(
453+
pr: EnrichablePullRequest | undefined,
454+
{ ownerAndRepo, prNumber }: PullRequestURLIdentity,
455+
): boolean {
456+
if (pr == null) {
457+
return false;
458+
}
459+
const satisfiesPrNumber = prNumber != null && pr.number === parseInt(prNumber, 10);
460+
if (!satisfiesPrNumber) {
461+
return false;
462+
}
463+
const satisfiesOwnerAndRepo = ownerAndRepo != null && pr.repoIdentity.name === ownerAndRepo;
464+
if (!satisfiesOwnerAndRepo) {
465+
return false;
466+
}
467+
return true;
468+
}

src/plus/integrations/providers/github.ts

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type {
1313
} from '../../../git/models/pullRequest';
1414
import type { RepositoryMetadata } from '../../../git/models/repositoryMetadata';
1515
import { log } from '../../../system/decorators/log';
16-
import type { LaunchpadPullRequest } from '../../launchpad/launchpadProvider';
1716
import { ensurePaidPlan } from '../../utils';
1817
import type {
1918
IntegrationAuthenticationProviderDescriptor,
@@ -318,53 +317,3 @@ export class GitHubEnterpriseIntegration extends GitHubIntegrationBase<SelfHoste
318317
return super.connect(source);
319318
}
320319
}
321-
322-
export type GitHubPullRequestURLIdentity = {
323-
ownerAndRepo?: string;
324-
prNumber?: string;
325-
};
326-
327-
export function getPullRequestIdentityValuesFromSearch(search: string): GitHubPullRequestURLIdentity {
328-
let ownerAndRepo: string | undefined = undefined;
329-
let prNumber: string | undefined = undefined;
330-
331-
let match = search.match(/([^/]+\/[^/]+)\/pull\/(\d+)/); // with org and rep name
332-
if (match != null) {
333-
ownerAndRepo = match[1];
334-
prNumber = match[2];
335-
}
336-
337-
if (prNumber == null) {
338-
match = search.match(/(?:\/|^)pull\/(\d+)/); // without repo name
339-
if (match != null) {
340-
prNumber = match[1];
341-
}
342-
}
343-
344-
if (prNumber == null) {
345-
match = search.match(/(?:#|\/)(\d+)/); // any number starting with # or /
346-
if (match != null) {
347-
prNumber = match[1];
348-
}
349-
}
350-
351-
return { ownerAndRepo: ownerAndRepo, prNumber: prNumber };
352-
}
353-
354-
export function doesPullRequestSatisfyGitHubRepositoryURLIdentity(
355-
pr: LaunchpadPullRequest | undefined,
356-
{ ownerAndRepo, prNumber }: GitHubPullRequestURLIdentity,
357-
): boolean {
358-
if (pr == null) {
359-
return false;
360-
}
361-
const satisfiesPrNumber = prNumber != null && pr.number === parseInt(prNumber, 10);
362-
if (!satisfiesPrNumber) {
363-
return false;
364-
}
365-
const satisfiesOwnerAndRepo = ownerAndRepo != null && pr.repoIdentity.name === ownerAndRepo;
366-
if (!satisfiesOwnerAndRepo) {
367-
return false;
368-
}
369-
return true;
370-
}

src/plus/launchpad/launchpad.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ import { HostingIntegrationId, SelfHostedIntegrationId } from '../../constants.i
4242
import type { LaunchpadTelemetryContext, Source, Sources, TelemetryEvents } from '../../constants.telemetry';
4343
import type { Container } from '../../container';
4444
import { PlusFeatures } from '../../features';
45+
import {
46+
doesPullRequestSatisfyRepositoryURLIdentity,
47+
getPullRequestIdentityValuesFromSearch,
48+
} from '../../git/models/pullRequest';
4549
import type { QuickPickItemOfT } from '../../quickpicks/items/common';
4650
import { createQuickPickItemOfT, createQuickPickSeparator } from '../../quickpicks/items/common';
4751
import type { DirectiveQuickPickItem } from '../../quickpicks/items/directive';
@@ -54,10 +58,6 @@ import { createAsyncDebouncer } from '../../system/vscode/asyncDebouncer';
5458
import { executeCommand } from '../../system/vscode/command';
5559
import { configuration } from '../../system/vscode/configuration';
5660
import { openUrl } from '../../system/vscode/utils';
57-
import {
58-
doesPullRequestSatisfyGitHubRepositoryURLIdentity,
59-
getPullRequestIdentityValuesFromSearch,
60-
} from '../integrations/providers/github';
6161
import { ProviderBuildStatusState, ProviderPullRequestReviewState } from '../integrations/providers/models';
6262
import type {
6363
LaunchpadAction,
@@ -603,7 +603,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
603603
const launchpadItems = quickpick.items.filter((i): i is LaunchpadItemQuickPickItem => 'item' in i);
604604
let item = launchpadItems.find(i =>
605605
// perform strict match first
606-
doesPullRequestSatisfyGitHubRepositoryURLIdentity(i.item, prUrlIdentity),
606+
doesPullRequestSatisfyRepositoryURLIdentity(i.item, prUrlIdentity),
607607
);
608608
if (item == null) {
609609
// Haven't found full match, so let's at least find something with the same pr number
@@ -623,7 +623,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
623623
// Nothing is found above, so let's perform search in the API:
624624
await updateItems(quickpick, value);
625625
quickpick.items.forEach(i => {
626-
if ('item' in i && doesPullRequestSatisfyGitHubRepositoryURLIdentity(i.item, prUrlIdentity)) {
626+
if ('item' in i && doesPullRequestSatisfyRepositoryURLIdentity(i.item, prUrlIdentity)) {
627627
i.alwaysShow = true;
628628
}
629629
});

src/plus/launchpad/launchpadProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { PullRequest, SearchedPullRequest } from '../../git/models/pullRequ
1919
import {
2020
getComparisonRefsForPullRequest,
2121
getOrOpenPullRequestRepository,
22+
getPullRequestIdentityValuesFromSearch,
2223
getRepositoryIdentityForPullRequest,
2324
} from '../../git/models/pullRequest';
2425
import type { GitRemote } from '../../git/models/remote';
@@ -42,7 +43,6 @@ import type { ShowWipArgs } from '../../webviews/commitDetails/protocol';
4243
import type { IntegrationResult } from '../integrations/integration';
4344
import type { ConnectionStateChangeEvent } from '../integrations/integrationService';
4445
import type { GitHubRepositoryDescriptor } from '../integrations/providers/github';
45-
import { getPullRequestIdentityValuesFromSearch } from '../integrations/providers/github';
4646
import type { EnrichablePullRequest, ProviderActionablePullRequest } from '../integrations/providers/models';
4747
import {
4848
fromProviderPullRequest,

0 commit comments

Comments
 (0)