swarm-ui: link a gitlab account to an agent
This commit is contained in:
parent
b7d8c0dfc0
commit
3e5b5b54a2
1 changed files with 240 additions and 56 deletions
|
|
@ -1,10 +1,11 @@
|
|||
// <LinkForgeAccountForm> — writes an external forge account (base URL +
|
||||
// token) for one agent into the swarm secret store.
|
||||
// PUTs `/api/hives/{hive}/agents/{agent}/forge-accounts/{label}` — 200
|
||||
// token) for one agent into the swarm secret store. `kind` picks the
|
||||
// route and the wording: "forge" (Forgejo) or "gitlab".
|
||||
// PUTs `/api/hives/{hive}/agents/{agent}/{forge,gitlab}-accounts/{label}` — 200
|
||||
// (`{ url }`) on success, 400/409/500 as `problem+json`, shown via
|
||||
// `ApiErrorPanel` like `LinkMatrixAccountForm`.
|
||||
//
|
||||
// The label is what the agent passes to `hive-forge -f <label>`. A blind
|
||||
// A forge label is what the agent passes to `hive-forge -f <label>`. A blind
|
||||
// set: no route hands a token back. Linked labels and URLs show on
|
||||
// the agent panel (`LinkedAccounts`).
|
||||
import { useState } from "preact/hooks";
|
||||
|
|
@ -13,11 +14,17 @@ import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
|||
import { Panel } from "../ui/panel/Panel.js";
|
||||
import { TextField } from "../ui/text-field/TextField.js";
|
||||
import { Button } from "../ui/button/Button.js";
|
||||
import {
|
||||
checkLink,
|
||||
LABEL_PATTERN,
|
||||
linkBody,
|
||||
linkUrl,
|
||||
type ForgeLinkKind,
|
||||
} from "../agent/detail/account-api.js";
|
||||
import "./LinkMatrixAccountForm.css";
|
||||
|
||||
// `hive_types::Ident`, which the server checks: `hive-forge -f` accepts
|
||||
// nothing wider. A UX hint only; the server is the gate.
|
||||
const LABEL_PATTERN = "[a-z0-9\\-]{1,63}";
|
||||
// Prefilled for gitlab; the field stays editable for a self-hosted instance.
|
||||
const GITLAB_DEFAULT_URL = "https://gitlab.com";
|
||||
|
||||
type SubmitState =
|
||||
| { status: "idle" }
|
||||
|
|
@ -26,31 +33,35 @@ type SubmitState =
|
|||
| { status: "error"; problem: ProblemDetails };
|
||||
|
||||
export function LinkForgeAccountForm({
|
||||
kind,
|
||||
hive,
|
||||
agent,
|
||||
onClose,
|
||||
}: {
|
||||
kind: ForgeLinkKind;
|
||||
hive: string;
|
||||
agent: string;
|
||||
onClose?: () => void;
|
||||
}) {
|
||||
const [label, setLabel] = useState("");
|
||||
const [url, setUrl] = useState("");
|
||||
const [url, setUrl] = useState(kind === "gitlab" ? GITLAB_DEFAULT_URL : "");
|
||||
const [token, setToken] = useState("");
|
||||
const [result, setResult] = useState<SubmitState>({ status: "idle" });
|
||||
|
||||
async function submit(e: Event) {
|
||||
e.preventDefault();
|
||||
const invalid = checkLink(label, url, token);
|
||||
if (invalid) {
|
||||
setResult({ status: "error", problem: { detail: invalid } });
|
||||
return;
|
||||
}
|
||||
setResult({ status: "submitting" });
|
||||
try {
|
||||
const r = await fetch(
|
||||
`/api/hives/${encodeURIComponent(hive)}/agents/${encodeURIComponent(agent)}/forge-accounts/${encodeURIComponent(label)}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ url: url.trim(), token }),
|
||||
},
|
||||
);
|
||||
const r = await fetch(linkUrl(hive, agent, kind, label), {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: linkBody(url, token),
|
||||
});
|
||||
if (!r.ok) {
|
||||
setResult({ status: "error", problem: await readApiError(r) });
|
||||
return;
|
||||
|
|
@ -68,38 +79,59 @@ export function LinkForgeAccountForm({
|
|||
|
||||
return (
|
||||
<Panel
|
||||
title={`link a forge account — ${agent}`}
|
||||
title={`link a ${kind === "gitlab" ? "gitlab" : "forge"} account — ${agent}`}
|
||||
icon="🔗"
|
||||
onClose={onClose}
|
||||
>
|
||||
<p>
|
||||
Writes the account to the swarm secret store. The agent fetches it
|
||||
within two minutes and can then use{" "}
|
||||
<code>hive-forge -f {label || "<label>"}</code>. Create the token on the
|
||||
external forge first; nothing is created there.
|
||||
</p>
|
||||
{kind === "gitlab" ? (
|
||||
<>
|
||||
<p>
|
||||
Writes the account to the swarm secret store. The agent fetches it
|
||||
within two minutes and can then use <code>glab</code> and{" "}
|
||||
<code>git push</code> against the instance. Create the token on
|
||||
GitLab first; nothing is created there.
|
||||
</p>
|
||||
<p>
|
||||
Scope the token <code>api</code> and <code>write_repository</code>:
|
||||
the first is for <code>glab</code>, the second for pushing over
|
||||
HTTPS. Use a dedicated bot account; the agent can act within the
|
||||
token's scopes.
|
||||
</p>
|
||||
</>
|
||||
) : (
|
||||
<p>
|
||||
Writes the account to the swarm secret store. The agent fetches it
|
||||
within two minutes and can then use{" "}
|
||||
<code>hive-forge -f {label || "<label>"}</code>. Create the token on
|
||||
the external forge first; nothing is created there.
|
||||
</p>
|
||||
)}
|
||||
<form class="link-matrix-account-form" onSubmit={submit}>
|
||||
<TextField
|
||||
id="forge-account-label"
|
||||
id={`${kind}-account-label`}
|
||||
label="label"
|
||||
value={label}
|
||||
pattern={LABEL_PATTERN}
|
||||
title="1-63 chars: lowercase letters, digits, hyphen"
|
||||
placeholder="e.g. myforge"
|
||||
placeholder={kind === "gitlab" ? "e.g. gitlab-com" : "e.g. myforge"}
|
||||
required
|
||||
onInput={setLabel}
|
||||
/>
|
||||
<TextField
|
||||
id="forge-account-url"
|
||||
label="forge URL"
|
||||
id={`${kind}-account-url`}
|
||||
label={kind === "gitlab" ? "instance URL" : "forge URL"}
|
||||
type="url"
|
||||
value={url}
|
||||
placeholder="https://forge.example.org"
|
||||
placeholder={
|
||||
kind === "gitlab"
|
||||
? "https://gitlab.example.org"
|
||||
: "https://forge.example.org"
|
||||
}
|
||||
required
|
||||
onInput={setUrl}
|
||||
/>
|
||||
<TextField
|
||||
id="forge-account-token"
|
||||
id={`${kind}-account-token`}
|
||||
label="access token"
|
||||
type="password"
|
||||
value={token}
|
||||
|
|
|
|||
Loading…
Reference in a new issue