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