diff --git a/crates/forge-web/src/api/statuses.rs b/crates/forge-web/src/api/statuses.rs index 83a8300..16a6602 100644 --- a/crates/forge-web/src/api/statuses.rs +++ b/crates/forge-web/src/api/statuses.rs @@ -107,6 +107,9 @@ async fn report_status(cx: &Cx, body: Body) -> topcoat::Result { } Err(e) => return Ok(*api::internal(e)), }; + state + .live + .bump(&crate::live::Topic::CommitStatuses(repo.id.clone())); crate::automerge::on_status(state, &repo.id, &sha).await; crate::merge_queue::on_status(state, &repo, &sha).await; Ok(api::json(StatusCode::CREATED, &status_json(&saved))) diff --git a/crates/forge-web/src/ci/mod.rs b/crates/forge-web/src/ci/mod.rs index 0b55ff7..f9a7fe2 100644 --- a/crates/forge-web/src/ci/mod.rs +++ b/crates/forge-web/src/ci/mod.rs @@ -111,6 +111,9 @@ pub(crate) async fn publish( tracing::error!(error = %e, run_id = %run.id, context, "ci: writing commit status failed"); return; } + state + .live + .bump(&crate::live::Topic::CommitStatuses(repo.id.clone())); crate::automerge::on_status(state, &repo.id, &run.commit_oid).await; // Boxed: a queue advance can start the next candidate's run, which publishes again. Box::pin(crate::merge_queue::on_status(state, repo, &run.commit_oid)).await; diff --git a/crates/forge-web/src/live.rs b/crates/forge-web/src/live.rs index d185511..783d111 100644 --- a/crates/forge-web/src/live.rs +++ b/crates/forge-web/src/live.rs @@ -111,6 +111,9 @@ pub enum Topic { /// secret, webhook or delivery, mirror sync, security alert, or a pending transfer changed. /// Keyed by repo id. Bumped by the settings write layer and by the background writers. RepoSettings(String), + /// The commit statuses of a repo: a status was reported or replaced. Keyed by repo id. Bumped + /// by the CI runner and the statuses API, after the write. + CommitStatuses(String), } /// The hub of the admin pages. It is process-wide, not part of [`crate::state::AppState`]: the diff --git a/crates/forge-web/src/pages/blame.rs b/crates/forge-web/src/pages/blame.rs index 1f3a1b0..de2fac2 100644 --- a/crates/forge-web/src/pages/blame.rs +++ b/crates/forge-web/src/pages/blame.rs @@ -16,6 +16,7 @@ use crate::i18n::browse as b; use crate::i18n::code::{self as g, K}; use crate::i18n::{self, Lang}; use crate::pages::blob::{Line, text_lines}; +use crate::pages::ref_live; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::load_repo; use crate::pages::tree::{MAX_BLOB_BYTES, path_crumbs, read_file, resolve}; @@ -218,7 +219,8 @@ async fn blame_page(cx: &Cx) -> Result { }); let has_rows = !rows.is_empty(); - let header = ctx.head(state, "code").await?; + let mut header = ctx.head(state, "code").await?; + header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name); Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header) diff --git a/crates/forge-web/src/pages/branches.rs b/crates/forge-web/src/pages/branches.rs index 5ed7b9c..788a3b1 100644 --- a/crates/forge-web/src/pages/branches.rs +++ b/crates/forge-web/src/pages/branches.rs @@ -22,6 +22,7 @@ use crate::i18n::code::{self as g, K}; use crate::i18n::{self, Lang}; use crate::internal::{is_zero_oid, may_push_directly}; use crate::pages::commits::url_path; +use crate::pages::ref_live; use crate::pages::refs::ref_rows; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::{RepoCtx, load_repo}; @@ -293,7 +294,8 @@ async fn branches_page(cx: &Cx) -> Result { g::t(lang, K::BranchesTitle) ); let empty = rows.is_empty(); - let header = ctx.head(state, "branches").await?; + let mut header = ctx.head(state, "branches").await?; + header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name); Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header) diff --git a/crates/forge-web/src/pages/commit.rs b/crates/forge-web/src/pages/commit.rs index 5dd85e3..143bd8f 100644 --- a/crates/forge-web/src/pages/commit.rs +++ b/crates/forge-web/src/pages/commit.rs @@ -12,6 +12,7 @@ use topcoat::view::{View, view}; use crate::i18n::code::{self as g, K}; use crate::i18n::{self}; use crate::pages::commits::{SigBadgeView, blocking, iso_time, now_secs, short_oid}; +use crate::pages::ref_live; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::{RawDownload, load_repo}; use crate::render::emoji::emojify; @@ -168,7 +169,8 @@ async fn commit_page(cx: &Cx) -> Result { .get(&full_oid) .map(|badge| SigBadgeView::from_badge(badge, lang)); - let header = ctx.head(state, "commits").await?; + let mut header = ctx.head(state, "commits").await?; + header.live = ref_live::live_url(&owner_name, &repo_name); Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header) diff --git a/crates/forge-web/src/pages/commits.rs b/crates/forge-web/src/pages/commits.rs index 9a6cf99..1fe4835 100644 --- a/crates/forge-web/src/pages/commits.rs +++ b/crates/forge-web/src/pages/commits.rs @@ -17,6 +17,7 @@ use topcoat::view::{View, view}; use crate::i18n::code::{self as g, K}; use crate::i18n::{self, Lang}; +use crate::pages::ref_live; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::load_repo; use crate::render::emoji::emojify; @@ -275,7 +276,8 @@ async fn commits_page(cx: &Cx) -> Result { let filter_text = filter.unwrap_or_default(); let page_label = g::page_label(lang, page_no); - let header = ctx.head(state, "commits").await?; + let mut header = ctx.head(state, "commits").await?; + header.live = ref_live::live_url(&owner_name, &repo_name); Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header) @@ -345,6 +347,8 @@ pub(crate) mod test_support { pub struct Fixture { pub router: Router, pub store: Store, + /// The live hub the router's pages share, to bump topics from a test. + pub live: std::sync::Arc, /// Root commit: adds `a.txt` and `dir/b.txt`. pub root: String, /// Adds `new.txt` and changes `a.txt`. @@ -383,6 +387,7 @@ pub(crate) mod test_support { let dir = tmp.path().to_path_buf(); let state = crate::test_support::test_state(&dir).await; let store = state.store.clone(); + let live = state.live.clone(); let router = crate::test_support::app_router(state); let owner = store @@ -459,6 +464,7 @@ pub(crate) mod test_support { Self { router, store, + live, root, modified, work, diff --git a/crates/forge-web/src/pages/compare.rs b/crates/forge-web/src/pages/compare.rs index 173ebf1..258f2df 100644 --- a/crates/forge-web/src/pages/compare.rs +++ b/crates/forge-web/src/pages/compare.rs @@ -31,6 +31,7 @@ use crate::i18n::pulls::{self as g, K}; use crate::notify::{self, Activity}; use crate::pages::commit::Download; use crate::pages::commits::{CommitRow, blocking, now_secs, short_oid, url_path}; +use crate::pages::ref_live; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::{RepoCtx, load_repo}; use crate::state::AppState; @@ -324,7 +325,8 @@ async fn compare_view( let heading_base = base.clone(); let heading_head = head.clone(); - let header = ctx.head(state, "commits").await?; + let mut header = ctx.head(state, "commits").await?; + header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name); let __cx = cx; Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), @@ -410,7 +412,8 @@ async fn picker_view(cx: &Cx, ctx: RepoCtx, base: String, head: String) -> Resul .collect(); let title = format!("{}/{}", ctx.repo.owner_name, ctx.repo.name); let action = repo_href(&ctx.repo.owner_name, &ctx.repo.name, "compare", ""); - let header = ctx.head(state, "commits").await?; + let mut header = ctx.head(state, "commits").await?; + header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name); let __cx = cx; Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), diff --git a/crates/forge-web/src/pages/mod.rs b/crates/forge-web/src/pages/mod.rs index 69c9f5d..01b1630 100644 --- a/crates/forge-web/src/pages/mod.rs +++ b/crates/forge-web/src/pages/mod.rs @@ -96,6 +96,7 @@ pub mod push; pub mod reactions; #[cfg(test)] mod reactions_tests; +pub mod ref_live; pub mod refs; pub mod register; pub mod releases; diff --git a/crates/forge-web/src/pages/ref_live.rs b/crates/forge-web/src/pages/ref_live.rs new file mode 100644 index 0000000..7bf956d --- /dev/null +++ b/crates/forge-web/src/pages/ref_live.rs @@ -0,0 +1,121 @@ +//! Live stream of the ref-driven repo pages: the commit list, one commit, the branch list, the tag +//! list, compare and blame. They render `data-live` with `/{owner}/{repo}/refs/events` in their +//! repo header, so the header's own counts follow too. +//! +//! The stream sends `changed` when a branch or tag is created, deleted or moved +//! (`Topic::RepoRefs`, bumped by `post_update::after_ref_update` for every ref write, web or +//! push), when a commit status is reported (`Topic::CommitStatuses`), or when the repo header +//! changes (`Topic::Repo`). Each wake is one `changed`: these pages have no cheap fingerprint of +//! the commits they show, and a soft re-read is cheap. + +use topcoat::Result; +use topcoat::context::{Cx, app_context}; +use topcoat::router::response::Response; +use topcoat::router::{path_param, route}; + +use crate::live::{LiveHub, Topic, stream_wakes}; +use crate::pages::repo_ctx::load_repo; +use crate::state::AppState; + +path_param!(owner); +path_param!(repo); + +/// The stream URL of a ref-driven repo page. +pub fn live_url(owner: &str, repo: &str) -> String { + format!("/{owner}/{repo}/refs/events") +} + +/// The topics a ref-driven page follows: its repo's header, refs and commit statuses. +pub fn topics(repo: &forge_core::Repo) -> [Topic; 3] { + [ + Topic::Repo(repo.id.clone()), + Topic::RepoRefs(repo.id.clone()), + Topic::CommitStatuses(repo.id.clone()), + ] +} + +/// The live stream of the ref-driven pages. Auth is the repo read check of those pages: a viewer +/// who cannot read the repo gets the same 404 as the page. +#[route(GET "/{owner}/{repo}/refs/events")] +async fn ref_events(cx: &Cx) -> Result { + let owner = path_param::(cx); + let name = path_param::(cx); + let ctx = load_repo(cx, owner, name).await?; + let hub: &LiveHub = &app_context::(cx).live; + stream_wakes(cx, hub, &topics(&ctx.repo)) +} + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use forge_core::Visibility; + use topcoat::router::Body; + use topcoat::router::request::Request; + + use crate::live::Topic; + use crate::pages::commits::test_support::{Fixture, OWNER, REPO}; + + /// GET `path` through the router, anonymously. + async fn open(fx: &Fixture, path: &str) -> topcoat::router::response::Response { + let req = Request::builder() + .method("GET") + .uri(path) + .body(Body::empty()) + .unwrap(); + fx.router.handle(req).await + } + + #[tokio::test] + async fn a_ref_write_or_a_status_wakes_the_stream() { + use futures_util::StreamExt; + + let fx = Fixture::new(Visibility::Public).await; + let repo = fx + .store + .repo_by_owner_and_name(OWNER, REPO) + .await + .unwrap() + .unwrap(); + let res = open(&fx, &format!("/{OWNER}/{REPO}/refs/events")).await; + assert_eq!(res.status().as_u16(), 200); + assert_eq!( + res.headers().get("content-type").unwrap(), + "text/event-stream" + ); + let mut frames = res.into_body().into_data_stream(); + // The stream opens with its retry field and sends nothing on connect. + let retry = frames.next().await.expect("a frame").expect("data"); + assert!(retry.starts_with(b"retry: 1000"), "{retry:?}"); + + fx.live.bump(&Topic::RepoRefs(repo.id.clone())); + let first = tokio::time::timeout(Duration::from_secs(5), frames.next()) + .await + .expect("woken") + .expect("a frame") + .expect("data"); + assert_eq!(&first[..], b"event: changed\ndata: 1\n\n"); + + fx.live.bump(&Topic::CommitStatuses(repo.id.clone())); + let second = tokio::time::timeout(Duration::from_secs(5), frames.next()) + .await + .expect("woken") + .expect("a frame") + .expect("data"); + assert_eq!(&second[..], b"event: changed\ndata: 1\n\n"); + } + + #[tokio::test] + async fn a_private_repo_stream_is_404_for_anonymous() { + let fx = Fixture::new(Visibility::Private).await; + let res = open(&fx, &format!("/{OWNER}/{REPO}/refs/events")).await; + assert_eq!(res.status().as_u16(), 404); + } + + #[tokio::test] + async fn an_unknown_repo_stream_is_404() { + let fx = Fixture::new(Visibility::Public).await; + let res = open(&fx, &format!("/{OWNER}/nope/refs/events")).await; + assert_eq!(res.status().as_u16(), 404); + } +} diff --git a/crates/forge-web/src/pages/refs.rs b/crates/forge-web/src/pages/refs.rs index 9e8592a..bccc8af 100644 --- a/crates/forge-web/src/pages/refs.rs +++ b/crates/forge-web/src/pages/refs.rs @@ -13,6 +13,7 @@ use topcoat::view::{View, component, view}; use crate::i18n::code::{self as g, K}; use crate::i18n::{self, Lang}; +use crate::pages::ref_live; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::{RepoCtx, load_repo}; use crate::pages::tree::{LastCommit, last_commit_view}; @@ -178,7 +179,8 @@ async fn tags_page(cx: &Cx) -> Result { ctx.repo.name, g::t(lang, K::TagsTitle) ); - let header = ctx.head(app_context::(cx), "tags").await?; + let mut header = ctx.head(app_context::(cx), "tags").await?; + header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name); Ok(view! { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header) diff --git a/docs/architecture.md b/docs/architecture.md index 5547899..8611991 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -257,6 +257,7 @@ Pages update without a reload. A write that changes what a page shows calls `Liv | Dashboard `/` | `/dashboard/events` | the viewer's user topic (repos, pushes, repo changes) and inbox | | Profile `/{username}` | `/users/{username}/events` | the owner's user topic (repos, stars, pins, pushes by the owner) | | Code view `/{owner}/{repo}/...` | `/{owner}/{repo}/events`, with `?ref_name=` on a branch code view | the repo (header counts); on a branch, the repo refs | +| Commits `/{owner}/{repo}/commits/...`, a commit, branches, tags, compare, blame | `/{owner}/{repo}/refs/events` | the repo header, the refs (a branch or tag created, deleted or moved) and commit statuses | | Issues `/{owner}/{repo}/issues` and an issue | `/{owner}/{repo}/issues/live`, `/{owner}/{repo}/issues/{number}/live` | the repo issues (list, labels, milestones, tabs) and the one issue | | Pull requests `/{owner}/{repo}/pulls` and a pull | `/{owner}/{repo}/pulls/events`, `/{owner}/{repo}/pulls/{number}/events` | the repo pull list, and the one pull (conversation, commits, files) | | Explore `/explore` | `/explore/events`, with the search, sort and page | the public listing |