diff --git a/crates/forge-core/migrations/0078_remove_features.sql b/crates/forge-core/migrations/0078_remove_features.sql new file mode 100644 index 0000000..b58b53d --- /dev/null +++ b/crates/forge-core/migrations/0078_remove_features.sql @@ -0,0 +1,27 @@ +DROP TABLE IF EXISTS discussion_notifications; +DROP TABLE IF EXISTS discussion_comments; +DROP TABLE IF EXISTS discussions; +DROP TABLE IF EXISTS snippet_revisions; +DROP TABLE IF EXISTS snippet_files; +DROP TABLE IF EXISTS snippets; +DROP TABLE IF EXISTS advisory_notifications; +DROP TABLE IF EXISTS repo_alerts; +DROP TABLE IF EXISTS repo_dependencies; +DROP TABLE IF EXISTS advisories; +DROP TABLE IF EXISTS import_map; +DROP TABLE IF EXISTS import_events; +DROP TABLE IF EXISTS import_jobs; +DROP TABLE IF EXISTS data_exports; +DROP TABLE IF EXISTS oci_manifest_refs; +DROP TABLE IF EXISTS oci_tags; +DROP TABLE IF EXISTS oci_manifests; +DROP TABLE IF EXISTS oci_blobs; +DROP TABLE IF EXISTS package_versions; +DROP TABLE IF EXISTS packages; +DROP TABLE IF EXISTS push_subscriptions; +DROP TABLE IF EXISTS web_push_keys; +ALTER TABLE repos DROP COLUMN wiki_enabled; +ALTER TABLE repos DROP COLUMN discussions_enabled; +DELETE FROM reactions WHERE target_kind IN ('discussion', 'discussion_comment'); +DELETE FROM server_settings WHERE key LIKE 'advisories.%'; +DELETE FROM replication_status WHERE subject LIKE '%.wiki'; diff --git a/crates/forge-core/src/store/migrate.rs b/crates/forge-core/src/store/migrate.rs index 8f27a2a..d3eb58c 100644 --- a/crates/forge-core/src/store/migrate.rs +++ b/crates/forge-core/src/store/migrate.rs @@ -143,6 +143,10 @@ pub const MIGRATIONS: &[(u32, &str)] = &[ 77, include_str!("../../migrations/0077_runner_stopped_at.sql"), ), + ( + 78, + include_str!("../../migrations/0078_remove_features.sql"), + ), ]; pub fn latest_schema_version() -> u32 { diff --git a/crates/forge-git/src/repo.rs b/crates/forge-git/src/repo.rs index 04ad099..f314699 100644 --- a/crates/forge-git/src/repo.rs +++ b/crates/forge-git/src/repo.rs @@ -107,9 +107,6 @@ pub fn list_trash(data_dir: &Path) -> Result> { let Ok(deleted_at) = ts.parse::() else { continue; }; - if id.ends_with(".wiki") { - continue; - } entries.push(TrashEntry { path: path.clone(), repo_id: id.to_owned(), diff --git a/crates/forge-web-app/src/leftovers.rs b/crates/forge-web-app/src/leftovers.rs new file mode 100644 index 0000000..8ea2c50 --- /dev/null +++ b/crates/forge-web-app/src/leftovers.rs @@ -0,0 +1,102 @@ +use std::path::Path; +use std::time::SystemTime; + +use crate::state::AppState; + +const BLOB_PREFIXES: [&str; 2] = ["oci", "packages"]; + +pub fn spawn(state: AppState) { + tokio::spawn(async move { sweep(&state).await }); +} + +async fn sweep(state: &AppState) { + sweep_dirs(&state.config.data_dir).await; + sweep_blobs(state).await; +} + +async fn sweep_dirs(data_dir: &Path) { + remove_dir(&data_dir.join("exports")).await; + remove_matching(&data_dir.join(forge_git::repo::TRASH_DIR), |name| { + name.contains(".wiki-") + }) + .await; + for shard in sub_dirs(&data_dir.join("repos")).await { + for inner in sub_dirs(&shard).await { + remove_matching(&inner, |name| name.ends_with(".wiki.git")).await; + } + } +} + +async fn sweep_blobs(state: &AppState) { + for prefix in BLOB_PREFIXES { + let keys = match state.blobs.keys_older_than(prefix, SystemTime::now()).await { + Ok(keys) => keys, + Err(e) => { + tracing::error!(error = %e, prefix, "leftovers: listing blobs failed"); + continue; + } + }; + for key in keys { + if let Err(e) = state.blobs.delete(&key).await { + tracing::error!(error = %e, key, "leftovers: deleting blob failed"); + } + } + } +} + +async fn sub_dirs(dir: &Path) -> Vec { + let mut dirs = Vec::new(); + let Ok(mut entries) = tokio::fs::read_dir(dir).await else { + return dirs; + }; + while let Ok(Some(entry)) = entries.next_entry().await { + if entry.file_type().await.is_ok_and(|t| t.is_dir()) { + dirs.push(entry.path()); + } + } + dirs +} + +async fn remove_matching(dir: &Path, matches: impl Fn(&str) -> bool) { + for path in sub_dirs(dir).await { + if path + .file_name() + .and_then(|n| n.to_str()) + .is_some_and(&matches) + { + remove_dir(&path).await; + } + } +} + +async fn remove_dir(path: &Path) { + match tokio::fs::remove_dir_all(path).await { + Ok(()) => tracing::info!(path = %path.display(), "leftovers: removed"), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => tracing::error!(error = %e, path = %path.display(), "leftovers: removing failed"), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn removes_wiki_repos_and_trash_but_keeps_repos() { + let dir = tempfile::tempdir().unwrap(); + let shard = dir.path().join("repos/01/ab"); + let keep = shard.join("01abcdefghjkmnpqrstvwxyz01.git"); + let wiki = shard.join("01abcdefghjkmnpqrstvwxyz01.wiki.git"); + let trash = dir.path().join("trash"); + let trash_keep = trash.join("01abcdefghjkmnpqrstvwxyz01-1.git"); + let trash_wiki = trash.join("01abcdefghjkmnpqrstvwxyz01.wiki-1.git"); + for d in [&keep, &wiki, &trash_keep, &trash_wiki] { + std::fs::create_dir_all(d).unwrap(); + } + std::fs::create_dir_all(dir.path().join("exports/x")).unwrap(); + sweep_dirs(dir.path()).await; + assert!(keep.exists() && trash_keep.exists()); + assert!(!wiki.exists() && !trash_wiki.exists()); + assert!(!dir.path().join("exports").exists()); + } +} diff --git a/crates/forge-web-app/src/lib.rs b/crates/forge-web-app/src/lib.rs index 336629f..fd827f4 100644 --- a/crates/forge-web-app/src/lib.rs +++ b/crates/forge-web-app/src/lib.rs @@ -20,6 +20,7 @@ pub mod git_http; pub mod hook_cmd; pub mod internal; pub mod layers; +pub mod leftovers; pub mod lfs; pub mod lfs_pointer; pub mod live; diff --git a/crates/forge-web/src/boot.rs b/crates/forge-web/src/boot.rs index df81c36..5f8015d 100644 --- a/crates/forge-web/src/boot.rs +++ b/crates/forge-web/src/boot.rs @@ -146,6 +146,7 @@ pub async fn serve( forge_web_app::ci::reaper::spawn(state.clone()); forge_web_app::merge_queue::spawn_resume(state.clone()); forge_web_app::prune::spawn(state.clone()); + forge_web_app::leftovers::spawn(state.clone()); forge_web_account::pages::admin_health::spawn_repo_size(state.clone()); forge_web_app::ci::scheduled::spawn(state.clone()); diff --git a/docs/architecture.md b/docs/architecture.md index 0b88e14..570ae23 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -547,7 +547,7 @@ Known gaps: - `range-diff` for "what changed since the last review." AGit (`refs/for/`) is considered later. - Live pages: the pull list and every pull tab (conversation, commits, files) update while open. Each has an event stream (`/{o}/{r}/pulls/events`, `/{o}/{r}/pulls/{n}/events`) that sends `changed` when a write changes what the page shows. A POST under the pulls paths, a push, a commit status for the head, and a mergeability or auto-merge run each wake it. The page re-reads in place and keeps typed text, focus and open inline forms; the Files tab keeps the file in view at the same offset. Streams are in-process, event driven, and never poll. Topics live in `live.rs`; the bump helpers are in `pages/pull_live.rs`. -### Webhooks, push mirrors, replication, backups, export +### Webhooks, push mirrors, replication, backups - Webhooks: HMAC-SHA256 signature over the raw body, delivery id header, 10s timeout, retry queue, delivery log. - Pull mirrors on an interval with a minimum; push mirrors with sealed credentials, run after each push with backoff. SSRF rules from section 9 apply.