* refactor(forge): forge-neutral foundation (GitHub-only) Decouple git-hosting from GitHub behind a neutral abstraction (issue #1616), GitHub-only for now; existing GitHub behaviour is unchanged. - Forge manifest, neutral ForgeService contract, forge registry + resolver, and a client forge-module registry. - GitHub code renamed to the neutral shape; PR/Issue attachment wording preserved. - forge.search.response enums parse tolerantly (unknown kind/auth state degrade instead of breaking the client). - createPullRequest reports typed CLI/auth errors instead of a generic message. - forge-resolver host/remote caches are LRU-bounded. - Forge host trust is explicit: only a known cloud host or a CLI-authenticated host is ever talked to; an unauthenticated GitHub Enterprise host fails resolution instead of routing to github.com. - Docs: forge-providers guide, glossary and i18n forge-copy conventions, architecture and rpc-namespacing terminology. - Vitest React Native mocks (unistyles, svg, linking, lucide) consolidated into shared aliased test-stubs. * feat(forge): GitLab adapter, forge-aware UI, pipelines and approvals GitLab adapter over the glab CLI on the neutral contracts: MR status, forge-aware UI, pipeline tree, and N-of-M approvals. - threadIsResolved is part of the neutral timeline item. - Pipeline load failures show an error instead of an empty section. - Manual pipeline jobs render as pending. - Fork/detached MR head pipelines are fetched by MR iid (glab ci get --merge-request). * feat(forge): Gitea family adapter (Gitea, Forgejo, Codeberg) One adapter over the tea CLI serving Gitea, Forgejo, and Codeberg on the neutral contracts. - CI status aggregates commit statuses and Actions runs together. - Gitea's terminal "warning" state maps to failure on server and client. - Gitea Actions check details are reachable from the PR pane by workflowRunId. * refactor(forge): localize compatibility handling * test(forge): expect normalized GitLab facts --------- Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
244 lines
6.3 KiB
TypeScript
244 lines
6.3 KiB
TypeScript
import { useQueryClient, type QueryClient } from "@tanstack/react-query";
|
|
import {
|
|
useCallback,
|
|
useEffect,
|
|
useMemo,
|
|
useRef,
|
|
type Dispatch,
|
|
type RefObject,
|
|
type SetStateAction,
|
|
} from "react";
|
|
import type { ComposerAttachment, UserComposerAttachment } from "@/attachments/types";
|
|
import { buildForgeSearchQueryOptions, type ForgeSearchClient } from "@/git/use-forge-search-query";
|
|
import { extractGithubRefs, type GithubRef } from "@/utils/github-refs";
|
|
import type { ForgeSearchItem } from "@getpaseo/protocol/messages";
|
|
import { isAttachmentSelectedForGithubItem, toggleGithubAttachment } from "../actions";
|
|
|
|
const AUTO_ATTACH_DEBOUNCE_MS = 300;
|
|
|
|
interface ComposerGithubAutoAttachInput {
|
|
text: string;
|
|
remoteUrl: string | null | undefined;
|
|
attachments: UserComposerAttachment[];
|
|
client: ForgeSearchClient | null;
|
|
isConnected: boolean;
|
|
serverId: string;
|
|
cwd: string;
|
|
supportsForgeSearch?: boolean;
|
|
setAttachments: Dispatch<SetStateAction<UserComposerAttachment[]>>;
|
|
}
|
|
|
|
interface ComposerGithubAutoAttachResult {
|
|
markGithubAttachmentRemoved: (attachment: ComposerAttachment | undefined) => void;
|
|
}
|
|
|
|
export function useComposerGithubAutoAttach(
|
|
params: ComposerGithubAutoAttachInput,
|
|
): ComposerGithubAutoAttachResult {
|
|
const queryClient = useQueryClient();
|
|
const latestRef = useRef(params);
|
|
const removedRefKeysRef = useRef(new Set<string>());
|
|
const pendingRefKeysRef = useRef(new Set<string>());
|
|
|
|
latestRef.current = params;
|
|
|
|
useEffect(() => {
|
|
const refs = refsReadyForLookup({
|
|
params: latestRef.current,
|
|
removedRefKeys: removedRefKeysRef.current,
|
|
pendingRefKeys: pendingRefKeysRef.current,
|
|
});
|
|
if (refs.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const timerId = setTimeout(() => {
|
|
void attachRefs({
|
|
refs,
|
|
queryClient,
|
|
latestRef,
|
|
removedRefKeys: removedRefKeysRef.current,
|
|
pendingRefKeys: pendingRefKeysRef.current,
|
|
});
|
|
}, AUTO_ATTACH_DEBOUNCE_MS);
|
|
|
|
return () => {
|
|
clearTimeout(timerId);
|
|
};
|
|
}, [
|
|
params.text,
|
|
params.remoteUrl,
|
|
params.attachments,
|
|
params.client,
|
|
params.isConnected,
|
|
params.serverId,
|
|
params.cwd,
|
|
params.supportsForgeSearch,
|
|
queryClient,
|
|
]);
|
|
|
|
const markGithubAttachmentRemoved = useCallback((attachment: ComposerAttachment | undefined) => {
|
|
const key = attachmentKey(attachment);
|
|
if (key) {
|
|
removedRefKeysRef.current.add(key);
|
|
}
|
|
}, []);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
markGithubAttachmentRemoved,
|
|
}),
|
|
[markGithubAttachmentRemoved],
|
|
);
|
|
}
|
|
|
|
async function attachRefs({
|
|
refs,
|
|
queryClient,
|
|
latestRef,
|
|
removedRefKeys,
|
|
pendingRefKeys,
|
|
}: {
|
|
refs: GithubRef[];
|
|
queryClient: QueryClient;
|
|
latestRef: RefObject<ComposerGithubAutoAttachInput>;
|
|
removedRefKeys: Set<string>;
|
|
pendingRefKeys: Set<string>;
|
|
}): Promise<void> {
|
|
for (const ref of refs) {
|
|
const key = githubRefKey(ref);
|
|
if (pendingRefKeys.has(key)) {
|
|
continue;
|
|
}
|
|
pendingRefKeys.add(key);
|
|
try {
|
|
await attachRef({ ref, key, queryClient, latestRef, removedRefKeys });
|
|
} finally {
|
|
pendingRefKeys.delete(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function attachRef({
|
|
ref,
|
|
key,
|
|
queryClient,
|
|
latestRef,
|
|
removedRefKeys,
|
|
}: {
|
|
ref: GithubRef;
|
|
key: string;
|
|
queryClient: QueryClient;
|
|
latestRef: RefObject<ComposerGithubAutoAttachInput>;
|
|
removedRefKeys: Set<string>;
|
|
}): Promise<void> {
|
|
const snapshot = latestRef.current;
|
|
if (!snapshot.client || !snapshot.isConnected || !isRefStillPresent(ref, snapshot)) {
|
|
return;
|
|
}
|
|
|
|
const search = await fetchGithubRefSearch({ ref, snapshot, queryClient });
|
|
if (!search) {
|
|
return;
|
|
}
|
|
const item = search.items.find((candidate) => githubItemMatchesRef(candidate, ref));
|
|
if (!item || removedRefKeys.has(key) || !isRefStillPresent(ref, latestRef.current)) {
|
|
return;
|
|
}
|
|
|
|
latestRef.current.setAttachments((current) => {
|
|
if (removedRefKeys.has(key) || isAttachmentSelectedForGithubItem(current, item)) {
|
|
return current;
|
|
}
|
|
return toggleGithubAttachment(current, item);
|
|
});
|
|
}
|
|
|
|
function refsReadyForLookup({
|
|
params,
|
|
removedRefKeys,
|
|
pendingRefKeys,
|
|
}: {
|
|
params: ComposerGithubAutoAttachInput;
|
|
removedRefKeys: Set<string>;
|
|
pendingRefKeys: Set<string>;
|
|
}): GithubRef[] {
|
|
if (!params.client || !params.isConnected || params.cwd.trim().length === 0) {
|
|
return [];
|
|
}
|
|
|
|
return extractGithubRefs(params.text, params.remoteUrl).filter((ref) => {
|
|
const key = githubRefKey(ref);
|
|
return (
|
|
!removedRefKeys.has(key) &&
|
|
!pendingRefKeys.has(key) &&
|
|
!hasGithubAttachment(params.attachments, ref)
|
|
);
|
|
});
|
|
}
|
|
|
|
async function fetchGithubRefSearch({
|
|
ref,
|
|
snapshot,
|
|
queryClient,
|
|
}: {
|
|
ref: GithubRef;
|
|
snapshot: ComposerGithubAutoAttachInput;
|
|
queryClient: QueryClient;
|
|
}) {
|
|
if (!snapshot.client) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return await queryClient.fetchQuery(
|
|
buildForgeSearchQueryOptions({
|
|
client: snapshot.client,
|
|
serverId: snapshot.serverId,
|
|
cwd: snapshot.cwd,
|
|
query: String(ref.number),
|
|
supportsForgeSearch: snapshot.supportsForgeSearch,
|
|
enabled: true,
|
|
}),
|
|
);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function isRefStillPresent(ref: GithubRef, params: ComposerGithubAutoAttachInput): boolean {
|
|
return extractGithubRefs(params.text, params.remoteUrl).some(
|
|
(candidate) => githubRefKey(candidate) === githubRefKey(ref),
|
|
);
|
|
}
|
|
|
|
function hasGithubAttachment(attachments: UserComposerAttachment[], ref: GithubRef): boolean {
|
|
return attachments.some((attachment) => attachmentKey(attachment) === githubRefKey(ref));
|
|
}
|
|
|
|
function githubItemMatchesRef(item: ForgeSearchItem, ref: GithubRef): boolean {
|
|
return item.kind === githubItemKind(ref) && item.number === ref.number;
|
|
}
|
|
|
|
function githubItemKind(ref: GithubRef): ForgeSearchItem["kind"] {
|
|
return ref.kind === "pull" ? "change_request" : "issue";
|
|
}
|
|
|
|
function githubRefKey(ref: GithubRef): string {
|
|
return `${githubItemKind(ref)}:${ref.number}`;
|
|
}
|
|
|
|
function attachmentKey(attachment: ComposerAttachment | undefined): string | null {
|
|
if (
|
|
!attachment ||
|
|
attachment.kind === "image" ||
|
|
(attachment.kind !== "forge_change_request" &&
|
|
attachment.kind !== "forge_issue" &&
|
|
attachment.kind !== "github_pr" &&
|
|
attachment.kind !== "github_issue")
|
|
) {
|
|
return null;
|
|
}
|
|
return `${attachment.item.kind}:${attachment.item.number}`;
|
|
}
|