| 1 | |
|
| 2 | |
|
| 3 | | use super::settings::{csrf, require}; |
| 4 | | use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell}; |
| 5 | | use crate::auth::{Access, Role, Viewer}; |
| 6 | | use crate::db::{self, accounts, issues as idb, notify as ndb}; |
| 7 | | use crate::notify::{self, Event}; |
| 8 | | use crate::render::{self, refs::Numbers}; |
| 9 | | use crate::repos::view; |
| 10 | | use crate::Shared; |
| 11 | | use askama::Template; |
| 12 | | use axum::extract::{Form, Path, Query, State}; |
| 13 | | use axum::response::{IntoResponse, Redirect, Response}; |
| 14 | | use serde::Deserialize; |
| 15 | | use std::collections::HashMap; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | 142× | pub async fn markdown(app: &Shared, row: &db::repos::RepoRow, reference: &str, text: &str) -> String { |
| 21 | 142× | let repo_url = format!("/{}", row.slug()); |
| 22 | 142× | let (issues, proposals) = idb::numbers(&app.db, row.id).await.unwrap_or_default(); |
| 23 | 142× | let linked = render::refs::link(text, &Numbers { repo_url: &repo_url, issues: &issues, proposals: &proposals }); |
| 24 | 142× | render::markdown::render(&linked, &format!("{repo_url}/file/{reference}/"), &format!("{repo_url}/raw/{reference}/")) |
| 25 | 142× | } |
| 26 | |
|
| 27 | |
|
| 28 | 34× | pub async fn reindex(app: &Shared, repo_id: i64, issue: &idb::IssueRow) { |
| 29 | 34× | let comments = idb::comments(&app.db, issue.id).await.unwrap_or_default(); |
| 30 | 34× | let text = std::iter::once(issue.body.clone()).chain(comments.into_iter().map(|c| c.body)).collect::<Vec<_>>().join("\n\n"); |
| 31 | 34× | ndb::index(&app.db, "issue", repo_id, issue.id, issue.number, &issue.title, &text).await.ok(); |
| 32 | 34× | } |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | | pub struct IssueLine { |
| 37 | | pub issue: idb::IssueRow, |
| 38 | | pub labels: Vec<idb::Label>, |
| 39 | | pub assignees: Vec<String>, |
| 40 | | pub milestone: Option<String>, |
| 41 | | pub comments: i64, |
| 42 | | } |
| 43 | |
|
| 44 | | #[derive(Template)] |
| 45 | | #[template(path = "issues.html")] |
| 46 | | pub struct IssuesPage { |
| 47 | | shell: Shell, |
| 48 | | repo: RepoShell, |
| 49 | | rows: Vec<IssueLine>, |
| 50 | | state: String, |
| 51 | | q: String, |
| 52 | | counts: (usize, usize), |
| 53 | | sort: String, |
| 54 | | pager: super::Pager, |
| 55 | | } |
| 56 | |
|
| 57 | | #[derive(Deserialize)] |
| 58 | | pub struct ListQuery { |
| 59 | | state: Option<String>, |
| 60 | | q: Option<String>, |
| 61 | | sort: Option<String>, |
| 62 | | page: Option<usize>, |
| 63 | | } |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | | pub const SORTS: &[(&str, &str)] = |
| 68 | | &[("newest", "newest"), ("oldest", "oldest"), ("updated", "recently updated"), ("comments", "most comments")]; |
| 69 | |
|
| 70 | |
|
| 71 | | struct Filter { |
| 72 | | state: String, |
| 73 | | labels: Vec<String>, |
| 74 | | assignees: Vec<String>, |
| 75 | | authors: Vec<String>, |
| 76 | | milestone: Option<String>, |
| 77 | | words: Vec<String>, |
| 78 | | } |
| 79 | |
|
| 80 | 7× | fn parse_filter(state: Option<String>, q: &str) -> Filter { |
| 81 | 7× | let mut f = Filter { |
| 82 | 7× | state: state.unwrap_or_else(|| "open".into()), |
| 83 | 7× | labels: vec![], |
| 84 | 7× | assignees: vec![], |
| 85 | 7× | authors: vec![], |
| 86 | 7× | milestone: None, |
| 87 | 7× | words: vec![], |
| 88 | | }; |
| 89 | 7× | for tok in q.split_whitespace() { |
| 90 | 5× | match tok.split_once(':') { |
| 91 | 5× | Some(("label", v)) => f.labels.push(v.to_lowercase()), |
| 92 | 3× | Some(("assignee", v)) => f.assignees.push(v.to_lowercase()), |
| 93 | 2× | Some(("author", v)) => f.authors.push(v.to_lowercase()), |
| 94 | 1× | Some(("milestone", v)) => f.milestone = Some(v.to_lowercase()), |
| 95 | 0× | Some(("state", v)) => f.state = v.to_string(), |
| 96 | 0× | _ => f.words.push(tok.to_lowercase()), |
| 97 | | } |
| 98 | | } |
| 99 | 7× | f |
| 100 | 7× | } |
| 101 | |
|
| 102 | 7× | pub async fn list( |
| 103 | 7× | State(app): State<Shared>, |
| 104 | 7× | viewer: Viewer, |
| 105 | 7× | Path((owner, name)): Path<(String, String)>, |
| 106 | 7× | Query(q): Query<ListQuery>, |
| 107 | 7× | ) -> Result<Page<IssuesPage>, Error> { |
| 108 | 7× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issues"), &viewer); |
| 109 | 7× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 110 | 7× | let e = |e| Error::from_db(shell.clone(), e); |
| 111 | 7× | let all = idb::list(&app.db, row.id).await.map_err(e)?; |
| 112 | 123× | let counts = (all.iter().filter(|i| i.state == "open").count(), all.iter().filter(|i| i.state == "closed").count()); |
| 113 | 7× | let qtext = q.q.clone().unwrap_or_default(); |
| 114 | 7× | let f = parse_filter(q.state, &qtext); |
| 115 | 7× | let mut labels_by: HashMap<i64, Vec<idb::Label>> = HashMap::new(); |
| 116 | 7× | for (id, l) in idb::labels_by_issue(&app.db, row.id).await.map_err(e)? { |
| 117 | 3× | labels_by.entry(id).or_default().push(l); |
| 118 | 3× | } |
| 119 | 7× | let mut assignees_by: HashMap<i64, Vec<String>> = HashMap::new(); |
| 120 | 7× | for (id, u) in idb::assignees_by_issue(&app.db, row.id).await.map_err(e)? { |
| 121 | 3× | assignees_by.entry(id).or_default().push(u); |
| 122 | 3× | } |
| 123 | 7× | let milestones: HashMap<i64, String> = |
| 124 | 7× | idb::milestones(&app.db, row.id).await.map_err(e)?.into_iter().map(|m| (m.id, m.title)).collect(); |
| 125 | 7× | let mut rows = Vec::new(); |
| 126 | 123× | for i in all { |
| 127 | 123× | let labels = labels_by.remove(&i.id).unwrap_or_default(); |
| 128 | 123× | let assignees = assignees_by.remove(&i.id).unwrap_or_default(); |
| 129 | 123× | let milestone = i.milestone_id.and_then(|m| milestones.get(&m).cloned()); |
| 130 | 123× | let keep = (f.state == "all" || i.state == f.state) |
| 131 | 123× | && f.labels.iter().all(|l| labels.iter().any(|x| x.name.to_lowercase() == *l)) |
| 132 | 122× | && f.assignees.iter().all(|a| assignees.iter().any(|x| x.to_lowercase() == *a)) |
| 133 | 122× | && (f.authors.is_empty() || f.authors.contains(&i.author.to_lowercase())) |
| 134 | 102× | && f.milestone.as_ref().is_none_or(|m| milestone.as_ref().is_some_and(|x| x.to_lowercase() == *m)) |
| 135 | 102× | && f.words.iter().all(|w| i.title.to_lowercase().contains(w) || i.body.to_lowercase().contains(w)); |
| 136 | 123× | if keep { |
| 137 | 102× | let comments: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM issue_comments WHERE issue_id = $1") |
| 138 | 102× | .bind(i.id) |
| 139 | 102× | .fetch_one(&app.db) |
| 140 | 102× | .await |
| 141 | 102× | .map_err(e)?; |
| 142 | 102× | rows.push(IssueLine { issue: i, labels, assignees, milestone, comments }); |
| 143 | 21× | } |
| 144 | | } |
| 145 | 7× | let sort = q.sort.filter(|s| SORTS.iter().any(|(k, _)| k == s)).unwrap_or_else(|| "newest".into()); |
| 146 | 7× | match sort.as_str() { |
| 147 | 7× | "oldest" => rows.sort_by_key(|r| r.issue.number), |
| 148 | 6× | "updated" => rows.sort_by_key(|r| std::cmp::Reverse(r.issue.updated_ms)), |
| 149 | 6× | "comments" => rows.sort_by_key(|r| (std::cmp::Reverse(r.comments), std::cmp::Reverse(r.issue.number))), |
| 150 | 6× | _ => {} |
| 151 | | } |
| 152 | 7× | let pager = super::Pager::new(rows.len(), q.page, &[("state", &f.state), ("q", &qtext), ("sort", &sort)]); |
| 153 | 7× | let rows = pager.slice(rows); |
| 154 | 7× | Ok(Page(IssuesPage { |
| 155 | 7× | shell, |
| 156 | 7× | repo: RepoShell::new(&app, &row, "issues").with_role(role), |
| 157 | 7× | rows, |
| 158 | 7× | state: f.state, |
| 159 | 7× | q: qtext, |
| 160 | 7× | counts, |
| 161 | 7× | sort, |
| 162 | 7× | pager, |
| 163 | 7× | })) |
| 164 | 7× | } |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | | #[derive(Template)] |
| 169 | | #[template(path = "issue_new.html")] |
| 170 | | pub struct NewIssuePage { |
| 171 | | shell: Shell, |
| 172 | | repo: RepoShell, |
| 173 | | templates: Vec<(String, String)>, |
| 174 | | template: String, |
| 175 | | error: Option<String>, |
| 176 | | } |
| 177 | |
|
| 178 | | #[derive(Deserialize)] |
| 179 | | pub struct NewQuery { |
| 180 | | template: Option<String>, |
| 181 | | } |
| 182 | |
|
| 183 | |
|
| 184 | 6× | async fn issue_templates(app: &Shared, repo: &crate::repos::RepoName, default: &str) -> Vec<(String, String)> { |
| 185 | 6× | let default = default.to_string(); |
| 186 | 6× | app.repos |
| 187 | 6× | .read(repo, move |r, cache| { |
| 188 | 6× | let Ok(target) = view::resolve(r, cache, &default) else { return Ok(vec![]) }; |
| 189 | 6× | let loaded = cache.load(r, &target.heads)?; |
| 190 | 6× | let mut out = Vec::new(); |
| 191 | 6× | for (path, f) in &loaded.snapshot.files { |
| 192 | 3× | if let Some(rest) = path.strip_prefix(".oxyhub/ISSUE_TEMPLATE/") { |
| 193 | 0× | if let (Some(name), oxy_core::state::FileContent::Text { bytes, .. }) = (rest.strip_suffix(".md"), &f.content) { |
| 194 | 0× | out.push((name.to_string(), String::from_utf8_lossy(bytes).into_owned())); |
| 195 | 0× | } |
| 196 | 3× | } |
| 197 | | } |
| 198 | 6× | Ok(out) |
| 199 | 6× | }) |
| 200 | 6× | .await |
| 201 | 6× | .unwrap_or_default() |
| 202 | 6× | } |
| 203 | |
|
| 204 | 6× | pub async fn new_form( |
| 205 | 6× | State(app): State<Shared>, |
| 206 | 6× | viewer: Viewer, |
| 207 | 6× | Path((owner, name)): Path<(String, String)>, |
| 208 | 6× | Query(q): Query<NewQuery>, |
| 209 | 6× | ) -> Result<Page<NewIssuePage>, Error> { |
| 210 | 6× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issues/new"), &viewer); |
| 211 | 6× | require(&viewer, &shell)?; |
| 212 | 6× | let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 213 | 6× | let templates = issue_templates(&app, &repo, &row.default_bookmark).await; |
| 214 | 6× | let template = match q.template.as_deref() { |
| 215 | 0× | Some("blank") => " ".into(), |
| 216 | 0× | Some(t) => templates.iter().find(|(n, _)| n == t).map(|(_, b)| b.clone()).unwrap_or_default(), |
| 217 | 6× | None if templates.is_empty() => " ".into(), |
| 218 | 0× | None => String::new(), |
| 219 | | }; |
| 220 | 6× | Ok(Page(NewIssuePage { shell, repo: RepoShell::new(&app, &row, "issues").with_role(role), templates, template, error: None })) |
| 221 | 6× | } |
| 222 | |
|
| 223 | | #[derive(Deserialize)] |
| 224 | | pub struct NewIssueForm { |
| 225 | | #[serde(rename = "_csrf")] |
| 226 | | csrf_token: String, |
| 227 | | title: String, |
| 228 | | #[serde(default)] |
| 229 | | body: String, |
| 230 | | } |
| 231 | |
|
| 232 | 7× | pub async fn create( |
| 233 | 7× | State(app): State<Shared>, |
| 234 | 7× | viewer: Viewer, |
| 235 | 7× | Path((owner, name)): Path<(String, String)>, |
| 236 | 7× | Form(f): Form<NewIssueForm>, |
| 237 | 7× | ) -> Result<Response, Error> { |
| 238 | 7× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issues/new"), &viewer); |
| 239 | 7× | let me = require(&viewer, &shell)?; |
| 240 | 7× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 241 | 7× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 242 | 7× | super::writable(&row, &shell)?; |
| 243 | 6× | if f.title.trim().is_empty() { |
| 244 | 0× | return Ok(Page(NewIssuePage { |
| 245 | 0× | shell, |
| 246 | 0× | repo: RepoShell::new(&app, &row, "issues").with_role(role), |
| 247 | 0× | templates: vec![], |
| 248 | 0× | template: f.body, |
| 249 | 0× | error: Some("Give the issue a title.".into()), |
| 250 | 0× | }) |
| 251 | 0× | .into_response()); |
| 252 | 6× | } |
| 253 | 6× | let issue = idb::create( |
| 254 | 6× | &app.db, |
| 255 | 6× | idb::NewIssue { |
| 256 | 6× | repo_id: row.id, |
| 257 | 6× | title: f.title.trim(), |
| 258 | 6× | body: f.body.trim(), |
| 259 | 6× | author_id: Some(me.account.id), |
| 260 | 6× | author: &me.account.username, |
| 261 | 6× | }, |
| 262 | 6× | ) |
| 263 | 6× | .await |
| 264 | 6× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 265 | 6× | idb::add_event(&app.db, issue.id, &me.account.username, "opened", "").await.ok(); |
| 266 | 6× | reindex(&app, row.id, &issue).await; |
| 267 | 6× | let url = format!("/{owner}/{name}/issue/{}", issue.number); |
| 268 | 6× | notify::deliver( |
| 269 | 6× | &app, |
| 270 | 6× | Event { |
| 271 | 6× | kind: "issue.opened", |
| 272 | 6× | title: format!("{owner}/{name}#{}: {}", issue.number, issue.title), |
| 273 | 6× | url: url.clone(), |
| 274 | 6× | actor: &me.account, |
| 275 | 6× | repo_id: row.id, |
| 276 | 6× | thread: Some(("issue", issue.id)), |
| 277 | 6× | mention_text: &f.body, |
| 278 | 6× | direct: vec![], |
| 279 | 6× | }, |
| 280 | 6× | ) |
| 281 | 6× | .await; |
| 282 | 6× | crate::hooks::emit( |
| 283 | 6× | &app, |
| 284 | 6× | &row, |
| 285 | 6× | "issue.opened", |
| 286 | 6× | &me.account.username, |
| 287 | 6× | serde_json::json!({ "issue": { "number": issue.number, "title": issue.title, "body": issue.body } }), |
| 288 | 6× | ) |
| 289 | 6× | .await; |
| 290 | 6× | Ok(Redirect::to(&url).into_response()) |
| 291 | 7× | } |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | | pub enum Item { |
| 296 | | Comment(idb::IssueComment, String), |
| 297 | | Event(idb::IssueEvent), |
| 298 | | |
| 299 | | |
| 300 | | References(String, Vec<idb::IssueEvent>, bool), |
| 301 | | } |
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | 29× | fn group_references(timeline: Vec<(i64, Item)>) -> Vec<(i64, Item)> { |
| 307 | 29× | let day = |ms: i64| ms.div_euclid(86_400_000); |
| 308 | 29× | let mut out: Vec<(i64, Item)> = Vec::new(); |
| 309 | 29× | let mut run: Vec<idb::IssueEvent> = Vec::new(); |
| 310 | | |
| 311 | | |
| 312 | 29× | let mut seen = std::collections::HashSet::new(); |
| 313 | 130× | let flush = |run: &mut Vec<idb::IssueEvent>, out: &mut Vec<(i64, Item)>| match run.len() { |
| 314 | 123× | 0 => {} |
| 315 | 5× | 1 => { |
| 316 | 5× | let e = run.remove(0); |
| 317 | 5× | out.push((e.at_ms, Item::Event(e))); |
| 318 | 5× | } |
| 319 | 2× | _ => { |
| 320 | 2× | let events = std::mem::take(run); |
| 321 | 2× | let at = events[0].at_ms; |
| 322 | 2× | out.push((at, Item::References(crate::render::time::day(at as u64), events, false))); |
| 323 | 2× | } |
| 324 | 130× | }; |
| 325 | 112× | for (at, item) in timeline { |
| 326 | 94× | match item { |
| 327 | 94× | Item::Event(e) if e.event == "referenced by change" => { |
| 328 | 13× | if !seen.insert(e.detail.split(' ').next().unwrap_or("").to_string()) { |
| 329 | 2× | continue; |
| 330 | 11× | } |
| 331 | 11× | if run.last().is_some_and(|l| day(l.at_ms) != day(e.at_ms)) { |
| 332 | 2× | flush(&mut run, &mut out); |
| 333 | 9× | } |
| 334 | 11× | run.push(e); |
| 335 | | } |
| 336 | 99× | other => { |
| 337 | 99× | flush(&mut run, &mut out); |
| 338 | 99× | out.push((at, other)); |
| 339 | 99× | } |
| 340 | | } |
| 341 | | } |
| 342 | 29× | flush(&mut run, &mut out); |
| 343 | 104× | if let Some((_, Item::References(_, _, open))) = out.iter_mut().rev().find(|(_, i)| matches!(i, Item::References(..))) { |
| 344 | 2× | *open = true; |
| 345 | 27× | } |
| 346 | 29× | out |
| 347 | 29× | } |
| 348 | |
|
| 349 | | #[derive(Template)] |
| 350 | | #[template(path = "issue.html")] |
| 351 | | pub struct IssuePage { |
| 352 | | shell: Shell, |
| 353 | | repo: RepoShell, |
| 354 | | i: idb::IssueRow, |
| 355 | | body_html: String, |
| 356 | | timeline: Vec<Item>, |
| 357 | | comments: Vec<idb::IssueComment>, |
| 358 | | labels: Vec<idb::Label>, |
| 359 | | all_labels: Vec<idb::Label>, |
| 360 | | assignees: Vec<String>, |
| 361 | | |
| 362 | | assignable: Vec<String>, |
| 363 | | milestone: Option<idb::Milestone>, |
| 364 | | milestones: Vec<(idb::Milestone, bool)>, |
| 365 | | subscribed: bool, |
| 366 | | can_edit: bool, |
| 367 | | can_triage: bool, |
| 368 | | |
| 369 | | projects: Vec<(crate::db::projects::Project, String)>, |
| 370 | | |
| 371 | | closed_by: Vec<super::chain::Link>, |
| 372 | | |
| 373 | | project_choices: Option<Vec<crate::db::projects::Project>>, |
| 374 | | notice: Option<String>, |
| 375 | | error: Option<String>, |
| 376 | | } |
| 377 | |
|
| 378 | | impl IssuePage { |
| 379 | | |
| 380 | 29× | pub fn live_topic(&self) -> String { |
| 381 | 29× | crate::events::topic::issue(self.i.repo_id, self.i.number) |
| 382 | 29× | } |
| 383 | | } |
| 384 | |
|
| 385 | |
|
| 386 | | #[allow(clippy::too_many_arguments)] |
| 387 | 29× | async fn detail( |
| 388 | 29× | app: &Shared, |
| 389 | 29× | shell: Shell, |
| 390 | 29× | viewer: &Viewer, |
| 391 | 29× | owner: &str, |
| 392 | 29× | name: &str, |
| 393 | 29× | number: i64, |
| 394 | 29× | notice: Option<String>, |
| 395 | 29× | error: Option<String>, |
| 396 | 29× | ) -> Result<Page<IssuePage>, Error> { |
| 397 | 29× | let (_, row, role) = lookup(app, &shell, viewer, owner, name).await?; |
| 398 | 29× | let e = |e| Error::from_db(shell.clone(), e); |
| 399 | 29× | let i = idb::get(&app.db, row.id, number).await.map_err(e)?.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 400 | 29× | let body_html = markdown(app, &row, &row.default_bookmark, &i.body).await; |
| 401 | 29× | let comments = idb::comments(&app.db, i.id).await.map_err(e)?; |
| 402 | 29× | let events = idb::events(&app.db, i.id).await.map_err(e)?; |
| 403 | 29× | let mut timeline: Vec<(i64, Item)> = Vec::new(); |
| 404 | 29× | for c in &comments { |
| 405 | 18× | timeline.push((c.created_ms, Item::Comment(c.clone(), markdown(app, &row, &row.default_bookmark, &c.body).await))); |
| 406 | | } |
| 407 | 117× | for ev in events { |
| 408 | 117× | if ev.event != "opened" { |
| 409 | 94× | timeline.push((ev.at_ms, Item::Event(ev))); |
| 410 | 94× | } |
| 411 | | } |
| 412 | 29× | timeline.sort_by_key(|(t, _)| *t); |
| 413 | 29× | let timeline = group_references(timeline); |
| 414 | 29× | let labels = idb::issue_labels(&app.db, i.id).await.map_err(e)?; |
| 415 | 29× | let all_labels = idb::labels(&app.db, row.id).await.map_err(e)?; |
| 416 | 29× | let assignees = idb::assignees(&app.db, i.id).await.map_err(e)?; |
| 417 | 29× | let milestones: Vec<(idb::Milestone, bool)> = idb::milestones(&app.db, row.id) |
| 418 | 29× | .await |
| 419 | 29× | .map_err(e)? |
| 420 | 29× | .into_iter() |
| 421 | 29× | .map(|m| { |
| 422 | 20× | let sel = i.milestone_id == Some(m.id); |
| 423 | 20× | (m, sel) |
| 424 | 20× | }) |
| 425 | 29× | .collect(); |
| 426 | 29× | let milestone = match i.milestone_id { |
| 427 | 10× | Some(id) => idb::milestone(&app.db, id).await.map_err(e)?, |
| 428 | 19× | None => None, |
| 429 | | }; |
| 430 | 29× | let me = viewer.account(); |
| 431 | 29× | let subscribed = match me { |
| 432 | 17× | Some(a) => ndb::subscribed(&app.db, a.id, "issue", i.id).await.map_err(e)?, |
| 433 | 12× | None => false, |
| 434 | | }; |
| 435 | 29× | let can_triage = role >= Role::Triage; |
| 436 | 29× | let can_edit = me.is_some_and(|a| Some(a.id) == i.author_id) || can_triage; |
| 437 | 29× | let projects = super::projects::placements_for(app, viewer, &row, i.number).await; |
| 438 | 29× | let closed_by = super::chain::proposals_closing(app, &row, i.number).await; |
| 439 | 29× | let project_choices = super::projects::project_choices(app, viewer, &row, &projects).await; |
| 440 | 29× | let mut assignable = if can_triage { accounts::assignable(&app.db, row.id, &row.owner).await.map_err(e)? } else { Vec::new() }; |
| 441 | 29× | for a in &assignees { |
| 442 | 14× | if !assignable.iter().any(|x| x.eq_ignore_ascii_case(a)) { |
| 443 | 13× | assignable.push(a.clone()); |
| 444 | 13× | } |
| 445 | | } |
| 446 | | Ok(Page(IssuePage { |
| 447 | 29× | shell, |
| 448 | 29× | repo: RepoShell::new(app, &row, "issues").with_role(role), |
| 449 | 29× | i, |
| 450 | 29× | closed_by, |
| 451 | 29× | project_choices, |
| 452 | 29× | body_html, |
| 453 | 29× | timeline: timeline.into_iter().map(|(_, it)| it).collect(), |
| 454 | 29× | comments, |
| 455 | 29× | labels, |
| 456 | 29× | all_labels, |
| 457 | 29× | assignees, |
| 458 | 29× | assignable, |
| 459 | 29× | milestone, |
| 460 | 29× | milestones, |
| 461 | 29× | subscribed, |
| 462 | 29× | can_edit, |
| 463 | 29× | can_triage, |
| 464 | 29× | projects, |
| 465 | 29× | notice, |
| 466 | 29× | error, |
| 467 | | })) |
| 468 | 29× | } |
| 469 | |
|
| 470 | 18× | pub async fn show( |
| 471 | 18× | State(app): State<Shared>, |
| 472 | 18× | viewer: Viewer, |
| 473 | 18× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 474 | 18× | ) -> Result<Response, Error> { |
| 475 | 18× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 476 | | |
| 477 | 18× | let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 478 | 18× | if idb::get(&app.db, row.id, number).await.map_err(|e| Error::from_db(shell.clone(), e))?.is_none() |
| 479 | 1× | && db::proposals::get(&app.db, row.id, number).await.map_err(|e| Error::from_db(shell.clone(), e))?.is_some() |
| 480 | | { |
| 481 | 1× | return Ok(Redirect::to(&format!("/{owner}/{name}/proposal/{number}")).into_response()); |
| 482 | 17× | } |
| 483 | 17× | Ok(detail(&app, shell, &viewer, &owner, &name, number, None, None).await?.into_response()) |
| 484 | 18× | } |
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | 13× | async fn issue_for( |
| 489 | 13× | app: &Shared, |
| 490 | 13× | shell: &Shell, |
| 491 | 13× | viewer: &Viewer, |
| 492 | 13× | owner: &str, |
| 493 | 13× | name: &str, |
| 494 | 13× | number: i64, |
| 495 | 13× | access: Access, |
| 496 | 13× | ) -> Result<(db::repos::RepoRow, idb::IssueRow, Role), Error> { |
| 497 | 13× | let (_, row, role) = lookup_role(app, shell, viewer, owner, name, access).await?; |
| 498 | 13× | let i = idb::get(&app.db, row.id, number) |
| 499 | 13× | .await |
| 500 | 13× | .map_err(|e| Error::from_db(shell.clone(), e))? |
| 501 | 13× | .ok_or_else(|| Error::not_found(shell.clone()))?; |
| 502 | 13× | Ok((row, i, role)) |
| 503 | 13× | } |
| 504 | |
|
| 505 | | #[derive(Deserialize)] |
| 506 | | pub struct CommentForm { |
| 507 | | #[serde(rename = "_csrf")] |
| 508 | | csrf_token: String, |
| 509 | | body: String, |
| 510 | | #[serde(default)] |
| 511 | | close: Option<String>, |
| 512 | | #[serde(default)] |
| 513 | | reopen: Option<String>, |
| 514 | | } |
| 515 | |
|
| 516 | 5× | pub async fn comment( |
| 517 | 5× | State(app): State<Shared>, |
| 518 | 5× | viewer: Viewer, |
| 519 | 5× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 520 | 5× | Form(f): Form<CommentForm>, |
| 521 | 5× | ) -> Result<Page<IssuePage>, Error> { |
| 522 | 5× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 523 | 5× | let me = require(&viewer, &shell)?; |
| 524 | 5× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 525 | 5× | let (row, i, role) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 526 | 5× | if i.locked && role < Role::Triage { |
| 527 | 0× | return detail(&app, shell, &viewer, &owner, &name, number, None, Some("This issue is locked.".into())).await; |
| 528 | 5× | } |
| 529 | 5× | if f.body.trim().is_empty() { |
| 530 | 0× | return detail(&app, shell, &viewer, &owner, &name, number, None, Some("An empty comment says nothing.".into())).await; |
| 531 | 5× | } |
| 532 | 5× | let e = |e| Error::from_db(shell.clone(), e); |
| 533 | 5× | idb::add_comment(&app.db, i.id, Some(me.account.id), &me.account.username, f.body.trim()).await.map_err(e)?; |
| 534 | 5× | let url = format!("/{owner}/{name}/issue/{number}"); |
| 535 | 5× | let mut kind = "issue.comment"; |
| 536 | 5× | let may_close = Some(me.account.id) == i.author_id || role >= Role::Triage; |
| 537 | 5× | if f.close.is_some() && i.state == "open" && may_close { |
| 538 | 1× | idb::set_state(&app.db, i.id, "closed", "completed").await.map_err(e)?; |
| 539 | 1× | idb::add_event(&app.db, i.id, &me.account.username, "closed", "").await.ok(); |
| 540 | 1× | kind = "issue.closed"; |
| 541 | 4× | } else if f.reopen.is_some() && i.state == "closed" && may_close { |
| 542 | 0× | idb::set_state(&app.db, i.id, "open", "").await.map_err(e)?; |
| 543 | 0× | idb::add_event(&app.db, i.id, &me.account.username, "reopened", "").await.ok(); |
| 544 | 0× | kind = "issue.reopened"; |
| 545 | 4× | } |
| 546 | 5× | reindex(&app, row.id, &i).await; |
| 547 | 5× | let hook_event = if kind == "issue.comment" { "comment.issue" } else { kind }; |
| 548 | 5× | crate::hooks::emit( |
| 549 | 5× | &app, |
| 550 | 5× | &row, |
| 551 | 5× | hook_event, |
| 552 | 5× | &me.account.username, |
| 553 | 5× | serde_json::json!({ "number": number, "issue": { "number": number, "title": i.title }, "comment": { "body": f.body.trim() } }), |
| 554 | 5× | ) |
| 555 | 5× | .await; |
| 556 | 5× | let direct = i.author_id.into_iter().collect(); |
| 557 | 5× | notify::deliver( |
| 558 | 5× | &app, |
| 559 | 5× | Event { |
| 560 | 5× | kind, |
| 561 | 5× | title: format!("{owner}/{name}#{number}: {}", i.title), |
| 562 | 5× | url, |
| 563 | 5× | actor: &me.account, |
| 564 | 5× | repo_id: row.id, |
| 565 | 5× | thread: Some(("issue", i.id)), |
| 566 | 5× | mention_text: &f.body, |
| 567 | 5× | direct, |
| 568 | 5× | }, |
| 569 | 5× | ) |
| 570 | 5× | .await; |
| 571 | 5× | detail(&app, shell, &viewer, &owner, &name, number, None, None).await |
| 572 | 5× | } |
| 573 | |
|
| 574 | 0× | pub async fn delete_comment( |
| 575 | 0× | State(app): State<Shared>, |
| 576 | 0× | viewer: Viewer, |
| 577 | 0× | Path((owner, name, number, id)): Path<(String, String, i64, i64)>, |
| 578 | 0× | Form(f): Form<super::session::CsrfForm>, |
| 579 | 0× | ) -> Result<Page<IssuePage>, Error> { |
| 580 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 581 | 0× | let me = require(&viewer, &shell)?; |
| 582 | 0× | csrf(&viewer, &f.csrf, &shell)?; |
| 583 | 0× | let (row, i, role) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 584 | 0× | let c = idb::comments(&app.db, i.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.into_iter().find(|c| c.id == id); |
| 585 | 0× | if let Some(c) = c { |
| 586 | 0× | if c.author_id == Some(me.account.id) || role >= Role::Triage { |
| 587 | 0× | idb::delete_comment(&app.db, id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 588 | 0× | idb::add_event(&app.db, i.id, &me.account.username, "deleted a comment", "").await.ok(); |
| 589 | 0× | reindex(&app, row.id, &i).await; |
| 590 | 0× | } |
| 591 | 0× | } |
| 592 | 0× | detail(&app, shell, &viewer, &owner, &name, number, None, None).await |
| 593 | 0× | } |
| 594 | |
|
| 595 | | #[derive(Deserialize)] |
| 596 | | pub struct EditForm { |
| 597 | | #[serde(rename = "_csrf")] |
| 598 | | csrf_token: String, |
| 599 | | title: String, |
| 600 | | #[serde(default)] |
| 601 | | body: String, |
| 602 | | } |
| 603 | |
|
| 604 | 0× | pub async fn edit( |
| 605 | 0× | State(app): State<Shared>, |
| 606 | 0× | viewer: Viewer, |
| 607 | 0× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 608 | 0× | Form(f): Form<EditForm>, |
| 609 | 0× | ) -> Result<Page<IssuePage>, Error> { |
| 610 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 611 | 0× | let me = require(&viewer, &shell)?; |
| 612 | 0× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 613 | 0× | let (row, i, role) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 614 | 0× | if Some(me.account.id) != i.author_id && role < Role::Triage { |
| 615 | 0× | return Err(Error::not_found(shell)); |
| 616 | 0× | } |
| 617 | 0× | if f.title.trim().is_empty() { |
| 618 | 0× | return detail(&app, shell, &viewer, &owner, &name, number, None, Some("An issue keeps a title.".into())).await; |
| 619 | 0× | } |
| 620 | 0× | idb::edit(&app.db, i.id, f.title.trim(), f.body.trim()).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 621 | 0× | idb::add_event(&app.db, i.id, &me.account.username, "edited", "").await.ok(); |
| 622 | 0× | if let Ok(Some(fresh)) = idb::get(&app.db, row.id, number).await { |
| 623 | 0× | reindex(&app, row.id, &fresh).await; |
| 624 | 0× | } |
| 625 | 0× | detail(&app, shell, &viewer, &owner, &name, number, Some("Saved.".into()), None).await |
| 626 | 0× | } |
| 627 | |
|
| 628 | | #[derive(Deserialize)] |
| 629 | | pub struct StateForm { |
| 630 | | #[serde(rename = "_csrf")] |
| 631 | | csrf_token: String, |
| 632 | | action: String, |
| 633 | | } |
| 634 | |
|
| 635 | 3× | pub async fn state( |
| 636 | 3× | State(app): State<Shared>, |
| 637 | 3× | viewer: Viewer, |
| 638 | 3× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 639 | 3× | Form(f): Form<StateForm>, |
| 640 | 3× | ) -> Result<Page<IssuePage>, Error> { |
| 641 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 642 | 3× | let me = require(&viewer, &shell)?; |
| 643 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 644 | 3× | let (row, i, _) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 645 | 3× | let role = crate::auth::roles::role_for(&app.db, Some(&me.account), &row).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 646 | 3× | if !crate::auth::roles::allows(role, Access::Read) || (role < Some(Role::Triage) && Some(me.account.id) != i.author_id) { |
| 647 | 1× | return Err(Error::not_found(shell)); |
| 648 | 2× | } |
| 649 | 2× | let e = |e| Error::from_db(shell.clone(), e); |
| 650 | 2× | let (event, kind) = match f.action.as_str() { |
| 651 | 2× | "close" => { |
| 652 | 1× | idb::set_state(&app.db, i.id, "closed", "completed").await.map_err(e)?; |
| 653 | 1× | ("closed", "issue.closed") |
| 654 | | } |
| 655 | 1× | "not_planned" => { |
| 656 | 0× | idb::set_state(&app.db, i.id, "closed", "not_planned").await.map_err(e)?; |
| 657 | 0× | ("closed as not planned", "issue.closed") |
| 658 | | } |
| 659 | 1× | "reopen" => { |
| 660 | 0× | idb::set_state(&app.db, i.id, "open", "").await.map_err(e)?; |
| 661 | 0× | ("reopened", "issue.reopened") |
| 662 | | } |
| 663 | 1× | "lock" if role >= Some(Role::Triage) => { |
| 664 | 0× | idb::set_flags(&app.db, i.id, Some(true), None).await.map_err(e)?; |
| 665 | 0× | ("locked", "") |
| 666 | | } |
| 667 | 1× | "unlock" if role >= Some(Role::Triage) => { |
| 668 | 0× | idb::set_flags(&app.db, i.id, Some(false), None).await.map_err(e)?; |
| 669 | 0× | ("unlocked", "") |
| 670 | | } |
| 671 | 1× | "pin" if role >= Some(Role::Triage) => { |
| 672 | 1× | idb::set_flags(&app.db, i.id, None, Some(true)).await.map_err(e)?; |
| 673 | 1× | ("pinned", "") |
| 674 | | } |
| 675 | 0× | "unpin" if role >= Some(Role::Triage) => { |
| 676 | 0× | idb::set_flags(&app.db, i.id, None, Some(false)).await.map_err(e)?; |
| 677 | 0× | ("unpinned", "") |
| 678 | | } |
| 679 | 0× | _ => return Err(Error::bad(shell, "unknown action")), |
| 680 | | }; |
| 681 | 2× | idb::add_event(&app.db, i.id, &me.account.username, event, "").await.ok(); |
| 682 | 2× | if !kind.is_empty() { |
| 683 | 1× | crate::hooks::emit( |
| 684 | 1× | &app, |
| 685 | 1× | &row, |
| 686 | 1× | kind, |
| 687 | 1× | &me.account.username, |
| 688 | 1× | serde_json::json!({ "issue": { "number": number, "title": i.title }, "reason": event }), |
| 689 | 1× | ) |
| 690 | 1× | .await; |
| 691 | 1× | let direct = i.author_id.into_iter().collect(); |
| 692 | 1× | notify::deliver( |
| 693 | 1× | &app, |
| 694 | 1× | Event { |
| 695 | 1× | kind, |
| 696 | 1× | title: format!("{owner}/{name}#{number}: {}", i.title), |
| 697 | 1× | url: format!("/{owner}/{name}/issue/{number}"), |
| 698 | 1× | actor: &me.account, |
| 699 | 1× | repo_id: row.id, |
| 700 | 1× | thread: Some(("issue", i.id)), |
| 701 | 1× | mention_text: "", |
| 702 | 1× | direct, |
| 703 | 1× | }, |
| 704 | 1× | ) |
| 705 | 1× | .await; |
| 706 | 1× | } |
| 707 | 2× | detail(&app, shell, &viewer, &owner, &name, number, None, None).await |
| 708 | 3× | } |
| 709 | |
|
| 710 | | #[derive(Deserialize)] |
| 711 | | pub struct LabelsForm { |
| 712 | | #[serde(rename = "_csrf")] |
| 713 | | csrf_token: String, |
| 714 | | #[serde(default)] |
| 715 | | label: Vec<i64>, |
| 716 | | } |
| 717 | |
|
| 718 | 1× | pub async fn set_labels( |
| 719 | 1× | State(app): State<Shared>, |
| 720 | 1× | viewer: Viewer, |
| 721 | 1× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 722 | 1× | axum_extra::extract::Form(f): axum_extra::extract::Form<LabelsForm>, |
| 723 | 1× | ) -> Result<Page<IssuePage>, Error> { |
| 724 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 725 | 1× | let me = require(&viewer, &shell)?; |
| 726 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 727 | 1× | let (row, i, _) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 728 | 1× | let role = crate::auth::roles::role_for(&app.db, Some(&me.account), &row).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 729 | 1× | if role < Some(Role::Triage) { |
| 730 | 0× | return Err(Error::not_found(shell)); |
| 731 | 1× | } |
| 732 | 1× | let e = |e| Error::from_db(shell.clone(), e); |
| 733 | 1× | for l in idb::labels(&app.db, row.id).await.map_err(e)? { |
| 734 | 1× | idb::set_issue_label(&app.db, i.id, l.id, f.label.contains(&l.id)).await.map_err(e)?; |
| 735 | | } |
| 736 | 1× | idb::add_event(&app.db, i.id, &me.account.username, "set labels", "").await.ok(); |
| 737 | 1× | detail(&app, shell, &viewer, &owner, &name, number, None, None).await |
| 738 | 1× | } |
| 739 | |
|
| 740 | | #[derive(Deserialize)] |
| 741 | | pub struct AssignForm { |
| 742 | | #[serde(rename = "_csrf")] |
| 743 | | csrf_token: String, |
| 744 | | |
| 745 | | #[serde(default)] |
| 746 | | username: Vec<String>, |
| 747 | | |
| 748 | | action: String, |
| 749 | | } |
| 750 | |
|
| 751 | 3× | pub async fn assign( |
| 752 | 3× | State(app): State<Shared>, |
| 753 | 3× | viewer: Viewer, |
| 754 | 3× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 755 | 3× | axum_extra::extract::Form(f): axum_extra::extract::Form<AssignForm>, |
| 756 | 3× | ) -> Result<Page<IssuePage>, Error> { |
| 757 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 758 | 3× | let me = require(&viewer, &shell)?; |
| 759 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 760 | 3× | let (row, i, role) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 761 | 3× | if role < Role::Triage { |
| 762 | 0× | return Err(Error::not_found(shell)); |
| 763 | 3× | } |
| 764 | 3× | let e = |e| Error::from_db(shell.clone(), e); |
| 765 | 3× | let mut named = Vec::new(); |
| 766 | 4× | for n in f.username.iter().map(|n| n.trim()).filter(|n| !n.is_empty()) { |
| 767 | 4× | match accounts::account_by_name(&app.db, n).await.map_err(e)? { |
| 768 | 4× | Some(a) => named.push(a), |
| 769 | 0× | None => return detail(&app, shell, &viewer, &owner, &name, number, None, Some(format!("No account {n}."))).await, |
| 770 | | } |
| 771 | | } |
| 772 | 3× | let current = idb::assignees(&app.db, i.id).await.map_err(e)?; |
| 773 | 4× | let has = |a: &accounts::Account| current.iter().any(|c| c.eq_ignore_ascii_case(&a.username)); |
| 774 | | |
| 775 | | |
| 776 | 3× | let (add, remove): (Vec<&accounts::Account>, Vec<String>) = match f.action.as_str() { |
| 777 | 3× | "add" => (named.iter().filter(|a| !has(a)).collect(), Vec::new()), |
| 778 | 2× | "remove" => (Vec::new(), named.iter().filter(|a| has(a)).map(|a| a.username.clone()).collect()), |
| 779 | 2× | "set" => ( |
| 780 | 3× | named.iter().filter(|a| !has(a)).collect(), |
| 781 | 4× | current.iter().filter(|c| !named.iter().any(|a| a.username.eq_ignore_ascii_case(c))).cloned().collect(), |
| 782 | | ), |
| 783 | 0× | _ => return Err(Error::bad(shell, "unknown action")), |
| 784 | | }; |
| 785 | 3× | for n in &remove { |
| 786 | 1× | if let Some(a) = accounts::account_by_name(&app.db, n).await.map_err(e)? { |
| 787 | 1× | idb::set_assignee(&app.db, i.id, a.id, false).await.map_err(e)?; |
| 788 | 1× | idb::add_event(&app.db, i.id, &me.account.username, "unassigned", &a.username).await.ok(); |
| 789 | 0× | } |
| 790 | | } |
| 791 | 3× | for target in add { |
| 792 | 2× | idb::set_assignee(&app.db, i.id, target.id, true).await.map_err(e)?; |
| 793 | 2× | idb::add_event(&app.db, i.id, &me.account.username, "assigned", &target.username).await.ok(); |
| 794 | 2× | notify::deliver( |
| 795 | 2× | &app, |
| 796 | 2× | Event { |
| 797 | 2× | kind: "issue.assigned", |
| 798 | 2× | title: format!("{owner}/{name}#{number}: {}", i.title), |
| 799 | 2× | url: format!("/{owner}/{name}/issue/{number}"), |
| 800 | 2× | actor: &me.account, |
| 801 | 2× | repo_id: row.id, |
| 802 | 2× | thread: None, |
| 803 | 2× | mention_text: "", |
| 804 | 2× | direct: vec![target.id], |
| 805 | 2× | }, |
| 806 | 2× | ) |
| 807 | 2× | .await; |
| 808 | 2× | ndb::subscribe(&app.db, target.id, "issue", i.id).await.ok(); |
| 809 | | } |
| 810 | 3× | detail(&app, shell, &viewer, &owner, &name, number, None, None).await |
| 811 | 3× | } |
| 812 | |
|
| 813 | | #[derive(Deserialize)] |
| 814 | | pub struct MilestoneForm { |
| 815 | | #[serde(rename = "_csrf")] |
| 816 | | csrf_token: String, |
| 817 | | #[serde(default)] |
| 818 | | milestone: String, |
| 819 | | } |
| 820 | |
|
| 821 | 1× | pub async fn set_milestone( |
| 822 | 1× | State(app): State<Shared>, |
| 823 | 1× | viewer: Viewer, |
| 824 | 1× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 825 | 1× | Form(f): Form<MilestoneForm>, |
| 826 | 1× | ) -> Result<Page<IssuePage>, Error> { |
| 827 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 828 | 1× | let me = require(&viewer, &shell)?; |
| 829 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 830 | 1× | let (_, i, role) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 831 | 1× | if role < Role::Triage { |
| 832 | 0× | return Err(Error::not_found(shell)); |
| 833 | 1× | } |
| 834 | 1× | let id = f.milestone.trim().parse::<i64>().ok(); |
| 835 | 1× | idb::set_milestone(&app.db, i.id, id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 836 | 1× | idb::add_event(&app.db, i.id, &me.account.username, "set milestone", "").await.ok(); |
| 837 | 1× | detail(&app, shell, &viewer, &owner, &name, number, None, None).await |
| 838 | 1× | } |
| 839 | |
|
| 840 | 0× | pub async fn subscribe( |
| 841 | 0× | State(app): State<Shared>, |
| 842 | 0× | viewer: Viewer, |
| 843 | 0× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 844 | 0× | Form(f): Form<super::session::CsrfForm>, |
| 845 | 0× | ) -> Result<Page<IssuePage>, Error> { |
| 846 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/issue/{number}"), &viewer); |
| 847 | 0× | let me = require(&viewer, &shell)?; |
| 848 | 0× | csrf(&viewer, &f.csrf, &shell)?; |
| 849 | 0× | let (_, i, _) = issue_for(&app, &shell, &viewer, &owner, &name, number, Access::Read).await?; |
| 850 | 0× | let e = |e| Error::from_db(shell.clone(), e); |
| 851 | 0× | if ndb::subscribed(&app.db, me.account.id, "issue", i.id).await.map_err(e)? { |
| 852 | 0× | ndb::unsubscribe(&app.db, me.account.id, "issue", i.id).await.map_err(e)?; |
| 853 | | } else { |
| 854 | 0× | ndb::subscribe(&app.db, me.account.id, "issue", i.id).await.map_err(e)?; |
| 855 | | } |
| 856 | 0× | detail(&app, shell, &viewer, &owner, &name, number, None, None).await |
| 857 | 0× | } |
| 858 | |
|
| 859 | |
|
| 860 | |
|
| 861 | | #[derive(Template)] |
| 862 | | #[template(path = "labels.html")] |
| 863 | | pub struct LabelsPage { |
| 864 | | shell: Shell, |
| 865 | | repo: RepoShell, |
| 866 | | labels: Vec<idb::Label>, |
| 867 | | can_triage: bool, |
| 868 | | notice: Option<String>, |
| 869 | | } |
| 870 | |
|
| 871 | 3× | pub async fn labels( |
| 872 | 3× | State(app): State<Shared>, |
| 873 | 3× | viewer: Viewer, |
| 874 | 3× | Path((owner, name)): Path<(String, String)>, |
| 875 | 3× | ) -> Result<Page<LabelsPage>, Error> { |
| 876 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/labels"), &viewer); |
| 877 | 3× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 878 | 3× | super::writable(&row, &shell)?; |
| 879 | 3× | let labels = idb::labels(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 880 | 3× | Ok(Page(LabelsPage { |
| 881 | 3× | shell, |
| 882 | 3× | repo: RepoShell::new(&app, &row, "issues").with_role(role), |
| 883 | 3× | labels, |
| 884 | 3× | can_triage: role >= Role::Triage, |
| 885 | 3× | notice: None, |
| 886 | 3× | })) |
| 887 | 3× | } |
| 888 | |
|
| 889 | | #[derive(Deserialize)] |
| 890 | | pub struct LabelForm { |
| 891 | | #[serde(rename = "_csrf")] |
| 892 | | csrf_token: String, |
| 893 | | name: String, |
| 894 | | #[serde(default = "default_colour")] |
| 895 | | colour: String, |
| 896 | | #[serde(default)] |
| 897 | | description: String, |
| 898 | | #[serde(default)] |
| 899 | | delete: Option<String>, |
| 900 | | } |
| 901 | |
|
| 902 | 0× | fn default_colour() -> String { |
| 903 | 0× | "#0072ff".into() |
| 904 | 0× | } |
| 905 | |
|
| 906 | 1× | pub async fn save_label( |
| 907 | 1× | State(app): State<Shared>, |
| 908 | 1× | viewer: Viewer, |
| 909 | 1× | Path((owner, name)): Path<(String, String)>, |
| 910 | 1× | Form(f): Form<LabelForm>, |
| 911 | 1× | ) -> Result<Page<LabelsPage>, Error> { |
| 912 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/labels"), &viewer); |
| 913 | 1× | require(&viewer, &shell)?; |
| 914 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 915 | 1× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 916 | 1× | if role < Role::Triage { |
| 917 | 0× | return Err(Error::not_found(shell)); |
| 918 | 1× | } |
| 919 | 1× | let e = |e| Error::from_db(shell.clone(), e); |
| 920 | 1× | let notice = if f.delete.is_some() { |
| 921 | 0× | if let Some(l) = idb::label_by_name(&app.db, row.id, f.name.trim()).await.map_err(e)? { |
| 922 | 0× | idb::delete_label(&app.db, l.id).await.map_err(e)?; |
| 923 | 0× | } |
| 924 | 0× | format!("Deleted {}.", f.name.trim()) |
| 925 | | } else { |
| 926 | 1× | let colour = if regex::Regex::new(r"^#[0-9a-fA-F]{6}$").unwrap().is_match(&f.colour) { f.colour.clone() } else { default_colour() }; |
| 927 | 1× | idb::upsert_label(&app.db, row.id, f.name.trim(), &colour, f.description.trim()).await.map_err(e)?; |
| 928 | 1× | format!("Saved {}.", f.name.trim()) |
| 929 | | }; |
| 930 | 1× | let labels = idb::labels(&app.db, row.id).await.map_err(e)?; |
| 931 | 1× | Ok(Page(LabelsPage { |
| 932 | 1× | shell, |
| 933 | 1× | repo: RepoShell::new(&app, &row, "issues").with_role(role), |
| 934 | 1× | labels, |
| 935 | 1× | can_triage: true, |
| 936 | 1× | notice: Some(notice), |
| 937 | 1× | })) |
| 938 | 1× | } |
| 939 | |
|
| 940 | |
|
| 941 | |
|
| 942 | | #[derive(Template)] |
| 943 | | #[template(path = "milestones.html")] |
| 944 | | pub struct MilestonesPage { |
| 945 | | shell: Shell, |
| 946 | | repo: RepoShell, |
| 947 | | rows: Vec<(idb::Milestone, i64, i64)>, |
| 948 | | can_triage: bool, |
| 949 | | notice: Option<String>, |
| 950 | | } |
| 951 | |
|
| 952 | 2× | async fn milestones_page( |
| 953 | 2× | app: &Shared, |
| 954 | 2× | shell: Shell, |
| 955 | 2× | row: db::repos::RepoRow, |
| 956 | 2× | role: Role, |
| 957 | 2× | notice: Option<String>, |
| 958 | 2× | ) -> Result<Page<MilestonesPage>, Error> { |
| 959 | 2× | let mut rows = Vec::new(); |
| 960 | 2× | for m in idb::milestones(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 961 | 2× | let (open, closed) = idb::milestone_counts(&app.db, m.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 962 | 2× | rows.push((m, open, closed)); |
| 963 | | } |
| 964 | 2× | Ok(Page(MilestonesPage { |
| 965 | 2× | shell, |
| 966 | 2× | repo: RepoShell::new(app, &row, "issues").with_role(role), |
| 967 | 2× | rows, |
| 968 | 2× | can_triage: role >= Role::Triage, |
| 969 | 2× | notice, |
| 970 | 2× | })) |
| 971 | 2× | } |
| 972 | |
|
| 973 | 1× | pub async fn milestones( |
| 974 | 1× | State(app): State<Shared>, |
| 975 | 1× | viewer: Viewer, |
| 976 | 1× | Path((owner, name)): Path<(String, String)>, |
| 977 | 1× | ) -> Result<Page<MilestonesPage>, Error> { |
| 978 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/milestones"), &viewer); |
| 979 | 1× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 980 | 1× | milestones_page(&app, shell, row, role, None).await |
| 981 | 1× | } |
| 982 | |
|
| 983 | | #[derive(Deserialize)] |
| 984 | | pub struct MilestoneSaveForm { |
| 985 | | #[serde(rename = "_csrf")] |
| 986 | | csrf_token: String, |
| 987 | | title: String, |
| 988 | | #[serde(default)] |
| 989 | | description: String, |
| 990 | | #[serde(default)] |
| 991 | | due: String, |
| 992 | | #[serde(default)] |
| 993 | | state: Option<String>, |
| 994 | | } |
| 995 | |
|
| 996 | 1× | pub async fn save_milestone( |
| 997 | 1× | State(app): State<Shared>, |
| 998 | 1× | viewer: Viewer, |
| 999 | 1× | Path((owner, name)): Path<(String, String)>, |
| 1000 | 1× | Form(f): Form<MilestoneSaveForm>, |
| 1001 | 1× | ) -> Result<Page<MilestonesPage>, Error> { |
| 1002 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/milestones"), &viewer); |
| 1003 | 1× | require(&viewer, &shell)?; |
| 1004 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1005 | 1× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 1006 | 1× | if role < Role::Triage { |
| 1007 | 0× | return Err(Error::not_found(shell)); |
| 1008 | 1× | } |
| 1009 | 1× | let e = |e| Error::from_db(shell.clone(), e); |
| 1010 | 1× | if let Some(state) = f.state.as_deref() { |
| 1011 | 0× | if let Some(m) = idb::milestones(&app.db, row.id).await.map_err(e)?.into_iter().find(|m| m.title == f.title) { |
| 1012 | 0× | idb::set_milestone_state(&app.db, m.id, if state == "closed" { "closed" } else { "open" }).await.map_err(e)?; |
| 1013 | 0× | } |
| 1014 | 0× | return milestones_page(&app, shell, row, role, Some("Updated.".into())).await; |
| 1015 | 1× | } |
| 1016 | 1× | let due_ms = time::Date::parse(f.due.trim(), &time::macros::format_description!("[year]-[month]-[day]")) |
| 1017 | 1× | .ok() |
| 1018 | 1× | .map(|d| d.midnight().assume_utc().unix_timestamp() * 1000); |
| 1019 | 1× | idb::upsert_milestone(&app.db, row.id, f.title.trim(), f.description.trim(), due_ms).await.map_err(e)?; |
| 1020 | 1× | milestones_page(&app, shell, row, role, Some("Saved.".into())).await |
| 1021 | 1× | } |
| 1022 | |
|
| 1023 | |
|
| 1024 | |
|
| 1025 | 3× | pub async fn watch( |
| 1026 | 3× | State(app): State<Shared>, |
| 1027 | 3× | viewer: Viewer, |
| 1028 | 3× | Path((owner, name)): Path<(String, String)>, |
| 1029 | 3× | Form(f): Form<super::session::CsrfForm>, |
| 1030 | 3× | ) -> Result<Response, Error> { |
| 1031 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}"), &viewer); |
| 1032 | 3× | let me = require(&viewer, &shell)?; |
| 1033 | 3× | csrf(&viewer, &f.csrf, &shell)?; |
| 1034 | 3× | let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 1035 | 3× | let on = !ndb::watching(&app.db, me.account.id, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1036 | 3× | ndb::set_watch(&app.db, me.account.id, row.id, on).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1037 | 3× | Ok(Redirect::to(&format!("/{owner}/{name}")).into_response()) |
| 1038 | 3× | } |
| 1039 | |
|
| 1040 | |
|
| 1041 | 5× | pub async fn star( |
| 1042 | 5× | State(app): State<Shared>, |
| 1043 | 5× | viewer: Viewer, |
| 1044 | 5× | Path((owner, name)): Path<(String, String)>, |
| 1045 | 5× | Form(f): Form<super::session::CsrfForm>, |
| 1046 | 5× | ) -> Result<Response, Error> { |
| 1047 | 5× | let shell = Shell::new(&app, &format!("/{owner}/{name}"), &viewer); |
| 1048 | 5× | let me = require(&viewer, &shell)?; |
| 1049 | 5× | csrf(&viewer, &f.csrf, &shell)?; |
| 1050 | 5× | let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 1051 | 5× | db::repos::toggle_star(&app.db, me.account.id, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1052 | 5× | Ok(Redirect::to(&format!("/{owner}/{name}")).into_response()) |
| 1053 | 5× | } |
| 1054 | |
|
| 1055 | |
|
| 1056 | |
|
| 1057 | 252× | pub async fn references_from_messages(app: &Shared, row: &db::repos::RepoRow, messages: &[(String, String)], bookmark: &str, actor: &str) { |
| 1058 | 252× | let Ok((issues, proposals)) = idb::numbers(&app.db, row.id).await else { return }; |
| 1059 | 251× | for (hash, message) in messages { |
| 1060 | 244× | let short = &hash[..12.min(hash.len())]; |
| 1061 | 244× | let detail = format!("{short} on {bookmark}"); |
| 1062 | | |
| 1063 | | |
| 1064 | 244× | let mut named = render::refs::references(message); |
| 1065 | 244× | named.sort_unstable(); |
| 1066 | 244× | named.dedup(); |
| 1067 | 244× | for n in named { |
| 1068 | 2× | if issues.contains(&n) { |
| 1069 | 1× | if let Ok(Some(i)) = idb::get(&app.db, row.id, n).await { |
| 1070 | 1× | if !idb::referenced_by(&app.db, i.id, short).await.unwrap_or(false) { |
| 1071 | 1× | idb::add_event(&app.db, i.id, actor, "referenced by change", &detail).await.ok(); |
| 1072 | 0× | } |
| 1073 | 0× | } |
| 1074 | 1× | } else if proposals.contains(&n) { |
| 1075 | 1× | if let Ok(Some(p)) = db::proposals::get(&app.db, row.id, n).await { |
| 1076 | 1× | if !db::proposals::referenced_by(&app.db, p.id, short).await.unwrap_or(false) { |
| 1077 | 1× | db::proposals::add_event(&app.db, p.id, actor, "referenced by change", &detail).await.ok(); |
| 1078 | 0× | } |
| 1079 | 0× | } |
| 1080 | 0× | } |
| 1081 | | } |
| 1082 | | } |
| 1083 | 251× | } |
| 1084 | |
|
| 1085 | |
|
| 1086 | |
|
| 1087 | |
|
| 1088 | |
|
| 1089 | |
|
| 1090 | |
|
| 1091 | |
|
| 1092 | |
|
| 1093 | 236× | pub async fn close_from_arrived( |
| 1094 | 236× | app: &Shared, |
| 1095 | 236× | row: &db::repos::RepoRow, |
| 1096 | 236× | old_heads: Vec<oxy_core::hash::ChangeHash>, |
| 1097 | 236× | new_heads: Vec<oxy_core::hash::ChangeHash>, |
| 1098 | 236× | extra: Option<(String, String)>, |
| 1099 | 236× | ) { |
| 1100 | 236× | let Some(name) = crate::repos::RepoName::new(&row.owner, &row.name) else { return }; |
| 1101 | 236× | let mut messages: Vec<(String, String)> = app |
| 1102 | 236× | .repos |
| 1103 | 236× | .read(&name, move |r, cache| { |
| 1104 | 236× | let new = cache.summary(r, &new_heads)?; |
| 1105 | 236× | let old = if old_heads.is_empty() { None } else { cache.summary(r, &old_heads).ok() }; |
| 1106 | 236× | Ok(new |
| 1107 | 236× | .changes |
| 1108 | 236× | .iter() |
| 1109 | 460× | .filter(|h| !old.as_ref().is_some_and(|o| o.changes.contains(*h))) |
| 1110 | 241× | .filter_map(|h| r.store().get_change_meta(h).ok().map(|c| (h.to_hex(), c.message))) |
| 1111 | 236× | .collect()) |
| 1112 | 236× | }) |
| 1113 | 236× | .await |
| 1114 | 236× | .unwrap_or_default(); |
| 1115 | 236× | messages.extend(extra); |
| 1116 | 236× | close_from_messages(app, row, &messages).await; |
| 1117 | 236× | } |
| 1118 | |
|
| 1119 | 304× | pub async fn close_from_messages(app: &Shared, row: &db::repos::RepoRow, messages: &[(String, String)]) { |
| 1120 | 304× | for (hash, message) in messages { |
| 1121 | 294× | for n in render::refs::closes(message) { |
| 1122 | 5× | if let Ok(Some(i)) = idb::get(&app.db, row.id, n).await { |
| 1123 | 5× | if i.state == "open" { |
| 1124 | 4× | idb::set_state(&app.db, i.id, "closed", "completed").await.ok(); |
| 1125 | 4× | idb::add_event(&app.db, i.id, "", "closed by change", &hash[..12.min(hash.len())]).await.ok(); |
| 1126 | 4× | for id in ndb::subscribers(&app.db, "issue", i.id).await.unwrap_or_default() { |
| 1127 | 4× | let title = format!("{}#{}: {} (closed by {})", row.slug(), i.number, i.title, &hash[..12.min(hash.len())]); |
| 1128 | 4× | let url = format!("/{}/issue/{}", row.slug(), i.number); |
| 1129 | 4× | ndb::add( |
| 1130 | 4× | &app.db, |
| 1131 | 4× | ndb::New { |
| 1132 | 4× | account_id: id, |
| 1133 | 4× | repo_id: Some(row.id), |
| 1134 | 4× | reason: ndb::Reason::Participating, |
| 1135 | 4× | kind: "issue.closed", |
| 1136 | 4× | title: &title, |
| 1137 | 4× | url: &url, |
| 1138 | 4× | actor: "", |
| 1139 | 4× | }, |
| 1140 | 4× | ) |
| 1141 | 4× | .await |
| 1142 | 4× | .ok(); |
| 1143 | | } |
| 1144 | 1× | } |
| 1145 | 0× | } |
| 1146 | | } |
| 1147 | | } |
| 1148 | 304× | } |