forge
Explore
Log in

Don't you dare go Hollow

The registration token page moves to the runner list once its token has registered a runner, instead of showing a spent token until reloaded

Browse files at this commit

b6feaae7a2f7ec84cc6b82f91d73dea9fe08fb3dAhmet Kaan GümüşParent: f4a7add
Some checks failed
Show checks
  • ✗ci/buildworkspace: workspace io: No such file or directory (os error 2)Details
  • feat
    • the registration token page moves to the runner list once its token has registered a runner, instead of showing a spent token until reloaded
Show full message
✨ feat: the registration token page moves to the runner list once its token has registered a runner, instead of showing a spent token until reloaded
crates/forge-core/src/store/ci.rsmodified+13 -0
@@ -452,6 +452,19 @@ impl Store {
452452 Ok(token)
453453 }
454454
455+ /// Whether the registration token with this hash can no longer register a runner: used or gone.
456+ pub async fn registration_token_used(&self, token_hash: &str) -> Result<bool> {
457+ let conn = self.inner.conn.lock().await;
458+ let mut rows = conn
459+ .query(
460+ "SELECT 1 FROM ci_registration_tokens WHERE token_hash = ?1 AND used_at IS NULL",
461+ vec![text(token_hash)],
462+ )
463+ .await
464+ .map_err(db_err)?;
465+ Ok(rows.next().await.map_err(db_err)?.is_none())
466+ }
467+
455468 /// Consumes a registration token and creates a runner. Returns the runner and its secret,
456469 /// which is shown once. `Invalid` when the token is unknown, used or expired.
457470 pub async fn register_runner(
crates/forge-web/assets/nav.jsmodified+4 -0
@@ -2777,6 +2777,10 @@
27772777 liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000);
27782778 });
27792779 es.addEventListener('log', function (e) { appendLog(e.data, e.lastEventId); });
2780+ es.addEventListener('go', function (e) {
2781+ var url = new URL(e.data, location.href);
2782+ load({ target: url, dest: url, init: GET_INIT, how: 'nav', keep: false, post: false, form: null });
2783+ });
27802784 return es;
27812785 }
27822786
crates/forge-web/src/pages/admin_runners.rsmodified+65 -0
@@ -41,6 +41,7 @@ use crate::views::repo_list::{PageLink, pager, pager_links};
4141 use crate::views::{alert, enc, now_unix, relative_time, shell};
4242
4343 path_param!(runner_id);
44+path_param!(token_hash);
4445
4546 /// Runners per page on the list.
4647 const PER_PAGE: usize = 6;
@@ -486,8 +487,13 @@ async fn load_meter(cx: &Cx, lang: Lang, running: u32, max: u32) -> Result<impl
486487
487488 #[component]
488489 async fn token_view(cx: &Cx, lang: Lang, token: String, command: String) -> Result<impl View> {
490+ let live = format!(
491+ "/admin/runners/tokens/{}/events",
492+ forge_core::token::hash_token(&token)
493+ );
489494 Ok(view! {
490495 head(title: g::t(lang, K::RegistrationToken))
496+ <div hidden="" data-live=(live.as_str())></div>
491497 <p class="state" role="status"><strong>(g::t(lang, K::CopyTokenNow))</strong></p>
492498 <div class="sheet">
493499 <div class="sheet-tools"><strong>(g::t(lang, K::Token))</strong> <a class="act" href="/admin/runners">(g::t(lang, K::BackToRunners))</a></div>
@@ -1770,6 +1776,38 @@ async fn runners_events(cx: &Cx) -> Result<Response> {
17701776 events_response(cx, events)
17711777 }
17721778
1779+/// The token page's stream: one `go` event to the runner list once its token has registered a
1780+/// runner, so the page does not keep showing a spent token.
1781+#[route(GET "/admin/runners/tokens/{token_hash}/events")]
1782+async fn token_events(cx: &Cx) -> Result<Response> {
1783+ admin_only(cx).await?;
1784+ let state = context::<AppState>(cx).clone();
1785+ let hash = token_hash(cx).to_owned();
1786+ let wake = state.ci_live.subscribe_runners();
1787+ let events = futures_util::stream::unfold(Some(wake), move |wake| {
1788+ let state = state.clone();
1789+ let hash = hash.clone();
1790+ async move {
1791+ let mut wake = wake?;
1792+ loop {
1793+ if state
1794+ .store
1795+ .registration_token_used(&hash)
1796+ .await
1797+ .unwrap_or(false)
1798+ {
1799+ let go = topcoat::router::content::sse::Event::new()
1800+ .event("go")
1801+ .data("/admin/runners");
1802+ return Some((Ok(go), None));
1803+ }
1804+ wake.changed().await.ok()?;
1805+ }
1806+ }
1807+ });
1808+ events_response(cx, events)
1809+}
1810+
17731811 /// Sets or resets one config field of a runner. Concurrency and labels are live; the rest are
17741812 /// stored for the runner's next start. Read-only fields and bad values change nothing.
17751813 #[route(POST "/admin/runners/{runner_id}/config")]
@@ -1988,6 +2026,33 @@ mod tests {
19882026 store.list_runners().await.expect("list").remove(0)
19892027 }
19902028
2029+ #[tokio::test]
2030+ async fn a_used_token_sends_its_page_to_the_runner_list() {
2031+ let dir = tempfile::tempdir().expect("tempdir");
2032+ let (store, router) = fixture(dir.path()).await;
2033+ let (_, created) = send(
2034+ &router,
2035+ "POST",
2036+ "/admin/runners/tokens",
2037+ Some(ADMIN_TOKEN),
2038+ csrf_form(""),
2039+ )
2040+ .await;
2041+ let token = token_from(&created);
2042+ let live = format!(
2043+ "/admin/runners/tokens/{}/events",
2044+ forge_core::token::hash_token(&token)
2045+ );
2046+ assert!(created.contains(&live));
2047+ store
2048+ .register_runner(&token, "box", &[])
2049+ .await
2050+ .expect("register");
2051+ let (status, body) = send(&router, "GET", &live, Some(ADMIN_TOKEN), None).await;
2052+ assert_eq!(status, 200);
2053+ assert!(body.contains("event: go\ndata: /admin/runners"), "{body}");
2054+ }
2055+
19912056 // Scenario: the page gates, the one-time token, and labels as chips with a state badge.
19922057 #[tokio::test]
19932058 async fn gates_token_once_and_labels_render_as_chips() {