π OpenHands: AI-Driven Development
feat(automations): hide the GitHub token secret field on self-hosted instances when GitHub is connected (OHE-3699) (#18829)
Hiep Lef8c1f554c5d6
diff --git a/__tests__/components/manifest/manifest-setup-dialog.test.tsx b/__tests__/components/manifest/manifest-setup-dialog.test.tsxindex d3767bbc3b..5bdc273da2 100644--- a/__tests__/components/manifest/manifest-setup-dialog.test.tsx+++ b/__tests__/components/manifest/manifest-setup-dialog.test.tsx@@ -5,6 +5,7 @@ import userEvent from "@testing-library/user-event";import { beforeEach, describe, expect, it, vi } from "vitest";import AutomationService from "#/api/automation-service/automation-service.api";import { SetupDialog } from "#/components/features/manifest/manifest-setup-dialog";+import type { useNativeGitIntegrations } from "#/hooks/query/use-native-git-integrations";import { SUPER_ADMIN_SETUP_STEP_EVENT } from "#/components/features/setup-guide/super-admin-setup-step-event";import { I18nKey } from "#/i18n/declaration";import type { SetupPrerequisitesResult } from "#/hooks/query/use-manifest-prerequisites";@@ -26,6 +27,7 @@ const mocks = vi.hoisted(() => ({prerequisites: vi.fn(),capabilities: vi.fn(),llmProfiles: vi.fn(),+ nativeIntegrations: vi.fn(),missingCreateEndpoints: vi.fn<(entry: SetupEntry) => string[]>(() => []),tracking: {trackAutomationSetupOpened: vi.fn(),@@ -69,6 +71,10 @@ vi.mock("#/hooks/query/use-llm-profiles", () => ({mocks.llmProfiles(options),}));+vi.mock("#/hooks/query/use-native-git-integrations", () => ({+ useNativeGitIntegrations: () => mocks.nativeIntegrations(),+}));+// Which endpoints an entry cannot be created without is read off the published// interface manifest, so a real one that declares them leaves the refusal path// unreachable. Stubbed so the case states the manifest it is about, rather than@@ -97,6 +103,30 @@ const NOTHING_TO_CONNECT: SetupPrerequisitesResult = {isLoading: false,};+type NativeGitIntegrations = ReturnType<typeof useNativeGitIntegrations>;++/** A backend with no native git integration to use: local, or cloud with none. */+const NO_NATIVE_INTEGRATIONS: NativeGitIntegrations = {+ getNativeIntegration: () => null,+ isJiraEnabled: false,+ isLinearEnabled: false,+ isSlackEnabled: false,+ deploymentMode: null,+ isLoading: false,+};++/** An Enterprise instance offering GitHub natively, connected when `connected`. */+function selfHostedGithub(connected: boolean): NativeGitIntegrations {+ return {+ ...NO_NATIVE_INTEGRATIONS,+ deploymentMode: "self_hosted",+ getNativeIntegration: (entryId) =>+ entryId === "github"+ ? { provider: "github", isConnected: connected }+ : null,+ };+}+const ENTRY: SetupEntry = createSetupEntry();const CUSTOM_AUTOMATION_ENTRY = AUTOMATION_CATALOG.find((entry) => entry.id === "custom-automation",@@ -131,6 +161,7 @@ beforeEach(() => {// stubs a manifest without the bundle endpoints would leak into the rest.mocks.missingCreateEndpoints.mockReturnValue([]);mocks.prerequisites.mockReturnValue(NOTHING_TO_CONNECT);+ mocks.nativeIntegrations.mockReturnValue(NO_NATIVE_INTEGRATIONS);mocks.capabilities.mockReturnValue({capabilities: null,supported: "unknown",@@ -176,6 +207,38 @@ const BUNDLE_ENTRY: SetupEntry = createSetupEntry({}),});+/** A bundle naming the secret that holds its GitHub token, as the GitHub entries do. */+const SECRET_NAME_BUNDLE_ENTRY: SetupEntry = (() => {+ const { form } = createSetup();+ return createSetupEntry({+ setup: createSetup({+ prompt: undefined,+ form: {+ ...form,+ args: {+ ...form.args,+ githubTokenSecret: {+ type: "text",+ label: "GitHub token secret",+ help: "Name of the secret holding the GitHub token.",+ default: "GITHUB_PERSONAL_ACCESS_TOKEN",+ required: true,+ },+ },+ },+ bundle: {+ version: "1.0.0",+ entrypoint: "python3 main.py",+ files: { "main.py": "skills/widget-monitor/scripts/main.py" },+ config: {+ repos: ["{{form.repository}}"],+ github_token_secret: "{{form.githubTokenSecret}}",+ },+ },+ }),+ });+})();+/** A deployment that answered discovery and came up short. */const UNSUPPORTED = {capabilities: null,@@ -682,6 +745,72 @@ describe("SetupDialog", () => {expect(screen.queryByTestId("setup-review")).toBeNull();expect(mocks.navigate).not.toHaveBeenCalled();});++ it("omits a connected provider's secret-name field on a self-hosted instance and still names the default", async () => {+ // Arrange β OpenHands Enterprise with GitHub connected under+ // Settings > Integrations: the bundle will use that account's token, so+ // there is nothing to ask, but its config must still name a secret.+ mocks.nativeIntegrations.mockReturnValue(selfHostedGithub(true));+ mocks.runAction.mockResolvedValue({ response: { id: "automation-1" } });+ const { user } = renderDialog(SECRET_NAME_BUNDLE_ENTRY);++ // Assert β not asked, on the form or on the review step.+ expect(screen.queryByTestId("setup-field-githubTokenSecret")).toBeNull();+ await fillForm(user);+ await user.click(screen.getByTestId("setup-continue-button"));+ await waitFor(() =>+ expect(screen.getByTestId("setup-review")).toBeInTheDocument(),+ );+ expect(screen.getByTestId("setup-review")).not.toHaveTextContent(+ "GitHub token secret",+ );++ // Act+ await user.click(screen.getByTestId("setup-continue-button"));++ // Assert β the declared default still reaches the bundle and its config.+ await waitFor(() => expect(mocks.runAction).toHaveBeenCalled());+ expect(mocks.runAction.mock.calls[0][1]).toMatchObject({+ githubTokenSecret: "GITHUB_PERSONAL_ACCESS_TOKEN",+ });+ expect(mocks.runAction.mock.calls[0][2]).toMatchObject({+ template: {+ config: { github_token_secret: "GITHUB_PERSONAL_ACCESS_TOKEN" },+ },+ });+ });++ it.each([+ [+ "GitHub is not connected on a self-hosted instance",+ selfHostedGithub(false),+ ],+ [+ "GitHub is connected on OpenHands Cloud",+ { ...selfHostedGithub(true), deploymentMode: "cloud" },+ ],+ ])(+ "keeps asking for the secret name when %s",+ async (_case, integrations) => {+ // Arrange β without the connected account's token to fall back on, or+ // outside Enterprise, the field is shown as the manifest declares it.+ mocks.nativeIntegrations.mockReturnValue(integrations);+ const { user } = renderDialog(SECRET_NAME_BUNDLE_ENTRY);++ // Assert+ expect(screen.getByTestId("setup-field-githubTokenSecret")).toHaveValue(+ "GITHUB_PERSONAL_ACCESS_TOKEN",+ );+ await fillForm(user);+ await user.click(screen.getByTestId("setup-continue-button"));+ await waitFor(() =>+ expect(screen.getByTestId("setup-review")).toBeInTheDocument(),+ );+ expect(screen.getByTestId("setup-review")).toHaveTextContent(+ "GitHub token secret",+ );+ },+ );});it("creates a bundle with an optional selected agent profile", async () => {diff --git a/src/components/features/manifest/manifest-review-step.tsx b/src/components/features/manifest/manifest-review-step.tsxindex 224ab8b905..a1e2fa9a2a 100644--- a/src/components/features/manifest/manifest-review-step.tsx+++ b/src/components/features/manifest/manifest-review-step.tsx@@ -1,15 +1,17 @@import { useTranslation } from "react-i18next";import { I18nKey } from "#/i18n/declaration";-import {- collectFields,- fieldValues,-} from "#/manifests/manifest-local-validation";-import type { SetupBlock, SetupFormValues } from "#/manifests/types";+import { fieldValues } from "#/manifests/manifest-local-validation";+import type {+ SetupBlock,+ SetupFormFields,+ SetupFormValues,+} from "#/manifests/types";export interface SetupReviewStepProps {setup: SetupBlock;+ /** The fields the form showed, in the order it showed them. */+ fields: SetupFormFields;values: SetupFormValues;- selectedTrigger?: string | null;selectedAction?: string | null;}@@ -18,13 +20,13 @@ export interface SetupReviewStepProps {** The last cheap moment to catch a wrong answer, and the last point at which* nothing has been created yet. A manifest declares no summary of its own: one- * row per declared field, labelled the way the field was labelled, says the- * same thing without asking every entry to restate it.+ * row per field the form showed, labelled the way the field was labelled, says+ * the same thing without asking every entry to restate it.*/export function SetupReviewStep({setup,+ fields,values,- selectedTrigger,selectedAction,}: SetupReviewStepProps) {const { t } = useTranslation("openhands");@@ -46,9 +48,7 @@ export function SetupReviewStep({</dd></div>)}- {Object.entries(- collectFields(setup, selectedTrigger, selectedAction),- ).map(([name, field]) => (+ {Object.entries(fields).map(([name, field]) => (<div key={name} className="flex flex-col gap-0.5"><dt className="text-xs text-muted">{field.label}</dt><dd className="text-sm break-words">diff --git a/src/components/features/manifest/manifest-setup-dialog.tsx b/src/components/features/manifest/manifest-setup-dialog.tsxindex b232222669..2dbb8da5bf 100644--- a/src/components/features/manifest/manifest-setup-dialog.tsx+++ b/src/components/features/manifest/manifest-setup-dialog.tsx@@ -15,6 +15,7 @@ import { useTracking } from "#/hooks/use-tracking";import { notifySuperAdminSetupStep } from "#/components/features/setup-guide/super-admin-setup-step-event";import { useSetupCapabilities } from "#/hooks/query/use-manifest-capabilities";import { useSetupPrerequisites } from "#/hooks/query/use-manifest-prerequisites";+import { useNativeGitIntegrations } from "#/hooks/query/use-native-git-integrations";import { useLlmProfiles } from "#/hooks/query/use-llm-profiles";import { useSetupPreflight } from "#/hooks/use-manifest-preflight";import { useSetupAction } from "#/manifests/manifest-actions";@@ -49,6 +50,7 @@ import {import { automationDetailPath } from "#/manifests/automation-interface";import { findAutomationCommand } from "#/utils/automation-catalog";import type { GitRepository } from "#/types/git";+import type { Provider } from "#/types/settings";import type {SetupEntry,SetupFormValue,@@ -70,6 +72,18 @@ const NO_SERVICE_ERRORS: MappedManifestErrors = {formErrors: [],};+/**+ * Fields naming the secret a bundle reads only when the user has not connected+ * the provider under Settings > Integrations; once connected, the bundle uses+ * that account's token. On an OpenHands Enterprise instance (`self_hosted`)+ * the field therefore has nothing to ask and is not shown while the provider+ * is connected. It keeps its default, so the config still names a secret.+ * OpenHands Cloud and local backends show it as declared.+ */+const CONNECTED_PROVIDER_SECRET_FIELDS: Record<string, Provider> = {+ githubTokenSecret: "github",+};+function hasAnyError(errors: MappedManifestErrors): boolean {return (errors.formErrors.length > 0 || Object.keys(errors.fieldErrors).length > 0@@ -106,6 +120,7 @@ export function SetupDialog({ entry, onClose }: SetupDialogProps) {const prerequisites = useSetupPrerequisites(entry);const runPreflight = useSetupPreflight(entry);const runAction = useSetupAction();+ const { getNativeIntegration, deploymentMode } = useNativeGitIntegrations();const {trackAutomationSetupOpened,trackAutomationSetupValidated,@@ -168,13 +183,18 @@ export function SetupDialog({ entry, onClose }: SetupDialogProps) {() => collectFields(entry.setup, selectedTrigger, selectedAction),[entry, selectedTrigger, selectedAction],);- const visibleFields = values.agent_profile_id- ? Object.fromEntries(- Object.entries(fields).filter(- ([, field]) => field.type !== "llm-profile",- ),- )- : fields;+ const hidesConnectedProviderSecrets = deploymentMode === "self_hosted";+ const visibleFields = Object.fromEntries(+ Object.entries(fields).filter(([name, field]) => {+ if (values.agent_profile_id && field.type === "llm-profile") return false;+ if (!hidesConnectedProviderSecrets) return true;+ if (!Object.hasOwn(CONNECTED_PROVIDER_SECRET_FIELDS, name)) return true;+ return (+ getNativeIntegration(CONNECTED_PROVIDER_SECRET_FIELDS[name])+ ?.isConnected !== true+ );+ }),+ );const canSelectAgentProfile =capabilities.capabilities?.features.includes("agentProfiles") === true &&supportsAgentProfile(entry, selectedAction);@@ -590,8 +610,8 @@ export function SetupDialog({ entry, onClose }: SetupDialogProps) {{!isLoading && !isUnsupported && currentStep === "review" && (<SetupReviewStepsetup={entry.setup}+ fields={visibleFields}values={values}- selectedTrigger={selectedTrigger}selectedAction={selectedAction}/>)}