feat(github): unify workspace GitHub access (#2084)

This commit is contained in:
Carlos Florêncio
2026-07-31 19:20:49 +01:00
committed by GitHub
parent 1c267cf527
commit aeb7c62e49
27 changed files with 1573 additions and 419 deletions
@@ -1,6 +1,8 @@
import { useState } from "react";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ToastProvider } from "@/components/toast-provider";
import type { TaskGitCredentialsState } from "@/hooks/domains/github/use-task-git-credentials";
import type { GitHubAppRegistrationCatalogItem, GitHubStatus } from "@/lib/types/github";
import { GitHubAppConnectionPanel } from "./github-app-connection-panel";
import { GitHubConnectionDialog } from "./github-connection-dialog";
@@ -8,11 +10,21 @@ import { GitHubConnectionDialog } from "./github-connection-dialog";
const mocks = vi.hoisted(() => ({
mobile: false,
registrations: [] as GitHubAppRegistrationCatalogItem[],
setConnection: vi.fn(),
}));
const changeConnectionLabel = "Change connection";
const registrationDisplayName = "Work automation";
const githubAppLabel = "GitHub App";
const saveChangesLabel = "Save changes";
const WORKSPACE_ID = "workspace-1";
const DESKTOP_CONNECTION_TEST_ID = "github-connection-desktop";
const taskAccess: TaskGitCredentialsState = {
mode: "managed",
loading: false,
error: false,
save: vi.fn(),
};
vi.mock("@/hooks/use-responsive-breakpoint", () => ({
useResponsiveBreakpoint: () => ({ isMobile: mocks.mobile }),
@@ -44,10 +56,15 @@ vi.mock("./github-app-import-form", () => ({
),
}));
vi.mock("@/lib/api/domains/github-api", async (importOriginal) => ({
...(await importOriginal<typeof import("@/lib/api/domains/github-api")>()),
setGitHubWorkspaceConnection: mocks.setConnection,
}));
const status: GitHubStatus = {
workspace_id: "workspace-1",
workspace_id: WORKSPACE_ID,
automation: {
workspace_id: "workspace-1",
workspace_id: WORKSPACE_ID,
source: "pat",
github_host: "github.com",
login: "octocat",
@@ -89,21 +106,107 @@ const registration: GitHubAppRegistrationCatalogItem = {
webhook_url: "https://kandev.example/webhook",
};
function view(workspaceId = "workspace-1") {
function view(workspaceId = WORKSPACE_ID, currentTaskAccess = taskAccess) {
const onSaved = vi.fn();
return render(
<ToastProvider>
<GitHubConnectionDialog
status={{ ...status, workspace_id: workspaceId }}
workspaceId={workspaceId}
onSaved={vi.fn()}
onSaved={onSaved}
taskAccess={currentTaskAccess}
/>
</ToastProvider>,
);
}
function PartialSaveHarness() {
const [mode, setMode] = useState<TaskGitCredentialsState["mode"]>("managed");
const save = vi.fn(async (nextMode: TaskGitCredentialsState["mode"]) => {
setMode(nextMode);
return true;
});
return (
<ToastProvider>
<GitHubConnectionDialog
status={status}
workspaceId={WORKSPACE_ID}
onSaved={vi.fn()}
taskAccess={{ mode, loading: false, error: false, save }}
/>
</ToastProvider>
);
}
beforeEach(() => {
mocks.mobile = false;
mocks.registrations = [registration];
mocks.setConnection.mockReset();
mocks.setConnection.mockResolvedValue(status.automation);
});
describe("GitHubConnectionDialog task access", () => {
it("saves connection and task access drafts with one action", async () => {
const save = vi.fn().mockResolvedValue(true);
const rendered = view(WORKSPACE_ID, { ...taskAccess, save });
fireEvent.click(screen.getByRole("button", { name: changeConnectionLabel }));
const dialog = await screen.findByTestId(DESKTOP_CONNECTION_TEST_ID);
const executor = screen
.getByTestId("github-task-access-option-executor")
.querySelector('[role="radio"]')!;
fireEvent.click(executor);
fireEvent.change(screen.getByLabelText("Personal access token"), {
target: { value: "ghp_replacement" },
});
expect(screen.queryByRole("button", { name: "Connect token" })).toBeNull();
expect(screen.queryByRole("button", { name: "Save task access" })).toBeNull();
expect(screen.getAllByRole("button", { name: saveChangesLabel })).toHaveLength(1);
fireEvent.click(screen.getByRole("button", { name: saveChangesLabel }));
await waitFor(() =>
expect(mocks.setConnection).toHaveBeenCalledWith(WORKSPACE_ID, {
source: "pat",
token: "ghp_replacement",
}),
);
expect(save).toHaveBeenCalledWith("executor");
await waitFor(() => expect(dialog.getAttribute("data-state")).toBe("closed"));
expect(rendered.baseElement.textContent).toContain("GitHub access settings saved");
});
it("does not report a discarded task access save as successful", async () => {
const save = vi.fn().mockResolvedValue(false);
view(WORKSPACE_ID, { ...taskAccess, save });
fireEvent.click(screen.getByRole("button", { name: changeConnectionLabel }));
const dialog = await screen.findByTestId(DESKTOP_CONNECTION_TEST_ID);
const executor = screen
.getByTestId("github-task-access-option-executor")
.querySelector('[role="radio"]')!;
fireEvent.click(executor);
fireEvent.click(screen.getByRole("button", { name: saveChangesLabel }));
await waitFor(() => expect(save).toHaveBeenCalledWith("executor"));
expect(screen.queryByText("GitHub access settings saved")).toBeNull();
expect(dialog.getAttribute("data-state")).toBe("open");
});
it("preserves a failed PAT draft when task access saves successfully", async () => {
mocks.setConnection.mockRejectedValue(new Error("Token rejected"));
render(<PartialSaveHarness />);
fireEvent.click(screen.getByRole("button", { name: changeConnectionLabel }));
const token = screen.getByLabelText("Personal access token") as HTMLInputElement;
fireEvent.change(token, { target: { value: "ghp_retry_me" } });
fireEvent.click(
screen.getByTestId("github-task-access-option-executor").querySelector('[role="radio"]')!,
);
fireEvent.click(screen.getByRole("button", { name: saveChangesLabel }));
await screen.findByText(/Some settings were saved/);
expect(token.value).toBe("ghp_retry_me");
});
});
afterEach(() => cleanup());
@@ -111,7 +214,7 @@ describe("GitHubConnectionDialog", () => {
it("uses a method list and shows workspace App choices and sharing impact", async () => {
view();
fireEvent.click(screen.getByRole("button", { name: changeConnectionLabel }));
expect(await screen.findByTestId("github-connection-desktop")).toBeTruthy();
expect(await screen.findByTestId(DESKTOP_CONNECTION_TEST_ID)).toBeTruthy();
expect(screen.getByLabelText("Connection method")).toBeTruthy();
expect(screen.getAllByText("Personal access token")).not.toHaveLength(0);
expect(screen.getByText("GitHub CLI account")).toBeTruthy();
@@ -132,7 +235,7 @@ describe("GitHubConnectionDialog", () => {
const drawer = await screen.findByTestId("github-connection-mobile");
expect(drawer.className).toContain("100dvh");
expect(drawer.querySelectorAll(".overflow-y-auto")).toHaveLength(1);
expect(screen.queryByTestId("github-connection-desktop")).toBeNull();
expect(screen.queryByTestId(DESKTOP_CONNECTION_TEST_ID)).toBeNull();
});
it("returns to the registration catalog after importing an App", async () => {
@@ -202,9 +305,10 @@ describe("GitHubConnectionDialog", () => {
status={{ ...status, workspace_id: "workspace-2" }}
workspaceId="workspace-2"
onSaved={vi.fn()}
taskAccess={taskAccess}
/>
</ToastProvider>,
);
await waitFor(() => expect(screen.queryByTestId("github-connection-desktop")).toBeNull());
await waitFor(() => expect(screen.queryByTestId(DESKTOP_CONNECTION_TEST_ID)).toBeNull());
});
});