Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/git/models/pullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { formatDate, fromNow } from '../../system/date';
import { memoize } from '../../system/decorators/memoize';
import type { LeftRightCommitCountResult } from '../gitProvider';
import type { IssueOrPullRequest, IssueRepository, IssueOrPullRequestState as PullRequestState } from './issue';
import type { PullRequestURLIdentity } from './pullRequest.utils';
import type { PullRequestUrlIdentity } from './pullRequest.utils';
import type { ProviderReference } from './remoteProvider';
import type { Repository } from './repository';
import { createRevisionRange , shortenRevision } from './revision.utils';
import { createRevisionRange, shortenRevision } from './revision.utils';

export type { PullRequestState };

Expand Down Expand Up @@ -420,7 +420,7 @@ export async function getOpenedPullRequestRepo(

export function doesPullRequestSatisfyRepositoryURLIdentity(
pr: EnrichablePullRequest | undefined,
{ ownerAndRepo, prNumber }: PullRequestURLIdentity,
{ ownerAndRepo, prNumber }: PullRequestUrlIdentity,
): boolean {
if (pr == null) {
return false;
Expand Down
10 changes: 7 additions & 3 deletions src/git/models/pullRequest.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
// To avoid this file has been created that can collect more simple functions which
// don't require Container and can be tested.

export type PullRequestURLIdentity = {
import type { HostingIntegrationId } from '../../constants.integrations';

export interface PullRequestUrlIdentity {
provider?: HostingIntegrationId;

ownerAndRepo?: string;
prNumber?: string;
};
}

export function getPullRequestIdentityValuesFromSearch(search: string): PullRequestURLIdentity {
export function getPullRequestIdentityValuesFromSearch(search: string): PullRequestUrlIdentity {
let ownerAndRepo: string | undefined = undefined;
let prNumber: string | undefined = undefined;

Expand Down
19 changes: 19 additions & 0 deletions src/plus/integrations/providers/github/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Endpoints } from '@octokit/types';
import { HostingIntegrationId } from '../../../../constants.integrations';
import { GitFileIndexStatus } from '../../../../git/models/file';
import type { IssueLabel } from '../../../../git/models/issue';
import { Issue, RepositoryAccessLevel } from '../../../../git/models/issue';
Expand All @@ -10,6 +11,7 @@ import {
PullRequestReviewState,
PullRequestStatusCheckRollupState,
} from '../../../../git/models/pullRequest';
import type { PullRequestUrlIdentity } from '../../../../git/models/pullRequest.utils';
import type { Provider } from '../../../../git/models/remoteProvider';

export interface GitHubBlame {
Expand Down Expand Up @@ -508,3 +510,20 @@ export function fromCommitFileStatus(
}
return undefined;
}

const prUrlRegex = /^(?:https?:\/\/)?(?:github\.com\/)?([^/]+\/[^/]+)\/pull\/(\d+)/i;

export function isMaybeGitHubPullRequestUrl(url: string): boolean {
if (url == null) return false;

return prUrlRegex.test(url);
}

export function getGitHubPullRequestIdentityFromMaybeUrl(url: string): RequireSome<PullRequestUrlIdentity, 'provider'> {
if (url == null) return { prNumber: undefined, ownerAndRepo: undefined, provider: HostingIntegrationId.GitHub };

const match = prUrlRegex.exec(url);
if (match == null) return { prNumber: undefined, ownerAndRepo: undefined, provider: HostingIntegrationId.GitHub };

return { prNumber: match[2], ownerAndRepo: match[1], provider: HostingIntegrationId.GitHub };
}
17 changes: 17 additions & 0 deletions src/plus/integrations/providers/gitlab/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HostingIntegrationId } from '../../../../constants.integrations';
import type { PullRequestState } from '../../../../git/models/pullRequest';
import { PullRequest } from '../../../../git/models/pullRequest';
import type { PullRequestUrlIdentity } from '../../../../git/models/pullRequest.utils';
import type { Provider } from '../../../../git/models/remoteProvider';
import type { Integration } from '../../integration';
import type { ProviderPullRequest } from '../models';
Expand Down Expand Up @@ -150,3 +152,18 @@ export function fromGitLabMergeRequestProvidersApi(pr: ProviderPullRequest, prov
};
return fromProviderPullRequest(wrappedPr, provider);
}

const prUrlRegex = /^(?:https?:\/\/)?(?:gitlab\.com\/)?(.+?)\/-\/merge_requests\/(\d+)/i;

export function isMaybeGitLabPullRequestUrl(url: string): boolean {
return prUrlRegex.test(url);
}

export function getGitLabPullRequestIdentityFromMaybeUrl(url: string): RequireSome<PullRequestUrlIdentity, 'provider'> {
if (url == null) return { prNumber: undefined, ownerAndRepo: undefined, provider: HostingIntegrationId.GitLab };

const match = prUrlRegex.exec(url);
if (match == null) return { prNumber: undefined, ownerAndRepo: undefined, provider: HostingIntegrationId.GitLab };

return { prNumber: match[2], ownerAndRepo: match[1], provider: HostingIntegrationId.GitLab };
}
Loading
Loading