GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor(source-control): GitManager reads provider capabilities, not… · pingdotgg/t3code@e1398d3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • git
      • GitManager.test.ts
      • GitManager.ts
    • sourceControl
      • SourceControlProviderRegistry.ts
  • packages
    • source-control-core/src/server
      • SourceControlProvider.ts
    • source-control-forgejo/src/server
      • ForgejoSourceControlProvider.ts
    • source-control-github/src/server
      • GitHubSourceControlProvider.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/git/GitManager.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/git/GitManager.test.ts
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import * as VcsProjectConfig from "../vcs/VcsProjectConfig.ts";
5555
import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
5656
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
5757
import * as GitWorkflowService from "./GitWorkflowService.ts";
58+
import * as ForgejoSourceControlProvider from "@t3tools/source-control-forgejo/server/ForgejoSourceControlProvider";
5859
import * as GitLabSourceControlProvider from "@t3tools/source-control-gitlab/server/GitLabSourceControlProvider";
5960
import {
6061
ForgejoPullRequestSchema,
@@ -545,6 +546,10 @@ function createGitHubProviderWithFakeGh(scenario: FakeGhScenario = {}): {
545546
return {
546547
service: {
547548
kind: "github",
549+
// The GitHub provider's own capabilities, which GitManager reads instead of the kind.
550+
headBranchProbeLimit: 100,
551+
searchesOwnerHeadSelectors: false,
552+
followsChangeRequestTemplates: true,
548553
listChangeRequests: (input) =>
549554
input.state === "open"
550555
? execute({
@@ -4232,7 +4237,7 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
42324237
};
42334238
const repository = GitManager.parseRepositoryNameWithOwnerFromRemoteUrl(
42344239
`https://forgejo.example/forgejo/${owner}/project.git`,
4235-
"forgejo",
4240+
{ repositoryNameFromRemoteUrl: ForgejoSourceControlProvider.repositoryNameFromRemoteUrl },
42364241
);
42374242
expect(repository).toBe(`${owner}/project`);
42384243
const context = {
@@ -4253,13 +4258,13 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
42534258
expect(
42544259
GitManager.parseRepositoryNameWithOwnerFromRemoteUrl(
42554260
"[email protected]:maria/project.git",
4256-
"forgejo",
4261+
{ repositoryNameFromRemoteUrl: ForgejoSourceControlProvider.repositoryNameFromRemoteUrl },
42574262
),
42584263
).toBe("maria/project");
42594264
expect(
42604265
GitManager.parseRepositoryNameWithOwnerFromRemoteUrl(
42614266
"https://gitlab.example/group/maria/project.git",
4262-
"gitlab",
4267+
{},
42634268
),
42644269
).toBe("group/maria/project");
42654270
}),
Collapse file

‎apps/server/src/git/GitManager.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/git/GitManager.ts
+29-45Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
ModelSelection,
3636
type ProjectId,
3737
SourceControlProviderError,
38-
type SourceControlProviderKind,
3938
type SourceControlWritingStyleSettings,
4039
type ThreadId,
4140
type ThreadPullRequestKey,
@@ -81,6 +80,7 @@ import { detachStackFrame } from "./detachStackFrame.ts";
8180
import * as ServerSettings from "../serverSettings.ts";
8281
import type { GitManagerServiceError } from "@t3tools/contracts";
8382
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
83+
import * as SourceControlProvider from "@t3tools/source-control-core/server/SourceControlProvider";
8484
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
8585
import { detectPrTemplate } from "../sourceControl/PrTemplateDetection.ts";
8686
import type { ChangeRequest } from "@t3tools/contracts";
@@ -314,27 +314,20 @@ function resolvePullRequestWorktreeLocalBranchName(
314314
return `t3code/pr-${pullRequest.number}/${suffix}`;
315315
}
316316

317+
/** The repository's `owner/name` from a remote URL, as `provider` reads its remote paths. */
317318
export function parseRepositoryNameWithOwnerFromRemoteUrl(
318319
url: string | null,
319-
providerKind?: ChangeRequest["provider"],
320+
provider?: Pick<
321+
SourceControlProvider.SourceControlProvider["Service"],
322+
"repositoryNameFromRemoteUrl"
323+
>,
320324
): string | null {
321325
const trimmed = url?.trim() ?? "";
322-
if (trimmed.length === 0) {
323-
return null;
324-
}
325-
326-
const match =
327-
/^(?:[^@/\s]+@[^:/\s]+:|(?:ssh|https?|git):\/\/[^/]+\/)((?:[^/\s]+\/)+[^/\s]+?)(?:\.git)?\/?$/iu.exec(
328-
trimmed,
329-
);
330-
const repositoryNameWithOwner = match?.[1]?.trim() ?? "";
331-
// Forgejo HTTP paths can include an installation mount; its API always names owner/repo.
332-
if (providerKind === "forgejo" && /^https?:\/\//iu.test(trimmed)) {
333-
return repositoryNameWithOwner.length > 0
334-
? repositoryNameWithOwner.split("/").slice(-2).join("/")
335-
: null;
336-
}
337-
return repositoryNameWithOwner.length > 0 ? repositoryNameWithOwner : null;
326+
if (trimmed.length === 0) return null;
327+
return (
328+
provider?.repositoryNameFromRemoteUrl?.(trimmed) ??
329+
SourceControlProvider.repositoryPathFromRemoteUrl(trimmed)
330+
);
338331
}
339332

340333
function parseRepositoryOwnerLogin(nameWithOwner: string | null): string | null {
@@ -625,22 +618,14 @@ function parseCustomCommitMessage(raw: string): { subject: string; body: string
625618
};
626619
}
627620

628-
// Without the owner selector, a bare branch name also lists same-named
629-
// branches on other forks (`main`, `patch-1`), so GitHub probes ask for a full
630-
// page and let matchesBranchHeadContext pick the right head. gh fetches up to
631-
// 100 in one request, and GitHub prices a first:100 connection like first:1.
632-
const GITHUB_HEAD_BRANCH_PROBE_LIMIT = 100;
633-
634-
// `gh pr list --head` filters on the head ref name alone and accepts anything, so an
635-
// `owner:branch` or `remote:branch` selector silently lists zero pull requests
636-
// while spending a GraphQL call. Git branch names cannot contain ":", and the
637-
// bare head branch is always among the selectors, so GitHub probes skip them and
638-
// leave the owner check to matchesBranchHeadContext.
621+
// Git branch names cannot contain ":", and the bare head branch is always among the
622+
// selectors, so a host that cannot search `owner:branch` loses nothing by skipping them:
623+
// matchesBranchHeadContext does the owner check on the results.
639624
function probeableHeadSelectors(
640-
providerKind: SourceControlProviderKind,
625+
provider: SourceControlProvider.SourceControlProvider["Service"],
641626
headSelectors: ReadonlyArray<string>,
642627
): ReadonlyArray<string> {
643-
return providerKind === "github"
628+
return provider.searchesOwnerHeadSelectors === false
644629
? headSelectors.filter((selector) => !selector.includes(":"))
645630
: headSelectors;
646631
}
@@ -1430,15 +1415,14 @@ export const make = Effect.gen(function* () {
14301415
/^https?:\/\//iu.test(remoteUrl) &&
14311416
(repositoryNameWithOwner?.split("/").length ?? 0) > 2
14321417
) {
1433-
const detected = detectSourceControlProviderFromGitRemoteUrl(remoteUrl);
1434-
const kind =
1435-
detected?.kind === "unknown"
1436-
? yield* sourceControlProvider(cwd).pipe(
1437-
Effect.map((provider) => provider.kind),
1438-
Effect.orElseSucceed(() => undefined),
1439-
)
1440-
: detected?.kind;
1441-
repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl, kind);
1418+
const kind = detectSourceControlProviderFromGitRemoteUrl(remoteUrl)?.kind;
1419+
const provider =
1420+
kind === undefined
1421+
? undefined
1422+
: yield* (
1423+
kind === "unknown" ? sourceControlProvider(cwd) : sourceControlProviders.get(kind)
1424+
).pipe(Effect.orElseSucceed(() => undefined));
1425+
repositoryNameWithOwner = parseRepositoryNameWithOwnerFromRemoteUrl(remoteUrl, provider);
14421426
}
14431427
return {
14441428
remoteUrlKey: remoteUrl ? normalizeGitRemoteUrl(remoteUrl) : null,
@@ -1701,13 +1685,13 @@ export const make = Effect.gen(function* () {
17011685
>,
17021686
) {
17031687
const provider = yield* sourceControlProvider(cwd);
1704-
const headSelectors = probeableHeadSelectors(provider.kind, headContext.headSelectors);
1688+
const headSelectors = probeableHeadSelectors(provider, headContext.headSelectors);
17051689
for (const headSelector of headSelectors) {
17061690
const pullRequests = yield* provider.listChangeRequests({
17071691
cwd,
17081692
headSelector,
17091693
state: "open",
1710-
limit: provider.kind === "github" ? GITHUB_HEAD_BRANCH_PROBE_LIMIT : 1,
1694+
limit: provider.headBranchProbeLimit ?? 1,
17111695
});
17121696
const normalizedPullRequests = pullRequests.map(toPullRequestInfo);
17131697

@@ -1733,12 +1717,12 @@ export const make = Effect.gen(function* () {
17331717
const parsedByNumber = new Map<number, PullRequestInfo>();
17341718

17351719
const provider = yield* sourceControlProvider(cwd);
1736-
for (const headSelector of probeableHeadSelectors(provider.kind, headContext.headSelectors)) {
1720+
for (const headSelector of probeableHeadSelectors(provider, headContext.headSelectors)) {
17371721
const pullRequests = yield* provider.listChangeRequests({
17381722
cwd,
17391723
headSelector,
17401724
state: "all",
1741-
limit: provider.kind === "github" ? GITHUB_HEAD_BRANCH_PROBE_LIMIT : 20,
1725+
limit: provider.headBranchProbeLimit ?? 20,
17421726
});
17431727

17441728
for (const pr of pullRequests.map(toPullRequestInfo)) {
@@ -2129,7 +2113,7 @@ export const make = Effect.gen(function* () {
21292113
const rangeContext = yield* gitCore.readRangeContext(cwd, baseRangeRef);
21302114
const policy = yield* resolveStylePolicy(cwd, settings);
21312115
const changeRequestTemplate =
2132-
settings.style.followChangeRequestTemplates && provider.kind === "github"
2116+
settings.style.followChangeRequestTemplates && provider.followsChangeRequestTemplates === true
21332117
? Option.getOrUndefined(yield* detectPrTemplate(cwd, baseRangeRef, gitCore.execute))
21342118
: undefined;
21352119

Collapse file

‎apps/server/src/sourceControl/SourceControlProviderRegistry.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/sourceControl/SourceControlProviderRegistry.ts
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ function bindProviderContext(
158158
}
159159

160160
return SourceControlProvider.SourceControlProvider.of({
161-
kind: provider.kind,
162-
...(provider.resolveLink ? { resolveLink: provider.resolveLink } : {}),
161+
...provider,
163162
listChangeRequests: (input) =>
164163
provider.listChangeRequests({
165164
...input,
Collapse file

‎packages/source-control-core/src/server/SourceControlProvider.ts‎

Copy file name to clipboardExpand all lines: packages/source-control-core/src/server/SourceControlProvider.ts
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,42 @@ export function sourceControlRefFromInput(input: {
9292
return input.source ?? parseSourceControlOwnerRef(input.headSelector);
9393
}
9494

95+
/** The repository path a remote URL names (`owner/name`, or deeper for nested groups). */
96+
export function repositoryPathFromRemoteUrl(url: string | null): string | null {
97+
const trimmed = url?.trim() ?? "";
98+
if (trimmed.length === 0) {
99+
return null;
100+
}
101+
102+
const match =
103+
/^(?:[^@/\s]+@[^:/\s]+:|(?:ssh|https?|git):\/\/[^/]+\/)((?:[^/\s]+\/)+[^/\s]+?)(?:\.git)?\/?$/iu.exec(
104+
trimmed,
105+
);
106+
const path = match?.[1]?.trim() ?? "";
107+
return path.length > 0 ? path : null;
108+
}
109+
95110
export class SourceControlProvider extends Context.Service<
96111
SourceControlProvider,
97112
{
98113
readonly kind: SourceControlProviderKind;
114+
/**
115+
* How many change requests to read per head selector when looking up a branch's change
116+
* request. Defaults to 1 for the open one and 20 for the latest of any state.
117+
*/
118+
readonly headBranchProbeLimit?: number;
119+
/**
120+
* Whether a `owner:branch` head selector can be searched. When false, only bare branch
121+
* selectors are probed and the owner is checked against the results instead.
122+
*/
123+
readonly searchesOwnerHeadSelectors?: boolean;
124+
/** Whether change request content generation follows the repository's PR template. */
125+
readonly followsChangeRequestTemplates?: boolean;
126+
/**
127+
* The repository's `owner/name` from a remote URL, for a host whose remote paths can carry
128+
* more than that (Forgejo's HTTP installation mount). Absent means the whole path.
129+
*/
130+
readonly repositoryNameFromRemoteUrl?: (url: string) => string | null;
99131
/** Optional capability for issue and change-request subjects. */
100132
readonly resolveLink?: ResolveSourceControlLink;
101133
readonly listChangeRequests: (input: {
Collapse file

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.ts‎

Copy file name to clipboardExpand all lines: packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ export const makeDiscovery = Effect.gen(function* () {
158158
} satisfies SourceControlManagedCliDiscoverySpec;
159159
});
160160

161+
/** HTTP paths can include an installation mount; Forgejo's API always names owner/repo. */
162+
export function repositoryNameFromRemoteUrl(url: string): string | null {
163+
const path = SourceControlProvider.repositoryPathFromRemoteUrl(url);
164+
return path === null || !/^https?:\/\//iu.test(url.trim())
165+
? path
166+
: path.split("/").slice(-2).join("/");
167+
}
168+
161169
const RepositorySchema = Schema.Struct({
162170
full_name: Schema.String,
163171
clone_url: Schema.String,
@@ -228,6 +236,7 @@ export const make = Effect.gen(function* () {
228236
});
229237
return SourceControlProvider.SourceControlProvider.of({
230238
kind: "forgejo",
239+
repositoryNameFromRemoteUrl,
231240
listChangeRequests: (input) =>
232241
Effect.gen(function* () {
233242
const repo = yield* cli.resolveRepository(input);
Collapse file

‎packages/source-control-github/src/server/GitHubSourceControlProvider.ts‎

Copy file name to clipboardExpand all lines: packages/source-control-github/src/server/GitHubSourceControlProvider.ts
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,16 @@ export const make = Effect.gen(function* () {
939939

940940
return SourceControlProvider.SourceControlProvider.of({
941941
kind: "github",
942+
// Without the owner selector, a bare branch name also lists same-named branches on other
943+
// forks (`main`, `patch-1`), so probes ask for a full page and let the owner check pick the
944+
// right head. gh fetches up to 100 in one request, and GitHub prices a first:100
945+
// connection like first:1.
946+
headBranchProbeLimit: 100,
947+
// `gh pr list --head` filters on the head ref name alone and accepts anything, so an
948+
// `owner:branch` or `remote:branch` selector silently lists zero pull requests while
949+
// spending a GraphQL call.
950+
searchesOwnerHeadSelectors: false,
951+
followsChangeRequestTemplates: true,
942952
resolveLink: (input) => {
943953
// Automatic enrichment must not send ambient CLI credentials to a host from message text.
944954
if (input.url.host !== "github.com") return undefined;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.