feat: save Bitbucket credentials from Source Control settings #14103
Mergedgabrielelpidio(GitHub profile, opens in a new tab)merged 12 commits into
main fromrefs/pull/14103/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/settings/BitbucketCredentialsSettings.tsx line 88OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +16,21 @@16+ return `Using the saved API token for ${saved.email}.`;17+ }18+ return "No credentials saved. The server's T3CODE_BITBUCKET_* variables are used if set.";19+}20+21+/**22+ * Bitbucket credentials for one environment. Tokens are write-only: the server23+ * keeps them in its secret store and only reports whether each one is set.24+ */25+export function BitbucketCredentialsSettings({26+ environmentId,27+ onSaved,28+}: {29+ readonly environmentId: EnvironmentId;30+ readonly onSaved: () => void;31+}) {32+ const saved = useEnvironmentSettings(environmentId, (settings) => settings.bitbucket);33+ const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {34+ label: "save Bitbucket credentials",35+ });36+ const [accessToken, setAccessToken] = useState("");
- apps/web/src/components/settings/BitbucketCredentialsSettings.tsx lines 73 to 75OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +55,21 @@55+ isSaved,56+ draft,57+ onDraftChange,58+}: {59+ readonly id: string;60+ readonly isSaved: boolean;61+ readonly draft: string | null;62+ readonly onDraftChange: (draft: string | null) => void;63+}) {64+ const masked = isSaved && draft === null;65+ return (66+ <Input67+ id={id}68+ type="password"69+ autoComplete="off"70+ size="sm"71+ placeholder={isSaved ? undefined : "Not set"}72+ value={masked ? SAVED_TOKEN_MASK : (draft ?? "")}73+ onFocus={(event) => {74+ if (masked) event.target.select();75+ }}
- apps/server/src/sourceControl/BitbucketApi.ts lines 671 to 673Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -618,13 +662,12 @@618662 const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;619663620664 const apiUrl = (path: string) => `${config.baseUrl.replace(/\/+$/u, "")}${path}`;621665622- const withAuth = (request: HttpClientRequest.HttpClientRequest) => {623- if (Option.isSome(config.accessToken)) {624- return request.pipe(HttpClientRequest.bearerToken(config.accessToken.value));625- }626- if (Option.isSome(config.email) && Option.isSome(config.apiToken)) {627- return request.pipe(HttpClientRequest.basicAuth(config.email.value, config.apiToken.value));628- }629- return request;630- };666+ // Read on every request so credentials saved in settings apply without a restart.667+ const currentCredential = serverSettings.getSettings.pipe(668+ Effect.map((settings) => resolveCredential(settings.bitbucket, config)),669+ Effect.catch((error) =>670+ Effect.logWarning("failed to read Bitbucket credentials from settings", {671+ operation: error.operation,672+ cause: error.cause,673+ }).pipe(Effect.as(resolveCredential(DEFAULT_SERVER_SETTINGS.bitbucket, config))),
- apps/server/src/serverSettings.ts line 893OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -854,3 +880,14 @@854880 });855881 }856882883+ const bitbucket = { ...next.bitbucket };884+ for (const field of BITBUCKET_SECRET_FIELDS) {885+ let value = bitbucket[field];886+ if (value === SECRET_REDACTED) {887+ // The marker keeps what is saved. A plaintext value hand-edited into settings.json888+ // is not in the secret store yet, so move it there instead of dropping it.889+ const inline = current.bitbucket[field];890+ if (inline === SECRET_REDACTED || inline.length === 0) continue;891+ value = inline;892+ }893+ if (value.length === 0 && current.bitbucket[field].length === 0) continue;
- apps/server/src/serverSettings.ts line 914Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -854,3 +880,4 @@854880 });855881 }856882883+ const bitbucket = { ...next.bitbucket };
- apps/web/src/components/settings/BitbucketCredentialsSettings.tsx line 122Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +102,21 @@102+ method === "access-token"103+ ? newAccessToken104+ ? { accessToken: newAccessToken, email: "", apiToken: "" }105+ : null106+ : email && (newApiToken || saved.apiToken)107+ ? // Resending the saved token's redacted value keeps it.108+ { accessToken: "", email, apiToken: newApiToken || saved.apiToken }109+ : null;110+ const canSave =111+ patch !== null &&112+ (method === "access-token" || !methodIsSaved || newApiToken !== "" || email !== saved.email);113+114+ const save = async (next: BitbucketSettings) => {115+ setSaving(true);116+ try {117+ const result = await updateSettings({118+ environmentId,119+ input: { patch: { bitbucket: next } },120+ });121+ if (result._tag === "Success") {122+ setAccessToken("");
- apps/web/src/components/settings/BitbucketCredentialsSettings.tsx line 160OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +140,21 @@140+ {/* Locked while saving: a successful save clears the drafts, which would drop edits made mid-request. */}141+ <fieldset disabled={saving} className="contents">142+ <ToggleGroup143+ aria-label="Bitbucket sign-in method"144+ variant="segmented"145+ value={[method]}146+ onValueChange={(next) => {147+ const value = next[0];148+ if (value === "access-token" || value === "api-token") setMethodChoice(value);149+ }}150+ >151+ <Toggle value="access-token">{METHODS["access-token"].label}</Toggle>152+ <Toggle value="api-token">{METHODS["api-token"].label}</Toggle>153+ </ToggleGroup>154+ <p className="max-w-2xl text-xs leading-relaxed text-muted-foreground">155+ {info.description}{" "}156+ <a157+ href={info.link}158+ target="_blank"159+ rel="noreferrer noopener"160+ className="inline-flex items-center gap-0.5 text-foreground underline-offset-2 hover:underline"
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
juliusmarminge merged this5da5595
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed