forge: replace removed forgejo admin user edit CLI with PATCH /api/v1/admin/users/{name} (closes #574)

This commit is contained in:
damocles 2026-05-29 16:39:50 +02:00
commit 71abf2471e

View file

@ -227,11 +227,30 @@ async fn ensure_user_exists(name: &str, admin: bool) -> Result<()> {
/// Existing agents were created with `{name}@hive.local`; this corrects
/// that so git commits (which use `{name}@hyperhive`) link to profiles.
/// Best-effort: failures are warned, not propagated.
///
/// Uses the admin REST API (`PATCH /api/v1/admin/users/{name}`) rather
/// than `forgejo admin user edit` because the CLI dropped the `edit`
/// subcommand somewhere between forgejo 8 and current — see #574 for
/// the "flag provided but not defined: -username" error this fix
/// replaces. Body sets `source_id = 0` (local auth, the default for
/// users hive-c0re creates) which forgejo's PATCH validator requires
/// even when the only thing changing is the email.
async fn ensure_user_email(name: &str) {
let Some(token) = core_token() else {
tracing::debug!(%name, "forge: skipping ensure_user_email — no core token yet");
return;
};
let email = agent_email(name);
match forge_admin(&["user", "edit", "--username", name, "--email", &email]).await {
Ok(_) => tracing::debug!(%name, %email, "forge: user email aligned"),
Err(e) => tracing::warn!(%name, error = %e, "forge: could not align user email"),
let body = format!(r#"{{"email":"{email}","source_id":0}}"#);
let url = format!("{FORGE_HTTP}/api/v1/admin/users/{name}");
match forge_http(reqwest::Method::PATCH, &url, &token, &body).await {
Ok(status) if status.is_success() => {
tracing::debug!(%name, %email, "forge: user email aligned");
}
Ok(status) => {
tracing::warn!(%name, %email, %status, "forge: PATCH user email returned non-success");
}
Err(e) => tracing::warn!(%name, error = %e, "forge: PATCH user email transport error"),
}
}