From ed4cd43be12c260acb5f1e5e26ba993552f8fcf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Dec 2026 02:51:34 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Needs=20you=20can=20dismiss?= =?UTF-8?q?=20your=20failed=20runs:=20they=20leave=20the=20page=20and=20th?= =?UTF-8?q?e=20profile=20corner's=20dot=20until=20a=20run=20fails=20again;?= =?UTF-8?q?=20the=20corner=20and=20the=20page=20now=20count=20failed=20run?= =?UTF-8?q?s=20with=20one=20shared=20query;=20=F0=9F=90=9B=20fix:=20a=20Ne?= =?UTF-8?q?eds=20you=20section=20links=20its=20full=20list=20only=20when?= =?UTF-8?q?=20it=20has=20more=20rows=20than=20the=20overview=20shows,=20in?= =?UTF-8?q?stead=20of=20opening=20the=20same=20rows=20again?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0074_failed_runs_dismissed.sql | 3 + crates/forge-core/src/store/migrate.rs | 4 ++ crates/forge-core/src/store/needs.rs | 39 +++++++--- crates/forge-core/src/store/shell_activity.rs | 23 ++---- crates/forge-web-base/src/i18n/needs.rs | 4 ++ crates/forge-web/src/pages/needs_you.rs | 72 +++++++++++++++---- 6 files changed, 104 insertions(+), 41 deletions(-) create mode 100644 crates/forge-core/migrations/0074_failed_runs_dismissed.sql diff --git a/crates/forge-core/migrations/0074_failed_runs_dismissed.sql b/crates/forge-core/migrations/0074_failed_runs_dismissed.sql new file mode 100644 index 0000000..8ff5ea9 --- /dev/null +++ b/crates/forge-core/migrations/0074_failed_runs_dismissed.sql @@ -0,0 +1,3 @@ +-- When the user last dismissed their failed runs. Runs that finished before it are no longer listed +-- as needing them. Applied by store/migrate.rs. +ALTER TABLE users ADD COLUMN failed_runs_dismissed_at TEXT; diff --git a/crates/forge-core/src/store/migrate.rs b/crates/forge-core/src/store/migrate.rs index f124de9..ecacbbf 100644 --- a/crates/forge-core/src/store/migrate.rs +++ b/crates/forge-core/src/store/migrate.rs @@ -125,6 +125,10 @@ pub const MIGRATIONS: &[(u32, &str)] = &[ 73, include_str!("../../migrations/0073_sage_ink_default.sql"), ), + ( + 74, + include_str!("../../migrations/0074_failed_runs_dismissed.sql"), + ), ]; /// The highest migration version this binary knows about. diff --git a/crates/forge-core/src/store/needs.rs b/crates/forge-core/src/store/needs.rs index bf4afe2..f0b4197 100644 --- a/crates/forge-core/src/store/needs.rs +++ b/crates/forge-core/src/store/needs.rs @@ -9,13 +9,13 @@ use turso::Value; use super::notifications::{readable_params, readable_repo}; use super::shell_activity::WaitingItem; -use super::{Store, col_int, col_text, fetch_all}; +use super::{Store, col_int, col_text, db_err, fetch_all}; use crate::error::Result; use crate::notifications::Reason; -use crate::time::stamp_ago; +use crate::time::{now_stamp, stamp_ago}; -/// How far back a failing run counts. The same window as the profile corner's failing count. -const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60); +/// How far back a failing run counts, here and in the profile corner's failing count. +pub(super) const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60); /// A ref whose newest run by the user failed within `FAILING_WINDOW`. #[derive(Debug, Clone)] @@ -72,16 +72,17 @@ pub struct AssignedIssue { } /// The failing runs query without its page, newest first. -fn failing_runs_sql(readable: &str) -> String { +pub(super) fn failing_runs_sql(readable: &str) -> String { format!( "SELECT owner, name, number, git_ref, created_at FROM ( \ - SELECT ci_runs.state, ci_runs.created_at, ci_runs.number, ci_runs.git_ref, \ + SELECT ci_runs.state, ci_runs.created_at, ci_runs.finished_at, ci_runs.number, ci_runs.git_ref, \ o.username AS owner, r.name AS name, ROW_NUMBER() OVER ( \ PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \ FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \ JOIN users o ON o.id = r.owner_id \ WHERE ci_runs.actor_id = ? AND {readable} \ ) WHERE rn = 1 AND state IN ('failure', 'error') AND created_at >= ? \ + AND COALESCE(finished_at, created_at) > COALESCE((SELECT failed_runs_dismissed_at FROM users WHERE id = ?), '') \ ORDER BY created_at DESC, number DESC" ) } @@ -139,6 +140,14 @@ fn viewer_params(user_id: &str) -> Vec { params } +/// The parameters of `failing_runs_sql`, in order. +pub(super) fn failing_runs_params(user_id: &str) -> Vec { + let mut params = viewer_params(user_id); + params.push(Value::Text(stamp_ago(FAILING_WINDOW))); + params.push(Value::Text(user_id.to_string())); + params +} + /// `base` with its page: `LIMIT` and `OFFSET` are the last two placeholders. fn paged(base: &str) -> String { format!("{base} LIMIT ? OFFSET ?") @@ -169,8 +178,7 @@ impl Store { limit: i64, offset: i64, ) -> Result> { - let mut params = viewer_params(actor_id); - params.push(Value::Text(stamp_ago(FAILING_WINDOW))); + let mut params = failing_runs_params(actor_id); params.push(Value::Integer(limit)); params.push(Value::Integer(offset)); let conn = self.read_conn().await; @@ -191,10 +199,21 @@ impl Store { .await } + /// Hides the user's failed runs that have finished so far. A run that fails later shows again. + pub async fn dismiss_failed_runs(&self, user_id: &str) -> Result<()> { + let conn = self.inner.conn.lock().await; + conn.execute( + "UPDATE users SET failed_runs_dismissed_at = ?2 WHERE id = ?1", + (user_id.to_string(), now_stamp()), + ) + .await + .map_err(db_err)?; + Ok(()) + } + /// How many failing runs `failing_runs` lists in all. pub async fn failing_runs_total(&self, actor_id: &str) -> Result { - let mut params = viewer_params(actor_id); - params.push(Value::Text(stamp_ago(FAILING_WINDOW))); + let params = failing_runs_params(actor_id); self.count_rows(&failing_runs_sql(&readable_repo()), params) .await } diff --git a/crates/forge-core/src/store/shell_activity.rs b/crates/forge-core/src/store/shell_activity.rs index c92bba2..d052f8d 100644 --- a/crates/forge-core/src/store/shell_activity.rs +++ b/crates/forge-core/src/store/shell_activity.rs @@ -5,6 +5,7 @@ use std::time::Duration; use turso::{Row, Value}; +use super::needs::{failing_runs_params, failing_runs_sql}; use super::notifications::{readable_params, readable_repo}; use super::{Store, col_int, col_opt_text, col_text, fetch_all}; use crate::ci::RunState; @@ -16,9 +17,6 @@ use crate::time::stamp_ago; /// counts and list, bounded when runs pile up in the queue with no runner online. const MAX_ACTOR_RUNS: i64 = 100; -/// How far back a failing run still counts toward `ActorRuns::failing`. -const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60); - /// Unread inbox rows of the two kinds the corner shows. #[derive(Debug, Clone, Copy, Default)] pub struct InboxKinds { @@ -223,25 +221,14 @@ impl Store { ) .await?; - // Newest run per (repo, ref) among this actor's runs; the ones that failed are counted, and the - // newest of them is kept for its link. - let mut params = vec![Value::Text(actor_id.to_string())]; - params.extend(readable_params(actor_id)); - params.push(Value::Text(stamp_ago(FAILING_WINDOW))); + // The same failing runs `/needs-you` lists: counted, and the newest kept for its link. let failing = fetch_all( &conn, &format!( - "SELECT COUNT(*) OVER (), owner, name, number FROM ( \ - SELECT ci_runs.state, ci_runs.created_at, ci_runs.number, o.username AS owner, \ - r.name AS name, ROW_NUMBER() OVER ( \ - PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \ - FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \ - JOIN users o ON o.id = r.owner_id \ - WHERE ci_runs.actor_id = ? AND {readable} \ - ) WHERE rn = 1 AND state IN ('failure', 'error') AND created_at >= ? \ - ORDER BY created_at DESC LIMIT 1" + "SELECT COUNT(*) OVER (), owner, name, number FROM ({}) LIMIT 1", + failing_runs_sql(&readable) ), - params, + failing_runs_params(actor_id), |row| { Ok(( col_int(row, 0)?, diff --git a/crates/forge-web-base/src/i18n/needs.rs b/crates/forge-web-base/src/i18n/needs.rs index 3ad120d..c1e6b69 100644 --- a/crates/forge-web-base/src/i18n/needs.rs +++ b/crates/forge-web-base/src/i18n/needs.rs @@ -14,6 +14,7 @@ pub enum K { Approved, AllReviewRequests, AllFailedRuns, + DismissFailedRuns, AllYourPulls, AllAssignedIssues, AllMentions, @@ -34,6 +35,7 @@ pub const ALL: &[K] = &[ K::Approved, K::AllReviewRequests, K::AllFailedRuns, + K::DismissFailedRuns, K::AllYourPulls, K::AllAssignedIssues, K::AllMentions, @@ -65,6 +67,8 @@ pub fn t(lang: Lang, key: K) -> &'static str { (AllReviewRequests, Tr) => "Tüm inceleme istekleri", (AllFailedRuns, En) => "All failed runs", (AllFailedRuns, Tr) => "Tüm başarısız çalıştırmalar", + (DismissFailedRuns, En) => "Dismiss", + (DismissFailedRuns, Tr) => "Kapat", (AllYourPulls, En) => "All your pull requests", (AllYourPulls, Tr) => "Tüm pull request'leriniz", (AllAssignedIssues, En) => "All assigned issues", diff --git a/crates/forge-web/src/pages/needs_you.rs b/crates/forge-web/src/pages/needs_you.rs index c185fac..a2bd97f 100644 --- a/crates/forge-web/src/pages/needs_you.rs +++ b/crates/forge-web/src/pages/needs_you.rs @@ -8,20 +8,23 @@ use forge_core::User; use forge_core::authz::{Action, authorize}; use forge_core::store::needs::Verdict; +use serde::Deserialize; use topcoat::Result; use topcoat::context::{Cx, context}; +use topcoat::router::content::Form; +use topcoat::router::error::{SeeOther, see_other}; use topcoat::router::response::Response; use topcoat::router::{Body, StatusCode, page, query_params, route}; use topcoat::view::{View, component, view}; -use crate::auth::{current_user, require_user}; +use crate::auth::{current_user, require_csrf, require_user}; use crate::i18n::authoring::{self as ga}; use crate::i18n::needs::{self as ni, K}; use crate::i18n::{self, Lang}; use crate::live::{Topic, stream_wakes}; use crate::pages::ci_runs::run_href; use crate::state::AppState; -use crate::views::parts::head; +use crate::views::parts::{head, post_act}; use crate::views::repo_list::{PageLink, pager, pager_links}; use crate::views::{rel_time, repo_href, shell}; @@ -220,34 +223,45 @@ impl NeedPager { pub(crate) struct NeedSection { heading: &'static str, more: Option<(&'static str, &'static str)>, + /// The failed runs section can be dismissed. + dismiss: bool, rows: Vec, pager: NeedPager, } /// Adds an overview section for `rows`, unless there are none. -fn push_section(sections: &mut Vec, lang: Lang, section: Section, rows: Vec) { +fn push_section( + sections: &mut Vec, + lang: Lang, + section: Section, + mut rows: Vec, +) { if rows.is_empty() { return; } + let more = rows.len() > SECTION_ROWS; + rows.truncate(SECTION_ROWS); sections.push(NeedSection { heading: ni::t(lang, section.heading()), - more: Some((section.href(), ni::t(lang, section.all_key()))), + more: more.then(|| (section.href(), ni::t(lang, section.all_key()))), + dismiss: matches!(section, Section::Runs), rows, pager: NeedPager::single(), }); } -/// The sections that have rows, in page order, each capped at `SECTION_ROWS`. +/// The sections that have rows, in page order, each capped at `SECTION_ROWS`. One row more is read, +/// so a section links its full list only when it has rows the overview leaves out. async fn load_sections(state: &AppState, user: &User, lang: Lang) -> Result> { let mut sections = Vec::new(); - let limit = SECTION_ROWS as i64; + let limit = SECTION_ROWS as i64 + 1; let runs = Section::Runs.rows(state, user, lang, limit, 0).await?; push_section(&mut sections, lang, Section::Runs, runs); let mut reviews = Vec::new(); for item in state.store.review_requests_for_user(&user.id).await? { - if reviews.len() >= SECTION_ROWS { + if reviews.len() > SECTION_ROWS { break; } // 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 SECTION_ROWS; + reviews.truncate(SECTION_ROWS); sections.push(NeedSection { heading: ni::t(lang, K::ReviewsRequested), - more: Some(("/review-requests", ni::t(lang, K::AllReviewRequests))), + more: more.then(|| ("/review-requests", ni::t(lang, K::AllReviewRequests))), + dismiss: false, rows: reviews, pager: NeedPager::single(), }); @@ -331,6 +348,7 @@ async fn load_full( Ok(NeedSection { heading: ni::t(lang, section.heading()), more: None, + dismiss: matches!(section, Section::Runs), rows, pager: NeedPager { page, @@ -344,10 +362,16 @@ async fn load_full( /// One section of the page: its head, its rows as a compact list, its full-list link and its pager. #[component] -pub(crate) async fn need_section(cx: &Cx, lang: Lang, section: NeedSection) -> Result { +pub(crate) async fn need_section( + cx: &Cx, + lang: Lang, + csrf: String, + section: NeedSection, +) -> Result { let NeedSection { heading, more, + dismiss, rows, pager: NeedPager { @@ -371,9 +395,14 @@ pub(crate) async fn need_section(cx: &Cx, lang: Lang, section: NeedSection) -> R rel_time(lang: lang, stamp: row.stamp.as_str()) } - if let Some((href, text)) = more { + if more.is_some() || dismiss {
- (text) + if let Some((href, text)) = more { + (text) + } + if dismiss { + post_act(label: ni::t(lang, K::DismissFailedRuns), action: "/needs-you/runs/dismiss", csrf: csrf.as_str()) + }
} pager(page: page, pages: pages, links: links, prev: prev, next: next, lang: lang) @@ -410,6 +439,7 @@ async fn needs_you(cx: &Cx) -> Result { let back = full.as_ref().map(|_| ni::t(lang, K::BackToNeedsYou)); let heading = ni::t(lang, K::NeedsYou); let unread = state.store.unread_count(&user.id).await?; + let csrf = viewer.1.csrf_token.clone(); Ok(view! { shell(title: heading, viewer: Some(&viewer), head(title: heading, live: Some(live.as_str()), @@ -424,10 +454,10 @@ async fn needs_you(cx: &Cx) -> Result {

(ni::t(lang, K::AllClear))

} for section in sections.into_iter() { - need_section(lang: lang, section: section) + need_section(lang: lang, csrf: csrf.clone(), section: section) } if let Some(section) = full { - need_section(lang: lang, section: section) + need_section(lang: lang, csrf: csrf.clone(), section: section) } @@ -435,6 +465,22 @@ async fn needs_you(cx: &Cx) -> Result { }) } +#[derive(Deserialize)] +struct CsrfForm { + #[serde(default)] + csrf: String, +} + +/// Hides the viewer's failed runs so far, from this page and from the profile corner's dot. +#[route(POST "/needs-you/runs/dismiss")] +async fn dismiss_failed_runs(cx: &Cx, Form(form): Form) -> Result { + let (user, _) = require_csrf(cx, &form.csrf).await?; + let state = context::(cx); + state.store.dismiss_failed_runs(&user.id).await?; + state.live.bump(&Topic::Inbox(user.id)); + Ok(see_other("/needs-you")) +} + /// The page's stream: a wake on the viewer's review-request or inbox topic. Anonymous visitors get /// a bare 401. The query of a section view is ignored here, so the same stream serves both. #[route(GET "/needs-you/events")]