Support gh pr checkout pull request references (#1457)
This commit is contained in:
1 parent
02989fe600
commit
d8a485ea7e
8 files changed
+324
-17
No files matched your search
@@ -1,6 +1,7 @@
|
||||
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();
|
||||
@@ -8,14 +9,20 @@ export function parsePullRequestReference(input: string): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
const urlMatch = GITHUB_PULL_REQUEST_URL_PATTERN.exec(trimmed);
|
||||
if (urlMatch?.[1]) {
|
||||
return trimmed;
|
||||
const ghCliCheckoutMatch = GITHUB_CLI_PR_CHECKOUT_PATTERN.exec(trimmed);
|
||||
const normalizedInput = ghCliCheckoutMatch?.[1]?.trim() ?? trimmed;
|
||||
if (normalizedInput.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const numberMatch = PULL_REQUEST_NUMBER_PATTERN.exec(trimmed);
|
||||
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 trimmed.startsWith("#") ? trimmed : numberMatch[1];
|
||||
return normalizedInput.startsWith("#") ? normalizedInput : numberMatch[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user