drop the hive's push of the legacy core/meta repo
Nothing reads core/meta: builds come from internal/meta. Remove push_meta, ensure_meta_remote, meta_read_access, ensure_repo, the per-agent forge sync that only served them, and the git commit path in meta.rs; remove_permission_files now just deletes the seed files.
This commit is contained in:
parent
dd2f0d3ece
commit
b6b568e97e
1 changed files with 81 additions and 518 deletions
|
|
@ -1,207 +1,10 @@
|
|||
//! Repo + org plumbing on the local Forgejo: the meta + knowledge repos,
|
||||
//! meta mirroring (`push_meta`), collaborator grants, pull-mirrors, and
|
||||
//! branch-protection rules. The typed API-client constructor + org-name
|
||||
//! constants live in the module root (`super`).
|
||||
//! Repo plumbing on the local Forgejo: reseeding an agent's `applied` and
|
||||
//! `proposed` repos from `agent-configs/<name>`. The typed API-client
|
||||
//! constructor + org-name constants live in the module root (`super`).
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use forgejo_api::structs::{
|
||||
AddCollaboratorOption, AddCollaboratorOptionPermission, CreateRepoOption, Repository,
|
||||
};
|
||||
use forgejo_api::{ApiErrorKind, ForgejoError};
|
||||
use reqwest::StatusCode;
|
||||
|
||||
use crate::coordinator::Coordinator;
|
||||
|
||||
use super::{
|
||||
CONFIG_ORG, api, bounded, core_auth_header, core_token, forge_git_url, forge_http_base,
|
||||
is_present, reader_token,
|
||||
};
|
||||
|
||||
/// Creation options for an empty repo defaulting to `main`.
|
||||
fn repo_option(name: &str, private: bool) -> CreateRepoOption {
|
||||
CreateRepoOption {
|
||||
auto_init: Some(false),
|
||||
default_branch: Some("main".to_owned()),
|
||||
description: None,
|
||||
gitignores: None,
|
||||
issue_labels: None,
|
||||
license: None,
|
||||
name: name.to_owned(),
|
||||
object_format_name: None,
|
||||
private: Some(private),
|
||||
readme: None,
|
||||
template: None,
|
||||
trust_model: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a create-style call failed because the object already
|
||||
/// exists. Forgejo signals this as HTTP 409 (conflict) or 422
|
||||
/// (validation). The typed client surfaces those as
|
||||
/// `ApiErrorKind::Other(409)` / `ValidationFailed` when the endpoint
|
||||
/// spec lists the status, or as a bare `UnexpectedStatusCode`
|
||||
/// otherwise — match all shapes defensively.
|
||||
fn is_already_exists(e: &ForgejoError) -> bool {
|
||||
match e {
|
||||
ForgejoError::ApiError(api) => match api.error_kind() {
|
||||
ApiErrorKind::ValidationFailed => true,
|
||||
ApiErrorKind::Other(s) => *s == StatusCode::CONFLICT,
|
||||
_ => false,
|
||||
},
|
||||
ForgejoError::UnexpectedStatusCode(s) => {
|
||||
*s == StatusCode::CONFLICT || *s == StatusCode::UNPROCESSABLE_ENTITY
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Fold a repo-creation result's "already exists" (409 / 422) into
|
||||
/// success. `label` is `<owner>/<name>` — purely for log + error
|
||||
/// context.
|
||||
fn created_or_exists(res: Result<Repository, ForgejoError>, label: &str) -> Result<()> {
|
||||
match res {
|
||||
Ok(_) => {
|
||||
tracing::info!(%label, "forge: created repo");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) if is_already_exists(&e) => {
|
||||
tracing::debug!(%label, "forge: repo already exists");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(e).with_context(|| format!("create repo {label}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a repo in the token-owner's own namespace. `token` belongs
|
||||
/// to the user we want the repo owned by (we use `core`'s token for
|
||||
/// `core/meta`). Idempotent.
|
||||
pub async fn ensure_repo(name: &str, token: &str) -> Result<()> {
|
||||
let label = format!("core/{name}");
|
||||
let res = bounded(
|
||||
&format!("create repo {label}"),
|
||||
api(token)?.create_current_user_repo(repo_option(name, true)),
|
||||
)
|
||||
.await?;
|
||||
created_or_exists(res, &label)
|
||||
}
|
||||
|
||||
/// Dashboard-warning guard for the meta non-fast-forward condition.
|
||||
/// Held while the `core/meta` remote is ahead of our local mirror; cleared
|
||||
/// automatically when the next push succeeds.
|
||||
static META_NON_FF_GUARD: Mutex<Option<crate::warnings::WarningGuard>> = Mutex::new(None);
|
||||
|
||||
/// Push `dir` (the legacy hive meta repo) to `core/meta` on the local forge.
|
||||
/// Its one caller is `meta::remove_permission_files`' commit.
|
||||
/// Best-effort: returns Err which callers log + ignore. No-op when
|
||||
/// the core token isn't present yet (forge container not provisioned).
|
||||
pub async fn push_meta(dir: &Path) -> Result<()> {
|
||||
let Some(token) = core_token() else {
|
||||
return Ok(());
|
||||
};
|
||||
// Token-in-URL push. Forgejo accepts `oauth2:<token>` or just
|
||||
// any-username:<token>; using `core` matches the owner so the
|
||||
// remote name is self-describing.
|
||||
//
|
||||
// No --force: `meta.rs` uses regular `git commit` (append-only),
|
||||
// so the push is always fast-forward in normal operation. Per
|
||||
// operator directive, hive-c0re must not force-push anywhere. If
|
||||
// the remote is somehow ahead (e.g. split-brain or manual push) we
|
||||
// raise a dashboard warning and leave the remote intact rather than
|
||||
// silently erasing its history.
|
||||
let url = forge_git_url("core/meta");
|
||||
let out = crate::lifecycle::run_with_deadline(
|
||||
crate::lifecycle::git_command_authed(&core_auth_header(&token))
|
||||
.current_dir(dir)
|
||||
.args(["push", &url, "HEAD:main"]),
|
||||
crate::lifecycle::FETCH_DEADLINE,
|
||||
"git push core/meta",
|
||||
)
|
||||
.await?;
|
||||
if !out.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&out.stderr);
|
||||
if stderr.contains("non-fast-forward") || stderr.contains("fetch first") {
|
||||
let msg = "forge/core/meta push rejected (non-fast-forward): \
|
||||
remote is ahead of local; leaving remote history intact";
|
||||
tracing::warn!("{msg}");
|
||||
// Raise a persistent hive warning; cleared on next successful push.
|
||||
if let Ok(mut g) = META_NON_FF_GUARD.lock() {
|
||||
*g = Some(crate::warnings::set_warning(
|
||||
"forge-meta-non-ff",
|
||||
"warn",
|
||||
msg,
|
||||
));
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
anyhow::bail!(
|
||||
"git push core/meta failed ({}): {}",
|
||||
out.status,
|
||||
stderr.trim()
|
||||
);
|
||||
}
|
||||
// Successful push: clear any outstanding non-ff warning.
|
||||
if let Ok(mut g) = META_NON_FF_GUARD.lock() {
|
||||
*g = None;
|
||||
}
|
||||
tracing::info!("forge: pushed meta to core/meta");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Grant agent `name` read-only collaborator access to `core/meta` on
|
||||
/// the forge so the agent can clone/fetch that legacy repo. Idempotent:
|
||||
/// re-adding an existing collaborator succeeds (Forgejo answers 204
|
||||
/// either way).
|
||||
pub async fn meta_read_access(name: &str, core_token: &str) -> Result<()> {
|
||||
add_collaborator(
|
||||
"core",
|
||||
"meta",
|
||||
name,
|
||||
AddCollaboratorOptionPermission::Read,
|
||||
core_token,
|
||||
)
|
||||
.await?;
|
||||
tracing::info!(%name, "forge: granted meta read access");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add the forge `core/meta.git` URL as the `meta` remote in the
|
||||
/// agent's proposed config repo so the agent (and the manager) can
|
||||
/// fetch that legacy repo from the forge. Idempotent: no-op when the
|
||||
/// remote already points at the right URL, or when the proposed repo
|
||||
/// does not exist yet. No-op when the forge is not running.
|
||||
pub async fn ensure_meta_remote(name: &str) -> Result<()> {
|
||||
if !is_present().await {
|
||||
return Ok(());
|
||||
}
|
||||
// A malformed name has no proposed config repo (repos are only created
|
||||
// under a validated Ident), so there's nothing to wire — no-op.
|
||||
let Ok(agent) = hive_types::Ident::parse(name) else {
|
||||
return Ok(());
|
||||
};
|
||||
let proposed_dir = Coordinator::agent_proposed_dir(&agent);
|
||||
if !proposed_dir.join(".git").exists() {
|
||||
return Ok(());
|
||||
}
|
||||
let want = format!("{}/core/meta.git", forge_http_base());
|
||||
let existing = crate::lifecycle::git_command()
|
||||
.current_dir(&proposed_dir)
|
||||
.args(["remote", "get-url", "meta"])
|
||||
.output()
|
||||
.await
|
||||
.context("git remote get-url meta")?;
|
||||
if existing.status.success() {
|
||||
let current = String::from_utf8_lossy(&existing.stdout).trim().to_owned();
|
||||
if current == want {
|
||||
return Ok(());
|
||||
}
|
||||
crate::lifecycle::git(&proposed_dir, &["remote", "set-url", "meta", &want]).await
|
||||
} else {
|
||||
crate::lifecycle::git(&proposed_dir, &["remote", "add", "meta", &want]).await
|
||||
}
|
||||
}
|
||||
use super::{CONFIG_ORG, forge_git_url, reader_token};
|
||||
|
||||
/// Reseed a revived agent's `applied` repo — missing its `.git` entirely,
|
||||
/// e.g. after `destroy --purge` — by fetching `agent-configs/<name>`'s
|
||||
|
|
@ -334,38 +137,3 @@ pub async fn clone_config_into_proposed(dir: &Path, name: &str) -> bool {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Add `user` as a collaborator on `owner/repo` at `permission`.
|
||||
/// Idempotent: adding an existing collaborator just updates its
|
||||
/// permission (Forgejo answers 204 either way; a 201 from older
|
||||
/// versions is tolerated defensively).
|
||||
async fn add_collaborator(
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
user: &str,
|
||||
permission: AddCollaboratorOptionPermission,
|
||||
token: &str,
|
||||
) -> Result<()> {
|
||||
let op = format!("add collaborator {user} to {owner}/{repo}");
|
||||
let res = bounded(
|
||||
&op,
|
||||
api(token)?.repo_add_collaborator(
|
||||
owner,
|
||||
repo,
|
||||
user,
|
||||
AddCollaboratorOption {
|
||||
permission: Some(permission),
|
||||
},
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
match res {
|
||||
Ok(()) => {}
|
||||
Err(ForgejoError::UnexpectedStatusCode(s)) if s == StatusCode::CREATED => {}
|
||||
Err(e) => {
|
||||
return Err(e).context(op);
|
||||
}
|
||||
}
|
||||
tracing::debug!(%owner, %repo, %user, ?permission, "forge: collaborator set");
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue