Open History to see the last commit
1/**
2 * GitHub's client definition. Browser- and React Native-safe.
3 *
4 * @module source-control-github/client/definition
5 */
6import {
7 GitHubHost,
8 makeProviderSettingsSchema,
9 pullRequestHostOf,
10 SourceControlProviderKind,
11 TrimmedNonEmptyString,
12 TrimmedString,
13} from "@t3tools/contracts";
14import * as Effect from "effect/Effect";
15import * as Schema from "effect/Schema";
16import {
17 defineSourceControlClient,
18 isChangeRequestInProjectRepository,
19 isChangeRequestOnProjectHost,
20 isChangeRequestPath,
21} from "@t3tools/source-control-core/client/definition";
22
23const CHECKOUT_COMMAND = /^gh\s+pr\s+checkout\s+(.+)$/i;
24const CHANGE_REQUEST_REFERENCE =
25 /^https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/(\d+)(?:[/?#].*)?$/i;
26
27const KIND = SourceControlProviderKind.make("github");
28
29export const GitHubHostChoice = Schema.Struct({
30 account: Schema.optionalKey(TrimmedNonEmptyString),
31 enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
32});
33export type GitHubHostChoice = typeof GitHubHostChoice.Type;
34
35/**
36 * Per-host choices, keyed by lowercased host such as `github.com`. `hosts` pins one of the logins
37 * `gh` holds for a host instead of its active one, or turns the host off; `tokens` holds a token
38 * per host, used before `GH_TOKEN` and friends, which win over `gh`. Source Control settings draws
39 * both with GitHub's own panels, since the logins come from `gh` at runtime.
40 */
41export const settings = makeProviderSettingsSchema({
42 hosts: Schema.Record(GitHubHost, GitHubHostChoice).pipe(
43 Schema.withDecodingDefault(Effect.succeed({})),
44 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
45 ),
46 tokens: Schema.Record(GitHubHost, TrimmedString).pipe(
47 Schema.withDecodingDefault(Effect.succeed({})),
48 Schema.annotateKey({ providerSettingsForm: { hidden: true, secret: true } }),
49 ),
50});
51export type GitHubSettings = typeof settings.Type;
52
53export const definition = defineSourceControlClient({
54 kind: KIND,
55 label: "GitHub",
56 pickerLabel: "GitHub",
57 icon: "github",
58 changeRequest: { shortLabel: "PR", singular: "pull request" },
59 repositoryPathHint: "owner/repo",
60 publicHost: "github.com",
61 publishDescription: "github.com",
62 publishHost: () => "github.com",
63 newRepositoryOwner: (account) => ({ owner: account }),
64 defaultCloneTransport: "https",
65 changeRequestUrl: ({ host, repository, number }) =>
66 `https://${host}/${repository}/pull/${number}`,
67 changeRequestActions: new Set([
68 "merge",
69 "ready",
70 "draft",
71 "close",
72 "reopen",
73 "update-branch",
74 "enable-auto-merge",
75 "disable-auto-merge",
76 "revert",
77 "approve-workflows",
78 ] as const),
79 checkoutCommand: ({ number }) => `gh pr checkout ${number}`,
80 authorProfileUrl: (login, repositoryUrl) =>
81 login.endsWith("[bot]")
82 ? null
83 : new URL(`/${encodeURIComponent(login)}`, repositoryUrl).toString(),
84 referenceAutolinkRepositoryUrl: (repositoryUrl) => repositoryUrl,
85 reviewSummaryRequired: () => false,
86 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
87 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
88 changeRequestUrlHost: (url) => url.hostname,
89 // A GitHub web host is the checkout's own host, Enterprise installs included.
90 checkoutChangeRequestHost: (identity) => pullRequestHostOf(identity, KIND),
91 isChangeRequestInRepository: (identity, link) =>
92 isChangeRequestInProjectRepository(KIND, identity, link),
93 canReadChangeRequestOnHost: (identity, link) =>
94 isChangeRequestOnProjectHost(KIND, identity, link),
95 isChangeRequestUrl: (url) => isChangeRequestPath(url, "/pull/"),
96 settings,
97});