forge
Explore
Log in

Don't you dare go Hollow

The commit, branch, tag, compare and blame pages update live

Browse files at this commit

420b0e0dd113e550724a69d2bd9bfbd812d82f62Ahmet Kaan GümüşParent: ee30eab
  • feat
    • the commit, branch, tag, compare and blame pages update live
Show full message
⚡ feat: the commit, branch, tag, compare and blame pages update live
crates/forge-web/src/api/statuses.rsmodified+3 -0
@@ -107,6 +107,9 @@ async fn report_status(cx: &Cx, body: Body) -> topcoat::Result<Response> {
107107 }
108108 Err(e) => return Ok(*api::internal(e)),
109109 };
110+ state
111+ .live
112+ .bump(&crate::live::Topic::CommitStatuses(repo.id.clone()));
110113 crate::automerge::on_status(state, &repo.id, &sha).await;
111114 crate::merge_queue::on_status(state, &repo, &sha).await;
112115 Ok(api::json(StatusCode::CREATED, &status_json(&saved)))
crates/forge-web/src/ci/mod.rsmodified+3 -0
@@ -111,6 +111,9 @@ pub(crate) async fn publish(
111111 tracing::error!(error = %e, run_id = %run.id, context, "ci: writing commit status failed");
112112 return;
113113 }
114+ state
115+ .live
116+ .bump(&crate::live::Topic::CommitStatuses(repo.id.clone()));
114117 crate::automerge::on_status(state, &repo.id, &run.commit_oid).await;
115118 // Boxed: a queue advance can start the next candidate's run, which publishes again.
116119 Box::pin(crate::merge_queue::on_status(state, repo, &run.commit_oid)).await;
crates/forge-web/src/live.rsmodified+3 -0
@@ -111,6 +111,9 @@ pub enum Topic {
111111 /// secret, webhook or delivery, mirror sync, security alert, or a pending transfer changed.
112112 /// Keyed by repo id. Bumped by the settings write layer and by the background writers.
113113 RepoSettings(String),
114+ /// The commit statuses of a repo: a status was reported or replaced. Keyed by repo id. Bumped
115+ /// by the CI runner and the statuses API, after the write.
116+ CommitStatuses(String),
114117 }
115118
116119 /// The hub of the admin pages. It is process-wide, not part of [`crate::state::AppState`]: the
crates/forge-web/src/pages/blame.rsmodified+3 -1
@@ -16,6 +16,7 @@ use crate::i18n::browse as b;
1616 use crate::i18n::code::{self as g, K};
1717 use crate::i18n::{self, Lang};
1818 use crate::pages::blob::{Line, text_lines};
19+use crate::pages::ref_live;
1920 use crate::pages::repo::repo_head;
2021 use crate::pages::repo_ctx::load_repo;
2122 use crate::pages::tree::{MAX_BLOB_BYTES, path_crumbs, read_file, resolve};
@@ -218,7 +219,8 @@ async fn blame_page(cx: &Cx) -> Result<impl View> {
218219 });
219220 let has_rows = !rows.is_empty();
220221
221- let header = ctx.head(state, "code").await?;
222+ let mut header = ctx.head(state, "code").await?;
223+ header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name);
222224 Ok(view! {
223225 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
224226 repo_head(head: header)
crates/forge-web/src/pages/branches.rsmodified+3 -1
@@ -22,6 +22,7 @@ use crate::i18n::code::{self as g, K};
2222 use crate::i18n::{self, Lang};
2323 use crate::internal::{is_zero_oid, may_push_directly};
2424 use crate::pages::commits::url_path;
25+use crate::pages::ref_live;
2526 use crate::pages::refs::ref_rows;
2627 use crate::pages::repo::repo_head;
2728 use crate::pages::repo_ctx::{RepoCtx, load_repo};
@@ -293,7 +294,8 @@ async fn branches_page(cx: &Cx) -> Result<impl View> {
293294 g::t(lang, K::BranchesTitle)
294295 );
295296 let empty = rows.is_empty();
296- let header = ctx.head(state, "branches").await?;
297+ let mut header = ctx.head(state, "branches").await?;
298+ header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name);
297299 Ok(view! {
298300 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
299301 repo_head(head: header)
crates/forge-web/src/pages/commit.rsmodified+3 -1
@@ -12,6 +12,7 @@ use topcoat::view::{View, view};
1212 use crate::i18n::code::{self as g, K};
1313 use crate::i18n::{self};
1414 use crate::pages::commits::{SigBadgeView, blocking, iso_time, now_secs, short_oid};
15+use crate::pages::ref_live;
1516 use crate::pages::repo::repo_head;
1617 use crate::pages::repo_ctx::{RawDownload, load_repo};
1718 use crate::render::emoji::emojify;
@@ -168,7 +169,8 @@ async fn commit_page(cx: &Cx) -> Result<impl View> {
168169 .get(&full_oid)
169170 .map(|badge| SigBadgeView::from_badge(badge, lang));
170171
171- let header = ctx.head(state, "commits").await?;
172+ let mut header = ctx.head(state, "commits").await?;
173+ header.live = ref_live::live_url(&owner_name, &repo_name);
172174 Ok(view! {
173175 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
174176 repo_head(head: header)
crates/forge-web/src/pages/commits.rsmodified+7 -1
@@ -17,6 +17,7 @@ use topcoat::view::{View, view};
1717
1818 use crate::i18n::code::{self as g, K};
1919 use crate::i18n::{self, Lang};
20+use crate::pages::ref_live;
2021 use crate::pages::repo::repo_head;
2122 use crate::pages::repo_ctx::load_repo;
2223 use crate::render::emoji::emojify;
@@ -275,7 +276,8 @@ async fn commits_page(cx: &Cx) -> Result<impl View> {
275276 let filter_text = filter.unwrap_or_default();
276277 let page_label = g::page_label(lang, page_no);
277278
278- let header = ctx.head(state, "commits").await?;
279+ let mut header = ctx.head(state, "commits").await?;
280+ header.live = ref_live::live_url(&owner_name, &repo_name);
279281 Ok(view! {
280282 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
281283 repo_head(head: header)
@@ -345,6 +347,8 @@ pub(crate) mod test_support {
345347 pub struct Fixture {
346348 pub router: Router,
347349 pub store: Store,
350+ /// The live hub the router's pages share, to bump topics from a test.
351+ pub live: std::sync::Arc<crate::live::LiveHub>,
348352 /// Root commit: adds `a.txt` and `dir/b.txt`.
349353 pub root: String,
350354 /// Adds `new.txt` and changes `a.txt`.
@@ -383,6 +387,7 @@ pub(crate) mod test_support {
383387 let dir = tmp.path().to_path_buf();
384388 let state = crate::test_support::test_state(&dir).await;
385389 let store = state.store.clone();
390+ let live = state.live.clone();
386391 let router = crate::test_support::app_router(state);
387392
388393 let owner = store
@@ -459,6 +464,7 @@ pub(crate) mod test_support {
459464 Self {
460465 router,
461466 store,
467+ live,
462468 root,
463469 modified,
464470 work,
crates/forge-web/src/pages/compare.rsmodified+5 -2
@@ -31,6 +31,7 @@ use crate::i18n::pulls::{self as g, K};
3131 use crate::notify::{self, Activity};
3232 use crate::pages::commit::Download;
3333 use crate::pages::commits::{CommitRow, blocking, now_secs, short_oid, url_path};
34+use crate::pages::ref_live;
3435 use crate::pages::repo::repo_head;
3536 use crate::pages::repo_ctx::{RepoCtx, load_repo};
3637 use crate::state::AppState;
@@ -324,7 +325,8 @@ async fn compare_view(
324325 let heading_base = base.clone();
325326 let heading_head = head.clone();
326327
327- let header = ctx.head(state, "commits").await?;
328+ let mut header = ctx.head(state, "commits").await?;
329+ header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name);
328330 let __cx = cx;
329331 Ok(view! {
330332 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
410412 .collect();
411413 let title = format!("{}/{}", ctx.repo.owner_name, ctx.repo.name);
412414 let action = repo_href(&ctx.repo.owner_name, &ctx.repo.name, "compare", "");
413- let header = ctx.head(state, "commits").await?;
415+ let mut header = ctx.head(state, "commits").await?;
416+ header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name);
414417 let __cx = cx;
415418 Ok(view! {
416419 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
crates/forge-web/src/pages/mod.rsmodified+1 -0
@@ -96,6 +96,7 @@ pub mod push;
9696 pub mod reactions;
9797 #[cfg(test)]
9898 mod reactions_tests;
99+pub mod ref_live;
99100 pub mod refs;
100101 pub mod register;
101102 pub mod releases;
crates/forge-web/src/pages/ref_live.rsadded+121 -0
@@ -0,0 +1,121 @@
1+//! Live stream of the ref-driven repo pages: the commit list, one commit, the branch list, the tag
2+//! list, compare and blame. They render `data-live` with `/{owner}/{repo}/refs/events` in their
3+//! repo header, so the header's own counts follow too.
4+//!
5+//! The stream sends `changed` when a branch or tag is created, deleted or moved
6+//! (`Topic::RepoRefs`, bumped by `post_update::after_ref_update` for every ref write, web or
7+//! push), when a commit status is reported (`Topic::CommitStatuses`), or when the repo header
8+//! changes (`Topic::Repo`). Each wake is one `changed`: these pages have no cheap fingerprint of
9+//! the commits they show, and a soft re-read is cheap.
10+
11+use topcoat::Result;
12+use topcoat::context::{Cx, app_context};
13+use topcoat::router::response::Response;
14+use topcoat::router::{path_param, route};
15+
16+use crate::live::{LiveHub, Topic, stream_wakes};
17+use crate::pages::repo_ctx::load_repo;
18+use crate::state::AppState;
19+
20+path_param!(owner);
21+path_param!(repo);
22+
23+/// The stream URL of a ref-driven repo page.
24+pub fn live_url(owner: &str, repo: &str) -> String {
25+ format!("/{owner}/{repo}/refs/events")
26+}
27+
28+/// The topics a ref-driven page follows: its repo's header, refs and commit statuses.
29+pub fn topics(repo: &forge_core::Repo) -> [Topic; 3] {
30+ [
31+ Topic::Repo(repo.id.clone()),
32+ Topic::RepoRefs(repo.id.clone()),
33+ Topic::CommitStatuses(repo.id.clone()),
34+ ]
35+}
36+
37+/// The live stream of the ref-driven pages. Auth is the repo read check of those pages: a viewer
38+/// who cannot read the repo gets the same 404 as the page.
39+#[route(GET "/{owner}/{repo}/refs/events")]
40+async fn ref_events(cx: &Cx) -> Result<Response> {
41+ let owner = path_param::<Owner>(cx);
42+ let name = path_param::<Repo>(cx);
43+ let ctx = load_repo(cx, owner, name).await?;
44+ let hub: &LiveHub = &app_context::<AppState>(cx).live;
45+ stream_wakes(cx, hub, &topics(&ctx.repo))
46+}
47+
48+#[cfg(test)]
49+mod tests {
50+ use std::time::Duration;
51+
52+ use forge_core::Visibility;
53+ use topcoat::router::Body;
54+ use topcoat::router::request::Request;
55+
56+ use crate::live::Topic;
57+ use crate::pages::commits::test_support::{Fixture, OWNER, REPO};
58+
59+ /// GET `path` through the router, anonymously.
60+ async fn open(fx: &Fixture, path: &str) -> topcoat::router::response::Response {
61+ let req = Request::builder()
62+ .method("GET")
63+ .uri(path)
64+ .body(Body::empty())
65+ .unwrap();
66+ fx.router.handle(req).await
67+ }
68+
69+ #[tokio::test]
70+ async fn a_ref_write_or_a_status_wakes_the_stream() {
71+ use futures_util::StreamExt;
72+
73+ let fx = Fixture::new(Visibility::Public).await;
74+ let repo = fx
75+ .store
76+ .repo_by_owner_and_name(OWNER, REPO)
77+ .await
78+ .unwrap()
79+ .unwrap();
80+ let res = open(&fx, &format!("/{OWNER}/{REPO}/refs/events")).await;
81+ assert_eq!(res.status().as_u16(), 200);
82+ assert_eq!(
83+ res.headers().get("content-type").unwrap(),
84+ "text/event-stream"
85+ );
86+ let mut frames = res.into_body().into_data_stream();
87+ // The stream opens with its retry field and sends nothing on connect.
88+ let retry = frames.next().await.expect("a frame").expect("data");
89+ assert!(retry.starts_with(b"retry: 1000"), "{retry:?}");
90+
91+ fx.live.bump(&Topic::RepoRefs(repo.id.clone()));
92+ let first = tokio::time::timeout(Duration::from_secs(5), frames.next())
93+ .await
94+ .expect("woken")
95+ .expect("a frame")
96+ .expect("data");
97+ assert_eq!(&first[..], b"event: changed\ndata: 1\n\n");
98+
99+ fx.live.bump(&Topic::CommitStatuses(repo.id.clone()));
100+ let second = tokio::time::timeout(Duration::from_secs(5), frames.next())
101+ .await
102+ .expect("woken")
103+ .expect("a frame")
104+ .expect("data");
105+ assert_eq!(&second[..], b"event: changed\ndata: 1\n\n");
106+ }
107+
108+ #[tokio::test]
109+ async fn a_private_repo_stream_is_404_for_anonymous() {
110+ let fx = Fixture::new(Visibility::Private).await;
111+ let res = open(&fx, &format!("/{OWNER}/{REPO}/refs/events")).await;
112+ assert_eq!(res.status().as_u16(), 404);
113+ }
114+
115+ #[tokio::test]
116+ async fn an_unknown_repo_stream_is_404() {
117+ let fx = Fixture::new(Visibility::Public).await;
118+ let res = open(&fx, &format!("/{OWNER}/nope/refs/events")).await;
119+ assert_eq!(res.status().as_u16(), 404);
120+ }
121+}
crates/forge-web/src/pages/refs.rsmodified+3 -1
@@ -13,6 +13,7 @@ use topcoat::view::{View, component, view};
1313
1414 use crate::i18n::code::{self as g, K};
1515 use crate::i18n::{self, Lang};
16+use crate::pages::ref_live;
1617 use crate::pages::repo::repo_head;
1718 use crate::pages::repo_ctx::{RepoCtx, load_repo};
1819 use crate::pages::tree::{LastCommit, last_commit_view};
@@ -178,7 +179,8 @@ async fn tags_page(cx: &Cx) -> Result<impl View> {
178179 ctx.repo.name,
179180 g::t(lang, K::TagsTitle)
180181 );
181- let header = ctx.head(app_context::<AppState>(cx), "tags").await?;
182+ let mut header = ctx.head(app_context::<AppState>(cx), "tags").await?;
183+ header.live = ref_live::live_url(&ctx.repo.owner_name, &ctx.repo.name);
182184 Ok(view! {
183185 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
184186 repo_head(head: header)
docs/architecture.mdmodified+1 -0
@@ -257,6 +257,7 @@ Pages update without a reload. A write that changes what a page shows calls `Liv
257257 | Dashboard `/` | `/dashboard/events` | the viewer's user topic (repos, pushes, repo changes) and inbox |
258258 | Profile `/{username}` | `/users/{username}/events` | the owner's user topic (repos, stars, pins, pushes by the owner) |
259259 | 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 |
260+| 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 |
260261 | 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 |
261262 | 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) |
262263 | Explore `/explore` | `/explore/events`, with the search, sort and page | the public listing |