| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use super::settings::csrf; |
| 5 | | use super::{filters, Error, Page, Shell}; |
| 6 | | use crate::auth::{roles, Access, Viewer}; |
| 7 | | use crate::db::{self, issues as idb, projects as pdb}; |
| 8 | | use crate::Shared; |
| 9 | | use askama::Template; |
| 10 | | use axum::extract::{Form, Path, Query, State}; |
| 11 | | use axum::response::{IntoResponse, Redirect, Response}; |
| 12 | | use serde::Deserialize; |
| 13 | |
|
| 14 | |
|
| 15 | | pub struct Rights { |
| 16 | | |
| 17 | | pub member: bool, |
| 18 | | |
| 19 | | pub edit: bool, |
| 20 | | } |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | 383× | pub async fn rights_of(app: &Shared, account: Option<&db::accounts::Account>, owner: &str) -> sqlx::Result<Option<Rights>> { |
| 25 | 383× | let admin = account.is_some_and(|a| a.is_admin); |
| 26 | 383× | if let Some(a) = db::accounts::account_by_name(&app.db, owner).await? { |
| 27 | 367× | let me = account.is_some_and(|v| v.id == a.id); |
| 28 | 367× | return Ok(Some(Rights { member: me || admin, edit: me || admin })); |
| 29 | 16× | } |
| 30 | 16× | if let Some(o) = db::accounts::org_by_name(&app.db, owner).await? { |
| 31 | 14× | let member = match account { |
| 32 | 14× | Some(v) => db::accounts::org_role(&app.db, o.id, v.id).await?.is_some(), |
| 33 | 0× | None => false, |
| 34 | | }; |
| 35 | 14× | return Ok(Some(Rights { member: member || admin, edit: member || admin })); |
| 36 | 2× | } |
| 37 | 2× | Ok(None) |
| 38 | 383× | } |
| 39 | |
|
| 40 | 125× | async fn rights(app: &Shared, shell: &Shell, viewer: &Viewer, owner: &str) -> Result<Rights, Error> { |
| 41 | 125× | rights_of(app, viewer.account(), owner) |
| 42 | 125× | .await |
| 43 | 125× | .map_err(|e| Error::from_db(shell.clone(), e))? |
| 44 | 125× | .ok_or_else(|| Error::not_found(shell.clone())) |
| 45 | 125× | } |
| 46 | |
|
| 47 | | #[derive(Template)] |
| 48 | | #[template(path = "projects.html")] |
| 49 | | pub struct ProjectsPage { |
| 50 | | shell: Shell, |
| 51 | | owner: String, |
| 52 | | |
| 53 | | projects: Vec<pdb::Project>, |
| 54 | | can_edit: bool, |
| 55 | | error: Option<String>, |
| 56 | | |
| 57 | | |
| 58 | | orgs: Vec<String>, |
| 59 | | |
| 60 | | q: String, |
| 61 | | closed: bool, |
| 62 | | open_count: usize, |
| 63 | | closed_count: usize, |
| 64 | | } |
| 65 | |
|
| 66 | | impl ProjectsPage { |
| 67 | | |
| 68 | | |
| 69 | | #[allow(clippy::too_many_arguments)] |
| 70 | 13× | fn build( |
| 71 | 13× | shell: Shell, |
| 72 | 13× | owner: String, |
| 73 | 13× | all: Vec<pdb::Project>, |
| 74 | 13× | can_edit: bool, |
| 75 | 13× | error: Option<String>, |
| 76 | 13× | orgs: Vec<String>, |
| 77 | 13× | q: Option<String>, |
| 78 | 13× | closed: bool, |
| 79 | 13× | ) -> ProjectsPage { |
| 80 | 13× | let q = q.unwrap_or_default().trim().to_string(); |
| 81 | 13× | let needle = q.to_lowercase(); |
| 82 | 13× | let matching: Vec<pdb::Project> = all |
| 83 | 13× | .into_iter() |
| 84 | 13× | .filter(|p| needle.is_empty() || p.title.to_lowercase().contains(&needle) || p.description.to_lowercase().contains(&needle)) |
| 85 | 13× | .collect(); |
| 86 | 13× | let closed_count = matching.iter().filter(|p| p.closed).count(); |
| 87 | 13× | let open_count = matching.len() - closed_count; |
| 88 | 13× | let projects = matching.into_iter().filter(|p| p.closed == closed).collect(); |
| 89 | 13× | ProjectsPage { shell, owner, projects, can_edit, error, orgs, q, closed, open_count, closed_count } |
| 90 | 13× | } |
| 91 | | } |
| 92 | |
|
| 93 | |
|
| 94 | | #[derive(Deserialize, Default)] |
| 95 | | pub struct ProjectsQuery { |
| 96 | | #[serde(default)] |
| 97 | | q: Option<String>, |
| 98 | | #[serde(default)] |
| 99 | | state: Option<String>, |
| 100 | | } |
| 101 | |
|
| 102 | |
|
| 103 | 13× | async fn own_orgs(app: &Shared, viewer: &Viewer, owner: &str) -> Vec<String> { |
| 104 | 13× | match viewer.account() { |
| 105 | 12× | Some(a) if a.username == owner => { |
| 106 | 11× | crate::db::accounts::orgs_of(&app.db, a.id).await.map(|v| v.into_iter().map(|(o, _)| o.name).collect()).unwrap_or_default() |
| 107 | | } |
| 108 | 2× | _ => vec![], |
| 109 | | } |
| 110 | 13× | } |
| 111 | |
|
| 112 | 14× | pub async fn list( |
| 113 | 14× | State(app): State<Shared>, |
| 114 | 14× | viewer: Viewer, |
| 115 | 14× | Path(owner): Path<String>, |
| 116 | 14× | Query(query): Query<ProjectsQuery>, |
| 117 | 14× | ) -> Result<Page<ProjectsPage>, Error> { |
| 118 | 14× | let shell = Shell::new(&app, &format!("/projects/{owner}"), &viewer); |
| 119 | 14× | let r = rights(&app, &shell, &viewer, &owner).await?; |
| 120 | 13× | let projects = pdb::of_owner(&app.db, &owner) |
| 121 | 13× | .await |
| 122 | 13× | .map_err(|e| Error::from_db(shell.clone(), e))? |
| 123 | 13× | .into_iter() |
| 124 | 13× | .filter(|p| p.public || r.member) |
| 125 | 13× | .collect(); |
| 126 | 13× | let orgs = own_orgs(&app, &viewer, &owner).await; |
| 127 | 13× | let closed = query.state.as_deref() == Some("closed"); |
| 128 | 13× | Ok(Page(ProjectsPage::build(shell, owner, projects, r.edit, None, orgs, query.q, closed))) |
| 129 | 14× | } |
| 130 | |
|
| 131 | | #[derive(Deserialize)] |
| 132 | | pub struct NewProjectForm { |
| 133 | | #[serde(rename = "_csrf")] |
| 134 | | csrf_token: String, |
| 135 | | title: String, |
| 136 | | #[serde(default)] |
| 137 | | description: String, |
| 138 | | #[serde(default)] |
| 139 | | private: Option<String>, |
| 140 | | } |
| 141 | |
|
| 142 | 8× | pub async fn create( |
| 143 | 8× | State(app): State<Shared>, |
| 144 | 8× | viewer: Viewer, |
| 145 | 8× | Path(owner): Path<String>, |
| 146 | 8× | Form(f): Form<NewProjectForm>, |
| 147 | 8× | ) -> Result<Response, Error> { |
| 148 | 8× | let shell = Shell::new(&app, &format!("/projects/{owner}"), &viewer); |
| 149 | 8× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 150 | 8× | let r = rights(&app, &shell, &viewer, &owner).await?; |
| 151 | 8× | if !r.edit { |
| 152 | 0× | return Err(Error::not_found(shell)); |
| 153 | 8× | } |
| 154 | 8× | let title = f.title.trim(); |
| 155 | 8× | if title.is_empty() || title.len() > 100 { |
| 156 | 0× | let projects = pdb::of_owner(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 157 | 0× | let orgs = own_orgs(&app, &viewer, &owner).await; |
| 158 | 0× | return Ok(Page(ProjectsPage::build( |
| 159 | 0× | shell, |
| 160 | 0× | owner, |
| 161 | 0× | projects, |
| 162 | 0× | true, |
| 163 | 0× | Some("A project needs a title of up to 100 characters.".into()), |
| 164 | 0× | orgs, |
| 165 | 0× | None, |
| 166 | 0× | false, |
| 167 | 0× | )) |
| 168 | 0× | .into_response()); |
| 169 | 8× | } |
| 170 | 8× | let p = pdb::create(&app.db, &owner, title, f.description.trim(), f.private.is_none()) |
| 171 | 8× | .await |
| 172 | 8× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 173 | 8× | db::accounts::audit(&app.db, viewer.account(), "project.created", &format!("{owner}/{}", p.number), title).await.ok(); |
| 174 | 8× | Ok(Redirect::to(&format!("/projects/{owner}/{}", p.number)).into_response()) |
| 175 | 8× | } |
| 176 | |
|
| 177 | |
|
| 178 | | #[derive(Clone)] |
| 179 | | pub struct CardView { |
| 180 | | pub card: pdb::Card, |
| 181 | | |
| 182 | | pub reference: String, |
| 183 | | pub url: String, |
| 184 | | pub title: String, |
| 185 | | |
| 186 | | pub state: String, |
| 187 | | |
| 188 | | pub labels: Vec<idb::Label>, |
| 189 | | pub assignees: Vec<String>, |
| 190 | | |
| 191 | | |
| 192 | | |
| 193 | | pub repo: String, |
| 194 | | pub milestone: String, |
| 195 | | pub opened_ms: i64, |
| 196 | | } |
| 197 | |
|
| 198 | | #[derive(Clone)] |
| 199 | | pub struct ColumnView { |
| 200 | | pub column: pdb::Column, |
| 201 | | pub cards: Vec<CardView>, |
| 202 | | |
| 203 | | |
| 204 | | pub hidden: usize, |
| 205 | | pub total: usize, |
| 206 | | |
| 207 | | pub archived: usize, |
| 208 | | |
| 209 | | |
| 210 | | pub finished: bool, |
| 211 | | |
| 212 | | pub over_wip: bool, |
| 213 | | |
| 214 | | |
| 215 | | pub before_iteration: usize, |
| 216 | | } |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | | pub const COLUMN_SHOWN: usize = 30; |
| 221 | |
|
| 222 | |
|
| 223 | | #[derive(Deserialize, Default, Clone)] |
| 224 | | pub struct BoardQuery { |
| 225 | | |
| 226 | | #[serde(default)] |
| 227 | | all: Option<i64>, |
| 228 | | |
| 229 | | |
| 230 | | #[serde(default)] |
| 231 | | q: Option<String>, |
| 232 | | |
| 233 | | #[serde(default)] |
| 234 | | state: Option<String>, |
| 235 | | |
| 236 | | #[serde(default)] |
| 237 | | archived: Option<String>, |
| 238 | | |
| 239 | | #[serde(default)] |
| 240 | | iteration: Option<i64>, |
| 241 | | |
| 242 | | |
| 243 | | #[serde(default)] |
| 244 | | pub view: Option<String>, |
| 245 | | |
| 246 | | #[serde(default)] |
| 247 | | pub layout: Option<String>, |
| 248 | | |
| 249 | | #[serde(default)] |
| 250 | | pub lane: Option<String>, |
| 251 | | |
| 252 | | |
| 253 | | #[serde(default)] |
| 254 | | pub sort: Option<String>, |
| 255 | | |
| 256 | | #[serde(default)] |
| 257 | | pub compact: Option<String>, |
| 258 | | |
| 259 | | #[serde(default)] |
| 260 | | pub page: Option<usize>, |
| 261 | | } |
| 262 | |
|
| 263 | | impl BoardQuery { |
| 264 | | |
| 265 | | |
| 266 | 57× | pub fn over(&self, saved: Option<&pdb::View>) -> BoardQuery { |
| 267 | 57× | let mut q = self.clone(); |
| 268 | 57× | if let Some(v) = saved { |
| 269 | 4× | let fill = |x: &mut Option<String>, from: &str| { |
| 270 | 4× | if x.is_none() && !from.is_empty() { |
| 271 | 3× | *x = Some(from.to_string()); |
| 272 | 3× | } |
| 273 | 4× | }; |
| 274 | 1× | fill(&mut q.q, &v.q); |
| 275 | 1× | fill(&mut q.layout, &v.layout); |
| 276 | 1× | fill(&mut q.lane, &v.lane); |
| 277 | 1× | fill(&mut q.sort, &v.sort); |
| 278 | 56× | } |
| 279 | 57× | q |
| 280 | 57× | } |
| 281 | 57× | pub fn layout(&self) -> &str { |
| 282 | 57× | match self.layout.as_deref() { |
| 283 | 1× | Some("table") => "table", |
| 284 | 56× | _ => "board", |
| 285 | | } |
| 286 | 57× | } |
| 287 | 57× | pub fn lane(&self) -> &str { |
| 288 | 57× | self.lane.as_deref().filter(|l| matches!(*l, "repo" | "assignee" | "label" | "milestone")).unwrap_or("") |
| 289 | 57× | } |
| 290 | 114× | fn compact(&self) -> bool { |
| 291 | 114× | self.compact.as_deref().is_some_and(|c| !c.is_empty() && c != "0") |
| 292 | 114× | } |
| 293 | 173× | fn text(&self) -> String { |
| 294 | 173× | self.q.as_deref().map(str::trim).unwrap_or("").to_string() |
| 295 | 173× | } |
| 296 | 115× | fn state(&self) -> String { |
| 297 | 115× | self.state.as_deref().map(str::trim).unwrap_or("").to_ascii_lowercase() |
| 298 | 115× | } |
| 299 | 227× | fn archived(&self) -> bool { |
| 300 | 227× | self.archived.as_deref().is_some_and(|a| !a.is_empty() && a != "0") |
| 301 | 227× | } |
| 302 | | } |
| 303 | |
|
| 304 | |
|
| 305 | | #[derive(Default)] |
| 306 | | pub struct CardFilter { |
| 307 | | labels: Vec<String>, |
| 308 | | assignees: Vec<String>, |
| 309 | | state: String, |
| 310 | | words: Vec<String>, |
| 311 | | |
| 312 | | repos: Vec<String>, |
| 313 | | milestones: Vec<String>, |
| 314 | | |
| 315 | | is: String, |
| 316 | | |
| 317 | | |
| 318 | | older_than_days: Option<i64>, |
| 319 | | newer_than_days: Option<i64>, |
| 320 | | } |
| 321 | |
|
| 322 | | impl CardFilter { |
| 323 | 59× | fn of(view: &BoardQuery) -> CardFilter { |
| 324 | 59× | let mut f = CardFilter::parse(&view.text()); |
| 325 | 59× | if f.state.is_empty() { |
| 326 | 58× | f.state = view.state(); |
| 327 | 58× | } |
| 328 | 59× | f |
| 329 | 59× | } |
| 330 | |
|
| 331 | | |
| 332 | | |
| 333 | | |
| 334 | 76× | pub fn parse(text: &str) -> CardFilter { |
| 335 | 76× | let mut f = CardFilter::default(); |
| 336 | 76× | for tok in tokens(text) { |
| 337 | 25× | match tok.split_once(':') { |
| 338 | 21× | Some(("label", v)) if !v.is_empty() => f.labels.push(v.to_lowercase()), |
| 339 | 14× | Some(("assignee", v)) if !v.is_empty() => f.assignees.push(v.trim_start_matches('@').to_lowercase()), |
| 340 | 12× | Some(("state", v)) if !v.is_empty() => f.state = v.to_lowercase(), |
| 341 | 11× | Some(("repo", v)) if !v.is_empty() => f.repos.push(v.rsplit('/').next().unwrap_or(v).to_lowercase()), |
| 342 | 8× | Some(("milestone", v)) if !v.is_empty() => f.milestones.push(v.to_lowercase()), |
| 343 | 7× | Some(("is", v)) if matches!(v, "open" | "closed") => f.is = v.to_string(), |
| 344 | 4× | Some(("age", v)) if age_days(v).is_some() => match age_days(v) { |
| 345 | 2× | Some((true, d)) => f.older_than_days = Some(d), |
| 346 | 1× | Some((false, d)) => f.newer_than_days = Some(d), |
| 347 | 0× | None => {} |
| 348 | | }, |
| 349 | 5× | _ => f.words.push(tok.to_lowercase()), |
| 350 | | } |
| 351 | | } |
| 352 | 76× | f |
| 353 | 76× | } |
| 354 | |
|
| 355 | 170× | pub fn is_empty(&self) -> bool { |
| 356 | 170× | self.labels.is_empty() |
| 357 | 161× | && self.assignees.is_empty() |
| 358 | 158× | && self.state.is_empty() |
| 359 | 152× | && self.words.is_empty() |
| 360 | 146× | && self.repos.is_empty() |
| 361 | 143× | && self.milestones.is_empty() |
| 362 | 143× | && self.is.is_empty() |
| 363 | 140× | && self.older_than_days.is_none() |
| 364 | 140× | && self.newer_than_days.is_none() |
| 365 | 170× | } |
| 366 | |
|
| 367 | | |
| 368 | | |
| 369 | | |
| 370 | 129× | pub fn keeps(&self, c: &CardView, now: i64) -> bool { |
| 371 | 129× | let (title, reference) = (c.title.to_lowercase(), c.reference.to_lowercase()); |
| 372 | 129× | let age_days = (now - c.opened_ms) / DAY_MS; |
| 373 | 129× | self.words.iter().all(|w| title.contains(w) || reference.contains(w)) |
| 374 | 88× | && self.labels.iter().all(|l| c.labels.iter().any(|x| x.name.eq_ignore_ascii_case(l))) |
| 375 | 82× | && self.assignees.iter().all(|a| c.assignees.iter().any(|x| x.eq_ignore_ascii_case(a))) |
| 376 | 78× | && (self.state.is_empty() || c.state.eq_ignore_ascii_case(&self.state)) |
| 377 | 40× | && self.repos.iter().all(|r| c.repo.eq_ignore_ascii_case(r)) |
| 378 | 35× | && self.milestones.iter().all(|m| c.milestone.eq_ignore_ascii_case(m)) |
| 379 | 34× | && match self.is.as_str() { |
| 380 | 34× | "open" => matches!(c.state.as_str(), "open" | "draft"), |
| 381 | 28× | "closed" => matches!(c.state.as_str(), "closed" | "landed"), |
| 382 | 25× | _ => true, |
| 383 | | } |
| 384 | 30× | && self.older_than_days.is_none_or(|d| age_days > d) |
| 385 | 29× | && self.newer_than_days.is_none_or(|d| age_days < d) |
| 386 | 129× | } |
| 387 | | } |
| 388 | |
|
| 389 | |
|
| 390 | 76× | fn tokens(text: &str) -> Vec<String> { |
| 391 | 76× | let mut out = Vec::new(); |
| 392 | 76× | let mut cur = String::new(); |
| 393 | 76× | let mut quoted = false; |
| 394 | 243× | for ch in text.chars() { |
| 395 | 241× | match ch { |
| 396 | 2× | '"' => quoted = !quoted, |
| 397 | 241× | c if c.is_whitespace() && !quoted => { |
| 398 | 6× | if !cur.is_empty() { |
| 399 | 4× | out.push(std::mem::take(&mut cur)); |
| 400 | 4× | } |
| 401 | | } |
| 402 | 235× | c => cur.push(c), |
| 403 | | } |
| 404 | | } |
| 405 | 76× | if !cur.is_empty() { |
| 406 | 21× | out.push(cur); |
| 407 | 55× | } |
| 408 | 76× | out |
| 409 | 76× | } |
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | 7× | fn age_days(v: &str) -> Option<(bool, i64)> { |
| 414 | 7× | let (older, rest) = match v.as_bytes().first()? { |
| 415 | 4× | b'>' => (true, &v[1..]), |
| 416 | 2× | b'<' => (false, &v[1..]), |
| 417 | 1× | _ => return None, |
| 418 | | }; |
| 419 | 6× | let n: i64 = rest.trim_end_matches('d').parse().ok()?; |
| 420 | 6× | (n >= 0).then_some((older, n)) |
| 421 | 7× | } |
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | 59× | fn shape(columns: &mut [ColumnView], view: &BoardQuery, now: i64, done: Option<i64>, since: Option<i64>) { |
| 427 | 59× | let filter = CardFilter::of(view); |
| 428 | 169× | for col in columns.iter_mut() { |
| 429 | 169× | col.total = col.cards.len(); |
| 430 | 169× | if let (Some(d), Some(since), false) = (done, since, view.archived() || view.iteration.is_some()) { |
| 431 | 13× | if col.column.id == d { |
| 432 | 5× | let before = col.cards.len(); |
| 433 | 9× | col.cards.retain(|c| c.card.moved_ms >= since); |
| 434 | 5× | col.before_iteration = before - col.cards.len(); |
| 435 | 5× | col.total = col.cards.len(); |
| 436 | 8× | } |
| 437 | 156× | } |
| 438 | 169× | col.over_wip = col.column.wip_limit.is_some_and(|w| w >= 0 && col.total as i64 > w); |
| 439 | 169× | if !filter.is_empty() { |
| 440 | 92× | col.cards.retain(|c| filter.keeps(c, now)); |
| 441 | 139× | } else if col.cards.len() > COLUMN_SHOWN && view.all != Some(col.column.id) && !view.archived() && view.iteration.is_none() { |
| 442 | 1× | |
| 443 | 1× | let cut = col.cards.len() - COLUMN_SHOWN; |
| 444 | 1× | col.cards.drain(..cut); |
| 445 | 138× | } |
| 446 | 169× | col.hidden = col.total - col.cards.len(); |
| 447 | | } |
| 448 | 59× | } |
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | | pub struct LaneView { |
| 453 | | pub name: String, |
| 454 | | pub columns: Vec<ColumnView>, |
| 455 | | pub count: usize, |
| 456 | | } |
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | 164× | fn lanes_of(c: &CardView, lane: &str) -> Vec<String> { |
| 461 | 164× | let many = |v: Vec<String>, none: &str| if v.is_empty() { vec![none.to_string()] } else { v }; |
| 462 | 164× | match lane { |
| 463 | 164× | "repo" => vec![if c.repo.is_empty() { "(notes)".into() } else { c.repo.clone() }], |
| 464 | 141× | "assignee" => many(c.assignees.iter().map(|a| format!("@{a}")).collect(), "(nobody)"), |
| 465 | 138× | "label" => many(c.labels.iter().map(|l| l.name.clone()).collect(), "(no label)"), |
| 466 | 126× | "milestone" => vec![if c.milestone.is_empty() { "(no milestone)".into() } else { c.milestone.clone() }], |
| 467 | 126× | _ => vec![String::new()], |
| 468 | | } |
| 469 | 164× | } |
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | 3× | pub fn lanes(columns: &[ColumnView], lane: &str) -> Vec<LaneView> { |
| 474 | 9× | let mut names: Vec<String> = columns.iter().flat_map(|col| col.cards.iter().flat_map(|c| lanes_of(c, lane))).collect(); |
| 475 | 18× | names.sort_by_key(|n| (n.starts_with('('), n.to_lowercase())); |
| 476 | 3× | names.dedup(); |
| 477 | 3× | names |
| 478 | 3× | .into_iter() |
| 479 | 8× | .map(|name| { |
| 480 | 8× | let columns: Vec<ColumnView> = columns |
| 481 | 8× | .iter() |
| 482 | 19× | .map(|col| { |
| 483 | 19× | let mut col = col.clone(); |
| 484 | 24× | col.cards.retain(|c| lanes_of(c, lane).contains(&name)); |
| 485 | 19× | col.total = col.cards.len(); |
| 486 | 19× | col.hidden = 0; |
| 487 | 19× | col.before_iteration = 0; |
| 488 | 19× | col |
| 489 | 19× | }) |
| 490 | 8× | .collect(); |
| 491 | 19× | let count = columns.iter().map(|c| c.cards.len()).sum(); |
| 492 | 8× | LaneView { name, columns, count } |
| 493 | 8× | }) |
| 494 | 3× | .collect() |
| 495 | 3× | } |
| 496 | |
|
| 497 | |
|
| 498 | | pub struct RowView { |
| 499 | | pub c: CardView, |
| 500 | | pub column: String, |
| 501 | | pub column_id: i64, |
| 502 | | pub group: String, |
| 503 | | |
| 504 | | pub starts_group: bool, |
| 505 | | } |
| 506 | |
|
| 507 | |
|
| 508 | | pub const TABLE_PAGE: usize = 50; |
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | 5× | pub fn rows(columns: &[ColumnView], lane: &str, sort: &str, page: usize) -> (Vec<RowView>, usize) { |
| 514 | 5× | let mut all: Vec<RowView> = Vec::new(); |
| 515 | 10× | for (ci, col) in columns.iter().enumerate() { |
| 516 | 131× | for c in &col.cards { |
| 517 | 131× | for group in lanes_of(c, lane) { |
| 518 | 131× | all.push(RowView { c: c.clone(), column: col.column.name.clone(), column_id: ci as i64, group, starts_group: false }); |
| 519 | 131× | } |
| 520 | | } |
| 521 | | } |
| 522 | 5× | let (desc, key) = match sort.strip_prefix('-') { |
| 523 | 1× | Some(k) => (true, k), |
| 524 | 4× | None => (false, sort), |
| 525 | | }; |
| 526 | 129× | all.sort_by(|a, b| { |
| 527 | 129× | let by = match key { |
| 528 | 129× | "title" => a.c.title.to_lowercase().cmp(&b.c.title.to_lowercase()), |
| 529 | 126× | "reference" => a.c.reference.cmp(&b.c.reference), |
| 530 | 126× | "state" => a.c.state.cmp(&b.c.state), |
| 531 | 126× | "column" => a.column_id.cmp(&b.column_id), |
| 532 | 126× | "age" => b.c.opened_ms.cmp(&a.c.opened_ms), |
| 533 | 123× | _ => a.column_id.cmp(&b.column_id).then(a.c.card.position.cmp(&b.c.card.position)), |
| 534 | | }; |
| 535 | 129× | let by = if desc { by.reverse() } else { by }; |
| 536 | 129× | (a.group.starts_with('('), a.group.to_lowercase()).cmp(&(b.group.starts_with('('), b.group.to_lowercase())).then(by) |
| 537 | 129× | }); |
| 538 | 5× | let pages = all.len().div_ceil(TABLE_PAGE).max(1); |
| 539 | 5× | let page = page.clamp(1, pages); |
| 540 | 5× | let mut shown: Vec<RowView> = all.into_iter().skip((page - 1) * TABLE_PAGE).take(TABLE_PAGE).collect(); |
| 541 | 5× | let mut last: Option<String> = None; |
| 542 | 31× | for r in shown.iter_mut() { |
| 543 | 31× | r.starts_group = !lane.is_empty() && last.as_ref() != Some(&r.group); |
| 544 | 31× | last = Some(r.group.clone()); |
| 545 | | } |
| 546 | 5× | (shown, pages) |
| 547 | 5× | } |
| 548 | |
|
| 549 | |
|
| 550 | | pub struct IterationView { |
| 551 | | pub it: pdb::Iteration, |
| 552 | | |
| 553 | | pub first: String, |
| 554 | | pub last: String, |
| 555 | | pub days: i64, |
| 556 | | |
| 557 | | pub so_far: usize, |
| 558 | | |
| 559 | | pub done: usize, |
| 560 | | |
| 561 | | |
| 562 | | pub points: String, |
| 563 | | pub top: usize, |
| 564 | | } |
| 565 | |
|
| 566 | | pub const BURN_W: i64 = 280; |
| 567 | | pub const BURN_H: i64 = 64; |
| 568 | |
|
| 569 | |
|
| 570 | | const PAST_SHOWN: usize = 12; |
| 571 | |
|
| 572 | | const DAY_MS: i64 = 86_400_000; |
| 573 | |
|
| 574 | |
|
| 575 | 8× | fn day_ms(s: &str) -> Option<i64> { |
| 576 | 22× | let mut parts = s.trim().splitn(3, '-').map(|p| p.parse::<i32>().ok()); |
| 577 | 8× | let (y, m, d) = (parts.next()??, parts.next()??, parts.next()??); |
| 578 | 7× | let month = time::Month::try_from(u8::try_from(m).ok()?).ok()?; |
| 579 | 7× | let date = time::Date::from_calendar_date(y, month, u8::try_from(d).ok()?).ok()?; |
| 580 | 7× | Some(date.midnight().assume_utc().unix_timestamp() * 1000) |
| 581 | 8× | } |
| 582 | |
|
| 583 | |
|
| 584 | 136× | fn day_of(ms: i64) -> String { |
| 585 | 136× | let t = time::OffsetDateTime::from_unix_timestamp(ms.div_euclid(1000)).unwrap_or(time::OffsetDateTime::UNIX_EPOCH); |
| 586 | 136× | format!("{:04}-{:02}-{:02}", t.year(), u8::from(t.month()), t.day()) |
| 587 | 136× | } |
| 588 | |
|
| 589 | 11× | async fn iteration_view(app: &Shared, it: pdb::Iteration, done: Option<i64>, now: i64) -> sqlx::Result<IterationView> { |
| 590 | 11× | let times = pdb::done_times(&app.db, &it, done).await?; |
| 591 | 11× | let days = pdb::iteration_days(&it); |
| 592 | 11× | let up = pdb::burn_up(×, it.start_ms, it.end_ms, it.closed_ms.unwrap_or(now)); |
| 593 | 11× | let top = up.last().copied().unwrap_or(0).max(1); |
| 594 | | |
| 595 | 11× | let points = std::iter::once((0, 0)) |
| 596 | 44× | .chain(up.iter().enumerate().map(|(d, &n)| (d as i64 + 1, n))) |
| 597 | 55× | .map(|(d, n)| format!("{},{}", d * BURN_W / days, BURN_H - n as i64 * BURN_H / top as i64)) |
| 598 | 11× | .collect::<Vec<_>>() |
| 599 | 11× | .join(" "); |
| 600 | 11× | Ok(IterationView { |
| 601 | 11× | first: day_of(it.start_ms), |
| 602 | 11× | last: day_of(it.end_ms - 1), |
| 603 | 11× | days, |
| 604 | 11× | so_far: up.len(), |
| 605 | 11× | done: times.len(), |
| 606 | 11× | points, |
| 607 | 11× | top, |
| 608 | 11× | it, |
| 609 | 11× | }) |
| 610 | 11× | } |
| 611 | |
|
| 612 | | #[derive(Template)] |
| 613 | | #[template(path = "project.html")] |
| 614 | | pub struct BoardPage { |
| 615 | | shell: Shell, |
| 616 | | project: pdb::Project, |
| 617 | | columns: Vec<ColumnView>, |
| 618 | | |
| 619 | | repos: Vec<String>, |
| 620 | | |
| 621 | | can_edit: bool, |
| 622 | | |
| 623 | | can_manage: bool, |
| 624 | | notice: Option<String>, |
| 625 | | error: Option<String>, |
| 626 | | |
| 627 | | |
| 628 | | transfer_to: Vec<String>, |
| 629 | | |
| 630 | | q: String, |
| 631 | | state: String, |
| 632 | | filtered: bool, |
| 633 | | |
| 634 | | archived: bool, |
| 635 | | |
| 636 | | archived_total: usize, |
| 637 | | |
| 638 | | |
| 639 | | current: Option<IterationView>, |
| 640 | | past: Vec<IterationView>, |
| 641 | | shown_iteration: Option<IterationView>, |
| 642 | | |
| 643 | | next_first: String, |
| 644 | | next_last: String, |
| 645 | | |
| 646 | | views: Vec<pdb::View>, |
| 647 | | view_name: String, |
| 648 | | |
| 649 | | layout: String, |
| 650 | | lane: String, |
| 651 | | sort: String, |
| 652 | | compact: bool, |
| 653 | | |
| 654 | | lanes: Vec<LaneView>, |
| 655 | | |
| 656 | | rows: Vec<RowView>, |
| 657 | | page: usize, |
| 658 | | pages: usize, |
| 659 | | |
| 660 | | look: String, |
| 661 | | } |
| 662 | |
|
| 663 | | #[allow(clippy::too_many_arguments)] |
| 664 | 59× | async fn board( |
| 665 | 59× | app: &Shared, |
| 666 | 59× | shell: Shell, |
| 667 | 59× | viewer: &Viewer, |
| 668 | 59× | owner: &str, |
| 669 | 59× | number: i64, |
| 670 | 59× | notice: Option<String>, |
| 671 | 59× | error: Option<String>, |
| 672 | 59× | view: &BoardQuery, |
| 673 | 59× | ) -> Result<Page<BoardPage>, Error> { |
| 674 | 59× | let e = |e| Error::from_db(shell.clone(), e); |
| 675 | 59× | let r = rights(app, &shell, viewer, owner).await?; |
| 676 | 59× | let project = pdb::get(&app.db, owner, number) |
| 677 | 59× | .await |
| 678 | 59× | .map_err(e)? |
| 679 | 59× | .filter(|p| p.public || r.member) |
| 680 | 59× | .ok_or_else(|| Error::not_found(shell.clone()))?; |
| 681 | 57× | let now = db::now_ms(); |
| 682 | 57× | let views = pdb::views(&app.db, project.id).await.map_err(e)?; |
| 683 | 57× | let saved = view.view.as_deref().and_then(|n| views.iter().find(|v| v.name == n)); |
| 684 | 57× | let look = view.over(saved); |
| 685 | 57× | let view = &look; |
| 686 | 57× | let iterations = pdb::iterations(&app.db, project.id).await.map_err(e)?; |
| 687 | 57× | let shown = view.iteration.and_then(|id| iterations.iter().find(|i| i.id == id && i.closed_ms.is_some()).cloned()); |
| 688 | | |
| 689 | 57× | let archived = view.archived() || shown.is_some(); |
| 690 | 57× | let (mut columns, repos) = board_columns(app, viewer.account(), owner, &project, archived).await.map_err(e)?; |
| 691 | 57× | if let Some(it) = &shown { |
| 692 | 6× | for col in columns.iter_mut() { |
| 693 | 6× | col.cards.retain(|c| c.card.iteration_id == Some(it.id)); |
| 694 | | } |
| 695 | 55× | } |
| 696 | 165× | let done = pdb::done_column(&columns.iter().map(|c| c.column.clone()).collect::<Vec<_>>()); |
| 697 | 57× | let open_since = iterations.iter().find(|i| i.closed_ms.is_none()).map(|i| i.start_ms); |
| 698 | 57× | shape(&mut columns, view, now, done, open_since); |
| 699 | 57× | let archived_total = columns.iter().map(|c| c.archived).sum(); |
| 700 | 57× | let (layout, lane, sort) = (view.layout().to_string(), view.lane().to_string(), view.sort.clone().unwrap_or_default()); |
| 701 | 57× | let lanes = if layout == "board" && !lane.is_empty() { lanes(&columns, &lane) } else { Vec::new() }; |
| 702 | 57× | let (rows, pages) = if layout == "table" { rows(&columns, &lane, &sort, view.page.unwrap_or(1)) } else { (Vec::new(), 1) }; |
| 703 | 57× | let page = view.page.unwrap_or(1).clamp(1, pages); |
| 704 | 57× | let mut parts: Vec<String> = Vec::new(); |
| 705 | 342× | for (k, v) in [ |
| 706 | 57× | ("view", view.view.clone().unwrap_or_default()), |
| 707 | 57× | ("layout", if layout == "table" { layout.clone() } else { String::new() }), |
| 708 | 57× | ("q", view.text()), |
| 709 | 57× | ("lane", lane.clone()), |
| 710 | 57× | ("sort", sort.clone()), |
| 711 | 57× | ("compact", if view.compact() { "1".into() } else { String::new() }), |
| 712 | | ] { |
| 713 | 342× | if !v.is_empty() { |
| 714 | 13× | parts.push(format!("{k}={}", super::settings::urlencode(&v))); |
| 715 | 329× | } |
| 716 | | } |
| 717 | 57× | let look_query = parts.join("&"); |
| 718 | 57× | let (mut current, mut past) = (None, Vec::new()); |
| 719 | 57× | for it in iterations { |
| 720 | 9× | if it.closed_ms.is_none() { |
| 721 | 3× | current = Some(iteration_view(app, it, done, now).await.map_err(e)?); |
| 722 | 6× | } else if past.len() < PAST_SHOWN { |
| 723 | 6× | past.push(iteration_view(app, it, done, now).await.map_err(e)?); |
| 724 | 0× | } |
| 725 | | } |
| 726 | 57× | let shown_iteration = match shown { |
| 727 | 2× | Some(it) => Some(iteration_view(app, it, done, now).await.map_err(e)?), |
| 728 | 55× | None => None, |
| 729 | | }; |
| 730 | 57× | let can_edit = r.edit && !project.closed; |
| 731 | 57× | let mut transfer_to = Vec::new(); |
| 732 | 57× | if let (true, Some(a)) = (r.edit, viewer.account()) { |
| 733 | 56× | transfer_to.push(a.username.clone()); |
| 734 | 56× | let orgs = db::accounts::orgs_of(&app.db, a.id).await.map_err(e)?; |
| 735 | 56× | transfer_to.extend(orgs.into_iter().map(|(o, _)| o.name)); |
| 736 | 60× | transfer_to.retain(|o| o != owner); |
| 737 | 1× | } |
| 738 | 57× | let (q, state) = (view.text(), view.state()); |
| 739 | 57× | let filtered = !q.is_empty() || !state.is_empty(); |
| 740 | | Ok(Page(BoardPage { |
| 741 | 57× | shell, |
| 742 | 57× | project, |
| 743 | 57× | columns, |
| 744 | 57× | repos, |
| 745 | 57× | can_edit, |
| 746 | 57× | can_manage: r.edit, |
| 747 | 57× | notice, |
| 748 | 57× | error, |
| 749 | 57× | transfer_to, |
| 750 | 57× | q, |
| 751 | 57× | state, |
| 752 | 57× | filtered, |
| 753 | 57× | archived, |
| 754 | 57× | archived_total, |
| 755 | 57× | current, |
| 756 | 57× | past, |
| 757 | 57× | shown_iteration, |
| 758 | 57× | next_first: day_of(now), |
| 759 | 57× | next_last: day_of(now + 13 * DAY_MS), |
| 760 | 57× | view_name: view.view.clone().filter(|n| views.iter().any(|v| &v.name == n)).unwrap_or_default(), |
| 761 | 57× | views, |
| 762 | 57× | layout, |
| 763 | 57× | lane, |
| 764 | 57× | sort, |
| 765 | 57× | compact: view.compact(), |
| 766 | 57× | lanes, |
| 767 | 57× | rows, |
| 768 | 57× | page, |
| 769 | 57× | pages, |
| 770 | 57× | look: look_query, |
| 771 | | })) |
| 772 | 59× | } |
| 773 | |
|
| 774 | |
|
| 775 | |
|
| 776 | |
|
| 777 | |
|
| 778 | 92× | pub async fn board_columns( |
| 779 | 92× | app: &Shared, |
| 780 | 92× | account: Option<&db::accounts::Account>, |
| 781 | 92× | owner: &str, |
| 782 | 92× | project: &pdb::Project, |
| 783 | 92× | archived: bool, |
| 784 | 92× | ) -> sqlx::Result<(Vec<ColumnView>, Vec<String>)> { |
| 785 | 92× | let mut readable: std::collections::HashMap<i64, db::repos::RepoRow> = Default::default(); |
| 786 | 108× | for row in db::repos::list_by_owner(&app.db, owner).await? { |
| 787 | 108× | let role = roles::role_for(&app.db, account, &row).await?; |
| 788 | 108× | if roles::allows(role, Access::Read) { |
| 789 | 94× | readable.insert(row.id, row); |
| 790 | 94× | } |
| 791 | | } |
| 792 | 92× | let all = pdb::cards(&app.db, project.id).await?; |
| 793 | 1185× | let archived_in = |column: i64| all.iter().filter(|c| c.column_id == column && c.archived_ms.is_some()).count(); |
| 794 | 399× | let cards: Vec<&pdb::Card> = all.iter().filter(|c| c.archived_ms.is_some() == archived).collect(); |
| 795 | | |
| 796 | | |
| 797 | 92× | let mut shown: std::collections::HashMap<(i64, bool, i64), Shown> = Default::default(); |
| 798 | 92× | let mut repo_ids: Vec<(i64, bool)> = |
| 799 | 354× | cards.iter().filter_map(|c| Some((c.repo_id?, c.kind == "issue"))).filter(|(rid, _)| readable.contains_key(rid)).collect(); |
| 800 | 92× | repo_ids.sort(); |
| 801 | 92× | repo_ids.dedup(); |
| 802 | 92× | for (rid, issues) in repo_ids { |
| 803 | 154× | shown.extend(shown_in(app, rid, issues).await?.into_iter().map(|(n, s)| ((rid, issues, n), s))); |
| 804 | | } |
| 805 | 92× | let mut columns = Vec::new(); |
| 806 | 273× | for column in pdb::columns(&app.db, project.id).await? { |
| 807 | 273× | let mut views = Vec::new(); |
| 808 | 1050× | for card in cards.iter().copied().filter(|c| c.column_id == column.id) { |
| 809 | 354× | match (card.kind.as_str(), card.repo_id, card.number) { |
| 810 | 354× | ("note", _, _) => views.push(CardView { |
| 811 | 241× | card: card.clone(), |
| 812 | 241× | reference: String::new(), |
| 813 | 241× | url: String::new(), |
| 814 | 241× | title: card.note.clone(), |
| 815 | 241× | state: String::new(), |
| 816 | 241× | labels: Vec::new(), |
| 817 | 241× | assignees: Vec::new(), |
| 818 | 241× | repo: String::new(), |
| 819 | 241× | milestone: String::new(), |
| 820 | 241× | opened_ms: card.created_ms, |
| 821 | 241× | }), |
| 822 | 113× | (kind, Some(rid), Some(n)) => { |
| 823 | | |
| 824 | 113× | let Some(row) = readable.get(&rid) else { continue }; |
| 825 | 111× | let Some(s) = shown.get(&(rid, kind == "issue", n)) else { continue }; |
| 826 | 111× | let slug = format!("{}/{}", row.owner, row.name); |
| 827 | 111× | views.push(CardView { |
| 828 | 111× | card: card.clone(), |
| 829 | 111× | reference: format!("{slug}#{n}"), |
| 830 | 111× | url: format!("/{slug}/{kind}/{n}"), |
| 831 | 111× | title: s.title.clone(), |
| 832 | 111× | state: s.state.clone(), |
| 833 | 111× | labels: s.labels.clone(), |
| 834 | 111× | assignees: s.assignees.clone(), |
| 835 | 111× | repo: row.name.clone(), |
| 836 | 111× | milestone: s.milestone.clone(), |
| 837 | 111× | opened_ms: s.opened_ms, |
| 838 | 111× | }); |
| 839 | | } |
| 840 | 0× | _ => {} |
| 841 | | } |
| 842 | | } |
| 843 | 273× | let total = views.len(); |
| 844 | 273× | let archived = archived_in(column.id); |
| 845 | 352× | let items: Vec<&CardView> = views.iter().filter(|v| v.card.kind != "note").collect(); |
| 846 | 273× | let finished = !items.is_empty() && items.iter().all(|v| matches!(v.state.as_str(), "closed" | "landed")); |
| 847 | 273× | columns.push(ColumnView { column, cards: views, hidden: 0, total, archived, finished, over_wip: false, before_iteration: 0 }); |
| 848 | | } |
| 849 | 94× | let mut repos: Vec<String> = readable.values().map(|r| r.name.clone()).collect(); |
| 850 | 92× | repos.sort(); |
| 851 | 92× | Ok((columns, repos)) |
| 852 | 92× | } |
| 853 | |
|
| 854 | |
|
| 855 | | struct Shown { |
| 856 | | title: String, |
| 857 | | state: String, |
| 858 | | labels: Vec<idb::Label>, |
| 859 | | assignees: Vec<String>, |
| 860 | | milestone: String, |
| 861 | | opened_ms: i64, |
| 862 | | } |
| 863 | |
|
| 864 | |
|
| 865 | 81× | async fn shown_in(app: &Shared, repo_id: i64, issues: bool) -> sqlx::Result<Vec<(i64, Shown)>> { |
| 866 | | use std::collections::HashMap; |
| 867 | 81× | let mut out = Vec::new(); |
| 868 | 81× | if issues { |
| 869 | 70× | let mut labels: HashMap<i64, Vec<idb::Label>> = HashMap::new(); |
| 870 | 70× | for (id, l) in idb::labels_by_issue(&app.db, repo_id).await? { |
| 871 | 18× | labels.entry(id).or_default().push(l); |
| 872 | 18× | } |
| 873 | 70× | let mut assignees: HashMap<i64, Vec<String>> = HashMap::new(); |
| 874 | 70× | for (id, u) in idb::assignees_by_issue(&app.db, repo_id).await? { |
| 875 | 28× | assignees.entry(id).or_default().push(u); |
| 876 | 28× | } |
| 877 | 70× | let milestones: HashMap<i64, String> = idb::milestones(&app.db, repo_id).await?.into_iter().map(|m| (m.id, m.title)).collect(); |
| 878 | 143× | for i in idb::list(&app.db, repo_id).await? { |
| 879 | 143× | let mut a = assignees.remove(&i.id).unwrap_or_default(); |
| 880 | 143× | a.sort(); |
| 881 | 143× | let milestone = i.milestone_id.and_then(|m| milestones.get(&m).cloned()).unwrap_or_default(); |
| 882 | 143× | let s = Shown { |
| 883 | 143× | labels: labels.remove(&i.id).unwrap_or_default(), |
| 884 | 143× | assignees: a, |
| 885 | 143× | title: i.title, |
| 886 | 143× | state: i.state, |
| 887 | 143× | milestone, |
| 888 | 143× | opened_ms: i.created_ms, |
| 889 | 143× | }; |
| 890 | 143× | out.push((i.number, s)); |
| 891 | | } |
| 892 | | } else { |
| 893 | 11× | let mut labels: HashMap<i64, Vec<idb::Label>> = HashMap::new(); |
| 894 | 11× | for (id, l) in idb::labels_by_proposal(&app.db, repo_id).await? { |
| 895 | 0× | labels.entry(id).or_default().push(l); |
| 896 | 0× | } |
| 897 | 11× | for p in db::proposals::list(&app.db, repo_id, &[]).await? { |
| 898 | 11× | let s = Shown { |
| 899 | 11× | labels: labels.remove(&p.id).unwrap_or_default(), |
| 900 | 11× | assignees: Vec::new(), |
| 901 | 11× | title: p.title, |
| 902 | 11× | state: p.state, |
| 903 | 11× | milestone: String::new(), |
| 904 | 11× | opened_ms: p.created_ms, |
| 905 | 11× | }; |
| 906 | 11× | out.push((p.number, s)); |
| 907 | 11× | } |
| 908 | | } |
| 909 | 81× | Ok(out) |
| 910 | 81× | } |
| 911 | |
|
| 912 | 39× | pub async fn show( |
| 913 | 39× | State(app): State<Shared>, |
| 914 | 39× | viewer: Viewer, |
| 915 | 39× | Path((owner, number)): Path<(String, i64)>, |
| 916 | 39× | Query(view): Query<BoardQuery>, |
| 917 | 39× | ) -> Result<Response, Error> { |
| 918 | 39× | let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| 919 | | |
| 920 | 39× | let e = |e| Error::from_db(shell.clone(), e); |
| 921 | 39× | if pdb::get(&app.db, &owner, number).await.map_err(e)?.is_none() { |
| 922 | 2× | if let Some(p) = pdb::redirected(&app.db, &owner, number).await.map_err(e)? { |
| 923 | 1× | return Ok(Redirect::permanent(&format!("/projects/{}/{}", p.owner, p.number)).into_response()); |
| 924 | 1× | } |
| 925 | 37× | } |
| 926 | 38× | Ok(board(&app, shell, &viewer, &owner, number, None, None, &view).await?.into_response()) |
| 927 | 39× | } |
| 928 | |
|
| 929 | | #[derive(Deserialize)] |
| 930 | | pub struct BoardForm { |
| 931 | | #[serde(rename = "_csrf")] |
| 932 | | csrf_token: String, |
| 933 | | action: String, |
| 934 | | #[serde(default)] |
| 935 | | card: Option<i64>, |
| 936 | | #[serde(default)] |
| 937 | | column: Option<i64>, |
| 938 | | #[serde(default)] |
| 939 | | name: String, |
| 940 | | |
| 941 | | #[serde(default)] |
| 942 | | reference: String, |
| 943 | | #[serde(default)] |
| 944 | | repo: String, |
| 945 | | #[serde(default)] |
| 946 | | note: String, |
| 947 | | |
| 948 | | #[serde(default)] |
| 949 | | mode: String, |
| 950 | | #[serde(default)] |
| 951 | | title: String, |
| 952 | | #[serde(default)] |
| 953 | | description: String, |
| 954 | | #[serde(default)] |
| 955 | | private: Option<String>, |
| 956 | | #[serde(default)] |
| 957 | | closed: Option<String>, |
| 958 | | |
| 959 | | #[serde(default)] |
| 960 | | to: String, |
| 961 | | |
| 962 | | #[serde(default)] |
| 963 | | days: String, |
| 964 | | |
| 965 | | |
| 966 | | #[serde(default)] |
| 967 | | iteration: Option<i64>, |
| 968 | | #[serde(default)] |
| 969 | | start: String, |
| 970 | | #[serde(default)] |
| 971 | | end: String, |
| 972 | | |
| 973 | | #[serde(default)] |
| 974 | | limit: String, |
| 975 | | |
| 976 | | |
| 977 | | #[serde(default)] |
| 978 | | layout: String, |
| 979 | | #[serde(default)] |
| 980 | | q: String, |
| 981 | | #[serde(default)] |
| 982 | | lane: String, |
| 983 | | #[serde(default)] |
| 984 | | sort: String, |
| 985 | | #[serde(default)] |
| 986 | | view: Option<i64>, |
| 987 | | } |
| 988 | |
|
| 989 | |
|
| 990 | |
|
| 991 | |
|
| 992 | |
|
| 993 | 26× | fn parse_reference(reference: &str, repo: &str, owner: &str) -> Result<(String, i64), String> { |
| 994 | 26× | let bad = || "Name an issue or proposal as repo#12, or write a note.".to_string(); |
| 995 | 26× | let r = reference.trim(); |
| 996 | 26× | let (name, n) = r.split_once('#').ok_or_else(bad)?; |
| 997 | 25× | let name = if name.is_empty() { repo.trim() } else { name.trim() }; |
| 998 | 25× | let name = match name.split_once('/') { |
| 999 | 4× | Some((o, rest)) if o.eq_ignore_ascii_case(owner) => rest, |
| 1000 | 2× | Some((o, _)) => return Err(format!("This board holds only {owner}'s issues and proposals, not {o}'s.")), |
| 1001 | 21× | None => name, |
| 1002 | | }; |
| 1003 | 23× | let n: i64 = n.trim().parse().map_err(|_| bad())?; |
| 1004 | 23× | if name.is_empty() || n <= 0 || name.contains('/') { |
| 1005 | 2× | return Err(bad()); |
| 1006 | 21× | } |
| 1007 | 21× | Ok((name.to_string(), n)) |
| 1008 | 26× | } |
| 1009 | |
|
| 1010 | |
|
| 1011 | |
|
| 1012 | |
|
| 1013 | | #[allow(clippy::too_many_arguments)] |
| 1014 | 22× | pub async fn add_card( |
| 1015 | 22× | app: &Shared, |
| 1016 | 22× | account: Option<&db::accounts::Account>, |
| 1017 | 22× | owner: &str, |
| 1018 | 22× | project_id: i64, |
| 1019 | 22× | column_id: i64, |
| 1020 | 22× | reference: &str, |
| 1021 | 22× | repo: &str, |
| 1022 | 22× | note: &str, |
| 1023 | 22× | actor: &str, |
| 1024 | 22× | ) -> sqlx::Result<Result<pdb::Card, String>> { |
| 1025 | 22× | if !note.trim().is_empty() { |
| 1026 | 4× | let note: String = note.trim().chars().take(500).collect(); |
| 1027 | 4× | pdb::add_card( |
| 1028 | 4× | &app.db, |
| 1029 | 4× | project_id, |
| 1030 | 4× | pdb::NewCard { column_id, kind: "note", repo_id: None, number: None, note: ¬e, added_by: actor }, |
| 1031 | 4× | ) |
| 1032 | 4× | .await?; |
| 1033 | 4× | return Ok(Ok(newest_card(app, project_id).await?)); |
| 1034 | 18× | } |
| 1035 | 18× | let (repo, n) = match parse_reference(reference, repo, owner) { |
| 1036 | 17× | Ok(r) => r, |
| 1037 | 1× | Err(why) => return Ok(Err(why)), |
| 1038 | | }; |
| 1039 | 17× | let Some(row) = db::repos::get(&app.db, owner, &repo).await? else { return Ok(Err(format!("{owner} has no repository {repo}."))) }; |
| 1040 | 16× | let role = roles::role_for(&app.db, account, &row).await?; |
| 1041 | 16× | let (issues, proposals) = db::issues::numbers(&app.db, row.id).await?; |
| 1042 | 16× | let kind = if issues.contains(&n) { |
| 1043 | 13× | "issue" |
| 1044 | 3× | } else if proposals.contains(&n) { |
| 1045 | 2× | "proposal" |
| 1046 | | } else { |
| 1047 | 1× | return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 1048 | | }; |
| 1049 | 15× | if !roles::allows(role, Access::Read) { |
| 1050 | 0× | return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 1051 | 15× | } |
| 1052 | 15× | if !pdb::add_card( |
| 1053 | 15× | &app.db, |
| 1054 | 15× | project_id, |
| 1055 | 15× | pdb::NewCard { column_id, kind, repo_id: Some(row.id), number: Some(n), note: "", added_by: actor }, |
| 1056 | 15× | ) |
| 1057 | 15× | .await? |
| 1058 | | { |
| 1059 | 4× | return Ok(Err(format!("{repo}#{n} is already on this project."))); |
| 1060 | 11× | } |
| 1061 | 11× | Ok(Ok(newest_card(app, project_id).await?)) |
| 1062 | 22× | } |
| 1063 | |
|
| 1064 | 15× | async fn newest_card(app: &Shared, project_id: i64) -> sqlx::Result<pdb::Card> { |
| 1065 | 15× | sqlx::query_as("SELECT * FROM project_cards WHERE project_id = $1 ORDER BY id DESC LIMIT 1").bind(project_id).fetch_one(&app.db).await |
| 1066 | 15× | } |
| 1067 | |
|
| 1068 | 42× | pub async fn act( |
| 1069 | 42× | State(app): State<Shared>, |
| 1070 | 42× | viewer: Viewer, |
| 1071 | 42× | Path((owner, number)): Path<(String, i64)>, |
| 1072 | 42× | Form(f): Form<BoardForm>, |
| 1073 | 42× | ) -> Result<Response, Error> { |
| 1074 | 42× | let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| 1075 | 42× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1076 | 42× | let r = rights(&app, &shell, &viewer, &owner).await?; |
| 1077 | 42× | let e = |e| Error::from_db(shell.clone(), e); |
| 1078 | 42× | let project = pdb::get(&app.db, &owner, number) |
| 1079 | 42× | .await |
| 1080 | 42× | .map_err(e)? |
| 1081 | 42× | .filter(|p| p.public || r.member) |
| 1082 | 42× | .ok_or_else(|| Error::not_found(shell.clone()))?; |
| 1083 | 42× | if !r.edit { |
| 1084 | 1× | return Err(Error::not_found(shell)); |
| 1085 | 41× | } |
| 1086 | 41× | if project.closed && !matches!(f.action.as_str(), "settings" | "delete" | "transfer") { |
| 1087 | 1× | return Ok(board( |
| 1088 | 1× | &app, |
| 1089 | 1× | shell, |
| 1090 | 1× | &viewer, |
| 1091 | 1× | &owner, |
| 1092 | 1× | number, |
| 1093 | 1× | None, |
| 1094 | 1× | Some("This project is closed; reopen it to change it.".into()), |
| 1095 | 1× | &BoardQuery::default(), |
| 1096 | 1× | ) |
| 1097 | 1× | .await? |
| 1098 | 1× | .into_response()); |
| 1099 | 40× | } |
| 1100 | 40× | let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 1101 | 40× | let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 1102 | 47× | let column = f.column.filter(|c| columns.iter().any(|x| x.id == *c)).or(columns.first().map(|c| c.id)); |
| 1103 | 40× | let mut notice = None; |
| 1104 | 40× | let mut error = None; |
| 1105 | 40× | match f.action.as_str() { |
| 1106 | 40× | "add" => { |
| 1107 | 11× | let Some(column_id) = column else { return Err(Error::bad(shell, "no column")) }; |
| 1108 | 3× | if let Err(msg) = |
| 1109 | 11× | add_card(&app, viewer.account(), &owner, project.id, column_id, &f.reference, &f.repo, &f.note, &actor).await.map_err(e)? |
| 1110 | 3× | { |
| 1111 | 3× | error = Some(msg); |
| 1112 | 8× | } |
| 1113 | | } |
| 1114 | 29× | "move" => { |
| 1115 | 1× | if let (Some(card), Some(col)) = (f.card, f.column) { |
| 1116 | 1× | pdb::move_card(&app.db, project.id, card, col).await.map_err(e)?; |
| 1117 | 0× | } |
| 1118 | | } |
| 1119 | 28× | "up" | "down" => { |
| 1120 | 0× | if let Some(card) = f.card { |
| 1121 | 0× | pdb::shift_card(&app.db, project.id, card, if f.action == "up" { -1 } else { 1 }).await.map_err(e)?; |
| 1122 | 0× | } |
| 1123 | | } |
| 1124 | 28× | "remove" => { |
| 1125 | 0× | if let Some(card) = f.card { |
| 1126 | 0× | pdb::remove_card(&app.db, project.id, card).await.map_err(e)?; |
| 1127 | 0× | } |
| 1128 | | } |
| 1129 | 28× | "archive" => { |
| 1130 | 1× | if let Some(card) = f.card { |
| 1131 | 1× | pdb::archive_cards(&app.db, project.id, &[card]).await.map_err(e)?; |
| 1132 | 0× | } |
| 1133 | | } |
| 1134 | 27× | "restore" => { |
| 1135 | 2× | if let Some(card) = f.card { |
| 1136 | 2× | pdb::restore_card(&app.db, project.id, card).await.map_err(e)?; |
| 1137 | 0× | } |
| 1138 | 2× | return Ok(Redirect::to(&format!("/projects/{owner}/{number}?archived=1")).into_response()); |
| 1139 | | } |
| 1140 | 25× | "takes_finished" => { |
| 1141 | 2× | let Some(col) = f.column else { return Err(Error::bad(shell, "no column")) }; |
| 1142 | 5× | let name = columns.iter().find(|c| c.id == col).map(|c| c.name.clone()).unwrap_or_default(); |
| 1143 | 2× | let on = f.mode == "on"; |
| 1144 | 2× | pdb::set_takes_finished(&app.db, project.id, on.then_some(col)).await.map_err(e)?; |
| 1145 | 2× | notice = Some(if on { |
| 1146 | 1× | format!("Cards move to {name} when their issue closes or their proposal lands or closes.") |
| 1147 | | } else { |
| 1148 | 1× | "Cards stay where they are when their issue closes or their proposal lands.".into() |
| 1149 | | }); |
| 1150 | | } |
| 1151 | 23× | "archive_older" | "archive_after" => { |
| 1152 | 5× | let Some(col) = f.column else { return Err(Error::bad(shell, "no column")) }; |
| 1153 | 5× | let days = match (f.action.as_str(), f.mode.as_str(), f.days.trim()) { |
| 1154 | 5× | ("archive_after", "finished", _) | ("archive_after", "", "") => None, |
| 1155 | 1× | ("archive_after", "never", _) => Some(pdb::NEVER), |
| 1156 | 3× | (_, _, d) => match d.parse::<i64>() { |
| 1157 | 2× | Ok(n) if (0..=3650).contains(&n) => Some(n), |
| 1158 | | _ => { |
| 1159 | 1× | error = Some("Give a number of days, 0 to 3650.".into()); |
| 1160 | 1× | None |
| 1161 | | } |
| 1162 | | }, |
| 1163 | | }; |
| 1164 | 15× | let name = columns.iter().find(|c| c.id == col).map(|c| c.name.clone()).unwrap_or_default(); |
| 1165 | 5× | if error.is_none() && f.action == "archive_after" { |
| 1166 | 3× | pdb::set_archive_after(&app.db, project.id, col, days).await.map_err(e)?; |
| 1167 | 3× | notice = Some(match days { |
| 1168 | 1× | Some(pdb::NEVER) => format!("{name} keeps its cards until you archive them."), |
| 1169 | 1× | Some(n) => format!("{name} archives its cards {n} day{} after they reach it.", if n == 1 { "" } else { "s" }), |
| 1170 | 1× | None => format!("{name} archives finished cards {} days after they reach it.", pdb::FINISHED_KEPT_DAYS), |
| 1171 | | }); |
| 1172 | 2× | } else if let (None, Some(days)) = (&error, days) { |
| 1173 | 1× | let now = db::now_ms(); |
| 1174 | 1× | let ids: Vec<i64> = pdb::cards(&app.db, project.id) |
| 1175 | 1× | .await |
| 1176 | 1× | .map_err(e)? |
| 1177 | 1× | .into_iter() |
| 1178 | 4× | .filter(|c| c.column_id == col && c.archived_ms.is_none() && pdb::older_than(c, days, now)) |
| 1179 | 1× | .map(|c| c.id) |
| 1180 | 1× | .collect(); |
| 1181 | 1× | let n = pdb::archive_cards(&app.db, project.id, &ids).await.map_err(e)?; |
| 1182 | 1× | let detail = format!("{n} card(s) of {name} older than {days} days"); |
| 1183 | 1× | db::accounts::audit(&app.db, viewer.account(), "project.cards_archived", &format!("{owner}/{number}"), &detail).await.ok(); |
| 1184 | 1× | notice = Some(format!("Archived {n} card{} from {name}.", if n == 1 { "" } else { "s" })); |
| 1185 | 1× | } |
| 1186 | | } |
| 1187 | 18× | "start_iteration" => { |
| 1188 | 4× | let name: String = f.name.trim().chars().take(60).collect(); |
| 1189 | 4× | match (day_ms(&f.start), day_ms(&f.end)) { |
| 1190 | 4× | _ if name.is_empty() => error = Some("Name the iteration.".into()), |
| 1191 | 3× | (Some(start), Some(last)) if last >= start && last - start < 366 * DAY_MS => { |
| 1192 | 2× | match pdb::start_iteration(&app.db, project.id, &name, start, last + DAY_MS).await.map_err(e)? { |
| 1193 | 1× | Some(_) => notice = Some(format!("{name} started.")), |
| 1194 | 1× | None => error = Some("Close the open iteration before starting another.".into()), |
| 1195 | | } |
| 1196 | | } |
| 1197 | 1× | (Some(_), Some(_)) => error = Some("An iteration ends on or after its first day, within a year.".into()), |
| 1198 | 1× | _ => error = Some("Give the first and last days as dates.".into()), |
| 1199 | | } |
| 1200 | | } |
| 1201 | 14× | "close_iteration" => { |
| 1202 | 2× | let Some(id) = f.iteration else { return Err(Error::bad(shell, "no iteration")) }; |
| 1203 | 2× | match pdb::close_iteration(&app.db, project.id, id, pdb::done_column(&columns)).await.map_err(e)? { |
| 1204 | 1× | Some(n) => { |
| 1205 | 1× | let detail = format!("iteration {id}: {n} done card(s) archived"); |
| 1206 | 1× | db::accounts::audit(&app.db, viewer.account(), "project.iteration_closed", &format!("{owner}/{number}"), &detail) |
| 1207 | 1× | .await |
| 1208 | 1× | .ok(); |
| 1209 | 1× | notice = Some(format!("Closed: {n} done card{} archived under it.", if n == 1 { "" } else { "s" })); |
| 1210 | | } |
| 1211 | 1× | None => error = Some("That iteration is not open.".into()), |
| 1212 | | } |
| 1213 | | } |
| 1214 | 12× | "wip" => { |
| 1215 | 2× | let limit = f.limit.trim(); |
| 1216 | 2× | match (column, limit.parse::<i64>()) { |
| 1217 | 2× | (Some(col), _) if limit.is_empty() => pdb::set_wip_limit(&app.db, project.id, col, None).await.map_err(e)?, |
| 1218 | 1× | (Some(col), Ok(n)) if (0..=999).contains(&n) => pdb::set_wip_limit(&app.db, project.id, col, Some(n)).await.map_err(e)?, |
| 1219 | 0× | _ => error = Some("A work-in-progress limit is a number of cards, 0 to 999, or empty for none.".into()), |
| 1220 | | } |
| 1221 | | } |
| 1222 | 10× | "save_view" => { |
| 1223 | 1× | let name: String = f.name.trim().chars().take(40).collect(); |
| 1224 | 1× | let layout = if f.layout == "table" { "table" } else { "board" }; |
| 1225 | 1× | let lane = if matches!(f.lane.as_str(), "repo" | "assignee" | "label" | "milestone") { f.lane.as_str() } else { "" }; |
| 1226 | 1× | let sort: String = f.sort.trim().chars().take(20).collect(); |
| 1227 | 1× | let q: String = f.q.trim().chars().take(300).collect(); |
| 1228 | 1× | if name.is_empty() { |
| 1229 | 0× | error = Some("Name the view.".into()); |
| 1230 | 0× | } else { |
| 1231 | 1× | pdb::save_view(&app.db, project.id, &name, layout, &q, lane, &sort).await.map_err(e)?; |
| 1232 | 1× | return Ok(Redirect::to(&format!("/projects/{owner}/{number}?view={}", super::settings::urlencode(&name))).into_response()); |
| 1233 | | } |
| 1234 | | } |
| 1235 | 9× | "delete_view" => { |
| 1236 | 1× | if let Some(id) = f.view { |
| 1237 | 1× | pdb::delete_view(&app.db, project.id, id).await.map_err(e)?; |
| 1238 | 0× | } |
| 1239 | | } |
| 1240 | 8× | "add_column" => { |
| 1241 | 0× | let name = f.name.trim(); |
| 1242 | 0× | if name.is_empty() || name.len() > 40 { |
| 1243 | 0× | error = Some("A column has a name of up to 40 characters.".into()); |
| 1244 | 0× | } else { |
| 1245 | 0× | pdb::add_column(&app.db, project.id, name).await.map_err(e)?; |
| 1246 | | } |
| 1247 | | } |
| 1248 | 8× | "rename_column" => { |
| 1249 | 0× | if let (Some(col), name) = (f.column, f.name.trim()) { |
| 1250 | 0× | if !name.is_empty() && name.len() <= 40 { |
| 1251 | 0× | pdb::rename_column(&app.db, project.id, col, name).await.map_err(e)?; |
| 1252 | 0× | } |
| 1253 | 0× | } |
| 1254 | | } |
| 1255 | 8× | "left" | "right" => { |
| 1256 | 0× | if let Some(col) = f.column { |
| 1257 | 0× | pdb::shift_column(&app.db, project.id, col, if f.action == "left" { -1 } else { 1 }).await.map_err(e)?; |
| 1258 | 0× | } |
| 1259 | | } |
| 1260 | 8× | "remove_column" => { |
| 1261 | 3× | if let Some(col) = f.column { |
| 1262 | 3× | if !pdb::remove_column(&app.db, project.id, col).await.map_err(e)? { |
| 1263 | 1× | error = Some("A project keeps at least one column.".into()); |
| 1264 | 2× | } |
| 1265 | 0× | } |
| 1266 | | } |
| 1267 | 5× | "settings" => { |
| 1268 | 2× | let title = if f.title.trim().is_empty() { project.title.clone() } else { f.title.trim().chars().take(100).collect() }; |
| 1269 | 2× | pdb::set(&app.db, project.id, &title, f.description.trim(), f.private.is_none(), f.closed.is_some()).await.map_err(e)?; |
| 1270 | 2× | notice = Some("Saved.".into()); |
| 1271 | | } |
| 1272 | 3× | "transfer" => { |
| 1273 | 2× | let to = f.to.trim(); |
| 1274 | 2× | match rights_of(&app, viewer.account(), to).await.map_err(e)? { |
| 1275 | 2× | _ if to == owner => error = Some(format!("It is {owner}'s already.")), |
| 1276 | 1× | Some(t) if t.edit => { |
| 1277 | 1× | let moved = pdb::transfer(&app.db, project.id, to).await.map_err(e)?; |
| 1278 | 1× | let detail = format!("{owner}/{number} to {to}/{}", moved.number); |
| 1279 | 1× | db::accounts::audit(&app.db, viewer.account(), "project.transferred", &detail, &project.title).await.ok(); |
| 1280 | 1× | return Ok(Redirect::to(&format!("/projects/{to}/{}", moved.number)).into_response()); |
| 1281 | | } |
| 1282 | 1× | _ => error = Some(format!("You cannot make projects under {to}, so you cannot move this one there.")), |
| 1283 | | } |
| 1284 | | } |
| 1285 | 1× | "delete" => { |
| 1286 | 1× | pdb::delete(&app.db, project.id).await.map_err(e)?; |
| 1287 | 1× | db::accounts::audit(&app.db, viewer.account(), "project.deleted", &format!("{owner}/{number}"), &project.title).await.ok(); |
| 1288 | 1× | return Ok(Redirect::to(&format!("/projects/{owner}")).into_response()); |
| 1289 | | } |
| 1290 | 0× | _ => return Err(Error::bad(shell, "unknown action")), |
| 1291 | | } |
| 1292 | 35× | if notice.is_none() && error.is_none() { |
| 1293 | | |
| 1294 | 15× | return Ok(Redirect::to(&format!("/projects/{owner}/{number}")).into_response()); |
| 1295 | 20× | } |
| 1296 | 20× | Ok(board(&app, shell, &viewer, &owner, number, notice, error, &BoardQuery::default()).await?.into_response()) |
| 1297 | 42× | } |
| 1298 | |
|
| 1299 | |
|
| 1300 | |
|
| 1301 | 106× | pub async fn placements_for(app: &Shared, viewer: &Viewer, repo: &db::repos::RepoRow, number: i64) -> Vec<(pdb::Project, String)> { |
| 1302 | 106× | let Ok(all) = pdb::placements(&app.db, repo.id, number).await else { return vec![] }; |
| 1303 | 106× | let mut out = Vec::new(); |
| 1304 | 106× | for (p, col) in all { |
| 1305 | 5× | let visible = p.public |
| 1306 | 1× | || viewer.is_admin() |
| 1307 | 1× | || match viewer.account() { |
| 1308 | 0× | Some(v) if v.username == p.owner => true, |
| 1309 | 0× | Some(v) => match db::accounts::org_by_name(&app.db, &p.owner).await.ok().flatten() { |
| 1310 | 0× | Some(o) => db::accounts::org_role(&app.db, o.id, v.id).await.ok().flatten().is_some(), |
| 1311 | 0× | None => false, |
| 1312 | | }, |
| 1313 | 1× | None => false, |
| 1314 | | }; |
| 1315 | 5× | if visible { |
| 1316 | 4× | out.push((p, col)); |
| 1317 | 4× | } |
| 1318 | | } |
| 1319 | 106× | out |
| 1320 | 106× | } |
| 1321 | |
|
| 1322 | | #[cfg(test)] |
| 1323 | | mod tests { |
| 1324 | | use super::parse_reference; |
| 1325 | |
|
| 1326 | | #[test] |
| 1327 | 1× | fn references() { |
| 1328 | 7× | let p = |r: &str, repo: &str| parse_reference(r, repo, "andy").ok(); |
| 1329 | 1× | assert_eq!(p("oxygen#12", ""), Some(("oxygen".into(), 12))); |
| 1330 | 1× | assert_eq!(p("#7", "hub"), Some(("hub".into(), 7))); |
| 1331 | 1× | assert_eq!(p(" hub # 3 ", ""), Some(("hub".into(), 3))); |
| 1332 | 1× | assert_eq!(p("#7", ""), None, "a repository is needed"); |
| 1333 | 1× | assert_eq!(p("andy/hub#4", ""), Some(("hub".into(), 4)), "the board's own owner, as it shows cards"); |
| 1334 | 1× | assert!(parse_reference("other/repo#1", "", "andy").unwrap_err().contains("only andy's"), "another owner is refused, saying why"); |
| 1335 | 1× | assert_eq!(p("hub#0", ""), None); |
| 1336 | 1× | assert_eq!(p("hub", ""), None); |
| 1337 | 1× | } |
| 1338 | | } |
| 1339 | |
|
| 1340 | | #[derive(Deserialize)] |
| 1341 | | pub struct PlaceForm { |
| 1342 | | #[serde(rename = "_csrf")] |
| 1343 | | csrf_token: String, |
| 1344 | | |
| 1345 | | project: i64, |
| 1346 | | |
| 1347 | | action: String, |
| 1348 | | } |
| 1349 | |
|
| 1350 | |
|
| 1351 | |
|
| 1352 | 2× | async fn place( |
| 1353 | 2× | app: Shared, |
| 1354 | 2× | viewer: Viewer, |
| 1355 | 2× | owner: String, |
| 1356 | 2× | name: String, |
| 1357 | 2× | kind: &'static str, |
| 1358 | 2× | number: i64, |
| 1359 | 2× | f: PlaceForm, |
| 1360 | 2× | ) -> Result<Response, Error> { |
| 1361 | 2× | let page = format!("/{owner}/{name}/{kind}/{number}"); |
| 1362 | 2× | let shell = Shell::new(&app, &page, &viewer); |
| 1363 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1364 | 2× | let (_, row, _) = super::lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 1365 | 2× | let r = rights(&app, &shell, &viewer, &row.owner).await?; |
| 1366 | 2× | if !r.edit { |
| 1367 | 0× | return Err(Error::not_found(shell)); |
| 1368 | 2× | } |
| 1369 | 2× | let e = |e| Error::from_db(shell.clone(), e); |
| 1370 | 2× | let project = pdb::get(&app.db, &row.owner, f.project).await.map_err(e)?.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 1371 | 2× | let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 1372 | 2× | let mut error = None; |
| 1373 | 2× | match f.action.as_str() { |
| 1374 | 2× | "add" => { |
| 1375 | 1× | let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 1376 | 1× | let Some(first) = columns.first() else { return Err(Error::bad(shell, "that project has no columns")) }; |
| 1377 | 0× | if let Err(msg) = |
| 1378 | 1× | add_card(&app, viewer.account(), &row.owner, project.id, first.id, &format!("{}#{number}", row.name), &row.name, "", &actor) |
| 1379 | 1× | .await |
| 1380 | 1× | .map_err(e)? |
| 1381 | 0× | { |
| 1382 | 0× | error = Some(msg); |
| 1383 | 1× | } |
| 1384 | | } |
| 1385 | 1× | "remove" => { |
| 1386 | 1× | for c in pdb::cards(&app.db, project.id).await.map_err(e)? { |
| 1387 | 1× | if c.repo_id == Some(row.id) && c.number == Some(number) { |
| 1388 | 1× | pdb::remove_card(&app.db, project.id, c.id).await.map_err(e)?; |
| 1389 | 0× | } |
| 1390 | | } |
| 1391 | | } |
| 1392 | 0× | _ => return Err(Error::bad(shell, "add or remove")), |
| 1393 | | } |
| 1394 | 2× | let to = match error { |
| 1395 | 0× | Some(m) => format!("{page}?error={}", super::settings::urlencode(&m)), |
| 1396 | 2× | None => page, |
| 1397 | | }; |
| 1398 | 2× | Ok(Redirect::to(&to).into_response()) |
| 1399 | 2× | } |
| 1400 | |
|
| 1401 | |
|
| 1402 | 2× | pub async fn place_proposal( |
| 1403 | 2× | State(app): State<Shared>, |
| 1404 | 2× | viewer: Viewer, |
| 1405 | 2× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 1406 | 2× | Form(f): Form<PlaceForm>, |
| 1407 | 2× | ) -> Result<Response, Error> { |
| 1408 | 2× | place(app, viewer, owner, name, "proposal", number, f).await |
| 1409 | 2× | } |
| 1410 | |
|
| 1411 | |
|
| 1412 | 0× | pub async fn place_issue( |
| 1413 | 0× | State(app): State<Shared>, |
| 1414 | 0× | viewer: Viewer, |
| 1415 | 0× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 1416 | 0× | Form(f): Form<PlaceForm>, |
| 1417 | 0× | ) -> Result<Response, Error> { |
| 1418 | 0× | place(app, viewer, owner, name, "issue", number, f).await |
| 1419 | 0× | } |
| 1420 | |
|
| 1421 | |
|
| 1422 | |
|
| 1423 | 106× | pub async fn project_choices( |
| 1424 | 106× | app: &Shared, |
| 1425 | 106× | viewer: &Viewer, |
| 1426 | 106× | repo: &db::repos::RepoRow, |
| 1427 | 106× | placed: &[(pdb::Project, String)], |
| 1428 | 106× | ) -> Option<Vec<pdb::Project>> { |
| 1429 | 106× | let Ok(Some(r)) = rights_of(app, viewer.account(), &repo.owner).await else { return None }; |
| 1430 | 106× | if !r.edit { |
| 1431 | 45× | return None; |
| 1432 | 61× | } |
| 1433 | | Some( |
| 1434 | 61× | pdb::of_owner(&app.db, &repo.owner) |
| 1435 | 61× | .await |
| 1436 | 61× | .unwrap_or_default() |
| 1437 | 61× | .into_iter() |
| 1438 | 61× | .filter(|p| !p.closed && !placed.iter().any(|(q, _)| q.id == p.id)) |
| 1439 | 61× | .collect(), |
| 1440 | | ) |
| 1441 | 106× | } |
| 1442 | |
|
| 1443 | | #[cfg(test)] |
| 1444 | | mod view_tests { |
| 1445 | | use super::*; |
| 1446 | |
|
| 1447 | | const NOW: i64 = 100 * DAY_MS; |
| 1448 | |
|
| 1449 | | #[allow(clippy::too_many_arguments)] |
| 1450 | 166× | fn card( |
| 1451 | 166× | id: i64, |
| 1452 | 166× | title: &str, |
| 1453 | 166× | repo: &str, |
| 1454 | 166× | state: &str, |
| 1455 | 166× | assignees: &[&str], |
| 1456 | 166× | labels: &[&str], |
| 1457 | 166× | milestone: &str, |
| 1458 | 166× | age_days: i64, |
| 1459 | 166× | ) -> CardView { |
| 1460 | | CardView { |
| 1461 | 166× | card: pdb::Card { |
| 1462 | 166× | id, |
| 1463 | 166× | project_id: 1, |
| 1464 | 166× | column_id: 1, |
| 1465 | 166× | position: id, |
| 1466 | 166× | kind: "issue".into(), |
| 1467 | 166× | repo_id: Some(1), |
| 1468 | 166× | number: Some(id), |
| 1469 | 166× | note: String::new(), |
| 1470 | 166× | added_by: String::new(), |
| 1471 | 166× | created_ms: 0, |
| 1472 | 166× | archived_ms: None, |
| 1473 | 166× | moved_ms: NOW - age_days * DAY_MS, |
| 1474 | 166× | iteration_id: None, |
| 1475 | 166× | }, |
| 1476 | 166× | reference: format!("andy/{repo}#{id}"), |
| 1477 | 166× | url: format!("/andy/{repo}/issue/{id}"), |
| 1478 | 166× | title: title.into(), |
| 1479 | 166× | state: state.into(), |
| 1480 | 166× | labels: labels |
| 1481 | 166× | .iter() |
| 1482 | 166× | .map(|l| idb::Label { id: 0, repo_id: 1, name: (*l).into(), colour: "#123456".into(), description: String::new() }) |
| 1483 | 166× | .collect(), |
| 1484 | 166× | assignees: assignees.iter().map(|a| (*a).to_string()).collect(), |
| 1485 | 166× | repo: repo.into(), |
| 1486 | 166× | milestone: milestone.into(), |
| 1487 | 166× | opened_ms: NOW - age_days * DAY_MS, |
| 1488 | | } |
| 1489 | 166× | } |
| 1490 | |
|
| 1491 | 31× | fn column(id: i64, name: &str, wip: Option<i64>, cards: Vec<CardView>) -> ColumnView { |
| 1492 | 31× | ColumnView { |
| 1493 | 31× | column: pdb::Column { |
| 1494 | 31× | id, |
| 1495 | 31× | project_id: 1, |
| 1496 | 31× | name: name.into(), |
| 1497 | 31× | position: id, |
| 1498 | 31× | archive_after_days: None, |
| 1499 | 31× | takes_finished: name == "Done", |
| 1500 | 31× | wip_limit: wip, |
| 1501 | 31× | }, |
| 1502 | 31× | total: cards.len(), |
| 1503 | 31× | cards, |
| 1504 | 31× | hidden: 0, |
| 1505 | 31× | archived: 0, |
| 1506 | 31× | finished: false, |
| 1507 | 31× | over_wip: false, |
| 1508 | 31× | before_iteration: 0, |
| 1509 | 31× | } |
| 1510 | 31× | } |
| 1511 | |
|
| 1512 | 15× | fn board() -> Vec<ColumnView> { |
| 1513 | 15× | vec![ |
| 1514 | 15× | column( |
| 1515 | | 1, |
| 1516 | 15× | "Doing", |
| 1517 | 15× | Some(1), |
| 1518 | 15× | vec![ |
| 1519 | 15× | card(1, "Crash on start", "oxy", "open", &["andy"], &["bug"], "Release 1", 20), |
| 1520 | 15× | card(2, "Faster pull", "hub", "open", &[], &["perf", "bug"], "", 3), |
| 1521 | | ], |
| 1522 | | ), |
| 1523 | 15× | column(2, "Done", None, vec![card(3, "Docs", "oxy", "closed", &["kim"], &[], "Release 1", 40)]), |
| 1524 | | ] |
| 1525 | 15× | } |
| 1526 | |
|
| 1527 | 12× | fn keeps(q: &str) -> Vec<i64> { |
| 1528 | 12× | let f = CardFilter::parse(q); |
| 1529 | 36× | board().iter().flat_map(|c| c.cards.iter()).filter(|c| f.keeps(c, NOW)).map(|c| c.card.id).collect() |
| 1530 | 12× | } |
| 1531 | |
|
| 1532 | | #[test] |
| 1533 | 1× | fn the_filter_bar_reads_every_term() { |
| 1534 | 1× | assert_eq!(keeps(""), [1, 2, 3]); |
| 1535 | 1× | assert_eq!(keeps("repo:oxy"), [1, 3]); |
| 1536 | 1× | assert_eq!(keeps("repo:andy/hub"), [2]); |
| 1537 | 1× | assert_eq!(keeps("assignee:@andy"), [1]); |
| 1538 | 1× | assert_eq!(keeps("label:bug label:perf"), [2]); |
| 1539 | 1× | assert_eq!(keeps(r#"milestone:"release 1""#), [1, 3]); |
| 1540 | 1× | assert_eq!(keeps("is:open"), [1, 2]); |
| 1541 | 1× | assert_eq!(keeps("is:closed"), [3]); |
| 1542 | 1× | assert_eq!(keeps("age:>14d"), [1, 3]); |
| 1543 | 1× | assert_eq!(keeps("age:<7"), [2]); |
| 1544 | 1× | assert_eq!(keeps("crash age:>14d"), [1]); |
| 1545 | 1× | assert_eq!(keeps("age:soon"), Vec::<i64>::new(), "an unreadable term is a word, which matches nothing"); |
| 1546 | 1× | assert!(CardFilter::parse(" ").is_empty()); |
| 1547 | 1× | } |
| 1548 | |
|
| 1549 | | #[test] |
| 1550 | 1× | fn wip_limits_and_this_iterations_done() { |
| 1551 | 1× | let mut cols = board(); |
| 1552 | 1× | cols[1].cards.push(card(4, "Old", "oxy", "closed", &[], &[], "", 1)); |
| 1553 | 1× | cols[1].cards[1].card.moved_ms = NOW - 30 * DAY_MS; |
| 1554 | 1× | let q = BoardQuery::default(); |
| 1555 | 1× | shape(&mut cols, &q, NOW, Some(2), Some(NOW - 50 * DAY_MS + 1)); |
| 1556 | 1× | assert!(cols[0].over_wip, "two cards over a limit of one"); |
| 1557 | 1× | assert!(!cols[1].over_wip); |
| 1558 | | |
| 1559 | | |
| 1560 | 1× | assert_eq!((cols[1].cards.len(), cols[1].before_iteration), (2, 0)); |
| 1561 | 1× | let mut cols = board(); |
| 1562 | 1× | shape(&mut cols, &q, NOW, Some(2), Some(NOW - 10 * DAY_MS)); |
| 1563 | 1× | assert_eq!((cols[1].cards.len(), cols[1].before_iteration, cols[1].total), (0, 1, 0), "done before it began"); |
| 1564 | 1× | } |
| 1565 | |
|
| 1566 | | #[test] |
| 1567 | 1× | fn lanes_and_table_rows() { |
| 1568 | 1× | let cols = board(); |
| 1569 | 3× | let names: Vec<(String, usize)> = lanes(&cols, "label").into_iter().map(|l| (l.name, l.count)).collect(); |
| 1570 | 1× | assert_eq!(names, [("bug".to_string(), 2), ("perf".into(), 1), ("(no label)".into(), 1)]); |
| 1571 | 1× | let by_repo: Vec<String> = lanes(&cols, "repo").into_iter().map(|l| l.name).collect(); |
| 1572 | 1× | assert_eq!(by_repo, ["hub", "oxy"]); |
| 1573 | 1× | let (shown, pages) = super::rows(&cols, "", "title", 1); |
| 1574 | 1× | assert_eq!(pages, 1); |
| 1575 | 1× | assert_eq!(shown.iter().map(|r| r.c.card.id).collect::<Vec<_>>(), [1, 3, 2]); |
| 1576 | 1× | let (shown, _) = super::rows(&cols, "", "-age", 1); |
| 1577 | 1× | assert_eq!(shown.iter().map(|r| r.c.card.id).collect::<Vec<_>>(), [3, 1, 2], "oldest first"); |
| 1578 | 1× | let (shown, _) = super::rows(&cols, "assignee", "", 1); |
| 1579 | 3× | let groups: Vec<(&str, bool)> = shown.iter().map(|r| (r.group.as_str(), r.starts_group)).collect(); |
| 1580 | 1× | assert_eq!(groups, [("@andy", true), ("@kim", true), ("(nobody)", true)]); |
| 1581 | | |
| 1582 | 120× | let many = vec![column(1, "Doing", None, (0..120).map(|i| card(i, "x", "oxy", "open", &[], &[], "", 1)).collect())]; |
| 1583 | 1× | let (p3, pages) = super::rows(&many, "", "", 3); |
| 1584 | 1× | assert_eq!((p3.len(), pages), (20, 3)); |
| 1585 | 1× | } |
| 1586 | | } |