forge
Explore
Log in

Don't you dare go Hollow

Needs you can dismiss your failed runs: they leave the page and the profile corner's dot until a run fails again

Browse files at this commit

ed4cd43be12c260acb5f1e5e26ba993552f8fcf2Ahmet Kaan GümüşParent: 2ba0d23
  • feat
    • Needs you can dismiss your failed runs: they leave the page and the profile corner's dot until a run fails again
    • the corner and the page now count failed runs with one shared query
  • fix
    • a Needs you section links its full list only when it has more rows than the overview shows, instead of opening the same rows again
Show full message
✨ feat: Needs you can dismiss your failed runs: they leave the page and the profile corner's dot until a run fails again; the corner and the page now count failed runs with one shared query; 🐛 fix: a Needs you section links its full list only when it has more rows than the overview shows, instead of opening the same rows again
crates/forge-core/migrations/0074_failed_runs_dismissed.sqladded+3 -0
@@ -0,0 +1,3 @@
1+-- When the user last dismissed their failed runs. Runs that finished before it are no longer listed
2+-- as needing them. Applied by store/migrate.rs.
3+ALTER TABLE users ADD COLUMN failed_runs_dismissed_at TEXT;
crates/forge-core/src/store/migrate.rsmodified+4 -0
@@ -125,6 +125,10 @@ pub const MIGRATIONS: &[(u32, &str)] = &[
125 73,125 73,
126 include_str!("../../migrations/0073_sage_ink_default.sql"),126 include_str!("../../migrations/0073_sage_ink_default.sql"),
127 ),127 ),
128+ (
129+ 74,
130+ include_str!("../../migrations/0074_failed_runs_dismissed.sql"),
131+ ),
128 ];132 ];
129 133
130 /// The highest migration version this binary knows about.134 /// The highest migration version this binary knows about.
crates/forge-core/src/store/needs.rsmodified+29 -10
@@ -9,13 +9,13 @@ use turso::Value;
9 9
10 use super::notifications::{readable_params, readable_repo};10 use super::notifications::{readable_params, readable_repo};
11 use super::shell_activity::WaitingItem;11 use super::shell_activity::WaitingItem;
12-use super::{Store, col_int, col_text, fetch_all};12+use super::{Store, col_int, col_text, db_err, fetch_all};
13 use crate::error::Result;13 use crate::error::Result;
14 use crate::notifications::Reason;14 use crate::notifications::Reason;
15-use crate::time::stamp_ago;15+use crate::time::{now_stamp, stamp_ago};
16 16
17-/// How far back a failing run counts. The same window as the profile corner's failing count.17+/// How far back a failing run counts, here and in the profile corner's failing count.
18-const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60);18+pub(super) const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60);
19 19
20 /// A ref whose newest run by the user failed within `FAILING_WINDOW`.20 /// A ref whose newest run by the user failed within `FAILING_WINDOW`.
21 #[derive(Debug, Clone)]21 #[derive(Debug, Clone)]
@@ -72,16 +72,17 @@ pub struct AssignedIssue {
72 }72 }
73 73
74 /// The failing runs query without its page, newest first.74 /// The failing runs query without its page, newest first.
75-fn failing_runs_sql(readable: &str) -> String {75+pub(super) fn failing_runs_sql(readable: &str) -> String {
76 format!(76 format!(
77 "SELECT owner, name, number, git_ref, created_at FROM ( \77 "SELECT owner, name, number, git_ref, created_at FROM ( \
78- SELECT ci_runs.state, ci_runs.created_at, ci_runs.number, ci_runs.git_ref, \78+ SELECT ci_runs.state, ci_runs.created_at, ci_runs.finished_at, ci_runs.number, ci_runs.git_ref, \
79 o.username AS owner, r.name AS name, ROW_NUMBER() OVER ( \79 o.username AS owner, r.name AS name, ROW_NUMBER() OVER ( \
80 PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \80 PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \
81 FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \81 FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \
82 JOIN users o ON o.id = r.owner_id \82 JOIN users o ON o.id = r.owner_id \
83 WHERE ci_runs.actor_id = ? AND {readable} \83 WHERE ci_runs.actor_id = ? AND {readable} \
84 ) WHERE rn = 1 AND state IN ('failure', 'error') AND created_at >= ? \84 ) WHERE rn = 1 AND state IN ('failure', 'error') AND created_at >= ? \
85+ AND COALESCE(finished_at, created_at) > COALESCE((SELECT failed_runs_dismissed_at FROM users WHERE id = ?), '') \
85 ORDER BY created_at DESC, number DESC"86 ORDER BY created_at DESC, number DESC"
86 )87 )
87 }88 }
@@ -139,6 +140,14 @@ fn viewer_params(user_id: &str) -> Vec<Value> {
139 params140 params
140 }141 }
141 142
143+/// The parameters of `failing_runs_sql`, in order.
144+pub(super) fn failing_runs_params(user_id: &str) -> Vec<Value> {
145+ let mut params = viewer_params(user_id);
146+ params.push(Value::Text(stamp_ago(FAILING_WINDOW)));
147+ params.push(Value::Text(user_id.to_string()));
148+ params
149+}
150+
142 /// `base` with its page: `LIMIT` and `OFFSET` are the last two placeholders.151 /// `base` with its page: `LIMIT` and `OFFSET` are the last two placeholders.
143 fn paged(base: &str) -> String {152 fn paged(base: &str) -> String {
144 format!("{base} LIMIT ? OFFSET ?")153 format!("{base} LIMIT ? OFFSET ?")
@@ -169,8 +178,7 @@ impl Store {
169 limit: i64,178 limit: i64,
170 offset: i64,179 offset: i64,
171 ) -> Result<Vec<FailingRun>> {180 ) -> Result<Vec<FailingRun>> {
172- let mut params = viewer_params(actor_id);181+ let mut params = failing_runs_params(actor_id);
173- params.push(Value::Text(stamp_ago(FAILING_WINDOW)));
174 params.push(Value::Integer(limit));182 params.push(Value::Integer(limit));
175 params.push(Value::Integer(offset));183 params.push(Value::Integer(offset));
176 let conn = self.read_conn().await;184 let conn = self.read_conn().await;
@@ -191,10 +199,21 @@ impl Store {
191 .await199 .await
192 }200 }
193 201
202+ /// Hides the user's failed runs that have finished so far. A run that fails later shows again.
203+ pub async fn dismiss_failed_runs(&self, user_id: &str) -> Result<()> {
204+ let conn = self.inner.conn.lock().await;
205+ conn.execute(
206+ "UPDATE users SET failed_runs_dismissed_at = ?2 WHERE id = ?1",
207+ (user_id.to_string(), now_stamp()),
208+ )
209+ .await
210+ .map_err(db_err)?;
211+ Ok(())
212+ }
213+
194 /// How many failing runs `failing_runs` lists in all.214 /// How many failing runs `failing_runs` lists in all.
195 pub async fn failing_runs_total(&self, actor_id: &str) -> Result<u64> {215 pub async fn failing_runs_total(&self, actor_id: &str) -> Result<u64> {
196- let mut params = viewer_params(actor_id);216+ let params = failing_runs_params(actor_id);
197- params.push(Value::Text(stamp_ago(FAILING_WINDOW)));
198 self.count_rows(&failing_runs_sql(&readable_repo()), params)217 self.count_rows(&failing_runs_sql(&readable_repo()), params)
199 .await218 .await
200 }219 }
crates/forge-core/src/store/shell_activity.rsmodified+5 -18
@@ -5,6 +5,7 @@ use std::time::Duration;
5 5
6 use turso::{Row, Value};6 use turso::{Row, Value};
7 7
8+use super::needs::{failing_runs_params, failing_runs_sql};
8 use super::notifications::{readable_params, readable_repo};9 use super::notifications::{readable_params, readable_repo};
9 use super::{Store, col_int, col_opt_text, col_text, fetch_all};10 use super::{Store, col_int, col_opt_text, col_text, fetch_all};
10 use crate::ci::RunState;11 use crate::ci::RunState;
@@ -16,9 +17,6 @@ use crate::time::stamp_ago;
16 /// counts and list, bounded when runs pile up in the queue with no runner online.17 /// counts and list, bounded when runs pile up in the queue with no runner online.
17 const MAX_ACTOR_RUNS: i64 = 100;18 const MAX_ACTOR_RUNS: i64 = 100;
18 19
19-/// How far back a failing run still counts toward `ActorRuns::failing`.
20-const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60);
21-
22 /// Unread inbox rows of the two kinds the corner shows.20 /// Unread inbox rows of the two kinds the corner shows.
23 #[derive(Debug, Clone, Copy, Default)]21 #[derive(Debug, Clone, Copy, Default)]
24 pub struct InboxKinds {22 pub struct InboxKinds {
@@ -223,25 +221,14 @@ impl Store {
223 )221 )
224 .await?;222 .await?;
225 223
226- // Newest run per (repo, ref) among this actor's runs; the ones that failed are counted, and the224+ // The same failing runs `/needs-you` lists: counted, and the newest kept for its link.
227- // newest of them is kept for its link.
228- let mut params = vec![Value::Text(actor_id.to_string())];
229- params.extend(readable_params(actor_id));
230- params.push(Value::Text(stamp_ago(FAILING_WINDOW)));
231 let failing = fetch_all(225 let failing = fetch_all(
232 &conn,226 &conn,
233 &format!(227 &format!(
234- "SELECT COUNT(*) OVER (), owner, name, number FROM ( \228+ "SELECT COUNT(*) OVER (), owner, name, number FROM ({}) LIMIT 1",
235- SELECT ci_runs.state, ci_runs.created_at, ci_runs.number, o.username AS owner, \229+ failing_runs_sql(&readable)
236- r.name AS name, ROW_NUMBER() OVER ( \
237- PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \
238- FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \
239- JOIN users o ON o.id = r.owner_id \
240- WHERE ci_runs.actor_id = ? AND {readable} \
241- ) WHERE rn = 1 AND state IN ('failure', 'error') AND created_at >= ? \
242- ORDER BY created_at DESC LIMIT 1"
243 ),230 ),
244- params,231+ failing_runs_params(actor_id),
245 |row| {232 |row| {
246 Ok((233 Ok((
247 col_int(row, 0)?,234 col_int(row, 0)?,
crates/forge-web-base/src/i18n/needs.rsmodified+4 -0
@@ -14,6 +14,7 @@ pub enum K {
14 Approved,14 Approved,
15 AllReviewRequests,15 AllReviewRequests,
16 AllFailedRuns,16 AllFailedRuns,
17+ DismissFailedRuns,
17 AllYourPulls,18 AllYourPulls,
18 AllAssignedIssues,19 AllAssignedIssues,
19 AllMentions,20 AllMentions,
@@ -34,6 +35,7 @@ pub const ALL: &[K] = &[
34 K::Approved,35 K::Approved,
35 K::AllReviewRequests,36 K::AllReviewRequests,
36 K::AllFailedRuns,37 K::AllFailedRuns,
38+ K::DismissFailedRuns,
37 K::AllYourPulls,39 K::AllYourPulls,
38 K::AllAssignedIssues,40 K::AllAssignedIssues,
39 K::AllMentions,41 K::AllMentions,
@@ -65,6 +67,8 @@ pub fn t(lang: Lang, key: K) -> &'static str {
65 (AllReviewRequests, Tr) => "Tüm inceleme istekleri",67 (AllReviewRequests, Tr) => "Tüm inceleme istekleri",
66 (AllFailedRuns, En) => "All failed runs",68 (AllFailedRuns, En) => "All failed runs",
67 (AllFailedRuns, Tr) => "Tüm başarısız çalıştırmalar",69 (AllFailedRuns, Tr) => "Tüm başarısız çalıştırmalar",
70+ (DismissFailedRuns, En) => "Dismiss",
71+ (DismissFailedRuns, Tr) => "Kapat",
68 (AllYourPulls, En) => "All your pull requests",72 (AllYourPulls, En) => "All your pull requests",
69 (AllYourPulls, Tr) => "Tüm pull request'leriniz",73 (AllYourPulls, Tr) => "Tüm pull request'leriniz",
70 (AllAssignedIssues, En) => "All assigned issues",74 (AllAssignedIssues, En) => "All assigned issues",
crates/forge-web/src/pages/needs_you.rsmodified+59 -13
@@ -8,20 +8,23 @@
8 use forge_core::User;8 use forge_core::User;
9 use forge_core::authz::{Action, authorize};9 use forge_core::authz::{Action, authorize};
10 use forge_core::store::needs::Verdict;10 use forge_core::store::needs::Verdict;
11+use serde::Deserialize;
11 use topcoat::Result;12 use topcoat::Result;
12 use topcoat::context::{Cx, context};13 use topcoat::context::{Cx, context};
14+use topcoat::router::content::Form;
15+use topcoat::router::error::{SeeOther, see_other};
13 use topcoat::router::response::Response;16 use topcoat::router::response::Response;
14 use topcoat::router::{Body, StatusCode, page, query_params, route};17 use topcoat::router::{Body, StatusCode, page, query_params, route};
15 use topcoat::view::{View, component, view};18 use topcoat::view::{View, component, view};
16 19
17-use crate::auth::{current_user, require_user};20+use crate::auth::{current_user, require_csrf, require_user};
18 use crate::i18n::authoring::{self as ga};21 use crate::i18n::authoring::{self as ga};
19 use crate::i18n::needs::{self as ni, K};22 use crate::i18n::needs::{self as ni, K};
20 use crate::i18n::{self, Lang};23 use crate::i18n::{self, Lang};
21 use crate::live::{Topic, stream_wakes};24 use crate::live::{Topic, stream_wakes};
22 use crate::pages::ci_runs::run_href;25 use crate::pages::ci_runs::run_href;
23 use crate::state::AppState;26 use crate::state::AppState;
24-use crate::views::parts::head;27+use crate::views::parts::{head, post_act};
25 use crate::views::repo_list::{PageLink, pager, pager_links};28 use crate::views::repo_list::{PageLink, pager, pager_links};
26 use crate::views::{rel_time, repo_href, shell};29 use crate::views::{rel_time, repo_href, shell};
27 30
@@ -220,34 +223,45 @@ impl NeedPager {
220 pub(crate) struct NeedSection {223 pub(crate) struct NeedSection {
221 heading: &'static str,224 heading: &'static str,
222 more: Option<(&'static str, &'static str)>,225 more: Option<(&'static str, &'static str)>,
226+ /// The failed runs section can be dismissed.
227+ dismiss: bool,
223 rows: Vec<NeedRow>,228 rows: Vec<NeedRow>,
224 pager: NeedPager,229 pager: NeedPager,
225 }230 }
226 231
227 /// Adds an overview section for `rows`, unless there are none.232 /// Adds an overview section for `rows`, unless there are none.
228-fn push_section(sections: &mut Vec<NeedSection>, lang: Lang, section: Section, rows: Vec<NeedRow>) {233+fn push_section(
234+ sections: &mut Vec<NeedSection>,
235+ lang: Lang,
236+ section: Section,
237+ mut rows: Vec<NeedRow>,
238+) {
229 if rows.is_empty() {239 if rows.is_empty() {
230 return;240 return;
231 }241 }
242+ let more = rows.len() > SECTION_ROWS;
243+ rows.truncate(SECTION_ROWS);
232 sections.push(NeedSection {244 sections.push(NeedSection {
233 heading: ni::t(lang, section.heading()),245 heading: ni::t(lang, section.heading()),
234- more: Some((section.href(), ni::t(lang, section.all_key()))),246+ more: more.then(|| (section.href(), ni::t(lang, section.all_key()))),
247+ dismiss: matches!(section, Section::Runs),
235 rows,248 rows,
236 pager: NeedPager::single(),249 pager: NeedPager::single(),
237 });250 });
238 }251 }
239 252
240-/// The sections that have rows, in page order, each capped at `SECTION_ROWS`.253+/// The sections that have rows, in page order, each capped at `SECTION_ROWS`. One row more is read,
254+/// so a section links its full list only when it has rows the overview leaves out.
241 async fn load_sections(state: &AppState, user: &User, lang: Lang) -> Result<Vec<NeedSection>> {255 async fn load_sections(state: &AppState, user: &User, lang: Lang) -> Result<Vec<NeedSection>> {
242 let mut sections = Vec::new();256 let mut sections = Vec::new();
243- let limit = SECTION_ROWS as i64;257+ let limit = SECTION_ROWS as i64 + 1;
244 258
245 let runs = Section::Runs.rows(state, user, lang, limit, 0).await?;259 let runs = Section::Runs.rows(state, user, lang, limit, 0).await?;
246 push_section(&mut sections, lang, Section::Runs, runs);260 push_section(&mut sections, lang, Section::Runs, runs);
247 261
248 let mut reviews = Vec::new();262 let mut reviews = Vec::new();
249 for item in state.store.review_requests_for_user(&user.id).await? {263 for item in state.store.review_requests_for_user(&user.id).await? {
250- if reviews.len() >= SECTION_ROWS {264+ if reviews.len() > SECTION_ROWS {
251 break;265 break;
252 }266 }
253 // A repo the viewer can no longer read is not listed.267 // A repo the viewer can no longer read is not listed.
@@ -286,9 +300,12 @@ async fn load_sections(state: &AppState, user: &User, lang: Lang) -> Result<Vec<
286 });300 });
287 }301 }
288 if !reviews.is_empty() {302 if !reviews.is_empty() {
303+ let more = reviews.len() > SECTION_ROWS;
304+ reviews.truncate(SECTION_ROWS);
289 sections.push(NeedSection {305 sections.push(NeedSection {
290 heading: ni::t(lang, K::ReviewsRequested),306 heading: ni::t(lang, K::ReviewsRequested),
291- more: Some(("/review-requests", ni::t(lang, K::AllReviewRequests))),307+ more: more.then(|| ("/review-requests", ni::t(lang, K::AllReviewRequests))),
308+ dismiss: false,
292 rows: reviews,309 rows: reviews,
293 pager: NeedPager::single(),310 pager: NeedPager::single(),
294 });311 });
@@ -331,6 +348,7 @@ async fn load_full(
331 Ok(NeedSection {348 Ok(NeedSection {
332 heading: ni::t(lang, section.heading()),349 heading: ni::t(lang, section.heading()),
333 more: None,350 more: None,
351+ dismiss: matches!(section, Section::Runs),
334 rows,352 rows,
335 pager: NeedPager {353 pager: NeedPager {
336 page,354 page,
@@ -344,10 +362,16 @@ async fn load_full(
344 362
345 /// One section of the page: its head, its rows as a compact list, its full-list link and its pager.363 /// One section of the page: its head, its rows as a compact list, its full-list link and its pager.
346 #[component]364 #[component]
347-pub(crate) async fn need_section(cx: &Cx, lang: Lang, section: NeedSection) -> Result<impl View> {365+pub(crate) async fn need_section(
366+ cx: &Cx,
367+ lang: Lang,
368+ csrf: String,
369+ section: NeedSection,
370+) -> Result<impl View> {
348 let NeedSection {371 let NeedSection {
349 heading,372 heading,
350 more,373 more,
374+ dismiss,
351 rows,375 rows,
352 pager:376 pager:
353 NeedPager {377 NeedPager {
@@ -371,9 +395,14 @@ pub(crate) async fn need_section(cx: &Cx, lang: Lang, section: NeedSection) -> R
371 <span class="val">rel_time(lang: lang, stamp: row.stamp.as_str())</span>395 <span class="val">rel_time(lang: lang, stamp: row.stamp.as_str())</span>
372 </a>396 </a>
373 }397 }
374- if let Some((href, text)) = more {398+ if more.is_some() || dismiss {
375 <div class="acts">399 <div class="acts">
376- <a class="act" href=(href)>(text)</a>400+ if let Some((href, text)) = more {
401+ <a class="act" href=(href)>(text)</a>
402+ }
403+ if dismiss {
404+ post_act(label: ni::t(lang, K::DismissFailedRuns), action: "/needs-you/runs/dismiss", csrf: csrf.as_str())
405+ }
377 </div>406 </div>
378 }407 }
379 pager(page: page, pages: pages, links: links, prev: prev, next: next, lang: lang)408 pager(page: page, pages: pages, links: links, prev: prev, next: next, lang: lang)
@@ -410,6 +439,7 @@ async fn needs_you(cx: &Cx) -> Result<impl View> {
410 let back = full.as_ref().map(|_| ni::t(lang, K::BackToNeedsYou));439 let back = full.as_ref().map(|_| ni::t(lang, K::BackToNeedsYou));
411 let heading = ni::t(lang, K::NeedsYou);440 let heading = ni::t(lang, K::NeedsYou);
412 let unread = state.store.unread_count(&user.id).await?;441 let unread = state.store.unread_count(&user.id).await?;
442+ let csrf = viewer.1.csrf_token.clone();
413 Ok(view! {443 Ok(view! {
414 shell(title: heading, viewer: Some(&viewer),444 shell(title: heading, viewer: Some(&viewer),
415 head(title: heading, live: Some(live.as_str()),445 head(title: heading, live: Some(live.as_str()),
@@ -424,10 +454,10 @@ async fn needs_you(cx: &Cx) -> Result<impl View> {
424 <p class="state">(ni::t(lang, K::AllClear))</p>454 <p class="state">(ni::t(lang, K::AllClear))</p>
425 }455 }
426 for section in sections.into_iter() {456 for section in sections.into_iter() {
427- need_section(lang: lang, section: section)457+ need_section(lang: lang, csrf: csrf.clone(), section: section)
428 }458 }
429 if let Some(section) = full {459 if let Some(section) = full {
430- need_section(lang: lang, section: section)460+ need_section(lang: lang, csrf: csrf.clone(), section: section)
431 }461 }
432 </div>462 </div>
433 </div>463 </div>
@@ -435,6 +465,22 @@ async fn needs_you(cx: &Cx) -> Result<impl View> {
435 })465 })
436 }466 }
437 467
468+#[derive(Deserialize)]
469+struct CsrfForm {
470+ #[serde(default)]
471+ csrf: String,
472+}
473+
474+/// Hides the viewer's failed runs so far, from this page and from the profile corner's dot.
475+#[route(POST "/needs-you/runs/dismiss")]
476+async fn dismiss_failed_runs(cx: &Cx, Form(form): Form<CsrfForm>) -> Result<SeeOther> {
477+ let (user, _) = require_csrf(cx, &form.csrf).await?;
478+ let state = context::<AppState>(cx);
479+ state.store.dismiss_failed_runs(&user.id).await?;
480+ state.live.bump(&Topic::Inbox(user.id));
481+ Ok(see_other("/needs-you"))
482+}
483+
438 /// The page's stream: a wake on the viewer's review-request or inbox topic. Anonymous visitors get484 /// The page's stream: a wake on the viewer's review-request or inbox topic. Anonymous visitors get
439 /// a bare 401. The query of a section view is ignored here, so the same stream serves both.485 /// a bare 401. The query of a section view is ignored here, so the same stream serves both.
440 #[route(GET "/needs-you/events")]486 #[route(GET "/needs-you/events")]