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)] = &[
125125 73,
126126 include_str!("../../migrations/0073_sage_ink_default.sql"),
127127 ),
128+ (
129+ 74,
130+ include_str!("../../migrations/0074_failed_runs_dismissed.sql"),
131+ ),
128132 ];
129133
130134 /// The highest migration version this binary knows about.
crates/forge-core/src/store/needs.rsmodified+29 -10
@@ -9,13 +9,13 @@ use turso::Value;
99
1010 use super::notifications::{readable_params, readable_repo};
1111 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};
1313 use crate::error::Result;
1414 use crate::notifications::Reason;
15-use crate::time::stamp_ago;
15+use crate::time::{now_stamp, stamp_ago};
1616
17-/// How far back a failing run counts. The same window as the profile corner's failing count.
18-const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60);
17+/// How far back a failing run counts, here and in the profile corner's failing count.
18+pub(super) const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60);
1919
2020 /// A ref whose newest run by the user failed within `FAILING_WINDOW`.
2121 #[derive(Debug, Clone)]
@@ -72,16 +72,17 @@ pub struct AssignedIssue {
7272 }
7373
7474 /// 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 {
7676 format!(
7777 "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, \
7979 o.username AS owner, r.name AS name, ROW_NUMBER() OVER ( \
8080 PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \
8181 FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \
8282 JOIN users o ON o.id = r.owner_id \
8383 WHERE ci_runs.actor_id = ? AND {readable} \
8484 ) 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 = ?), '') \
8586 ORDER BY created_at DESC, number DESC"
8687 )
8788 }
@@ -139,6 +140,14 @@ fn viewer_params(user_id: &str) -> Vec<Value> {
139140 params
140141 }
141142
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+
142151 /// `base` with its page: `LIMIT` and `OFFSET` are the last two placeholders.
143152 fn paged(base: &str) -> String {
144153 format!("{base} LIMIT ? OFFSET ?")
@@ -169,8 +178,7 @@ impl Store {
169178 limit: i64,
170179 offset: i64,
171180 ) -> Result<Vec<FailingRun>> {
172- let mut params = viewer_params(actor_id);
173- params.push(Value::Text(stamp_ago(FAILING_WINDOW)));
181+ let mut params = failing_runs_params(actor_id);
174182 params.push(Value::Integer(limit));
175183 params.push(Value::Integer(offset));
176184 let conn = self.read_conn().await;
@@ -191,10 +199,21 @@ impl Store {
191199 .await
192200 }
193201
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+
194214 /// How many failing runs `failing_runs` lists in all.
195215 pub async fn failing_runs_total(&self, actor_id: &str) -> Result<u64> {
196- let mut params = viewer_params(actor_id);
197- params.push(Value::Text(stamp_ago(FAILING_WINDOW)));
216+ let params = failing_runs_params(actor_id);
198217 self.count_rows(&failing_runs_sql(&readable_repo()), params)
199218 .await
200219 }
crates/forge-core/src/store/shell_activity.rsmodified+5 -18
@@ -5,6 +5,7 @@ use std::time::Duration;
55
66 use turso::{Row, Value};
77
8+use super::needs::{failing_runs_params, failing_runs_sql};
89 use super::notifications::{readable_params, readable_repo};
910 use super::{Store, col_int, col_opt_text, col_text, fetch_all};
1011 use crate::ci::RunState;
@@ -16,9 +17,6 @@ use crate::time::stamp_ago;
1617 /// counts and list, bounded when runs pile up in the queue with no runner online.
1718 const MAX_ACTOR_RUNS: i64 = 100;
1819
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-
2220 /// Unread inbox rows of the two kinds the corner shows.
2321 #[derive(Debug, Clone, Copy, Default)]
2422 pub struct InboxKinds {
@@ -223,25 +221,14 @@ impl Store {
223221 )
224222 .await?;
225223
226- // Newest run per (repo, ref) among this actor's runs; the ones that failed are counted, and the
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)));
224+ // The same failing runs `/needs-you` lists: counted, and the newest kept for its link.
231225 let failing = fetch_all(
232226 &conn,
233227 &format!(
234- "SELECT COUNT(*) OVER (), owner, name, number FROM ( \
235- SELECT ci_runs.state, ci_runs.created_at, ci_runs.number, o.username AS owner, \
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"
228+ "SELECT COUNT(*) OVER (), owner, name, number FROM ({}) LIMIT 1",
229+ failing_runs_sql(&readable)
243230 ),
244- params,
231+ failing_runs_params(actor_id),
245232 |row| {
246233 Ok((
247234 col_int(row, 0)?,
crates/forge-web-base/src/i18n/needs.rsmodified+4 -0
@@ -14,6 +14,7 @@ pub enum K {
1414 Approved,
1515 AllReviewRequests,
1616 AllFailedRuns,
17+ DismissFailedRuns,
1718 AllYourPulls,
1819 AllAssignedIssues,
1920 AllMentions,
@@ -34,6 +35,7 @@ pub const ALL: &[K] = &[
3435 K::Approved,
3536 K::AllReviewRequests,
3637 K::AllFailedRuns,
38+ K::DismissFailedRuns,
3739 K::AllYourPulls,
3840 K::AllAssignedIssues,
3941 K::AllMentions,
@@ -65,6 +67,8 @@ pub fn t(lang: Lang, key: K) -> &'static str {
6567 (AllReviewRequests, Tr) => "Tüm inceleme istekleri",
6668 (AllFailedRuns, En) => "All failed runs",
6769 (AllFailedRuns, Tr) => "Tüm başarısız çalıştırmalar",
70+ (DismissFailedRuns, En) => "Dismiss",
71+ (DismissFailedRuns, Tr) => "Kapat",
6872 (AllYourPulls, En) => "All your pull requests",
6973 (AllYourPulls, Tr) => "Tüm pull request'leriniz",
7074 (AllAssignedIssues, En) => "All assigned issues",
crates/forge-web/src/pages/needs_you.rsmodified+59 -13
@@ -8,20 +8,23 @@
88 use forge_core::User;
99 use forge_core::authz::{Action, authorize};
1010 use forge_core::store::needs::Verdict;
11+use serde::Deserialize;
1112 use topcoat::Result;
1213 use topcoat::context::{Cx, context};
14+use topcoat::router::content::Form;
15+use topcoat::router::error::{SeeOther, see_other};
1316 use topcoat::router::response::Response;
1417 use topcoat::router::{Body, StatusCode, page, query_params, route};
1518 use topcoat::view::{View, component, view};
1619
17-use crate::auth::{current_user, require_user};
20+use crate::auth::{current_user, require_csrf, require_user};
1821 use crate::i18n::authoring::{self as ga};
1922 use crate::i18n::needs::{self as ni, K};
2023 use crate::i18n::{self, Lang};
2124 use crate::live::{Topic, stream_wakes};
2225 use crate::pages::ci_runs::run_href;
2326 use crate::state::AppState;
24-use crate::views::parts::head;
27+use crate::views::parts::{head, post_act};
2528 use crate::views::repo_list::{PageLink, pager, pager_links};
2629 use crate::views::{rel_time, repo_href, shell};
2730
@@ -220,34 +223,45 @@ impl NeedPager {
220223 pub(crate) struct NeedSection {
221224 heading: &'static str,
222225 more: Option<(&'static str, &'static str)>,
226+ /// The failed runs section can be dismissed.
227+ dismiss: bool,
223228 rows: Vec<NeedRow>,
224229 pager: NeedPager,
225230 }
226231
227232 /// 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+) {
229239 if rows.is_empty() {
230240 return;
231241 }
242+ let more = rows.len() > SECTION_ROWS;
243+ rows.truncate(SECTION_ROWS);
232244 sections.push(NeedSection {
233245 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),
235248 rows,
236249 pager: NeedPager::single(),
237250 });
238251 }
239252
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.
241255 async fn load_sections(state: &AppState, user: &User, lang: Lang) -> Result<Vec<NeedSection>> {
242256 let mut sections = Vec::new();
243- let limit = SECTION_ROWS as i64;
257+ let limit = SECTION_ROWS as i64 + 1;
244258
245259 let runs = Section::Runs.rows(state, user, lang, limit, 0).await?;
246260 push_section(&mut sections, lang, Section::Runs, runs);
247261
248262 let mut reviews = Vec::new();
249263 for item in state.store.review_requests_for_user(&user.id).await? {
250- if reviews.len() >= SECTION_ROWS {
264+ if reviews.len() > SECTION_ROWS {
251265 break;
252266 }
253267 // 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<
286300 });
287301 }
288302 if !reviews.is_empty() {
303+ let more = reviews.len() > SECTION_ROWS;
304+ reviews.truncate(SECTION_ROWS);
289305 sections.push(NeedSection {
290306 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,
292309 rows: reviews,
293310 pager: NeedPager::single(),
294311 });
@@ -331,6 +348,7 @@ async fn load_full(
331348 Ok(NeedSection {
332349 heading: ni::t(lang, section.heading()),
333350 more: None,
351+ dismiss: matches!(section, Section::Runs),
334352 rows,
335353 pager: NeedPager {
336354 page,
@@ -344,10 +362,16 @@ async fn load_full(
344362
345363 /// One section of the page: its head, its rows as a compact list, its full-list link and its pager.
346364 #[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> {
348371 let NeedSection {
349372 heading,
350373 more,
374+ dismiss,
351375 rows,
352376 pager:
353377 NeedPager {
@@ -371,9 +395,14 @@ pub(crate) async fn need_section(cx: &Cx, lang: Lang, section: NeedSection) -> R
371395 <span class="val">rel_time(lang: lang, stamp: row.stamp.as_str())</span>
372396 </a>
373397 }
374- if let Some((href, text)) = more {
398+ if more.is_some() || dismiss {
375399 <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+ }
377406 </div>
378407 }
379408 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> {
410439 let back = full.as_ref().map(|_| ni::t(lang, K::BackToNeedsYou));
411440 let heading = ni::t(lang, K::NeedsYou);
412441 let unread = state.store.unread_count(&user.id).await?;
442+ let csrf = viewer.1.csrf_token.clone();
413443 Ok(view! {
414444 shell(title: heading, viewer: Some(&viewer),
415445 head(title: heading, live: Some(live.as_str()),
@@ -424,10 +454,10 @@ async fn needs_you(cx: &Cx) -> Result<impl View> {
424454 <p class="state">(ni::t(lang, K::AllClear))</p>
425455 }
426456 for section in sections.into_iter() {
427- need_section(lang: lang, section: section)
457+ need_section(lang: lang, csrf: csrf.clone(), section: section)
428458 }
429459 if let Some(section) = full {
430- need_section(lang: lang, section: section)
460+ need_section(lang: lang, csrf: csrf.clone(), section: section)
431461 }
432462 </div>
433463 </div>
@@ -435,6 +465,22 @@ async fn needs_you(cx: &Cx) -> Result<impl View> {
435465 })
436466 }
437467
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+
438484 /// The page's stream: a wake on the viewer's review-request or inbox topic. Anonymous visitors get
439485 /// a bare 401. The query of a section view is ignored here, so the same stream serves both.
440486 #[route(GET "/needs-you/events")]