30 lines
927 B
TypeScript
30 lines
927 B
TypeScript
const GITHUB_PULL_REQUEST_URL_PATTERN =
|
|
/^https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/(\d+)(?:[/?#].*)?$/i;
|
|
const PULL_REQUEST_NUMBER_PATTERN = /^#?(\d+)$/;
|
|
const GITHUB_CLI_PR_CHECKOUT_PATTERN = /^gh\s+pr\s+checkout\s+(.+)$/i;
|
|
|
|
export function parsePullRequestReference(input: string): string | null {
|
|
const trimmed = input.trim();
|
|
if (trimmed.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const ghCliCheckoutMatch = GITHUB_CLI_PR_CHECKOUT_PATTERN.exec(trimmed);
|
|
const normalizedInput = ghCliCheckoutMatch?.[1]?.trim() ?? trimmed;
|
|
if (normalizedInput.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const urlMatch = GITHUB_PULL_REQUEST_URL_PATTERN.exec(normalizedInput);
|
|
if (urlMatch?.[1]) {
|
|
return normalizedInput;
|
|
}
|
|
|
|
const numberMatch = PULL_REQUEST_NUMBER_PATTERN.exec(normalizedInput);
|
|
if (numberMatch?.[1]) {
|
|
return normalizedInput.startsWith("#") ? normalizedInput : numberMatch[1];
|
|
}
|
|
|
|
return null;
|
|
}
|