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