| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use super::history::{render_files, FileRows}; |
| 5 |
use super::settings::{csrf, require}; |
| 6 |
use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell}; |
| 7 |
use crate::auth::{Access, Role, Viewer}; |
| 8 |
use crate::db::{self, accounts, proposals as pdb}; |
| 9 |
use crate::render::{self, diff}; |
| 10 |
use crate::repos::proposals::{self as pl, Projection, Source}; |
| 11 |
use crate::repos::view::{self, ChangeView, ConflictView}; |
| 12 |
use crate::repos::RepoName; |
| 13 |
use crate::Shared; |
| 14 |
use askama::Template; |
| 15 |
use axum::extract::{Form, Path, Query, State}; |
| 16 |
use axum::response::{IntoResponse, Redirect, Response}; |
| 17 |
use hub_api::{Approval, ProposalSource, ProposalState}; |
| 18 |
use oxy_core::hash::ChangeHash; |
| 19 |
use serde::Deserialize; |
| 20 |
use std::collections::{BTreeMap, HashMap}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#[derive(Template)] |
| 25 |
#[template(path = "proposals.html")] |
| 26 |
pub struct ProposalsPage { |
| 27 |
shell: Shell, |
| 28 |
repo: RepoShell, |
| 29 |
rows: Vec<pdb::ProposalRow>, |
| 30 |
filter: String, |
| 31 |
counts: (usize, usize, usize), |
| 32 |
bookmarks: Vec<String>, |
| 33 |
} |
| 34 |
|
| 35 |
#[derive(Deserialize)] |
| 36 |
pub struct ListQuery { |
| 37 |
state: Option<String>, |
| 38 |
} |
| 39 |
|
| 40 |
pub async fn list( |
| 41 |
State(app): State<Shared>, |
| 42 |
viewer: Viewer, |
| 43 |
Path((owner, name)): Path<(String, String)>, |
| 44 |
Query(q): Query<ListQuery>, |
| 45 |
) -> Result<Page<ProposalsPage>, Error> { |
| 46 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposals"), &viewer); |
| 47 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 48 |
let all = pdb::list(&app.db, row.id, &[]).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 49 |
let counts = ( |
| 50 |
all.iter().filter(|p| p.is_open()).count(), |
| 51 |
all.iter().filter(|p| p.state == "landed").count(), |
| 52 |
all.iter().filter(|p| p.state == "closed").count(), |
| 53 |
); |
| 54 |
let filter = q.state.unwrap_or_else(|| "open".into()); |
| 55 |
let rows = all |
| 56 |
.into_iter() |
| 57 |
.filter(|p| match filter.as_str() { |
| 58 |
"open" => p.is_open(), |
| 59 |
"landed" => p.state == "landed", |
| 60 |
"closed" => p.state == "closed", |
| 61 |
_ => true, |
| 62 |
}) |
| 63 |
.collect(); |
| 64 |
let bookmarks = app |
| 65 |
.repos |
| 66 |
.read(&repo, |r, _| Ok(r.bookmarks()?.into_iter().map(|b| b.name).collect::<Vec<_>>())) |
| 67 |
.await |
| 68 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 69 |
Ok(Page(ProposalsPage { shell, repo: RepoShell::new(&app, &row, "proposals").with_role(role), rows, filter, counts, bookmarks })) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#[derive(Template)] |
| 75 |
#[template(path = "proposal_new.html")] |
| 76 |
pub struct NewProposalPage { |
| 77 |
shell: Shell, |
| 78 |
repo: RepoShell, |
| 79 |
bookmarks: Vec<String>, |
| 80 |
source: String, |
| 81 |
target: String, |
| 82 |
error: Option<String>, |
| 83 |
|
| 84 |
preview: Vec<ChangeView>, |
| 85 |
conflicted: bool, |
| 86 |
} |
| 87 |
|
| 88 |
#[derive(Deserialize)] |
| 89 |
pub struct NewQuery { |
| 90 |
source: Option<String>, |
| 91 |
target: Option<String>, |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
fn parse_source(app: &Shared, repo: &RepoName, source: &str) -> Option<(Option<RepoName>, String)> { |
| 96 |
if let Some((slug, bookmark)) = source.split_once(':') { |
| 97 |
let fork = RepoName::parse(slug)?; |
| 98 |
if !app.repos.exists(&fork) || fork == *repo { |
| 99 |
return None; |
| 100 |
} |
| 101 |
return Some((Some(fork), bookmark.to_string())); |
| 102 |
} |
| 103 |
crate::repos::names::valid_segment(source).then(|| (None, source.to_string())) |
| 104 |
} |
| 105 |
|
| 106 |
async fn preview_for( |
| 107 |
app: &Shared, |
| 108 |
repo: &RepoName, |
| 109 |
target: &str, |
| 110 |
source: &str, |
| 111 |
) -> Result<(Vec<ChangeView>, bool), crate::repos::RepoError> { |
| 112 |
let Some((fork, bookmark)) = parse_source(app, repo, source) else { return Ok((vec![], false)) }; |
| 113 |
let target = target.to_string(); |
| 114 |
let fork_dir = fork.as_ref().map(|f| app.repos.dir(f)); |
| 115 |
app.repos |
| 116 |
.read(repo, move |r, cache| { |
| 117 |
let projection = match &fork_dir { |
| 118 |
Some(dir) => { |
| 119 |
let fork_repo = oxy_core::repo::Repo::open(dir)?; |
| 120 |
let heads = fork_repo.bookmarks()?.into_iter().find(|b| b.name == bookmark).map(|b| b.heads).unwrap_or_default(); |
| 121 |
pl::project(r, cache, &target, Source::Fork { fork: &fork_repo, heads })? |
| 122 |
} |
| 123 |
None => { |
| 124 |
let heads = r.bookmarks()?.into_iter().find(|b| b.name == bookmark).map(|b| b.heads).unwrap_or_default(); |
| 125 |
pl::project(r, cache, &target, Source::Bookmark(heads))? |
| 126 |
} |
| 127 |
}; |
| 128 |
Ok((projection.changes, projection.conflicted)) |
| 129 |
}) |
| 130 |
.await |
| 131 |
} |
| 132 |
|
| 133 |
pub async fn new_form( |
| 134 |
State(app): State<Shared>, |
| 135 |
viewer: Viewer, |
| 136 |
Path((owner, name)): Path<(String, String)>, |
| 137 |
Query(q): Query<NewQuery>, |
| 138 |
) -> Result<Page<NewProposalPage>, Error> { |
| 139 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposals/new"), &viewer); |
| 140 |
require(&viewer, &shell)?; |
| 141 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 142 |
let bookmarks = app |
| 143 |
.repos |
| 144 |
.read(&repo, |r, _| Ok(r.bookmarks()?.into_iter().map(|b| b.name).collect::<Vec<_>>())) |
| 145 |
.await |
| 146 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 147 |
let target = q.target.unwrap_or_else(|| row.default_bookmark.clone()); |
| 148 |
let source = q.source.unwrap_or_default(); |
| 149 |
let (preview, conflicted) = |
| 150 |
if source.is_empty() { (vec![], false) } else { preview_for(&app, &repo, &target, &source).await.unwrap_or((vec![], false)) }; |
| 151 |
Ok(Page(NewProposalPage { |
| 152 |
shell, |
| 153 |
repo: RepoShell::new(&app, &row, "proposals").with_role(role), |
| 154 |
bookmarks, |
| 155 |
source, |
| 156 |
target, |
| 157 |
error: None, |
| 158 |
preview, |
| 159 |
conflicted, |
| 160 |
})) |
| 161 |
} |
| 162 |
|
| 163 |
#[derive(Deserialize)] |
| 164 |
pub struct NewForm { |
| 165 |
#[serde(rename = "_csrf")] |
| 166 |
csrf_token: String, |
| 167 |
title: String, |
| 168 |
#[serde(default)] |
| 169 |
body: String, |
| 170 |
target: String, |
| 171 |
source: String, |
| 172 |
#[serde(default)] |
| 173 |
draft: Option<String>, |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
#[allow(clippy::too_many_arguments)] |
| 178 |
pub async fn open_proposal( |
| 179 |
app: &Shared, |
| 180 |
repo: &RepoName, |
| 181 |
row: &db::repos::RepoRow, |
| 182 |
me: &accounts::Account, |
| 183 |
title: &str, |
| 184 |
body: &str, |
| 185 |
target: &str, |
| 186 |
source: &str, |
| 187 |
draft: bool, |
| 188 |
) -> Result<pdb::ProposalRow, String> { |
| 189 |
if title.trim().is_empty() { |
| 190 |
return Err("Give the proposal a title.".into()); |
| 191 |
} |
| 192 |
let bookmarks = |
| 193 |
app.repos.read(repo, |r, _| Ok(r.bookmarks()?.into_iter().map(|b| b.name).collect::<Vec<_>>())).await.map_err(|e| e.to_string())?; |
| 194 |
if !bookmarks.iter().any(|b| b == target) { |
| 195 |
return Err("Pick a target bookmark of this repository.".into()); |
| 196 |
} |
| 197 |
let Some((fork, bookmark)) = parse_source(app, repo, source.trim()) else { |
| 198 |
return Err("The source is a bookmark of this repository, or owner/name:bookmark of a fork.".into()); |
| 199 |
}; |
| 200 |
if fork.is_none() && bookmark == target { |
| 201 |
return Err("The source and the target are the same bookmark.".into()); |
| 202 |
} |
| 203 |
let (preview, _) = preview_for(app, repo, target, source.trim()).await.map_err(|e| e.to_string())?; |
| 204 |
if preview.is_empty() { |
| 205 |
return Err("The source adds nothing to the target.".into()); |
| 206 |
} |
| 207 |
let source_repo_id = match &fork { |
| 208 |
Some(fk) => db::repos::get(&app.db, &fk.owner, &fk.name).await.map_err(|e| e.to_string())?.map(|r| r.id), |
| 209 |
None => None, |
| 210 |
}; |
| 211 |
let created = pdb::create( |
| 212 |
&app.db, |
| 213 |
pdb::NewProposal { |
| 214 |
repo_id: row.id, |
| 215 |
title: title.trim(), |
| 216 |
body: body.trim(), |
| 217 |
author_id: Some(me.id), |
| 218 |
author: &me.username, |
| 219 |
draft, |
| 220 |
target, |
| 221 |
source_kind: if fork.is_some() { "fork" } else { "bookmark" }, |
| 222 |
source_repo_id, |
| 223 |
source_bookmark: &bookmark, |
| 224 |
source_changes: "", |
| 225 |
}, |
| 226 |
) |
| 227 |
.await |
| 228 |
.map_err(|e| e.to_string())?; |
| 229 |
pdb::add_event(&app.db, created.id, &me.username, "opened", "").await.ok(); |
| 230 |
accounts::audit(&app.db, Some(me), "proposal.opened", &format!("{}#{}", row.slug(), created.number), title).await.ok(); |
| 231 |
db::notify::index(&app.db, "proposal", row.id, created.id, created.number, &created.title, &created.body).await.ok(); |
| 232 |
let url = format!("/{}/proposal/{}", row.slug(), created.number); |
| 233 |
crate::notify::deliver( |
| 234 |
app, |
| 235 |
crate::notify::Event { |
| 236 |
kind: "proposal.opened", |
| 237 |
title: format!("{}#{}: {}", row.slug(), created.number, created.title), |
| 238 |
url, |
| 239 |
actor: me, |
| 240 |
repo_id: row.id, |
| 241 |
thread: Some(("proposal", created.id)), |
| 242 |
mention_text: body, |
| 243 |
direct: vec![], |
| 244 |
}, |
| 245 |
) |
| 246 |
.await; |
| 247 |
crate::hooks::emit( |
| 248 |
app, |
| 249 |
row, |
| 250 |
"proposal.opened", |
| 251 |
&me.username, |
| 252 |
serde_json::json!({ "number": created.number, "title": created.title, "target": target, "source": source }), |
| 253 |
) |
| 254 |
.await; |
| 255 |
Ok(created) |
| 256 |
} |
| 257 |
|
| 258 |
pub async fn create( |
| 259 |
State(app): State<Shared>, |
| 260 |
viewer: Viewer, |
| 261 |
Path((owner, name)): Path<(String, String)>, |
| 262 |
Form(f): Form<NewForm>, |
| 263 |
) -> Result<Response, Error> { |
| 264 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposals/new"), &viewer); |
| 265 |
let me = require(&viewer, &shell)?; |
| 266 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 267 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 268 |
match open_proposal(&app, &repo, &row, &me.account, &f.title, &f.body, &f.target, &f.source, f.draft.is_some()).await { |
| 269 |
Ok(created) => Ok(Redirect::to(&format!("/{owner}/{name}/proposal/{}", created.number)).into_response()), |
| 270 |
Err(msg) => { |
| 271 |
let bookmarks = |
| 272 |
app.repos.read(&repo, |r, _| Ok(r.bookmarks()?.into_iter().map(|b| b.name).collect::<Vec<_>>())).await.unwrap_or_default(); |
| 273 |
Ok(Page(NewProposalPage { |
| 274 |
shell, |
| 275 |
repo: RepoShell::new(&app, &row, "proposals").with_role(role), |
| 276 |
bookmarks, |
| 277 |
source: f.source.clone(), |
| 278 |
target: f.target.clone(), |
| 279 |
error: Some(msg), |
| 280 |
preview: vec![], |
| 281 |
conflicted: false, |
| 282 |
}) |
| 283 |
.into_response()) |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
pub struct CommentView { |
| 292 |
pub row: pdb::CommentRow, |
| 293 |
pub body_html: String, |
| 294 |
pub line: Option<usize>, |
| 295 |
} |
| 296 |
|
| 297 |
pub struct ReviewView { |
| 298 |
pub row: pdb::ReviewRow, |
| 299 |
pub body_html: String, |
| 300 |
pub sealed: bool, |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
pub struct Loaded { |
| 305 |
pub row: pdb::ProposalRow, |
| 306 |
pub changes: Vec<ChangeView>, |
| 307 |
pub hashes: Vec<ChangeHash>, |
| 308 |
pub heads: Vec<ChangeHash>, |
| 309 |
pub target_heads: Vec<ChangeHash>, |
| 310 |
pub state: String, |
| 311 |
pub target_state: String, |
| 312 |
pub conflicted: bool, |
| 313 |
pub conflicts: Vec<ConflictView>, |
| 314 |
pub files: Vec<FileRows>, |
| 315 |
|
| 316 |
pub atoms: HashMap<(String, usize), (String, u32)>, |
| 317 |
pub sealing_keys: Vec<String>, |
| 318 |
pub touched: Vec<String>, |
| 319 |
pub owners: Vec<String>, |
| 320 |
pub landed_already: bool, |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
pub async fn load(app: &Shared, repo: &RepoName, row: pdb::ProposalRow) -> Result<Loaded, crate::repos::RepoError> { |
| 325 |
let fork_dir = match (row.source_kind.as_str(), row.source_repo_id) { |
| 326 |
("fork", Some(id)) => { |
| 327 |
db::repos::get_by_id(&app.db, id).await.ok().flatten().and_then(|r| RepoName::new(&r.owner, &r.name)).map(|n| app.repos.dir(&n)) |
| 328 |
} |
| 329 |
_ => None, |
| 330 |
}; |
| 331 |
let r2 = row.clone(); |
| 332 |
let loaded = app |
| 333 |
.repos |
| 334 |
.read(repo, move |r, cache| { |
| 335 |
let source = match (r2.source_kind.as_str(), &fork_dir) { |
| 336 |
("changes", _) => Source::Changes(r2.source_changes().iter().filter_map(|h| ChangeHash::from_hex(h)).collect()), |
| 337 |
("fork", Some(dir)) => { |
| 338 |
let fork_repo = oxy_core::repo::Repo::open(dir)?; |
| 339 |
let heads = |
| 340 |
fork_repo.bookmarks()?.into_iter().find(|b| b.name == r2.source_bookmark).map(|b| b.heads).unwrap_or_default(); |
| 341 |
|
| 342 |
let fork_state = fork_repo.load_state(&heads)?; |
| 343 |
let missing: Vec<ChangeHash> = fork_state.changes().map(|(h, _)| *h).filter(|h| !r.store().has(h)).collect(); |
| 344 |
if !missing.is_empty() { |
| 345 |
r.import_bundle(&fork_repo.bundle_for(&missing)?, r.current_policy()?.is_some())?; |
| 346 |
} |
| 347 |
Source::Changes(heads) |
| 348 |
} |
| 349 |
_ => { |
| 350 |
let heads = r.bookmarks()?.into_iter().find(|b| b.name == r2.source_bookmark).map(|b| b.heads).unwrap_or_default(); |
| 351 |
Source::Bookmark(heads) |
| 352 |
} |
| 353 |
}; |
| 354 |
let p: Projection = pl::project(r, cache, &r2.target, source)?; |
| 355 |
let target_loaded = cache.load(r, &p.target_heads)?; |
| 356 |
let files = render_files(&p.files, &p.bases); |
| 357 |
let mut atoms = HashMap::new(); |
| 358 |
for f in &files { |
| 359 |
for l in &f.lines { |
| 360 |
if let Some(n) = l.new_no { |
| 361 |
if let Some((h, i)) = pl::atom_at(&p.loaded, &f.path, n as usize) { |
| 362 |
atoms.insert((f.path.clone(), n as usize), (h.to_hex(), i)); |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
let touched: Vec<String> = p.files.iter().map(|f| f.path.clone()).collect(); |
| 368 |
let owners = pl::owners_for(&target_loaded, &touched); |
| 369 |
let sealing = pl::sealing_keys(r, &p.hashes)?.into_iter().map(|k| k.to_hex()).collect(); |
| 370 |
let landed_already = !p.hashes.is_empty() && p.hashes.iter().all(|h| target_loaded.state.contains(h)); |
| 371 |
Ok(Loaded { |
| 372 |
row: r2, |
| 373 |
changes: p.changes, |
| 374 |
hashes: p.hashes, |
| 375 |
heads: p.heads, |
| 376 |
target_heads: p.target_heads.clone(), |
| 377 |
state: p.state.to_hex(), |
| 378 |
target_state: p.target_state.to_hex(), |
| 379 |
conflicted: p.conflicted, |
| 380 |
conflicts: p.conflicts, |
| 381 |
files, |
| 382 |
atoms, |
| 383 |
sealing_keys: sealing, |
| 384 |
touched, |
| 385 |
owners, |
| 386 |
landed_already, |
| 387 |
}) |
| 388 |
}) |
| 389 |
.await?; |
| 390 |
|
| 391 |
let heads_csv = loaded.heads.iter().map(|h| h.to_hex()).collect::<Vec<_>>().join(","); |
| 392 |
let changes_csv = loaded.hashes.iter().map(|h| h.to_hex()).collect::<Vec<_>>().join(","); |
| 393 |
pdb::set_projection(&app.db, loaded.row.id, &loaded.state, &heads_csv, &loaded.target_state, &changes_csv, loaded.conflicted) |
| 394 |
.await |
| 395 |
.ok(); |
| 396 |
Ok(loaded) |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
pub struct Verdict { |
| 401 |
pub reviews: Vec<ReviewView>, |
| 402 |
pub approvals: Vec<Approval>, |
| 403 |
pub blockers: Vec<String>, |
| 404 |
pub depends_on: Vec<i64>, |
| 405 |
pub requirements: pdb::Requirements, |
| 406 |
} |
| 407 |
|
| 408 |
pub async fn verdict(app: &Shared, loaded: &Loaded, repo_id: i64) -> Result<Verdict, sqlx::Error> { |
| 409 |
let rows = pdb::reviews(&app.db, loaded.row.id).await?; |
| 410 |
let links = accounts::identity_accounts(&app.db).await?; |
| 411 |
let sealed_accounts: Vec<String> = links.iter().filter(|(k, _)| loaded.sealing_keys.contains(k)).map(|(_, u)| u.clone()).collect(); |
| 412 |
|
| 413 |
let mut latest: BTreeMap<String, &pdb::ReviewRow> = BTreeMap::new(); |
| 414 |
for r in &rows { |
| 415 |
latest.insert(r.author.clone(), r); |
| 416 |
} |
| 417 |
let mut approvals = Vec::new(); |
| 418 |
for (who, r) in &latest { |
| 419 |
if r.verdict == "approve" { |
| 420 |
let sealed = sealed_accounts.contains(who); |
| 421 |
let key = links.iter().find(|(k, u)| u == who && loaded.sealing_keys.contains(k)).map(|(k, _)| k.clone()); |
| 422 |
approvals.push(Approval { account: who.clone(), sealed, key }); |
| 423 |
} |
| 424 |
} |
| 425 |
let reviews = rows |
| 426 |
.iter() |
| 427 |
.map(|r| ReviewView { |
| 428 |
row: r.clone(), |
| 429 |
body_html: render::markdown::render(&r.body, "/", "/"), |
| 430 |
sealed: r.verdict == "approve" && sealed_accounts.contains(&r.author), |
| 431 |
}) |
| 432 |
.collect(); |
| 433 |
let requirements = pdb::requirements(&app.db, repo_id).await?; |
| 434 |
let mut blockers = Vec::new(); |
| 435 |
if loaded.row.state == "draft" { |
| 436 |
blockers.push("the proposal is a draft".into()); |
| 437 |
} |
| 438 |
if loaded.hashes.is_empty() { |
| 439 |
blockers.push("the proposal adds no changes".into()); |
| 440 |
} |
| 441 |
let sealed_count = approvals.iter().filter(|a| a.sealed).count() as i64; |
| 442 |
if sealed_count < requirements.required_approvals { |
| 443 |
blockers.push(format!("{sealed_count} of {} required sealed approvals", requirements.required_approvals)); |
| 444 |
} |
| 445 |
if requirements.require_owners { |
| 446 |
let missing: Vec<&String> = loaded.owners.iter().filter(|o| !approvals.iter().any(|a| a.sealed && &a.account == *o)).collect(); |
| 447 |
if !missing.is_empty() { |
| 448 |
blockers.push(format!("code owners still to approve: {}", missing.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", "))); |
| 449 |
} |
| 450 |
} |
| 451 |
if loaded.conflicted && !requirements.allow_conflicts { |
| 452 |
blockers.push("landing would carry conflicts, which this repository refuses".into()); |
| 453 |
} |
| 454 |
let required = db::hooks::required_checks(&app.db, repo_id).await?; |
| 455 |
if !required.is_empty() { |
| 456 |
let checks = db::hooks::checks_for_state(&app.db, repo_id, &loaded.state).await?; |
| 457 |
let missing: Vec<String> = |
| 458 |
required.iter().filter(|n| !checks.iter().any(|c| &c.name == *n && c.status == "passed")).cloned().collect(); |
| 459 |
if !missing.is_empty() { |
| 460 |
blockers.push(format!("required checks not passed on the projected state: {}", missing.join(", "))); |
| 461 |
} |
| 462 |
} |
| 463 |
let requesting: Vec<&String> = latest.iter().filter(|(_, r)| r.verdict == "changes").map(|(who, _)| who).collect(); |
| 464 |
if !requesting.is_empty() { |
| 465 |
blockers.push(format!("changes requested by {}", requesting.iter().map(|s| s.as_str()).collect::<Vec<_>>().join(", "))); |
| 466 |
} |
| 467 |
|
| 468 |
let mut depends_on = Vec::new(); |
| 469 |
for other in pdb::list(&app.db, repo_id, &["open", "draft"]).await? { |
| 470 |
if other.id == loaded.row.id || other.number > loaded.row.number { |
| 471 |
continue; |
| 472 |
} |
| 473 |
let theirs = other.projected_changes(); |
| 474 |
if !theirs.is_empty() && theirs.iter().all(|h| loaded.hashes.iter().any(|m| m.to_hex() == *h)) { |
| 475 |
depends_on.push(other.number); |
| 476 |
} |
| 477 |
} |
| 478 |
if !depends_on.is_empty() { |
| 479 |
blockers.push(format!("depends on {}", depends_on.iter().map(|n| format!("#{n}")).collect::<Vec<_>>().join(", "))); |
| 480 |
} |
| 481 |
Ok(Verdict { reviews, approvals, blockers, depends_on, requirements }) |
| 482 |
} |
| 483 |
|
| 484 |
#[derive(Template)] |
| 485 |
#[template(path = "proposal.html")] |
| 486 |
pub struct ProposalPage { |
| 487 |
shell: Shell, |
| 488 |
repo: RepoShell, |
| 489 |
p: pdb::ProposalRow, |
| 490 |
body_html: String, |
| 491 |
tab: String, |
| 492 |
changes: Vec<ChangeView>, |
| 493 |
files: Vec<FileRows>, |
| 494 |
conflicts: Vec<ConflictView>, |
| 495 |
conflicted: bool, |
| 496 |
comments: Vec<CommentView>, |
| 497 |
|
| 498 |
placed: HashMap<(String, usize), Vec<usize>>, |
| 499 |
outdated: Vec<usize>, |
| 500 |
reviews: Vec<ReviewView>, |
| 501 |
approvals: Vec<Approval>, |
| 502 |
blockers: Vec<String>, |
| 503 |
depends_on: Vec<i64>, |
| 504 |
events: Vec<pdb::EventRow>, |
| 505 |
owners: Vec<String>, |
| 506 |
requirements: pdb::Requirements, |
| 507 |
landed_already: bool, |
| 508 |
can_edit: bool, |
| 509 |
can_land: bool, |
| 510 |
hub_can_land: bool, |
| 511 |
notice: Option<String>, |
| 512 |
error: Option<String>, |
| 513 |
heads: Vec<String>, |
| 514 |
state_hash: String, |
| 515 |
source_label: String, |
| 516 |
api_url: String, |
| 517 |
labels: Vec<db::issues::Label>, |
| 518 |
all_labels: Vec<db::issues::Label>, |
| 519 |
checks: Vec<db::hooks::Check>, |
| 520 |
required_checks: Vec<String>, |
| 521 |
} |
| 522 |
|
| 523 |
#[derive(Deserialize)] |
| 524 |
pub struct DetailQuery { |
| 525 |
tab: Option<String>, |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
#[allow(clippy::too_many_arguments)] |
| 531 |
async fn detail( |
| 532 |
app: &Shared, |
| 533 |
shell: Shell, |
| 534 |
viewer: &Viewer, |
| 535 |
owner: &str, |
| 536 |
name: &str, |
| 537 |
number: i64, |
| 538 |
tab: String, |
| 539 |
notice: Option<String>, |
| 540 |
error: Option<String>, |
| 541 |
) -> Result<Page<ProposalPage>, Error> { |
| 542 |
let (repo, row, role) = lookup(app, &shell, viewer, owner, name).await?; |
| 543 |
let p = pdb::get(&app.db, row.id, number) |
| 544 |
.await |
| 545 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 546 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 547 |
let loaded = load(app, &repo, p).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 548 |
|
| 549 |
if loaded.landed_already && loaded.row.is_open() { |
| 550 |
pdb::set_state(&app.db, loaded.row.id, "landed", &loaded.target_state).await.ok(); |
| 551 |
pdb::add_event(&app.db, loaded.row.id, "", "landed", "every change reached the target").await.ok(); |
| 552 |
} |
| 553 |
let p = pdb::get(&app.db, row.id, number).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(loaded.row.clone()); |
| 554 |
let v = verdict(app, &loaded, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 555 |
let comment_rows = pdb::comments(&app.db, loaded.row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 556 |
let events = pdb::events(&app.db, loaded.row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 557 |
|
| 558 |
let mut by_atom: HashMap<(String, String, u32), usize> = HashMap::new(); |
| 559 |
for ((path, line), (h, i)) in &loaded.atoms { |
| 560 |
by_atom.insert((path.clone(), h.clone(), *i), *line); |
| 561 |
} |
| 562 |
let mut comments = Vec::new(); |
| 563 |
let mut placed: HashMap<(String, usize), Vec<usize>> = HashMap::new(); |
| 564 |
let mut outdated = Vec::new(); |
| 565 |
for (idx, c) in comment_rows.into_iter().enumerate() { |
| 566 |
let line = if c.anchor_change.is_empty() { |
| 567 |
None |
| 568 |
} else { |
| 569 |
by_atom.get(&(c.path.clone(), c.anchor_change.clone(), c.anchor_index as u32)).copied() |
| 570 |
}; |
| 571 |
if !c.anchor_change.is_empty() { |
| 572 |
match line { |
| 573 |
Some(l) => placed.entry((c.path.clone(), l)).or_default().push(idx), |
| 574 |
None => outdated.push(idx), |
| 575 |
} |
| 576 |
} |
| 577 |
comments.push(CommentView { body_html: render::markdown::render(&c.body, "/", "/"), row: c, line }); |
| 578 |
} |
| 579 |
let mut changes = loaded.changes.clone(); |
| 580 |
super::attribute(app, &mut changes.iter_mut().collect::<Vec<_>>()).await; |
| 581 |
let me = viewer.account(); |
| 582 |
let can_edit = me.is_some_and(|a| Some(a.id) == p.author_id || role >= Role::Maintain); |
| 583 |
let can_land = role >= Role::Write && p.is_open() && v.blockers.is_empty(); |
| 584 |
let hub_can_land = app.repos.read(&repo, |r, _| Ok(r.current_policy()?.is_none())).await.unwrap_or(false); |
| 585 |
let source_label = match p.source_kind.as_str() { |
| 586 |
"fork" => format!( |
| 587 |
"fork {}", |
| 588 |
db::repos::get_by_id(&app.db, p.source_repo_id.unwrap_or(0)) |
| 589 |
.await |
| 590 |
.ok() |
| 591 |
.flatten() |
| 592 |
.map(|r| format!("{}:{}", r.slug(), p.source_bookmark)) |
| 593 |
.unwrap_or_default() |
| 594 |
), |
| 595 |
"changes" => format!("{} changes", p.source_changes().len()), |
| 596 |
_ => p.source_bookmark.clone(), |
| 597 |
}; |
| 598 |
Ok(Page(ProposalPage { |
| 599 |
shell, |
| 600 |
repo: RepoShell::new(app, &row, "proposals").with_role(role), |
| 601 |
body_html: super::issues::markdown(app, row.id, &format!("/{owner}/{name}"), &p.body).await, |
| 602 |
p, |
| 603 |
tab, |
| 604 |
changes, |
| 605 |
files: loaded.files, |
| 606 |
conflicts: loaded.conflicts, |
| 607 |
conflicted: loaded.conflicted, |
| 608 |
comments, |
| 609 |
placed, |
| 610 |
outdated, |
| 611 |
reviews: v.reviews, |
| 612 |
approvals: v.approvals, |
| 613 |
blockers: v.blockers, |
| 614 |
depends_on: v.depends_on, |
| 615 |
events, |
| 616 |
owners: loaded.owners, |
| 617 |
requirements: v.requirements, |
| 618 |
landed_already: loaded.landed_already, |
| 619 |
can_edit, |
| 620 |
can_land, |
| 621 |
hub_can_land, |
| 622 |
notice, |
| 623 |
error, |
| 624 |
heads: loaded.heads.iter().map(|h| h.to_hex()).collect(), |
| 625 |
state_hash: loaded.state.clone(), |
| 626 |
source_label, |
| 627 |
api_url: format!("{}/api/v1/repos/{owner}/{name}/proposals/{number}", app.config.base_url()), |
| 628 |
labels: db::issues::proposal_labels(&app.db, loaded.row.id).await.unwrap_or_default(), |
| 629 |
all_labels: db::issues::labels(&app.db, row.id).await.unwrap_or_default(), |
| 630 |
checks: db::hooks::checks_for_state(&app.db, row.id, &loaded.state).await.unwrap_or_default(), |
| 631 |
required_checks: db::hooks::required_checks(&app.db, row.id).await.unwrap_or_default(), |
| 632 |
})) |
| 633 |
} |
| 634 |
|
| 635 |
pub async fn show( |
| 636 |
State(app): State<Shared>, |
| 637 |
viewer: Viewer, |
| 638 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 639 |
Query(q): Query<DetailQuery>, |
| 640 |
) -> Result<Page<ProposalPage>, Error> { |
| 641 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 642 |
detail(&app, shell, &viewer, &owner, &name, number, q.tab.unwrap_or_else(|| "conversation".into()), None, None).await |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
#[derive(Deserialize)] |
| 648 |
pub struct CommentForm { |
| 649 |
#[serde(rename = "_csrf")] |
| 650 |
csrf_token: String, |
| 651 |
body: String, |
| 652 |
#[serde(default)] |
| 653 |
path: String, |
| 654 |
#[serde(default)] |
| 655 |
line: Option<usize>, |
| 656 |
} |
| 657 |
|
| 658 |
pub async fn comment( |
| 659 |
State(app): State<Shared>, |
| 660 |
viewer: Viewer, |
| 661 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 662 |
Form(f): Form<CommentForm>, |
| 663 |
) -> Result<Page<ProposalPage>, Error> { |
| 664 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 665 |
let me = require(&viewer, &shell)?; |
| 666 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 667 |
let (repo, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 668 |
let p = pdb::get(&app.db, row.id, number) |
| 669 |
.await |
| 670 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 671 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 672 |
if f.body.trim().is_empty() { |
| 673 |
return detail( |
| 674 |
&app, |
| 675 |
shell, |
| 676 |
&viewer, |
| 677 |
&owner, |
| 678 |
&name, |
| 679 |
number, |
| 680 |
"conversation".into(), |
| 681 |
None, |
| 682 |
Some("An empty comment says nothing.".into()), |
| 683 |
) |
| 684 |
.await; |
| 685 |
} |
| 686 |
let anchor = match (f.path.trim(), f.line) { |
| 687 |
("", _) | (_, None) => None, |
| 688 |
(path, Some(line)) => { |
| 689 |
let loaded = load(&app, &repo, p.clone()).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 690 |
loaded.atoms.get(&(path.to_string(), line)).cloned() |
| 691 |
} |
| 692 |
}; |
| 693 |
let tab = if anchor.is_some() { "files" } else { "conversation" }; |
| 694 |
pdb::add_comment( |
| 695 |
&app.db, |
| 696 |
p.id, |
| 697 |
Some(me.account.id), |
| 698 |
&me.account.username, |
| 699 |
f.body.trim(), |
| 700 |
f.path.trim(), |
| 701 |
anchor.as_ref().map(|(h, i)| (h.as_str(), *i as i64)), |
| 702 |
) |
| 703 |
.await |
| 704 |
.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 705 |
detail(&app, shell, &viewer, &owner, &name, number, tab.into(), None, None).await |
| 706 |
} |
| 707 |
|
| 708 |
#[derive(Deserialize)] |
| 709 |
pub struct ReviewForm { |
| 710 |
#[serde(rename = "_csrf")] |
| 711 |
csrf_token: String, |
| 712 |
verdict: String, |
| 713 |
#[serde(default)] |
| 714 |
body: String, |
| 715 |
} |
| 716 |
|
| 717 |
pub async fn review( |
| 718 |
State(app): State<Shared>, |
| 719 |
viewer: Viewer, |
| 720 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 721 |
Form(f): Form<ReviewForm>, |
| 722 |
) -> Result<Page<ProposalPage>, Error> { |
| 723 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 724 |
let me = require(&viewer, &shell)?; |
| 725 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 726 |
let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 727 |
let p = pdb::get(&app.db, row.id, number) |
| 728 |
.await |
| 729 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 730 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 731 |
if !matches!(f.verdict.as_str(), "comment" | "changes" | "approve") { |
| 732 |
return Err(Error::bad(shell, "a review is comment, changes or approve")); |
| 733 |
} |
| 734 |
if f.verdict == "approve" && Some(me.account.id) == p.author_id { |
| 735 |
return detail( |
| 736 |
&app, |
| 737 |
shell, |
| 738 |
&viewer, |
| 739 |
&owner, |
| 740 |
&name, |
| 741 |
number, |
| 742 |
"conversation".into(), |
| 743 |
None, |
| 744 |
Some("You cannot approve your own proposal.".into()), |
| 745 |
) |
| 746 |
.await; |
| 747 |
} |
| 748 |
pdb::add_review(&app.db, p.id, me.account.id, &me.account.username, &f.verdict, f.body.trim()) |
| 749 |
.await |
| 750 |
.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 751 |
pdb::add_event(&app.db, p.id, &me.account.username, &format!("review.{}", f.verdict), "").await.ok(); |
| 752 |
crate::hooks::emit(&app, &row, "review", &me.account.username, serde_json::json!({ "number": number, "verdict": f.verdict })).await; |
| 753 |
let notice = if f.verdict == "approve" { |
| 754 |
Some(format!( |
| 755 |
"Approval recorded. It counts once you seal the changes: oxy seal --proposal {}/{owner}/{name}/proposal/{number}", |
| 756 |
app.config.base_url() |
| 757 |
)) |
| 758 |
} else { |
| 759 |
None |
| 760 |
}; |
| 761 |
detail(&app, shell, &viewer, &owner, &name, number, "conversation".into(), notice, None).await |
| 762 |
} |
| 763 |
|
| 764 |
#[derive(Deserialize)] |
| 765 |
pub struct EditForm { |
| 766 |
#[serde(rename = "_csrf")] |
| 767 |
csrf_token: String, |
| 768 |
title: String, |
| 769 |
#[serde(default)] |
| 770 |
body: String, |
| 771 |
target: String, |
| 772 |
} |
| 773 |
|
| 774 |
pub async fn edit( |
| 775 |
State(app): State<Shared>, |
| 776 |
viewer: Viewer, |
| 777 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 778 |
Form(f): Form<EditForm>, |
| 779 |
) -> Result<Page<ProposalPage>, Error> { |
| 780 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 781 |
let me = require(&viewer, &shell)?; |
| 782 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 783 |
let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 784 |
let p = pdb::get(&app.db, row.id, number) |
| 785 |
.await |
| 786 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 787 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 788 |
if Some(me.account.id) != p.author_id && role < Role::Maintain { |
| 789 |
return Err(Error::not_found(shell)); |
| 790 |
} |
| 791 |
let target = if crate::repos::names::valid_segment(f.target.trim()) { f.target.trim() } else { &p.target }; |
| 792 |
pdb::edit(&app.db, p.id, f.title.trim(), f.body.trim(), target).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 793 |
pdb::add_event(&app.db, p.id, &me.account.username, "edited", "").await.ok(); |
| 794 |
detail(&app, shell, &viewer, &owner, &name, number, "conversation".into(), Some("Saved.".into()), None).await |
| 795 |
} |
| 796 |
|
| 797 |
#[derive(Deserialize)] |
| 798 |
pub struct StateForm { |
| 799 |
#[serde(rename = "_csrf")] |
| 800 |
csrf_token: String, |
| 801 |
action: String, |
| 802 |
} |
| 803 |
|
| 804 |
pub async fn state( |
| 805 |
State(app): State<Shared>, |
| 806 |
viewer: Viewer, |
| 807 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 808 |
Form(f): Form<StateForm>, |
| 809 |
) -> Result<Page<ProposalPage>, Error> { |
| 810 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 811 |
let me = require(&viewer, &shell)?; |
| 812 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 813 |
let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 814 |
let p = pdb::get(&app.db, row.id, number) |
| 815 |
.await |
| 816 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 817 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 818 |
if Some(me.account.id) != p.author_id && role < Role::Triage { |
| 819 |
return Err(Error::not_found(shell)); |
| 820 |
} |
| 821 |
let new_state = match (f.action.as_str(), p.state.as_str()) { |
| 822 |
("close", "open" | "draft") => "closed", |
| 823 |
("reopen", "closed") => "open", |
| 824 |
("ready", "draft") => "open", |
| 825 |
("draft", "open") => "draft", |
| 826 |
_ => return Err(Error::bad(shell, "that transition is not possible")), |
| 827 |
}; |
| 828 |
pdb::set_state(&app.db, p.id, new_state, "").await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 829 |
pdb::add_event(&app.db, p.id, &me.account.username, new_state, "").await.ok(); |
| 830 |
crate::hooks::emit(&app, &row, &format!("proposal.{new_state}"), &me.account.username, serde_json::json!({ "number": number })).await; |
| 831 |
detail(&app, shell, &viewer, &owner, &name, number, "conversation".into(), None, None).await |
| 832 |
} |
| 833 |
|
| 834 |
pub async fn resolve_comment( |
| 835 |
State(app): State<Shared>, |
| 836 |
viewer: Viewer, |
| 837 |
Path((owner, name, number, id)): Path<(String, String, i64, i64)>, |
| 838 |
Form(f): Form<super::session::CsrfForm>, |
| 839 |
) -> Result<Page<ProposalPage>, Error> { |
| 840 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 841 |
require(&viewer, &shell)?; |
| 842 |
csrf(&viewer, &f.csrf, &shell)?; |
| 843 |
let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 844 |
let p = pdb::get(&app.db, row.id, number) |
| 845 |
.await |
| 846 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 847 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 848 |
let current: Option<bool> = sqlx::query_scalar("SELECT resolved FROM proposal_comments WHERE id = ? AND proposal_id = ?") |
| 849 |
.bind(id) |
| 850 |
.bind(p.id) |
| 851 |
.fetch_optional(&app.db) |
| 852 |
.await |
| 853 |
.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 854 |
if let Some(r) = current { |
| 855 |
pdb::set_comment_resolved(&app.db, id, !r).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 856 |
} |
| 857 |
detail(&app, shell, &viewer, &owner, &name, number, "files".into(), None, None).await |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
pub async fn land( |
| 863 |
State(app): State<Shared>, |
| 864 |
viewer: Viewer, |
| 865 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 866 |
Form(f): Form<super::session::CsrfForm>, |
| 867 |
) -> Result<Page<ProposalPage>, Error> { |
| 868 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 869 |
let me = require(&viewer, &shell)?; |
| 870 |
csrf(&viewer, &f.csrf, &shell)?; |
| 871 |
let (repo, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 872 |
let p = pdb::get(&app.db, row.id, number) |
| 873 |
.await |
| 874 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 875 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 876 |
let loaded = load(&app, &repo, p.clone()).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 877 |
let v = verdict(&app, &loaded, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 878 |
if !p.is_open() || !v.blockers.is_empty() { |
| 879 |
return detail( |
| 880 |
&app, |
| 881 |
shell, |
| 882 |
&viewer, |
| 883 |
&owner, |
| 884 |
&name, |
| 885 |
number, |
| 886 |
"conversation".into(), |
| 887 |
None, |
| 888 |
Some(format!("Cannot land: {}", v.blockers.join("; "))), |
| 889 |
) |
| 890 |
.await; |
| 891 |
} |
| 892 |
let target = p.target.clone(); |
| 893 |
let heads = loaded.heads.clone(); |
| 894 |
let result = app |
| 895 |
.repos |
| 896 |
.write(&repo, move |r, _| { |
| 897 |
if r.current_policy()?.is_some() { |
| 898 |
return Err(crate::repos::RepoError::Oxy(oxy_core::repo::RepoError::Msg( |
| 899 |
"this repository has a trust policy, so a reference update must be signed by an authorised key: land from a client with oxy proposal land".into(), |
| 900 |
))); |
| 901 |
} |
| 902 |
r.bookmark_set(&target, &heads)?; |
| 903 |
Ok(pl::state_ref(&heads).to_hex()) |
| 904 |
}) |
| 905 |
.await; |
| 906 |
match result { |
| 907 |
Ok(state) => { |
| 908 |
pdb::set_state(&app.db, p.id, "landed", &state).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 909 |
pdb::add_event(&app.db, p.id, &me.account.username, "landed", "from the hub").await.ok(); |
| 910 |
crate::hooks::emit( |
| 911 |
&app, |
| 912 |
&row, |
| 913 |
"proposal.landed", |
| 914 |
&me.account.username, |
| 915 |
serde_json::json!({ "number": number, "target": p.target, "state": state }), |
| 916 |
) |
| 917 |
.await; |
| 918 |
crate::notify::deliver( |
| 919 |
&app, |
| 920 |
crate::notify::Event { |
| 921 |
kind: "proposal.landed", |
| 922 |
title: format!("{owner}/{name}#{number}: {} landed", p.title), |
| 923 |
url: format!("/{owner}/{name}/proposal/{number}"), |
| 924 |
actor: &me.account, |
| 925 |
repo_id: row.id, |
| 926 |
thread: Some(("proposal", p.id)), |
| 927 |
mention_text: "", |
| 928 |
direct: p.author_id.into_iter().collect(), |
| 929 |
}, |
| 930 |
) |
| 931 |
.await; |
| 932 |
if p.target == row.default_bookmark { |
| 933 |
let messages: Vec<(String, String)> = |
| 934 |
loaded.changes.iter().map(|c| (c.hash.clone(), format!("{}\n{}", c.title, c.body))).collect(); |
| 935 |
super::issues::close_from_messages(&app, &row, &messages).await; |
| 936 |
} |
| 937 |
accounts::audit(&app.db, Some(&me.account), "proposal.landed", &format!("{owner}/{name}#{number}"), "").await.ok(); |
| 938 |
db::repos::record_push( |
| 939 |
&app.db, |
| 940 |
row.id, |
| 941 |
Some((me.account.id, me.account.username.as_str())), |
| 942 |
&[db::repos::PushOutcome { |
| 943 |
reference: p.target.clone(), |
| 944 |
ok: true, |
| 945 |
reason: format!("landed #{number}"), |
| 946 |
old_state: loaded.target_state.clone(), |
| 947 |
new_state: state, |
| 948 |
heads: loaded.heads.len() as i64, |
| 949 |
changes: loaded.hashes.len() as i64, |
| 950 |
}], |
| 951 |
) |
| 952 |
.await |
| 953 |
.ok(); |
| 954 |
detail(&app, shell, &viewer, &owner, &name, number, "conversation".into(), Some("Landed.".into()), None).await |
| 955 |
} |
| 956 |
Err(e) => detail(&app, shell, &viewer, &owner, &name, number, "conversation".into(), None, Some(e.to_string())).await, |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
#[derive(Deserialize)] |
| 961 |
pub struct LabelsForm { |
| 962 |
#[serde(rename = "_csrf")] |
| 963 |
csrf_token: String, |
| 964 |
#[serde(default)] |
| 965 |
label: Vec<i64>, |
| 966 |
} |
| 967 |
|
| 968 |
pub async fn set_labels( |
| 969 |
State(app): State<Shared>, |
| 970 |
viewer: Viewer, |
| 971 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 972 |
axum_extra::extract::Form(f): axum_extra::extract::Form<LabelsForm>, |
| 973 |
) -> Result<Page<ProposalPage>, Error> { |
| 974 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 975 |
require(&viewer, &shell)?; |
| 976 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 977 |
let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 978 |
if role < Role::Triage { |
| 979 |
return Err(Error::not_found(shell)); |
| 980 |
} |
| 981 |
let p = pdb::get(&app.db, row.id, number) |
| 982 |
.await |
| 983 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 984 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 985 |
for l in db::issues::labels(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 986 |
db::issues::set_proposal_label(&app.db, p.id, l.id, f.label.contains(&l.id)).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 987 |
} |
| 988 |
detail(&app, shell, &viewer, &owner, &name, number, "conversation".into(), None, None).await |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
pub async fn events( |
| 994 |
State(app): State<Shared>, |
| 995 |
viewer: Viewer, |
| 996 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 997 |
) -> Result<axum::response::Sse<impl futures::Stream<Item = Result<axum::response::sse::Event, std::convert::Infallible>>>, Error> { |
| 998 |
use futures::StreamExt; |
| 999 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/proposal/{number}"), &viewer); |
| 1000 |
let (_, row, _) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 1001 |
let rx = app.events.proposals.subscribe(); |
| 1002 |
let repo_id = row.id; |
| 1003 |
let stream = tokio_stream::wrappers::BroadcastStream::new(rx).filter_map(move |item| async move { |
| 1004 |
match item { |
| 1005 |
Ok((id, ev)) if id == repo_id => { |
| 1006 |
Some(Ok(axum::response::sse::Event::default().event(ev.kind.clone()).data(serde_json::to_string(&ev).unwrap_or_default()))) |
| 1007 |
} |
| 1008 |
_ => None, |
| 1009 |
} |
| 1010 |
}); |
| 1011 |
Ok(axum::response::Sse::new(stream).keep_alive(axum::response::sse::KeepAlive::default())) |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
pub async fn to_api(app: &Shared, owner: &str, name: &str, loaded: &Loaded, v: &Verdict, source_url: Option<String>) -> hub_api::Proposal { |
| 1017 |
let p = &loaded.row; |
| 1018 |
hub_api::Proposal { |
| 1019 |
number: p.number, |
| 1020 |
title: p.title.clone(), |
| 1021 |
body: p.body.clone(), |
| 1022 |
author: p.author.clone(), |
| 1023 |
state: ProposalState::parse(&p.state).unwrap_or(ProposalState::Open), |
| 1024 |
target: p.target.clone(), |
| 1025 |
source: match p.source_kind.as_str() { |
| 1026 |
"fork" => { |
| 1027 |
let (o, n) = source_url |
| 1028 |
.as_deref() |
| 1029 |
.and_then(|u| u.rsplit_once('/')) |
| 1030 |
.and_then(|(b, n)| b.rsplit_once('/').map(|(_, o)| (o.to_string(), n.to_string()))) |
| 1031 |
.unwrap_or_default(); |
| 1032 |
ProposalSource::Fork { owner: o, name: n, bookmark: p.source_bookmark.clone() } |
| 1033 |
} |
| 1034 |
"changes" => ProposalSource::Changes { hashes: p.source_changes() }, |
| 1035 |
_ => ProposalSource::Bookmark { bookmark: p.source_bookmark.clone() }, |
| 1036 |
}, |
| 1037 |
changes: loaded |
| 1038 |
.changes |
| 1039 |
.iter() |
| 1040 |
.map(|c| hub_api::ChangeSummary { |
| 1041 |
hash: c.hash.clone(), |
| 1042 |
message: if c.body.is_empty() { c.title.clone() } else { format!("{}\n\n{}", c.title, c.body) }, |
| 1043 |
author: hub_api::Author { |
| 1044 |
name: c.author.name.clone(), |
| 1045 |
email: c.author.email.clone(), |
| 1046 |
key: c.author.key.clone(), |
| 1047 |
account: c.author.account.clone(), |
| 1048 |
}, |
| 1049 |
when_ms: c.when_ms, |
| 1050 |
deps: c.deps.clone(), |
| 1051 |
head: c.head, |
| 1052 |
signature: match &c.signature { |
| 1053 |
view::SigView::Unsigned => hub_api::Signature::Unsigned, |
| 1054 |
view::SigView::Valid { key, sealed, revoked, .. } => { |
| 1055 |
hub_api::Signature::Valid { key: key.clone(), sealed: *sealed, revoked: *revoked } |
| 1056 |
} |
| 1057 |
view::SigView::Invalid(why) => hub_api::Signature::Invalid { reason: why.clone() }, |
| 1058 |
}, |
| 1059 |
}) |
| 1060 |
.collect(), |
| 1061 |
projected_heads: loaded.heads.iter().map(|h| h.to_hex()).collect(), |
| 1062 |
projected_state: Some(loaded.state.clone()), |
| 1063 |
conflicted: loaded.conflicted, |
| 1064 |
approvals: v.approvals.clone(), |
| 1065 |
landable: p.is_open() && v.blockers.is_empty(), |
| 1066 |
blockers: v.blockers.clone(), |
| 1067 |
created_ms: p.created_ms, |
| 1068 |
updated_ms: p.updated_ms, |
| 1069 |
url: format!("{}/{owner}/{name}/proposal/{}", app.config.base_url(), p.number), |
| 1070 |
repository_url: format!("{}/{owner}/{name}", app.config.base_url()), |
| 1071 |
source_url, |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
pub fn key(path: &str, line: impl filters::Num) -> (String, usize) { |
| 1077 |
(path.to_string(), line.as_u64() as usize) |
| 1078 |
} |
| 1079 |
|
| 1080 |
#[allow(dead_code)] |
| 1081 |
fn _unused(_: &diff::Line, _: &ConflictView, _: &Role) {} |