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 | buildCommitUrl, |
| 4 | changeRequestUrlBelow, |
| 5 | defineSourceControl, |
| 6 | isChangeRequestInProjectRepository, |
| 7 | isChangeRequestOnProjectHost, |
| 8 | parseChangeRequestPath, |
| 9 | parseIssuePath, |
| 10 | parseCommitPath, |
| 11 | repositoryUrlFromPagePath, |
| 12 | projectChangeRequestHost, |
| 13 | remoteWebOrigin, |
| 14 | } from "@t3tools/source-control-core/shared/definition"; |
| 15 | import { icon } from "./icon.ts"; |
| 16 | import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts"; |
| 17 | |
| 18 | const CHECKOUT_COMMAND = /^glab\s+mr\s+checkout\s+(.+)$/i; |
| 19 | const CHANGE_REQUEST_REFERENCE = |
| 20 | /^https?:\/\/[^/?#\s]+\/[^/?#\s]+(?:\/[^/?#\s]+)+\/-\/merge_requests\/(\d+)(?:[/?#].*)?$/i; |
| 21 | |
| 22 | // Self-managed GitLab runs on any hostname and nests projects in groups. |
| 23 | const CHANGE_REQUEST_PATH = /^\/([^/]+(?:\/[^/]+)+)\/-\/merge_requests\/(\d+)(?:\/|$)/u; |
| 24 | const COMMIT_PATH = /^\/([^/]+(?:\/[^/]+)+)\/-\/commit\/([0-9a-f]+)\/?$/iu; |
| 25 | const ISSUE_PATH = /^\/([^/]+(?:\/[^/]+)+)\/-\/(?:issues|work_items)\/(\d+)(?:\/|$)/u; |
| 26 | // Every GitLab page inside a project sits below its first `/-/`. |
| 27 | const REPOSITORY_PAGE_PATH = /^\/([^/]+(?:\/[^/]+)+?)\/-\//u; |
| 28 | |
| 29 | const KIND = SourceControlProviderKind.make("gitlab"); |
| 30 | |
| 31 | export const definition = defineSourceControl({ |
| 32 | kind: KIND, |
| 33 | label: "GitLab", |
| 34 | pickerLabel: "GitLab", |
| 35 | icon, |
| 36 | changeRequest: { |
| 37 | shortLabel: "MR", |
| 38 | shortLabelWithArticle: "an MR", |
| 39 | singular: "merge request", |
| 40 | plural: "merge requests", |
| 41 | }, |
| 42 | repositoryPathHint: "group/project", |
| 43 | publicHost: "gitlab.com", |
| 44 | publishDescription: "gitlab.com", |
| 45 | publishHost: () => "gitlab.com", |
| 46 | newRepositoryOwner: (account) => ({ owner: account }), |
| 47 | defaultCloneTransport: "ssh", |
| 48 | changeRequestUrl: ({ host, repository, number, remoteUrl, webUrl }) => |
| 49 | webUrl |
| 50 | ? changeRequestUrlBelow(webUrl, `-/merge_requests/${number}`) |
| 51 | : `${remoteWebOrigin(host, remoteUrl)}/${repository}/-/merge_requests/${number}`, |
| 52 | parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH), |
| 53 | parseIssueUrl: (url) => parseIssuePath(url, ISSUE_PATH), |
| 54 | commitUrl: (input) => buildCommitUrl(input, "-/commit"), |
| 55 | parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH), |
| 56 | repositoryUrlFromPage: (url) => repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH), |
| 57 | // `!12` already links the merge request itself; `#12` is always an issue. |
| 58 | changeRequestUrlFromReferenceAutolink: (url) => |
| 59 | CHANGE_REQUEST_PATH.test(url.pathname) ? url.toString() : null, |
| 60 | changeRequestActions: new Set([ |
| 61 | "merge", |
| 62 | "ready", |
| 63 | "draft", |
| 64 | "close", |
| 65 | "reopen", |
| 66 | "update-branch", |
| 67 | "enable-auto-merge", |
| 68 | "disable-auto-merge", |
| 69 | "revert", |
| 70 | "delete-branch", |
| 71 | ] as const), |
| 72 | checkoutCommand: ({ number }) => `glab mr checkout ${number}`, |
| 73 | // `accept` is glab's alias for `merge`. |
| 74 | changeRequestCloseCommand: /\bglab\s+mr\s+(?:merge|accept|close)\b/u, |
| 75 | // `new` is glab's alias for `create`. |
| 76 | changeRequestCreateCommand: /\bglab\s+mr\s+(?:create|new)\b/u, |
| 77 | // Bot users, unlike GitHub's, have real profile pages. |
| 78 | authorProfileUrl: (login, repositoryUrl) => |
| 79 | new URL(`/${encodeURIComponent(login)}`, repositoryUrl).toString(), |
| 80 | referenceAutolinks: gitLabReferenceAutolinks, |
| 81 | reviewSummaryRequired: () => false, |
| 82 | checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null, |
| 83 | isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url), |
| 84 | checkoutChangeRequestHost: () => null, |
| 85 | changeRequestHeadRef: (number) => `refs/merge-requests/${number}/head`, |
| 86 | recognizeRemote: ({ host, hostname }) => |
| 87 | hostname === "gitlab.com" || hostname.split(".").includes("gitlab") |
| 88 | ? { |
| 89 | kind: KIND, |
| 90 | name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted", |
| 91 | baseUrl: `https://${host}`, |
| 92 | } |
| 93 | : null, |
| 94 | isChangeRequestInRepository: (identity, link) => |
| 95 | isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link), |
| 96 | canReadChangeRequestOnHost: (identity, link) => |
| 97 | isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link), |
| 98 | }); |