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> {
107 }107 }
108 Err(e) => return Ok(*api::internal(e)),108 Err(e) => return Ok(*api::internal(e)),
109 };109 };
110+ state
111+ .live
112+ .bump(&crate::live::Topic::CommitStatuses(repo.id.clone()));
110 crate::automerge::on_status(state, &repo.id, &sha).await;113 crate::automerge::on_status(state, &repo.id, &sha).await;
111 crate::merge_queue::on_status(state, &repo, &sha).await;114 crate::merge_queue::on_status(state, &repo, &sha).await;
112 Ok(api::json(StatusCode::CREATED, &status_json(&saved)))115 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(
111 tracing::error!(error = %e, run_id = %run.id, context, "ci: writing commit status failed");111 tracing::error!(error = %e, run_id = %run.id, context, "ci: writing commit status failed");
112 return;112 return;
113 }113 }
114+ state
115+ .live
116+ .bump(&crate::live::Topic::CommitStatuses(repo.id.clone()));
114 crate::automerge::on_status(state, &repo.id, &run.commit_oid).await;117 crate::automerge::on_status(state, &repo.id, &run.commit_oid).await;
115 // Boxed: a queue advance can start the next candidate's run, which publishes again.118 // Boxed: a queue advance can start the next candidate's run, which publishes again.
116 Box::pin(crate::merge_queue::on_status(state, repo, &run.commit_oid)).await;119 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 {
111 /// secret, webhook or delivery, mirror sync, security alert, or a pending transfer changed.111 /// secret, webhook or delivery, mirror sync, security alert, or a pending transfer changed.
112 /// Keyed by repo id. Bumped by the settings write layer and by the background writers.112 /// Keyed by repo id. Bumped by the settings write layer and by the background writers.
113 RepoSettings(String),113 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),
114 }117 }
115 118
116 /// The hub of the admin pages. It is process-wide, not part of [`crate::state::AppState`]: the119 /// 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;
16 use crate::i18n::code::{self as g, K};16 use crate::i18n::code::{self as g, K};
17 use crate::i18n::{self, Lang};17 use crate::i18n::{self, Lang};
18 use crate::pages::blob::{Line, text_lines};18 use crate::pages::blob::{Line, text_lines};
19+use crate::pages::ref_live;
19 use crate::pages::repo::repo_head;20 use crate::pages::repo::repo_head;
20 use crate::pages::repo_ctx::load_repo;21 use crate::pages::repo_ctx::load_repo;
21 use crate::pages::tree::{MAX_BLOB_BYTES, path_crumbs, read_file, resolve};22 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> {
218 });219 });
219 let has_rows = !rows.is_empty();220 let has_rows = !rows.is_empty();
220 221
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);
222 Ok(view! {224 Ok(view! {
223 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),225 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
224 repo_head(head: header)226 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};
22 use crate::i18n::{self, Lang};22 use crate::i18n::{self, Lang};
23 use crate::internal::{is_zero_oid, may_push_directly};23 use crate::internal::{is_zero_oid, may_push_directly};
24 use crate::pages::commits::url_path;24 use crate::pages::commits::url_path;
25+use crate::pages::ref_live;
25 use crate::pages::refs::ref_rows;26 use crate::pages::refs::ref_rows;
26 use crate::pages::repo::repo_head;27 use crate::pages::repo::repo_head;
27 use crate::pages::repo_ctx::{RepoCtx, load_repo};28 use crate::pages::repo_ctx::{RepoCtx, load_repo};
@@ -293,7 +294,8 @@ async fn branches_page(cx: &Cx) -> Result<impl View> {
293 g::t(lang, K::BranchesTitle)294 g::t(lang, K::BranchesTitle)
294 );295 );
295 let empty = rows.is_empty();296 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);
297 Ok(view! {299 Ok(view! {
298 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),300 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
299 repo_head(head: header)301 repo_head(head: header)
crates/forge-web/src/pages/commit.rsmodified+3 -1
@@ -12,6 +12,7 @@ use topcoat::view::{View, view};
12 use crate::i18n::code::{self as g, K};12 use crate::i18n::code::{self as g, K};
13 use crate::i18n::{self};13 use crate::i18n::{self};
14 use crate::pages::commits::{SigBadgeView, blocking, iso_time, now_secs, short_oid};14 use crate::pages::commits::{SigBadgeView, blocking, iso_time, now_secs, short_oid};
15+use crate::pages::ref_live;
15 use crate::pages::repo::repo_head;16 use crate::pages::repo::repo_head;
16 use crate::pages::repo_ctx::{RawDownload, load_repo};17 use crate::pages::repo_ctx::{RawDownload, load_repo};
17 use crate::render::emoji::emojify;18 use crate::render::emoji::emojify;
@@ -168,7 +169,8 @@ async fn commit_page(cx: &Cx) -> Result<impl View> {
168 .get(&full_oid)169 .get(&full_oid)
169 .map(|badge| SigBadgeView::from_badge(badge, lang));170 .map(|badge| SigBadgeView::from_badge(badge, lang));
170 171
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);
172 Ok(view! {174 Ok(view! {
173 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),175 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
174 repo_head(head: header)176 repo_head(head: header)
crates/forge-web/src/pages/commits.rsmodified+7 -1
@@ -17,6 +17,7 @@ use topcoat::view::{View, view};
17 17
18 use crate::i18n::code::{self as g, K};18 use crate::i18n::code::{self as g, K};
19 use crate::i18n::{self, Lang};19 use crate::i18n::{self, Lang};
20+use crate::pages::ref_live;
20 use crate::pages::repo::repo_head;21 use crate::pages::repo::repo_head;
21 use crate::pages::repo_ctx::load_repo;22 use crate::pages::repo_ctx::load_repo;
22 use crate::render::emoji::emojify;23 use crate::render::emoji::emojify;
@@ -275,7 +276,8 @@ async fn commits_page(cx: &Cx) -> Result<impl View> {
275 let filter_text = filter.unwrap_or_default();276 let filter_text = filter.unwrap_or_default();
276 let page_label = g::page_label(lang, page_no);277 let page_label = g::page_label(lang, page_no);
277 278
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);
279 Ok(view! {281 Ok(view! {
280 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),282 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
281 repo_head(head: header)283 repo_head(head: header)
@@ -345,6 +347,8 @@ pub(crate) mod test_support {
345 pub struct Fixture {347 pub struct Fixture {
346 pub router: Router,348 pub router: Router,
347 pub store: Store,349 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>,
348 /// Root commit: adds `a.txt` and `dir/b.txt`.352 /// Root commit: adds `a.txt` and `dir/b.txt`.
349 pub root: String,353 pub root: String,
350 /// Adds `new.txt` and changes `a.txt`.354 /// Adds `new.txt` and changes `a.txt`.
@@ -383,6 +387,7 @@ pub(crate) mod test_support {
383 let dir = tmp.path().to_path_buf();387 let dir = tmp.path().to_path_buf();
384 let state = crate::test_support::test_state(&dir).await;388 let state = crate::test_support::test_state(&dir).await;
385 let store = state.store.clone();389 let store = state.store.clone();
390+ let live = state.live.clone();
386 let router = crate::test_support::app_router(state);391 let router = crate::test_support::app_router(state);
387 392
388 let owner = store393 let owner = store
@@ -459,6 +464,7 @@ pub(crate) mod test_support {
459 Self {464 Self {
460 router,465 router,
461 store,466 store,
467+ live,
462 root,468 root,
463 modified,469 modified,
464 work,470 work,
crates/forge-web/src/pages/compare.rsmodified+5 -2
@@ -31,6 +31,7 @@ use crate::i18n::pulls::{self as g, K};
31 use crate::notify::{self, Activity};31 use crate::notify::{self, Activity};
32 use crate::pages::commit::Download;32 use crate::pages::commit::Download;
33 use crate::pages::commits::{CommitRow, blocking, now_secs, short_oid, url_path};33 use crate::pages::commits::{CommitRow, blocking, now_secs, short_oid, url_path};
34+use crate::pages::ref_live;
34 use crate::pages::repo::repo_head;35 use crate::pages::repo::repo_head;
35 use crate::pages::repo_ctx::{RepoCtx, load_repo};36 use crate::pages::repo_ctx::{RepoCtx, load_repo};
36 use crate::state::AppState;37 use crate::state::AppState;
@@ -324,7 +325,8 @@ async fn compare_view(
324 let heading_base = base.clone();325 let heading_base = base.clone();
325 let heading_head = head.clone();326 let heading_head = head.clone();
326 327
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);
328 let __cx = cx;330 let __cx = cx;
329 Ok(view! {331 Ok(view! {
330 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),332 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
410 .collect();412 .collect();
411 let title = format!("{}/{}", ctx.repo.owner_name, ctx.repo.name);413 let title = format!("{}/{}", ctx.repo.owner_name, ctx.repo.name);
412 let action = repo_href(&ctx.repo.owner_name, &ctx.repo.name, "compare", "");414 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);
414 let __cx = cx;417 let __cx = cx;
415 Ok(view! {418 Ok(view! {
416 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),419 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;
96 pub mod reactions;96 pub mod reactions;
97 #[cfg(test)]97 #[cfg(test)]
98 mod reactions_tests;98 mod reactions_tests;
99+pub mod ref_live;
99 pub mod refs;100 pub mod refs;
100 pub mod register;101 pub mod register;
101 pub mod releases;102 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};
13 13
14 use crate::i18n::code::{self as g, K};14 use crate::i18n::code::{self as g, K};
15 use crate::i18n::{self, Lang};15 use crate::i18n::{self, Lang};
16+use crate::pages::ref_live;
16 use crate::pages::repo::repo_head;17 use crate::pages::repo::repo_head;
17 use crate::pages::repo_ctx::{RepoCtx, load_repo};18 use crate::pages::repo_ctx::{RepoCtx, load_repo};
18 use crate::pages::tree::{LastCommit, last_commit_view};19 use crate::pages::tree::{LastCommit, last_commit_view};
@@ -178,7 +179,8 @@ async fn tags_page(cx: &Cx) -> Result<impl View> {
178 ctx.repo.name,179 ctx.repo.name,
179 g::t(lang, K::TagsTitle)180 g::t(lang, K::TagsTitle)
180 );181 );
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);
182 Ok(view! {184 Ok(view! {
183 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),185 shell(title: title.as_str(), viewer: ctx.viewer.as_ref(),
184 repo_head(head: header)186 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
257 | Dashboard `/` | `/dashboard/events` | the viewer's user topic (repos, pushes, repo changes) and inbox |257 | Dashboard `/` | `/dashboard/events` | the viewer's user topic (repos, pushes, repo changes) and inbox |
258 | Profile `/{username}` | `/users/{username}/events` | the owner's user topic (repos, stars, pins, pushes by the owner) |258 | Profile `/{username}` | `/users/{username}/events` | the owner's user topic (repos, stars, pins, pushes by the owner) |
259 | 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 |259 | 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 |
260 | 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 |261 | 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 |
261 | 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) |262 | 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) |
262 | Explore `/explore` | `/explore/events`, with the search, sort and page | the public listing |263 | Explore `/explore` | `/explore/events`, with the search, sort and page | the public listing |