The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | defineSourceControl, |
| 4 | isChangeRequestInProjectRepository, |
| 5 | isChangeRequestOnProjectHost, |
| 6 | type ParsedChangeRequestUrl, |
| 7 | projectChangeRequestHost, |
| 8 | } from "@t3tools/source-control-core/shared/definition"; |
| 9 | import { icon } from "./icon.ts"; |
| 10 | |
| 11 | /** Origin's git host; its browser pages live under `cursor.com/codebase` instead. */ |
| 12 | export const CURSOR_ORIGIN_GIT_HOST = "origin.cursor.com"; |
| 13 | export const CURSOR_ORIGIN_WEB_BASE_URL = "https://cursor.com/codebase"; |
| 14 | |
| 15 | const CHECKOUT_COMMAND = /^origin\s+pr\s+checkout\s+(.+)$/i; |
| 16 | const CHANGE_REQUEST_PATH = /^\/codebase\/([^/]+\/[^/]+)\/pull\/(\d+)(?:\/|$)/u; |
| 17 | |
| 18 | const KIND = SourceControlProviderKind.make("cursor-origin"); |
| 19 | |
| 20 | /** A cursor.com page names a repository on Origin's git host, which is what projects key under. */ |
| 21 | export function parseChangeRequestUrl(url: URL): ParsedChangeRequestUrl | null { |
| 22 | const hostname = url.hostname.toLowerCase(); |
| 23 | if (url.protocol !== "https:" && url.protocol !== "http:") return null; |
| 24 | if (hostname !== "cursor.com" && hostname !== "www.cursor.com") return null; |
| 25 | const match = CHANGE_REQUEST_PATH.exec(url.pathname); |
| 26 | const repository = match?.[1]; |
| 27 | const number = Number(match?.[2]); |
| 28 | return repository && Number.isSafeInteger(number) && number > 0 |
| 29 | ? { |
| 30 | host: CURSOR_ORIGIN_GIT_HOST, |
| 31 | repository: repository.toLowerCase(), |
| 32 | number, |
| 33 | repositoryUrl: `${CURSOR_ORIGIN_WEB_BASE_URL}/${repository}`, |
| 34 | } |
| 35 | : null; |
| 36 | } |
| 37 | |
| 38 | export const definition = defineSourceControl({ |
| 39 | kind: KIND, |
| 40 | label: "Cursor Origin", |
| 41 | pickerLabel: "Cursor Origin", |
| 42 | icon, |
| 43 | searchKeywords: ["cursor", "origin"], |
| 44 | changeRequest: { |
| 45 | shortLabel: "PR", |
| 46 | shortLabelWithArticle: "a PR", |
| 47 | singular: "pull request", |
| 48 | plural: "pull requests", |
| 49 | }, |
| 50 | repositoryPathHint: "owner/repo", |
| 51 | publicHost: CURSOR_ORIGIN_GIT_HOST, |
| 52 | publishDescription: CURSOR_ORIGIN_GIT_HOST, |
| 53 | publishHost: () => CURSOR_ORIGIN_GIT_HOST, |
| 54 | newRepositoryOwner: (account) => ({ owner: account }), |
| 55 | defaultCloneTransport: "https", |
| 56 | changeRequestUrl: ({ repository, number }) => |
| 57 | `${CURSOR_ORIGIN_WEB_BASE_URL}/${repository}/pull/${number}`, |
| 58 | parseChangeRequestUrl, |
| 59 | changeRequestUrlFromReferenceAutolink: () => null, |
| 60 | changeRequestActions: new Set(["merge", "ready", "draft", "close", "reopen"] as const), |
| 61 | checkoutCommand: ({ number }) => `origin pr checkout ${number}`, |
| 62 | changeRequestCloseCommand: /\borigin\s+pr\s+(?:merge|close)\b/u, |
| 63 | authorProfileUrl: () => null, |
| 64 | reviewSummaryRequired: () => false, |
| 65 | checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null, |
| 66 | isChangeRequestReference: (url) => { |
| 67 | try { |
| 68 | return parseChangeRequestUrl(new URL(url)) !== null; |
| 69 | } catch { |
| 70 | return false; |
| 71 | } |
| 72 | }, |
| 73 | checkoutChangeRequestHost: (identity) => projectChangeRequestHost(identity, KIND), |
| 74 | recognizeRemote: ({ hostname }) => |
| 75 | hostname === CURSOR_ORIGIN_GIT_HOST |
| 76 | ? { kind: KIND, name: "Cursor Origin", baseUrl: CURSOR_ORIGIN_WEB_BASE_URL } |
| 77 | : null, |
| 78 | isChangeRequestInRepository: (identity, link) => |
| 79 | isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link), |
| 80 | canReadChangeRequestOnHost: (identity, link) => |
| 81 | isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link), |
| 82 | }); |