Open History to see the last commit
1import {
2 GitHubHost,
3 makeProviderSettingsSchema,
4 secretSetting,
5 pullRequestHostOf,
6 SourceControlProviderKind,
7 TrimmedNonEmptyString,
8 TrimmedString,
9} from "@t3tools/contracts";
10import * as Effect from "effect/Effect";
11import * as Schema from "effect/Schema";
12import {
13 defineSourceControlClient,
14 isChangeRequestInProjectRepository,
15 isChangeRequestOnProjectHost,
16 isChangeRequestPath,
17} from "@t3tools/source-control-core/client/definition";
18
19const CHECKOUT_COMMAND = /^gh\s+pr\s+checkout\s+(.+)$/i;
20const CHANGE_REQUEST_REFERENCE =
21 /^https:\/\/github\.com\/[^/\s]+\/[^/\s]+\/pull\/(\d+)(?:[/?#].*)?$/i;
22
23const KIND = SourceControlProviderKind.make("github");
24
25export const GitHubHostChoice = Schema.Struct({
26 account: Schema.optionalKey(TrimmedNonEmptyString),
27 enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
28});
29export type GitHubHostChoice = typeof GitHubHostChoice.Type;
30
31export const settings = makeProviderSettingsSchema({
32 hosts: Schema.Record(GitHubHost, GitHubHostChoice).pipe(
33 Schema.withDecodingDefault(Effect.succeed({})),
34 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
35 ),
36 tokens: Schema.Record(GitHubHost, TrimmedString).pipe(
37 Schema.withDecodingDefault(Effect.succeed({})),
38 secretSetting,
39 Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
40 ),
41});
42export type GitHubSettings = typeof settings.Type;
43
44export const definition = defineSourceControlClient({
45 kind: KIND,
46 label: "GitHub",
47 pickerLabel: "GitHub",
48 icon: "github",
49 changeRequest: { shortLabel: "PR", singular: "pull request" },
50 repositoryPathHint: "owner/repo",
51 publicHost: "github.com",
52 publishDescription: "github.com",
53 publishHost: () => "github.com",
54 newRepositoryOwner: (account) => ({ owner: account }),
55 defaultCloneTransport: "https",
56 changeRequestUrl: ({ host, repository, number }) =>
57 `https://${host}/${repository}/pull/${number}`,
58 changeRequestActions: new Set([
59 "merge",
60 "ready",
61 "draft",
62 "close",
63 "reopen",
64 "delete-branch",
65 "update-branch",
66 "enable-auto-merge",
67 "disable-auto-merge",
68 "revert",
69 "approve-workflows",
70 ] as const),
71 checkoutCommand: ({ number }) => `gh pr checkout ${number}`,
72 authorProfileUrl: (login, repositoryUrl) =>
73 login.endsWith("[bot]")
74 ? null
75 : new URL(`/${encodeURIComponent(login)}`, repositoryUrl).toString(),
76 referenceAutolinkRepositoryUrl: (repositoryUrl) => repositoryUrl,
77 reviewSummaryRequired: () => false,
78 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
79 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
80 changeRequestUrlHost: (url) => url.hostname,
81 checkoutChangeRequestHost: (identity) => pullRequestHostOf(identity, KIND),
82 isChangeRequestInRepository: (identity, link) =>
83 isChangeRequestInProjectRepository(KIND, identity, link),
84 canReadChangeRequestOnHost: (identity, link) =>
85 isChangeRequestOnProjectHost(KIND, identity, link),
86 isChangeRequestUrl: (url) => isChangeRequestPath(url, "/pull/"),
87 settings,
88});