| 1 | |
|
| 2 | |
|
| 3 | | use super::settings::{csrf, require}; |
| 4 | | use super::{filters, lookup_role, Error, Page, RepoShell, Shell}; |
| 5 | | use crate::auth::{roles, Access, Role, Viewer}; |
| 6 | | use crate::db::{self, accounts}; |
| 7 | | use crate::repos::RepoName; |
| 8 | | use crate::Shared; |
| 9 | | use askama::Template; |
| 10 | | use axum::extract::{Form, Path, Query, State}; |
| 11 | | use axum::http::Uri; |
| 12 | | use axum::response::{IntoResponse, Redirect, Response}; |
| 13 | | use hub_api::Visibility; |
| 14 | | use serde::Deserialize; |
| 15 | |
|
| 16 | | #[derive(Template)] |
| 17 | | #[template(path = "repo_new.html")] |
| 18 | | pub struct NewRepoPage { |
| 19 | | shell: Shell, |
| 20 | | owners: Vec<String>, |
| 21 | | error: Option<String>, |
| 22 | | |
| 23 | | |
| 24 | | boards: Vec<(String, i64, String)>, |
| 25 | | |
| 26 | | orgs: Vec<String>, |
| 27 | | licences: &'static [(&'static str, &'static str)], |
| 28 | | ignores: &'static [&'static str], |
| 29 | | base_url: String, |
| 30 | | |
| 31 | | admin: bool, |
| 32 | | |
| 33 | | runner_owners: Vec<String>, |
| 34 | | |
| 35 | | bookmarks: Vec<(String, String, String)>, |
| 36 | | } |
| 37 | |
|
| 38 | |
|
| 39 | 51× | async fn new_page(app: &Shared, shell: Shell, me: &accounts::Account, error: Option<String>) -> NewRepoPage { |
| 40 | 51× | let owners = owners_for(app, me).await; |
| 41 | 51× | let mut boards = Vec::new(); |
| 42 | 51× | let mut orgs = Vec::new(); |
| 43 | 51× | let mut runner_owners = Vec::new(); |
| 44 | 51× | let mut bookmarks = Vec::new(); |
| 45 | 62× | for o in &owners { |
| 46 | 62× | let (b, from) = crate::repo_setup::default_bookmark_for(app, o).await; |
| 47 | 62× | bookmarks.push((o.clone(), b, from)); |
| 48 | 62× | if crate::repo_setup::owner_has_runner(app, o).await.unwrap_or(false) { |
| 49 | 0× | runner_owners.push(o.clone()); |
| 50 | 62× | } |
| 51 | 62× | if accounts::org_by_name(&app.db, o).await.ok().flatten().is_some() { |
| 52 | 11× | orgs.push(o.clone()); |
| 53 | 51× | } |
| 54 | 62× | let may = crate::web::projects::rights_of(app, Some(me), o).await.ok().flatten().is_some_and(|r| r.edit); |
| 55 | 62× | if may { |
| 56 | 62× | for p in db::projects::of_owner(&app.db, o).await.unwrap_or_default().into_iter().filter(|p| !p.closed) { |
| 57 | 3× | boards.push((o.clone(), p.number, p.title)); |
| 58 | 3× | } |
| 59 | 0× | } |
| 60 | | } |
| 61 | 51× | NewRepoPage { |
| 62 | 51× | shell, |
| 63 | 51× | owners, |
| 64 | 51× | error, |
| 65 | 51× | boards, |
| 66 | 51× | orgs, |
| 67 | 51× | licences: crate::repo_setup::LICENCES, |
| 68 | 51× | ignores: crate::repo_setup::IGNORES, |
| 69 | 51× | base_url: app.config.base_url().trim_end_matches('/').to_string(), |
| 70 | 51× | admin: me.is_admin, |
| 71 | 51× | runner_owners, |
| 72 | 51× | bookmarks, |
| 73 | 51× | } |
| 74 | 51× | } |
| 75 | |
|
| 76 | 54× | pub(crate) async fn owners_for(app: &Shared, me: &accounts::Account) -> Vec<String> { |
| 77 | 54× | let mut owners = vec![me.username.clone()]; |
| 78 | 54× | if let Ok(orgs) = accounts::orgs_of(&app.db, me.id).await { |
| 79 | 54× | owners.extend(orgs.into_iter().map(|(o, _)| o.name)); |
| 80 | 0× | } |
| 81 | 54× | owners |
| 82 | 54× | } |
| 83 | |
|
| 84 | 25× | pub async fn new_form(State(app): State<Shared>, viewer: Viewer) -> Result<Page<NewRepoPage>, Error> { |
| 85 | 25× | let shell = Shell::new(&app, "/new", &viewer); |
| 86 | 25× | let me = require(&viewer, &shell)?; |
| 87 | 25× | Ok(Page(new_page(&app, shell, &me.account, None).await)) |
| 88 | 25× | } |
| 89 | |
|
| 90 | | #[derive(Deserialize)] |
| 91 | | pub struct NameCheck { |
| 92 | | #[serde(default)] |
| 93 | | owner: String, |
| 94 | | #[serde(default)] |
| 95 | | name: String, |
| 96 | | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | 3× | pub async fn check_name(State(app): State<Shared>, viewer: Viewer, Query(q): Query<NameCheck>) -> Response { |
| 101 | 3× | let Some(me) = viewer.account() else { |
| 102 | 0× | return (axum::http::StatusCode::UNAUTHORIZED, axum::Json(serde_json::json!({"ok": false, "message": "Sign in first."}))) |
| 103 | 0× | .into_response(); |
| 104 | | }; |
| 105 | 3× | if q.name.trim().is_empty() { |
| 106 | 0× | return axum::Json(serde_json::json!({"ok": false, "message": ""})).into_response(); |
| 107 | 3× | } |
| 108 | 3× | match crate::repo_setup::check_name(&app, me, &q.owner, &q.name).await { |
| 109 | 1× | Ok(n) => axum::Json(serde_json::json!({ |
| 110 | 1× | "ok": true, |
| 111 | 1× | "message": format!("{n} is free."), |
| 112 | 1× | "address": format!("{}{}", app.config.base_url().trim_end_matches('/'), n.url()), |
| 113 | 1× | })) |
| 114 | 1× | .into_response(), |
| 115 | 2× | Err(m) => axum::Json(serde_json::json!({"ok": false, "message": m})).into_response(), |
| 116 | | } |
| 117 | 3× | } |
| 118 | |
|
| 119 | | #[derive(Deserialize)] |
| 120 | | pub struct NewRepoForm { |
| 121 | | #[serde(rename = "_csrf")] |
| 122 | | csrf_token: String, |
| 123 | | owner: String, |
| 124 | | name: String, |
| 125 | | #[serde(default)] |
| 126 | | description: String, |
| 127 | | #[serde(default = "default_visibility")] |
| 128 | | visibility: String, |
| 129 | | |
| 130 | | #[serde(default)] |
| 131 | | storage: String, |
| 132 | | #[serde(flatten)] |
| 133 | | bucket: BucketForm, |
| 134 | | |
| 135 | | #[serde(default)] |
| 136 | | readme: Option<String>, |
| 137 | | #[serde(default)] |
| 138 | | licence: String, |
| 139 | | #[serde(default)] |
| 140 | | ignore: String, |
| 141 | | |
| 142 | | #[serde(default)] |
| 143 | | board: String, |
| 144 | | #[serde(default)] |
| 145 | | board_title: String, |
| 146 | | |
| 147 | | #[serde(default)] |
| 148 | | board_n: String, |
| 149 | | |
| 150 | | #[serde(default)] |
| 151 | | flow: String, |
| 152 | | |
| 153 | | #[serde(default)] |
| 154 | | wiki: Option<String>, |
| 155 | | |
| 156 | | #[serde(default)] |
| 157 | | default_bookmark: String, |
| 158 | | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | | #[derive(Deserialize, Default)] |
| 163 | | pub struct BucketForm { |
| 164 | | #[serde(default)] |
| 165 | | store_url: String, |
| 166 | | #[serde(default)] |
| 167 | | access_key_id: String, |
| 168 | | #[serde(default)] |
| 169 | | secret_access_key: String, |
| 170 | | #[serde(default)] |
| 171 | | service_account_key: String, |
| 172 | | #[serde(default)] |
| 173 | | tenant_id: String, |
| 174 | | #[serde(default)] |
| 175 | | client_id: String, |
| 176 | | #[serde(default)] |
| 177 | | client_secret: String, |
| 178 | | } |
| 179 | |
|
| 180 | | impl BucketForm { |
| 181 | 1× | fn credentials(&self, scheme: &str) -> Result<oxy_strata::Credentials, String> { |
| 182 | 1× | crate::storage::credentials_from( |
| 183 | 1× | scheme, |
| 184 | 1× | &self.access_key_id, |
| 185 | 1× | &self.secret_access_key, |
| 186 | 1× | &self.service_account_key, |
| 187 | 1× | &self.tenant_id, |
| 188 | 1× | &self.client_id, |
| 189 | 1× | &self.client_secret, |
| 190 | | ) |
| 191 | 1× | } |
| 192 | | } |
| 193 | |
|
| 194 | 18× | fn default_visibility() -> String { |
| 195 | 18× | "public".into() |
| 196 | 18× | } |
| 197 | |
|
| 198 | 26× | pub async fn create(State(app): State<Shared>, viewer: Viewer, Form(f): Form<NewRepoForm>) -> Result<Response, Error> { |
| 199 | 26× | let shell = Shell::new(&app, "/new", &viewer); |
| 200 | 26× | let me = require(&viewer, &shell)?; |
| 201 | 26× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 202 | 26× | let page = new_page(&app, shell.clone(), &me.account, None).await; |
| 203 | 26× | let fail = |msg: &str| Page(NewRepoPage { error: Some(msg.into()), ..clone_page(&page) }).into_response(); |
| 204 | 26× | let name = match crate::repo_setup::check_name(&app, &me.account, &f.owner, &f.name).await { |
| 205 | 24× | Ok(n) => n, |
| 206 | 2× | Err(m) => return Ok(fail(&m)), |
| 207 | | }; |
| 208 | 24× | let Some(visibility) = Visibility::parse(&f.visibility) else { return Ok(fail("Visibility is public, internal or private.")) }; |
| 209 | | |
| 210 | 24× | let choice = if f.board == "existing" { f.board_n.as_str() } else { f.board.as_str() }; |
| 211 | 24× | let board = match crate::repo_setup::Board::parse(choice, &f.board_title, &name.name) { |
| 212 | 24× | Ok(b) => b, |
| 213 | 0× | Err(m) => return Ok(fail(&m)), |
| 214 | | }; |
| 215 | 24× | if let Err(m) = crate::repo_setup::check_board(&app, &me.account, &name.owner, &board).await { |
| 216 | 0× | return Ok(fail(&m)); |
| 217 | 24× | } |
| 218 | 24× | let flow = match crate::repo_setup::Flow::parse(&f.flow) { |
| 219 | 24× | Ok(flow) => flow, |
| 220 | 0× | Err(m) => return Ok(fail(&m)), |
| 221 | | }; |
| 222 | 24× | if let Err(m) = crate::repo_setup::check_flow(&app, &me.account, &name.owner, flow).await { |
| 223 | 0× | return Ok(fail(&m)); |
| 224 | 24× | } |
| 225 | 24× | let wiki = f.wiki.is_some(); |
| 226 | 24× | if !f.default_bookmark.trim().is_empty() { |
| 227 | 0× | if let Err(m) = crate::repo_setup::check_bookmark(f.default_bookmark.trim()) { |
| 228 | 0× | return Ok(fail(&m)); |
| 229 | 0× | } |
| 230 | 24× | } |
| 231 | 24× | let start = |
| 232 | 24× | crate::repo_setup::Start { readme: f.readme.is_some(), licence: f.licence.trim().to_string(), ignore: f.ignore.trim().to_string() }; |
| 233 | 24× | let year = time::OffsetDateTime::now_utc().year(); |
| 234 | 24× | let holder = if me.account.display_name.trim().is_empty() { me.account.username.clone() } else { me.account.display_name.clone() }; |
| 235 | 24× | let files = match start.files(&name, &f.description, &holder, year) { |
| 236 | 24× | Ok(files) => files, |
| 237 | 0× | Err(m) => return Ok(fail(&m)), |
| 238 | | }; |
| 239 | 24× | if !files.is_empty() && f.storage == "bucket" { |
| 240 | 0× | return Ok(fail("A repository kept in your own bucket starts empty: push its first files to it.")); |
| 241 | 24× | } |
| 242 | 24× | if !files.is_empty() || wiki { |
| 243 | 1× | if let Err(m) = crate::web::edit::signer_for(&app, me.account.id).await { |
| 244 | 1× | return Ok(fail(&format!("The hub cannot write the starting files or the wiki for you yet. {m}"))); |
| 245 | 0× | } |
| 246 | 23× | } |
| 247 | | |
| 248 | 23× | if let Some(org) = accounts::org_by_name(&app.db, &name.owner).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 249 | 5× | let policy = crate::storage::policy(&app.db, org.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 250 | 5× | let store = (f.storage == "bucket").then_some(f.bucket.store_url.as_str()); |
| 251 | 5× | if let Err(e) = policy.permits(&org.name, store) { |
| 252 | 2× | return Ok(fail(&format!("{e}."))); |
| 253 | 3× | } |
| 254 | 18× | } |
| 255 | 21× | if f.storage == "bucket" { |
| 256 | | |
| 257 | | |
| 258 | 6× | let url = f.bucket.store_url.trim(); |
| 259 | 6× | let parsed = match crate::storage::check_url(url) { |
| 260 | 1× | Ok(p) => p, |
| 261 | 5× | Err(e) => return Ok(fail(&format!("Store: {e}."))), |
| 262 | | }; |
| 263 | 1× | let credentials = match f.bucket.credentials(&parsed.scheme) { |
| 264 | 0× | Ok(c) => c, |
| 265 | 1× | Err(e) => return Ok(fail(&format!("Credentials: {e}."))), |
| 266 | | }; |
| 267 | 0× | let row = db::repos::create(&app.db, &name.owner, &name.name, f.description.trim(), visibility) |
| 268 | 0× | .await |
| 269 | 0× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 270 | 0× | let row = |
| 271 | 0× | crate::repo_setup::set_default_bookmark(&app, row, &f.default_bookmark).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 272 | 0× | if let Err(e) = crate::storage::create(&app, &name, row.id, url, credentials).await { |
| 273 | 0× | db::repos::delete(&app.db, row.id).await.ok(); |
| 274 | 0× | return Ok(fail(&format!("The bucket did not pass the check, so nothing was created: {e}"))); |
| 275 | 0× | } |
| 276 | 0× | accounts::audit( |
| 277 | 0× | &app.db, |
| 278 | 0× | Some(&me.account), |
| 279 | 0× | "repo.created", |
| 280 | 0× | &name.to_string(), |
| 281 | 0× | &format!("{} in {}", visibility.as_str(), parsed.plain()), |
| 282 | 0× | ) |
| 283 | 0× | .await |
| 284 | 0× | .ok(); |
| 285 | 0× | crate::repo_setup::link_board(&app, &row, &board, visibility == Visibility::Public) |
| 286 | 0× | .await |
| 287 | 0× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 288 | 0× | crate::repo_setup::set_flow(&app, &row, &me.account, flow).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 289 | 0× | if wiki { |
| 290 | 0× | if let Err(e) = crate::repo_setup::start_wiki(&app, &row, &me.account, &f.description).await { |
| 291 | 0× | tracing::warn!("wiki for {name}: {e}"); |
| 292 | 0× | } |
| 293 | 0× | } |
| 294 | 0× | return Ok(Redirect::to(&name.url()).into_response()); |
| 295 | 15× | } |
| 296 | 15× | app.repos.create_async(&name, None).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 297 | 15× | let row = db::repos::create(&app.db, &name.owner, &name.name, f.description.trim(), visibility) |
| 298 | 15× | .await |
| 299 | 15× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 300 | 15× | let row = |
| 301 | 15× | crate::repo_setup::set_default_bookmark(&app, row, &f.default_bookmark).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 302 | 15× | accounts::audit(&app.db, Some(&me.account), "repo.created", &name.to_string(), visibility.as_str()).await.ok(); |
| 303 | 15× | crate::repo_setup::link_board(&app, &row, &board, visibility == Visibility::Public) |
| 304 | 15× | .await |
| 305 | 15× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 306 | 15× | crate::repo_setup::set_flow(&app, &row, &me.account, flow).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 307 | 15× | if let Err(e) = crate::repo_setup::commit_start(&app, &row, &me.account, files).await { |
| 308 | 0× | tracing::warn!("starting files for {name}: {e}"); |
| 309 | 15× | } |
| 310 | 15× | if wiki { |
| 311 | 0× | if let Err(e) = crate::repo_setup::start_wiki(&app, &row, &me.account, &f.description).await { |
| 312 | 0× | tracing::warn!("wiki for {name}: {e}"); |
| 313 | 0× | } |
| 314 | 15× | } |
| 315 | 15× | Ok(Redirect::to(&name.url()).into_response()) |
| 316 | 26× | } |
| 317 | |
|
| 318 | 11× | fn clone_page(p: &NewRepoPage) -> NewRepoPage { |
| 319 | 11× | NewRepoPage { |
| 320 | 11× | shell: p.shell.clone(), |
| 321 | 11× | owners: p.owners.clone(), |
| 322 | 11× | error: p.error.clone(), |
| 323 | 11× | boards: p.boards.clone(), |
| 324 | 11× | orgs: p.orgs.clone(), |
| 325 | 11× | licences: p.licences, |
| 326 | 11× | ignores: p.ignores, |
| 327 | 11× | base_url: p.base_url.clone(), |
| 328 | 11× | admin: p.admin, |
| 329 | 11× | runner_owners: p.runner_owners.clone(), |
| 330 | 11× | bookmarks: p.bookmarks.clone(), |
| 331 | 11× | } |
| 332 | 11× | } |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 337 | | pub enum Section { |
| 338 | | General, |
| 339 | | Collaborators, |
| 340 | | Landing, |
| 341 | | Checks, |
| 342 | | PushProtection, |
| 343 | | Pipelines, |
| 344 | | Runs, |
| 345 | | Gates, |
| 346 | | Runners, |
| 347 | | Variables, |
| 348 | | Secrets, |
| 349 | | Coverage, |
| 350 | | Webhooks, |
| 351 | | Assistants, |
| 352 | | GitMirror, |
| 353 | | Diffusion, |
| 354 | | Storage, |
| 355 | | Backup, |
| 356 | | Advanced, |
| 357 | | } |
| 358 | |
|
| 359 | |
|
| 360 | | const GROUPS: &[(&str, &[Section])] = &[ |
| 361 | | ("General", &[Section::General, Section::Collaborators]), |
| 362 | | ("Code", &[Section::Landing, Section::Checks, Section::PushProtection]), |
| 363 | | ( |
| 364 | | "Flow", |
| 365 | | &[Section::Pipelines, Section::Runs, Section::Gates, Section::Runners, Section::Variables, Section::Secrets, Section::Coverage], |
| 366 | | ), |
| 367 | | ("Integrations", &[Section::Webhooks, Section::Assistants, Section::GitMirror, Section::Diffusion]), |
| 368 | | ("Data", &[Section::Storage, Section::Backup]), |
| 369 | | ("Danger zone", &[Section::Advanced]), |
| 370 | | ]; |
| 371 | |
|
| 372 | | impl Section { |
| 373 | 5908× | pub fn slug(self) -> &'static str { |
| 374 | 5908× | match self { |
| 375 | 405× | Section::General => "general", |
| 376 | 372× | Section::Collaborators => "collaborators", |
| 377 | 388× | Section::Landing => "landing", |
| 378 | 385× | Section::Checks => "checks", |
| 379 | 392× | Section::PushProtection => "push-protection", |
| 380 | 366× | Section::Pipelines => "flow", |
| 381 | 343× | Section::Runs => "runs", |
| 382 | 331× | Section::Gates => "gates", |
| 383 | 302× | Section::Runners => "runners", |
| 384 | 299× | Section::Variables => "variables", |
| 385 | 291× | Section::Secrets => "secrets", |
| 386 | 313× | Section::Coverage => "coverage", |
| 387 | 289× | Section::Webhooks => "webhooks", |
| 388 | 274× | Section::Assistants => "assistants", |
| 389 | 296× | Section::GitMirror => "git-mirror", |
| 390 | 291× | Section::Diffusion => "diffusion", |
| 391 | 21× | Section::Storage => "storage", |
| 392 | 289× | Section::Backup => "backup", |
| 393 | 261× | Section::Advanced => "advanced", |
| 394 | | } |
| 395 | 5908× | } |
| 396 | |
|
| 397 | 2421× | pub fn title(self) -> &'static str { |
| 398 | 2421× | match self { |
| 399 | 133× | Section::General => "General", |
| 400 | 126× | Section::Collaborators => "Collaborators", |
| 401 | 136× | Section::Landing => "Landing rules", |
| 402 | 138× | Section::Checks => "Required checks", |
| 403 | 153× | Section::PushProtection => "Push protection", |
| 404 | 152× | Section::Pipelines => "Pipelines", |
| 405 | 141× | Section::Runs => "Runs & priority", |
| 406 | 136× | Section::Gates => "Gates & agents", |
| 407 | 126× | Section::Runners => "Runners", |
| 408 | 125× | Section::Variables => "Variables", |
| 409 | 123× | Section::Secrets => "Secrets", |
| 410 | 136× | Section::Coverage => "Coverage", |
| 411 | 129× | Section::Webhooks => "Webhooks", |
| 412 | 124× | Section::Assistants => "AI assistants", |
| 413 | 137× | Section::GitMirror => "Git mirror", |
| 414 | 136× | Section::Diffusion => "Diffusion", |
| 415 | 0× | Section::Storage => "Storage", |
| 416 | 139× | Section::Backup => "Backup", |
| 417 | 131× | Section::Advanced => "Advanced", |
| 418 | | } |
| 419 | 2421× | } |
| 420 | |
|
| 421 | 133× | pub fn parse(slug: &str) -> Option<Section> { |
| 422 | 1232× | GROUPS.iter().flat_map(|(_, pages)| pages.iter().copied()).find(|s| s.slug() == slug) |
| 423 | 133× | } |
| 424 | |
|
| 425 | | |
| 426 | | |
| 427 | 2668× | fn admin_only(self) -> bool { |
| 428 | 1552× | matches!( |
| 429 | 2668× | self, |
| 430 | | Section::Collaborators |
| 431 | | | Section::Runners |
| 432 | | | Section::Variables |
| 433 | | | Section::Secrets |
| 434 | | | Section::Webhooks |
| 435 | | | Section::Assistants |
| 436 | | | Section::Storage |
| 437 | | | Section::Advanced |
| 438 | | ) |
| 439 | 2668× | } |
| 440 | |
|
| 441 | | |
| 442 | | |
| 443 | 2668× | fn shown(self, role: Role, in_bucket: bool) -> bool { |
| 444 | 2668× | (!self.admin_only() || role == Role::Admin) && (self != Section::Storage || in_bucket) |
| 445 | 2668× | } |
| 446 | | } |
| 447 | |
|
| 448 | |
|
| 449 | 133× | fn sidebar(role: Role, in_bucket: bool) -> Vec<(&'static str, Vec<Section>)> { |
| 450 | 133× | GROUPS |
| 451 | 133× | .iter() |
| 452 | 2527× | .map(|(group, pages)| (*group, pages.iter().copied().filter(|s| s.shown(role, in_bucket)).collect::<Vec<_>>())) |
| 453 | 798× | .filter(|(_, pages)| !pages.is_empty()) |
| 454 | 133× | .collect() |
| 455 | 133× | } |
| 456 | |
|
| 457 | |
|
| 458 | 69× | fn back(row: &db::repos::RepoRow, section: Section, notice: Option<String>, error: Option<String>) -> Result<Response, Error> { |
| 459 | 69× | let mut to = format!("/{}/{}/settings/{}", row.owner, row.name, section.slug()); |
| 460 | 69× | if let Some(n) = notice { |
| 461 | 51× | to.push_str(&format!("?notice={}", super::settings::urlencode(&n))); |
| 462 | 51× | } else if let Some(e) = error { |
| 463 | 18× | to.push_str(&format!("?error={}", super::settings::urlencode(&e))); |
| 464 | 18× | } |
| 465 | 69× | Ok(Redirect::to(&to).into_response()) |
| 466 | 69× | } |
| 467 | |
|
| 468 | | #[derive(Template)] |
| 469 | | #[template(path = "repo_settings.html")] |
| 470 | | pub struct RepoSettingsPage { |
| 471 | | |
| 472 | | section: Section, |
| 473 | | nav: Vec<(&'static str, Vec<Section>)>, |
| 474 | | shell: Shell, |
| 475 | | repo: RepoShell, |
| 476 | | row: db::repos::RepoRow, |
| 477 | | collaborators: Vec<accounts::Collaborator>, |
| 478 | | teams: Vec<accounts::Team>, |
| 479 | | role: Role, |
| 480 | | requirements: db::proposals::Requirements, |
| 481 | | hooks: Vec<super::webhooks::HookRow>, |
| 482 | | required_checks: String, |
| 483 | | |
| 484 | | flow_default: bool, |
| 485 | | |
| 486 | | diffusion_site: String, |
| 487 | | flow_enabled: bool, |
| 488 | | site_admin: bool, |
| 489 | | |
| 490 | | own_runner: bool, |
| 491 | | notice: Option<String>, |
| 492 | | error: Option<String>, |
| 493 | | |
| 494 | | |
| 495 | | runners: Vec<(db::runners::Runner, String, bool)>, |
| 496 | | |
| 497 | | runner_token: Option<String>, |
| 498 | | |
| 499 | | assistants: super::assistants::Panel, |
| 500 | | |
| 501 | | variables: super::variables::Panel, |
| 502 | | |
| 503 | | secrets: super::secrets::Panel, |
| 504 | | |
| 505 | | storage: Option<crate::storage::Storage>, |
| 506 | | |
| 507 | | coverage: crate::coverage::Rules, |
| 508 | | |
| 509 | | backup_owner: Option<crate::backups::Setting>, |
| 510 | | |
| 511 | | |
| 512 | | push_findings: Vec<db::push_protection::FindingRow>, |
| 513 | | secret_bypasses: Vec<db::push_protection::Bypass>, |
| 514 | | |
| 515 | | |
| 516 | | history_scan: Option<db::push_protection::ScanRow>, |
| 517 | | history_findings: Vec<db::push_protection::HistoryRow>, |
| 518 | | resolved_findings: Vec<db::push_protection::HistoryRow>, |
| 519 | | history_scan_on: bool, |
| 520 | | } |
| 521 | |
|
| 522 | | impl RepoSettingsPage { |
| 523 | 6× | fn backup_choices(&self) -> &'static [(&'static str, &'static str)] { |
| 524 | 6× | crate::backups::REPO_CHOICES |
| 525 | 6× | } |
| 526 | | } |
| 527 | |
|
| 528 | |
|
| 529 | 141× | async fn settings_page( |
| 530 | 141× | app: &Shared, |
| 531 | 141× | shell: Shell, |
| 532 | 141× | row: db::repos::RepoRow, |
| 533 | 141× | role: Role, |
| 534 | 141× | section: Section, |
| 535 | 141× | notice: Option<String>, |
| 536 | 141× | error: Option<String>, |
| 537 | 141× | ) -> Result<Page<RepoSettingsPage>, Error> { |
| 538 | 141× | let storage = crate::storage::of(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 539 | 141× | if !section.shown(role, storage.is_some()) { |
| 540 | 8× | return Err(Error::not_found(shell)); |
| 541 | 133× | } |
| 542 | 133× | let nav = sidebar(role, storage.is_some()); |
| 543 | 133× | let collaborators = accounts::collaborators(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 544 | 133× | let teams = match accounts::org_by_name(&app.db, &row.owner).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 545 | 10× | Some(org) => accounts::teams(&app.db, org.id).await.map_err(|e| Error::from_db(shell.clone(), e))?, |
| 546 | 123× | None => vec![], |
| 547 | | }; |
| 548 | 133× | let repo = RepoShell::new(app, &row, "settings").with_role(role); |
| 549 | 133× | let requirements = db::proposals::requirements(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 550 | 133× | let hooks = db::hooks::hooks_for_repo(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 551 | 133× | let hooks = super::webhooks::rows(app, hooks).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 552 | 133× | let required_checks = db::hooks::required_checks(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.join(", "); |
| 553 | 133× | let flow_default = app.config.flow.allows(&row.owner, &row.name); |
| 554 | 133× | let diffusion_site = app.config.diffusion.follow.clone(); |
| 555 | 133× | let flow_enabled = crate::flow::enabled(app, &row); |
| 556 | 133× | let site_admin = shell.is_admin; |
| 557 | 133× | let own_runner = has_own_runner(app, &row).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 558 | 133× | let runners = db::runners::serving(&app.db, &row.owner, row.id) |
| 559 | 133× | .await |
| 560 | 133× | .map_err(|e| Error::from_db(shell.clone(), e))? |
| 561 | 133× | .into_iter() |
| 562 | 133× | .map(|r| { |
| 563 | 6× | let own = r.scope_repo_id == Some(row.id); |
| 564 | 6× | let serves = if own { |
| 565 | 4× | "this repository".to_string() |
| 566 | 2× | } else if r.scope_owner.is_empty() { |
| 567 | 2× | "the whole site".to_string() |
| 568 | | } else { |
| 569 | 0× | format!("{}'s repositories", r.scope_owner) |
| 570 | | }; |
| 571 | 6× | (r, serves, own) |
| 572 | 6× | }) |
| 573 | 133× | .collect(); |
| 574 | 133× | let assistants = super::assistants::panel(app, &repo_assistants(&row)).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 575 | 133× | let variables = super::variables::panel(app, &repo_assistants(&row)).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 576 | 133× | let secrets = super::secrets::panel(app, &repo_assistants(&row)).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 577 | 133× | let coverage = crate::coverage::rules(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 578 | 133× | let backup_owner = crate::backups::setting(&app.db, &row.owner).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 579 | 133× | let push_findings = db::push_protection::recent(&app.db, row.id, 50).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 580 | 133× | let secret_bypasses = db::push_protection::bypasses(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 581 | 133× | let history_scan = db::push_protection::scan_of(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 582 | 133× | let history_findings = db::push_protection::history(&app.db, row.id, 200).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 583 | 133× | let resolved_findings = db::push_protection::resolved(&app.db, row.id, 50).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 584 | 133× | let history_scan_on = crate::history_scan::Budget::of(&app.config).is_some(); |
| 585 | 133× | Ok(Page(RepoSettingsPage { |
| 586 | 133× | section, |
| 587 | 133× | nav, |
| 588 | 133× | push_findings, |
| 589 | 133× | secret_bypasses, |
| 590 | 133× | history_scan, |
| 591 | 133× | history_findings, |
| 592 | 133× | resolved_findings, |
| 593 | 133× | history_scan_on, |
| 594 | 133× | backup_owner, |
| 595 | 133× | coverage, |
| 596 | 133× | storage, |
| 597 | 133× | assistants, |
| 598 | 133× | variables, |
| 599 | 133× | secrets, |
| 600 | 133× | runners, |
| 601 | 133× | runner_token: None, |
| 602 | 133× | shell, |
| 603 | 133× | repo, |
| 604 | 133× | row, |
| 605 | 133× | collaborators, |
| 606 | 133× | teams, |
| 607 | 133× | role, |
| 608 | 133× | requirements, |
| 609 | 133× | hooks, |
| 610 | 133× | required_checks, |
| 611 | 133× | flow_default, |
| 612 | 133× | diffusion_site, |
| 613 | 133× | flow_enabled, |
| 614 | 133× | site_admin, |
| 615 | 133× | own_runner, |
| 616 | 133× | notice, |
| 617 | 133× | error, |
| 618 | 133× | })) |
| 619 | 141× | } |
| 620 | |
|
| 621 | |
|
| 622 | | #[derive(Deserialize, Default)] |
| 623 | | pub struct Outcome { |
| 624 | | #[serde(default)] |
| 625 | | notice: Option<String>, |
| 626 | | #[serde(default)] |
| 627 | | error: Option<String>, |
| 628 | | } |
| 629 | |
|
| 630 | |
|
| 631 | 12× | pub async fn settings( |
| 632 | 12× | State(app): State<Shared>, |
| 633 | 12× | viewer: Viewer, |
| 634 | 12× | Path((owner, name)): Path<(String, String)>, |
| 635 | 12× | Query(q): Query<Outcome>, |
| 636 | 12× | ) -> Result<Page<RepoSettingsPage>, Error> { |
| 637 | 12× | let path = format!("/{owner}/{name}/settings"); |
| 638 | 12× | show(&app, &viewer, &path, &owner, &name, Section::General, q).await |
| 639 | 12× | } |
| 640 | |
|
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | 133× | pub async fn page( |
| 645 | 133× | State(app): State<Shared>, |
| 646 | 133× | viewer: Viewer, |
| 647 | 133× | Path(params): Path<std::collections::HashMap<String, String>>, |
| 648 | 133× | uri: Uri, |
| 649 | 133× | Query(q): Query<Outcome>, |
| 650 | 133× | ) -> Result<Page<RepoSettingsPage>, Error> { |
| 651 | 133× | let (owner, name) = (params.get("owner").cloned().unwrap_or_default(), params.get("name").cloned().unwrap_or_default()); |
| 652 | 133× | let path = format!("/{owner}/{name}/settings"); |
| 653 | 133× | let slug = uri.path().rsplit('/').next().unwrap_or(""); |
| 654 | 133× | let Some(section) = Section::parse(slug) else { |
| 655 | 1× | return Err(Error::not_found(Shell::new(&app, &path, &viewer))); |
| 656 | | }; |
| 657 | 132× | show(&app, &viewer, &format!("{path}/{slug}"), &owner, &name, section, q).await |
| 658 | 133× | } |
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | 144× | async fn show( |
| 663 | 144× | app: &Shared, |
| 664 | 144× | viewer: &Viewer, |
| 665 | 144× | path: &str, |
| 666 | 144× | owner: &str, |
| 667 | 144× | name: &str, |
| 668 | 144× | section: Section, |
| 669 | 144× | q: Outcome, |
| 670 | 144× | ) -> Result<Page<RepoSettingsPage>, Error> { |
| 671 | 144× | let shell = Shell::new(app, path, viewer); |
| 672 | 144× | let (_, row, role) = lookup_role(app, &shell, viewer, owner, name, Access::Read).await?; |
| 673 | 144× | if role < Role::Maintain { |
| 674 | 5× | return Err(Error::not_found(shell)); |
| 675 | 139× | } |
| 676 | 139× | if row.archived && role < Role::Admin { |
| 677 | 0× | return Err(super::archived(&shell)); |
| 678 | 139× | } |
| 679 | 139× | settings_page(app, shell, row, role, section, q.notice, q.error).await |
| 680 | 144× | } |
| 681 | |
|
| 682 | | #[derive(Deserialize)] |
| 683 | | pub struct GeneralForm { |
| 684 | | #[serde(rename = "_csrf")] |
| 685 | | csrf_token: String, |
| 686 | | description: String, |
| 687 | | default_bookmark: String, |
| 688 | | visibility: String, |
| 689 | | #[serde(default)] |
| 690 | | topics: String, |
| 691 | | } |
| 692 | |
|
| 693 | 6× | pub async fn update( |
| 694 | 6× | State(app): State<Shared>, |
| 695 | 6× | viewer: Viewer, |
| 696 | 6× | Path((owner, name)): Path<(String, String)>, |
| 697 | 6× | Form(f): Form<GeneralForm>, |
| 698 | 6× | ) -> Result<Response, Error> { |
| 699 | 6× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 700 | 6× | let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 701 | 6× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 702 | 6× | let e = |e| Error::from_db(shell.clone(), e); |
| 703 | | |
| 704 | | |
| 705 | | |
| 706 | 6× | let wanted = f.default_bookmark.trim().to_string(); |
| 707 | 6× | if wanted != row.default_bookmark { |
| 708 | 2× | let names: Vec<String> = app |
| 709 | 2× | .repos |
| 710 | 2× | .read(&repo, |r, _| Ok(r.view()?.bookmarks.keys().cloned().collect::<Vec<String>>())) |
| 711 | 2× | .await |
| 712 | 2× | .map_err(|err| Error::from_repo(shell.clone(), err))?; |
| 713 | 2× | if !names.is_empty() && !names.contains(&wanted) { |
| 714 | 2× | let mut some: Vec<&str> = names.iter().map(String::as_str).filter(|n| *n == "main" || *n == "master").collect(); |
| 715 | 2× | some.extend(names.iter().map(String::as_str).filter(|n| *n != "main" && *n != "master").take(8 - some.len())); |
| 716 | 1× | let more = if names.len() > some.len() { format!(" and {} more", names.len() - some.len()) } else { String::new() }; |
| 717 | 1× | let msg = format!("{wanted} is not a bookmark here yet: push it first, or choose one of {}{more}.", some.join(", ")); |
| 718 | 1× | return back(&row, Section::General, None, Some(msg)); |
| 719 | 1× | } |
| 720 | 4× | } |
| 721 | 5× | let topics = match db::repos::parse_topics(&f.topics) { |
| 722 | 4× | Ok(t) => t, |
| 723 | 1× | Err(msg) => return back(&row, Section::General, None, Some(msg)), |
| 724 | | }; |
| 725 | 4× | db::repos::set_description(&app.db, row.id, f.description.trim()).await.map_err(e)?; |
| 726 | 4× | db::repos::set_topics(&app.db, row.id, &topics).await.map_err(e)?; |
| 727 | 4× | if crate::repos::names::valid_segment(f.default_bookmark.trim()) { |
| 728 | 4× | db::repos::set_default_bookmark(&app.db, row.id, f.default_bookmark.trim()).await.map_err(e)?; |
| 729 | 0× | } |
| 730 | 4× | if role >= Role::Admin { |
| 731 | 4× | if let Some(v) = Visibility::parse(&f.visibility) { |
| 732 | 4× | db::repos::set_visibility(&app.db, row.id, v).await.map_err(e)?; |
| 733 | 0× | } |
| 734 | 0× | } |
| 735 | 4× | accounts::audit(&app.db, viewer.account(), "repo.updated", &format!("{owner}/{name}"), "").await.ok(); |
| 736 | 4× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(e)?.unwrap_or(row); |
| 737 | 4× | back(&row, Section::General, Some("Saved.".into()), None) |
| 738 | 6× | } |
| 739 | |
|
| 740 | | #[derive(Deserialize)] |
| 741 | | pub struct CollaboratorForm { |
| 742 | | #[serde(rename = "_csrf")] |
| 743 | | csrf_token: String, |
| 744 | | #[serde(default)] |
| 745 | | username: String, |
| 746 | | #[serde(default)] |
| 747 | | team: String, |
| 748 | | #[serde(default)] |
| 749 | | role: String, |
| 750 | | #[serde(default)] |
| 751 | | remove: Option<String>, |
| 752 | | } |
| 753 | |
|
| 754 | 5× | pub async fn collaborator( |
| 755 | 5× | State(app): State<Shared>, |
| 756 | 5× | viewer: Viewer, |
| 757 | 5× | Path((owner, name)): Path<(String, String)>, |
| 758 | 5× | Form(f): Form<CollaboratorForm>, |
| 759 | 5× | ) -> Result<Response, Error> { |
| 760 | 5× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 761 | 5× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 762 | 5× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 763 | 5× | let e = |e| Error::from_db(shell.clone(), e); |
| 764 | 5× | let (account_id, team_id, label) = if !f.username.trim().is_empty() { |
| 765 | 5× | match accounts::account_by_name(&app.db, f.username.trim()).await.map_err(e)? { |
| 766 | 5× | Some(a) => (Some(a.id), None, a.username), |
| 767 | 0× | None => return back(&row, Section::Collaborators, None, Some("No such account.".into())), |
| 768 | | } |
| 769 | 0× | } else if !f.team.trim().is_empty() { |
| 770 | 0× | let org = accounts::org_by_name(&app.db, &row.owner).await.map_err(e)?; |
| 771 | 0× | let team = match org { |
| 772 | 0× | Some(o) => accounts::team_by_name(&app.db, o.id, f.team.trim()).await.map_err(e)?, |
| 773 | 0× | None => None, |
| 774 | | }; |
| 775 | 0× | match team { |
| 776 | 0× | Some(t) => (None, Some(t.id), format!("team {}", t.name)), |
| 777 | 0× | None => return back(&row, Section::Collaborators, None, Some("No such team in this organisation.".into())), |
| 778 | | } |
| 779 | | } else { |
| 780 | 0× | return back(&row, Section::Collaborators, None, Some("Name an account or a team.".into())); |
| 781 | | }; |
| 782 | 5× | if f.remove.is_some() { |
| 783 | 0× | accounts::remove_collaborator(&app.db, row.id, account_id, team_id).await.map_err(e)?; |
| 784 | 0× | accounts::audit(&app.db, viewer.account(), "repo.collaborator.removed", &format!("{owner}/{name}"), &label).await.ok(); |
| 785 | 0× | return back(&row, Section::Collaborators, Some(format!("Removed {label}.")), None); |
| 786 | 5× | } |
| 787 | 5× | let Some(new_role) = Role::parse(&f.role) else { |
| 788 | 0× | return back(&row, Section::Collaborators, None, Some("Pick a role.".into())); |
| 789 | | }; |
| 790 | 5× | accounts::set_collaborator(&app.db, row.id, account_id, team_id, new_role.as_str()).await.map_err(e)?; |
| 791 | 5× | accounts::audit( |
| 792 | 5× | &app.db, |
| 793 | 5× | viewer.account(), |
| 794 | 5× | "repo.collaborator.set", |
| 795 | 5× | &format!("{owner}/{name}"), |
| 796 | 5× | &format!("{label} {}", new_role.as_str()), |
| 797 | 5× | ) |
| 798 | 5× | .await |
| 799 | 5× | .ok(); |
| 800 | 5× | back(&row, Section::Collaborators, Some(format!("{label} now has {} access.", new_role.as_str())), None) |
| 801 | 5× | } |
| 802 | |
|
| 803 | | #[derive(Deserialize)] |
| 804 | | pub struct TransferForm { |
| 805 | | #[serde(rename = "_csrf")] |
| 806 | | csrf_token: String, |
| 807 | | owner: String, |
| 808 | | confirm: String, |
| 809 | | } |
| 810 | |
|
| 811 | |
|
| 812 | |
|
| 813 | |
|
| 814 | 3× | pub async fn transfer( |
| 815 | 3× | State(app): State<Shared>, |
| 816 | 3× | viewer: Viewer, |
| 817 | 3× | Path((owner, name)): Path<(String, String)>, |
| 818 | 3× | Form(f): Form<TransferForm>, |
| 819 | 3× | ) -> Result<Response, Error> { |
| 820 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 821 | 3× | let (repo, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 822 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 823 | 3× | let me = viewer.account().cloned().ok_or_else(|| Error::not_found(shell.clone()))?; |
| 824 | 3× | let fail = |msg: &str| back(&row, Section::Advanced, None, Some(msg.into())); |
| 825 | 3× | if f.confirm.trim() != repo.to_string() { |
| 826 | 1× | return fail("Type the repository's full name to confirm."); |
| 827 | 2× | } |
| 828 | 2× | let to = f.owner.trim().trim_start_matches('@').to_string(); |
| 829 | 2× | let Some(dest) = RepoName::new(&to, &row.name) else { |
| 830 | 0× | return fail("That is not an owner name."); |
| 831 | | }; |
| 832 | 2× | if dest.owner == row.owner { |
| 833 | 0× | return fail("The repository is already there."); |
| 834 | 2× | } |
| 835 | 2× | let e = |e| Error::from_db(shell.clone(), e); |
| 836 | 2× | let known = accounts::account_by_name(&app.db, &dest.owner).await.map_err(e)?.is_some() |
| 837 | 2× | || accounts::org_by_name(&app.db, &dest.owner).await.map_err(e)?.is_some(); |
| 838 | 2× | if !known { |
| 839 | 1× | return fail("There is no user or organisation of that name."); |
| 840 | 1× | } |
| 841 | 1× | if !roles::may_create_under(&app.db, &me, &dest.owner).await.map_err(e)? { |
| 842 | 0× | return fail("You cannot create repositories under that owner, so you cannot move one there."); |
| 843 | 1× | } |
| 844 | 1× | if db::repos::get(&app.db, &dest.owner, &dest.name).await.map_err(e)?.is_some() || app.repos.exists(&dest) { |
| 845 | 0× | return fail("That owner already has a repository of this name."); |
| 846 | 1× | } |
| 847 | | { |
| 848 | 1× | let _guard = app.repos.exclusive_guard(&repo).await; |
| 849 | 1× | app.repos.rename(&repo, &dest).map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 850 | 1× | crate::storage::renamed(&app, &repo, &dest); |
| 851 | 1× | if app.wikis.exists(&repo) { |
| 852 | 0× | app.wikis.rename(&repo, &dest).ok(); |
| 853 | 1× | } |
| 854 | 1× | db::repos::transfer(&app.db, row.id, &row.owner, &row.name, &dest.owner).await.map_err(e)?; |
| 855 | 1× | let mirror = crate::mirror::mirror_dir(&app, &repo); |
| 856 | 1× | if mirror.is_dir() { |
| 857 | 0× | let to = crate::mirror::mirror_dir(&app, &dest); |
| 858 | 0× | if let Some(parent) = to.parent() { |
| 859 | 0× | std::fs::create_dir_all(parent).ok(); |
| 860 | 0× | } |
| 861 | 0× | std::fs::rename(&mirror, &to).ok(); |
| 862 | 1× | } |
| 863 | | } |
| 864 | 1× | accounts::audit(&app.db, Some(&me), "repo.transferred", &repo.to_string(), &dest.to_string()).await.ok(); |
| 865 | 1× | let notice = super::settings::urlencode(&format!("Moved from {repo}; the old address redirects here.")); |
| 866 | 1× | Ok(Redirect::to(&format!("{}/settings/{}?notice={notice}", dest.url(), Section::Advanced.slug())).into_response()) |
| 867 | 3× | } |
| 868 | |
|
| 869 | | #[derive(Deserialize)] |
| 870 | | pub struct ArchiveForm { |
| 871 | | #[serde(rename = "_csrf")] |
| 872 | | csrf_token: String, |
| 873 | | |
| 874 | | action: String, |
| 875 | | #[serde(default)] |
| 876 | | reason: String, |
| 877 | | } |
| 878 | |
|
| 879 | |
|
| 880 | |
|
| 881 | 3× | pub async fn archive( |
| 882 | 3× | State(app): State<Shared>, |
| 883 | 3× | viewer: Viewer, |
| 884 | 3× | Path((owner, name)): Path<(String, String)>, |
| 885 | 3× | Form(f): Form<ArchiveForm>, |
| 886 | 3× | ) -> Result<Response, Error> { |
| 887 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 888 | 3× | let (repo, mut row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 889 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 890 | 3× | let archive = match f.action.as_str() { |
| 891 | 3× | "archive" => true, |
| 892 | 2× | "unarchive" => false, |
| 893 | 1× | _ => return back(&row, Section::Advanced, None, Some("Unknown action.".into())), |
| 894 | | }; |
| 895 | 2× | let reason: String = f.reason.trim().chars().take(500).collect(); |
| 896 | 2× | db::repos::set_archived(&app.db, row.id, archive, &reason).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 897 | 2× | accounts::audit(&app.db, viewer.account(), if archive { "repo.archived" } else { "repo.unarchived" }, &repo.to_string(), &reason) |
| 898 | 2× | .await |
| 899 | 2× | .ok(); |
| 900 | 2× | row.archived = archive; |
| 901 | 2× | row.archived_reason = if archive { reason } else { String::new() }; |
| 902 | 2× | let notice = if archive { |
| 903 | 1× | "The repository is archived: read-only until it is brought back." |
| 904 | | } else { |
| 905 | 1× | "The repository is back: writes are allowed again." |
| 906 | | }; |
| 907 | 2× | back(&row, Section::Advanced, Some(notice.into()), None) |
| 908 | 3× | } |
| 909 | |
|
| 910 | |
|
| 911 | 1× | pub async fn wiki_delete( |
| 912 | 1× | State(app): State<Shared>, |
| 913 | 1× | viewer: Viewer, |
| 914 | 1× | Path((owner, name)): Path<(String, String)>, |
| 915 | 1× | Form(f): Form<DeleteForm>, |
| 916 | 1× | ) -> Result<Response, Error> { |
| 917 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 918 | 1× | let (repo, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 919 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 920 | 1× | if f.confirm.trim() != repo.to_string() { |
| 921 | 0× | return back(&row, Section::Advanced, None, Some("Type the repository's full name to confirm.".into())); |
| 922 | 1× | } |
| 923 | 1× | if !crate::wiki::exists(&app, &repo) { |
| 924 | 0× | return back(&row, Section::Advanced, None, Some("This repository has no wiki.".into())); |
| 925 | 1× | } |
| 926 | 1× | let _guard = app.wikis.exclusive_guard(&repo).await; |
| 927 | 1× | crate::wiki::trash(&app, &repo).map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 928 | 1× | drop(_guard); |
| 929 | 1× | accounts::audit(&app.db, viewer.account(), "wiki.data_deleted", &repo.to_string(), "moved to trash").await.ok(); |
| 930 | 1× | back(&row, Section::Advanced, Some("The wiki is in the trash; a new first page starts a fresh one.".into()), None) |
| 931 | 1× | } |
| 932 | |
|
| 933 | | #[derive(Deserialize)] |
| 934 | | pub struct DeleteForm { |
| 935 | | #[serde(rename = "_csrf")] |
| 936 | | csrf_token: String, |
| 937 | | confirm: String, |
| 938 | | } |
| 939 | |
|
| 940 | 1× | pub async fn delete( |
| 941 | 1× | State(app): State<Shared>, |
| 942 | 1× | viewer: Viewer, |
| 943 | 1× | Path((owner, name)): Path<(String, String)>, |
| 944 | 1× | Form(f): Form<DeleteForm>, |
| 945 | 1× | ) -> Result<Response, Error> { |
| 946 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 947 | 1× | let (repo, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 948 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 949 | 1× | if f.confirm.trim() != repo.to_string() { |
| 950 | 0× | return back(&row, Section::Advanced, None, Some("Type the repository's full name to delete it.".into())); |
| 951 | 1× | } |
| 952 | 1× | let _guard = app.repos.exclusive_guard(&repo).await; |
| 953 | 1× | app.repos.trash(&repo).map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 954 | | |
| 955 | 1× | crate::storage::removed(&app, &repo); |
| 956 | 1× | crate::wiki::trash(&app, &repo).ok(); |
| 957 | 1× | db::repos::delete(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 958 | 1× | crate::assistants::delete_scope(&app.db, crate::levels::Scope::Repo(row.id)).await.ok(); |
| 959 | 1× | crate::variables::delete_scope(&app.db, crate::levels::Scope::Repo(row.id)).await.ok(); |
| 960 | 1× | crate::secrets::delete_scope(&app.db, crate::levels::Scope::Repo(row.id)).await.ok(); |
| 961 | 1× | accounts::audit(&app.db, viewer.account(), "repo.deleted", &repo.to_string(), "moved to trash").await.ok(); |
| 962 | 1× | Ok(Redirect::to(&format!("/{owner}")).into_response()) |
| 963 | 1× | } |
| 964 | |
|
| 965 | | #[derive(Deserialize)] |
| 966 | | pub struct RequirementsForm { |
| 967 | | #[serde(rename = "_csrf")] |
| 968 | | csrf_token: String, |
| 969 | | #[serde(default)] |
| 970 | | required_approvals: i64, |
| 971 | | #[serde(default)] |
| 972 | | require_owners: Option<String>, |
| 973 | | #[serde(default)] |
| 974 | | allow_conflicts: Option<String>, |
| 975 | | } |
| 976 | |
|
| 977 | 2× | pub async fn requirements( |
| 978 | 2× | State(app): State<Shared>, |
| 979 | 2× | viewer: Viewer, |
| 980 | 2× | Path((owner, name)): Path<(String, String)>, |
| 981 | 2× | Form(f): Form<RequirementsForm>, |
| 982 | 2× | ) -> Result<Response, Error> { |
| 983 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 984 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 985 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 986 | 2× | let r = db::proposals::Requirements { |
| 987 | 2× | required_approvals: f.required_approvals.max(0), |
| 988 | 2× | require_owners: f.require_owners.is_some(), |
| 989 | 2× | allow_conflicts: f.allow_conflicts.is_some(), |
| 990 | 2× | }; |
| 991 | 2× | db::proposals::set_requirements(&app.db, row.id, r).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 992 | 2× | accounts::audit( |
| 993 | 2× | &app.db, |
| 994 | 2× | viewer.account(), |
| 995 | 2× | "repo.requirements", |
| 996 | 2× | &format!("{owner}/{name}"), |
| 997 | 2× | &format!("{} approvals, owners {}, conflicts {}", r.required_approvals, r.require_owners, r.allow_conflicts), |
| 998 | 2× | ) |
| 999 | 2× | .await |
| 1000 | 2× | .ok(); |
| 1001 | 2× | back(&row, Section::Landing, Some("Landing requirements saved.".into()), None) |
| 1002 | 2× | } |
| 1003 | |
|
| 1004 | 6× | pub async fn hooks( |
| 1005 | 6× | State(app): State<Shared>, |
| 1006 | 6× | viewer: Viewer, |
| 1007 | 6× | Path((owner, name)): Path<(String, String)>, |
| 1008 | 6× | Form(f): Form<super::webhooks::HookForm>, |
| 1009 | 6× | ) -> Result<Response, Error> { |
| 1010 | 6× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1011 | 6× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1012 | 6× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1013 | 6× | let scope = super::webhooks::Scope::Repo { id: row.id, slug: format!("{owner}/{name}") }; |
| 1014 | 6× | match super::webhooks::apply(&app, &viewer, &scope, f).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 1015 | 5× | Ok(notice) => back(&row, Section::Webhooks, Some(notice), None), |
| 1016 | 1× | Err(error) => back(&row, Section::Webhooks, None, Some(error)), |
| 1017 | | } |
| 1018 | 6× | } |
| 1019 | |
|
| 1020 | 0× | pub async fn deliveries( |
| 1021 | 0× | State(app): State<Shared>, |
| 1022 | 0× | viewer: Viewer, |
| 1023 | 0× | Path((owner, name, id)): Path<(String, String, i64)>, |
| 1024 | 0× | ) -> Result<Page<super::webhooks::DeliveriesPage>, Error> { |
| 1025 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings/hooks/{id}/deliveries"), &viewer); |
| 1026 | 0× | let (_, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1027 | 0× | let scope = super::webhooks::Scope::Repo { id: row.id, slug: format!("{owner}/{name}") }; |
| 1028 | 0× | super::webhooks::deliveries_page(&app, shell, &scope, Some(RepoShell::new(&app, &row, "settings").with_role(role)), id).await |
| 1029 | 0× | } |
| 1030 | |
|
| 1031 | 0× | pub async fn redeliver( |
| 1032 | 0× | State(app): State<Shared>, |
| 1033 | 0× | viewer: Viewer, |
| 1034 | 0× | Path((owner, name, id, delivery)): Path<(String, String, i64, i64)>, |
| 1035 | 0× | Form(f): Form<super::session::CsrfForm>, |
| 1036 | 0× | ) -> Result<Page<super::webhooks::DeliveriesPage>, Error> { |
| 1037 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings/hooks/{id}/deliveries"), &viewer); |
| 1038 | 0× | let (_, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1039 | 0× | csrf(&viewer, &f.csrf, &shell)?; |
| 1040 | 0× | let scope = super::webhooks::Scope::Repo { id: row.id, slug: format!("{owner}/{name}") }; |
| 1041 | 0× | super::webhooks::redeliver(&app, &scope, id, delivery).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1042 | 0× | super::webhooks::deliveries_page(&app, shell, &scope, Some(RepoShell::new(&app, &row, "settings").with_role(role)), id).await |
| 1043 | 0× | } |
| 1044 | |
|
| 1045 | | #[derive(Deserialize)] |
| 1046 | | pub struct ChecksForm { |
| 1047 | | #[serde(rename = "_csrf")] |
| 1048 | | csrf_token: String, |
| 1049 | | #[serde(default)] |
| 1050 | | required_checks: String, |
| 1051 | | } |
| 1052 | |
|
| 1053 | 2× | pub async fn required_checks( |
| 1054 | 2× | State(app): State<Shared>, |
| 1055 | 2× | viewer: Viewer, |
| 1056 | 2× | Path((owner, name)): Path<(String, String)>, |
| 1057 | 2× | Form(f): Form<ChecksForm>, |
| 1058 | 2× | ) -> Result<Response, Error> { |
| 1059 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1060 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1061 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1062 | 2× | let names: Vec<String> = f.required_checks.split(',').map(str::trim).filter(|s| !s.is_empty()).map(String::from).collect(); |
| 1063 | 2× | db::hooks::set_required_checks(&app.db, row.id, &names).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1064 | 2× | back(&row, Section::Checks, Some("Required checks saved.".into()), None) |
| 1065 | 2× | } |
| 1066 | |
|
| 1067 | | #[derive(Deserialize)] |
| 1068 | | pub struct CoverageForm { |
| 1069 | | #[serde(rename = "_csrf")] |
| 1070 | | csrf_token: String, |
| 1071 | | #[serde(default)] |
| 1072 | | min: String, |
| 1073 | | #[serde(default)] |
| 1074 | | max_drop: String, |
| 1075 | | } |
| 1076 | |
|
| 1077 | |
|
| 1078 | 1× | pub async fn coverage( |
| 1079 | 1× | State(app): State<Shared>, |
| 1080 | 1× | viewer: Viewer, |
| 1081 | 1× | Path((owner, name)): Path<(String, String)>, |
| 1082 | 1× | Form(f): Form<CoverageForm>, |
| 1083 | 1× | ) -> Result<Response, Error> { |
| 1084 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1085 | 1× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1086 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1087 | 2× | let number = |v: &str, what: &str, hi: f64| -> Result<Option<f64>, String> { |
| 1088 | 2× | let v = v.trim().trim_end_matches('%').trim(); |
| 1089 | 2× | if v.is_empty() { |
| 1090 | 1× | return Ok(None); |
| 1091 | 1× | } |
| 1092 | 1× | match v.parse::<f64>() { |
| 1093 | 1× | Ok(n) if (0.0..=hi).contains(&n) => Ok(Some(n)), |
| 1094 | 0× | _ => Err(format!("{what} is a number from 0 to {hi}, or empty for no rule.")), |
| 1095 | | } |
| 1096 | 2× | }; |
| 1097 | 1× | let rules = match (number(&f.min, "The minimum", 100.0), number(&f.max_drop, "The largest drop", 100.0)) { |
| 1098 | 1× | (Ok(min), Ok(max_drop)) => crate::coverage::Rules { min, max_drop }, |
| 1099 | 0× | (Err(e), _) | (_, Err(e)) => return back(&row, Section::Coverage, None, Some(e)), |
| 1100 | | }; |
| 1101 | 1× | crate::coverage::set_rules(&app.db, row.id, rules).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1102 | 1× | accounts::audit( |
| 1103 | 1× | &app.db, |
| 1104 | 1× | viewer.account(), |
| 1105 | 1× | "repo.coverage_rules", |
| 1106 | 1× | &format!("{owner}/{name}"), |
| 1107 | 1× | &format!("min {:?}, max drop {:?}", rules.min, rules.max_drop), |
| 1108 | 1× | ) |
| 1109 | 1× | .await |
| 1110 | 1× | .ok(); |
| 1111 | 1× | back(&row, Section::Coverage, Some("Coverage rules saved; they apply from the next report.".into()), None) |
| 1112 | 1× | } |
| 1113 | |
|
| 1114 | | #[derive(Deserialize)] |
| 1115 | | pub struct StorageForm { |
| 1116 | | #[serde(rename = "_csrf")] |
| 1117 | | csrf_token: String, |
| 1118 | | |
| 1119 | | action: String, |
| 1120 | | #[serde(flatten)] |
| 1121 | | bucket: BucketForm, |
| 1122 | | } |
| 1123 | |
|
| 1124 | |
|
| 1125 | |
|
| 1126 | 0× | pub async fn storage( |
| 1127 | 0× | State(app): State<Shared>, |
| 1128 | 0× | viewer: Viewer, |
| 1129 | 0× | Path((owner, name)): Path<(String, String)>, |
| 1130 | 0× | Form(f): Form<StorageForm>, |
| 1131 | 0× | ) -> Result<Response, Error> { |
| 1132 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1133 | 0× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1134 | 0× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1135 | 0× | let Some(s) = crate::storage::of(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))? else { |
| 1136 | 0× | return back(&row, Section::General, None, Some("This repository is kept on this server's disk.".into())); |
| 1137 | | }; |
| 1138 | 0× | let scheme = s.url.split("://").next().unwrap_or("").to_string(); |
| 1139 | 0× | let outcome = match f.action.as_str() { |
| 1140 | 0× | "credentials" => match f.bucket.credentials(&scheme) { |
| 1141 | 0× | Ok(c) => crate::storage::set_credentials(&app, &s, c).await.map(|r| format!("New credentials saved: {r}.")), |
| 1142 | 0× | Err(e) => Err(format!("Credentials: {e}")), |
| 1143 | | }, |
| 1144 | 0× | _ => crate::storage::check(&app, &s).await.map(|r| format!("The bucket passed the check: {r}.")), |
| 1145 | | }; |
| 1146 | 0× | match outcome { |
| 1147 | 0× | Ok(notice) => { |
| 1148 | 0× | accounts::audit(&app.db, viewer.account(), "repo.storage", &format!("{owner}/{name}"), &f.action).await.ok(); |
| 1149 | 0× | back(&row, Section::Storage, Some(notice), None) |
| 1150 | | } |
| 1151 | 0× | Err(e) => back(&row, Section::Storage, None, Some(format!("Storage: {e}"))), |
| 1152 | | } |
| 1153 | 0× | } |
| 1154 | |
|
| 1155 | | #[derive(Deserialize)] |
| 1156 | | pub struct MirrorForm { |
| 1157 | | #[serde(rename = "_csrf")] |
| 1158 | | csrf_token: String, |
| 1159 | | #[serde(default)] |
| 1160 | | enabled: Option<String>, |
| 1161 | | } |
| 1162 | |
|
| 1163 | | #[derive(Deserialize)] |
| 1164 | | pub struct BackupForm { |
| 1165 | | #[serde(rename = "_csrf")] |
| 1166 | | csrf_token: String, |
| 1167 | | |
| 1168 | | #[serde(default)] |
| 1169 | | backup: String, |
| 1170 | | } |
| 1171 | |
|
| 1172 | |
|
| 1173 | |
|
| 1174 | 3× | pub async fn backup( |
| 1175 | 3× | State(app): State<Shared>, |
| 1176 | 3× | viewer: Viewer, |
| 1177 | 3× | Path((owner, name)): Path<(String, String)>, |
| 1178 | 3× | Form(f): Form<BackupForm>, |
| 1179 | 3× | ) -> Result<Response, Error> { |
| 1180 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1181 | 3× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1182 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1183 | 12× | if !crate::backups::REPO_CHOICES.iter().any(|(v, _)| *v == f.backup) { |
| 1184 | 1× | return back(&row, Section::Backup, None, Some("Pick how this repository is backed up.".into())); |
| 1185 | 2× | } |
| 1186 | 2× | db::repos::set_backup(&app.db, row.id, &f.backup).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1187 | 2× | let what = if f.backup.is_empty() { "with its owner's" } else { f.backup.as_str() }; |
| 1188 | 2× | accounts::audit(&app.db, viewer.account(), "repo.backup", &format!("{owner}/{name}"), what).await.ok(); |
| 1189 | 2× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1190 | 2× | let notice = match f.backup.as_str() { |
| 1191 | 2× | "off" => "This repository is left out of its owner's backups from the next run.".to_string(), |
| 1192 | 0× | "" => "This repository is backed up with its owner's.".to_string(), |
| 1193 | 0× | every => { |
| 1194 | 0× | format!("This repository is backed up at least {} from now on.", if every == "hourly" { "every hour" } else { "every day" }) |
| 1195 | | } |
| 1196 | | }; |
| 1197 | 2× | back(&row, Section::Backup, Some(notice), None) |
| 1198 | 3× | } |
| 1199 | |
|
| 1200 | | #[derive(Deserialize)] |
| 1201 | | pub struct DiffusionForm { |
| 1202 | | #[serde(rename = "_csrf")] |
| 1203 | | csrf_token: String, |
| 1204 | | |
| 1205 | | diffusion: String, |
| 1206 | | |
| 1207 | | #[serde(default)] |
| 1208 | | land: String, |
| 1209 | | } |
| 1210 | |
|
| 1211 | |
|
| 1212 | |
|
| 1213 | 1× | pub async fn diffusion( |
| 1214 | 1× | State(app): State<Shared>, |
| 1215 | 1× | viewer: Viewer, |
| 1216 | 1× | Path((owner, name)): Path<(String, String)>, |
| 1217 | 1× | Form(f): Form<DiffusionForm>, |
| 1218 | 1× | ) -> Result<Response, Error> { |
| 1219 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1220 | 1× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1221 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1222 | 1× | let setting = match f.diffusion.as_str() { |
| 1223 | 1× | "off" | "green" | "any" => f.diffusion.as_str(), |
| 1224 | 0× | "default" => "", |
| 1225 | 0× | _ => return back(&row, Section::Diffusion, None, Some("Pick off, green, any or the site default.".into())), |
| 1226 | | }; |
| 1227 | 1× | let land = match f.land.as_str() { |
| 1228 | 1× | "auto" | "propose" => Some(f.land.as_str()), |
| 1229 | 0× | "" => None, |
| 1230 | 0× | _ => return back(&row, Section::Diffusion, None, Some("Pick propose or auto for landing.".into())), |
| 1231 | | }; |
| 1232 | 1× | db::repos::set_diffusion(&app.db, row.id, setting).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1233 | 1× | if let Some(l) = land { |
| 1234 | 1× | db::repos::set_diffusion_land(&app.db, row.id, l).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1235 | 0× | } |
| 1236 | 1× | let what = |
| 1237 | 1× | format!("{}{}", if setting.is_empty() { "default" } else { setting }, land.map(|l| format!(", land {l}")).unwrap_or_default()); |
| 1238 | 1× | accounts::audit(&app.db, viewer.account(), "repo.diffusion", &format!("{owner}/{name}"), &what).await.ok(); |
| 1239 | 1× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1240 | | |
| 1241 | 1× | if land == Some("auto") { |
| 1242 | 0× | let (app, row) = (app.clone(), row.clone()); |
| 1243 | 0× | tokio::spawn(async move { crate::diffusion::land_ready(&app, &row).await }); |
| 1244 | 1× | } |
| 1245 | 1× | if crate::diffusion::rule(&app.config.diffusion.follow, &row, None, "").follow != crate::diffusion::Follow::Off { |
| 1246 | 0× | let (app, row) = (app.clone(), row.clone()); |
| 1247 | 0× | tokio::spawn(async move { crate::diffusion::catch_up(&app, &row).await }); |
| 1248 | 1× | } |
| 1249 | 1× | let notice = match crate::diffusion::rule(&app.config.diffusion.follow, &row, None, "").follow { |
| 1250 | 1× | crate::diffusion::Follow::Off => "Diffusion is off: upstream moves open no proposals here.", |
| 1251 | | crate::diffusion::Follow::Green => { |
| 1252 | 0× | "Diffusion is on: when a subrepo's upstream moves and its checks pass, a proposal moves the pin here." |
| 1253 | | } |
| 1254 | 0× | crate::diffusion::Follow::Any => "Diffusion is on: every move of a subrepo's upstream opens or updates a proposal here.", |
| 1255 | | }; |
| 1256 | 1× | back(&row, Section::Diffusion, Some(notice.into()), None) |
| 1257 | 1× | } |
| 1258 | |
|
| 1259 | | #[derive(Deserialize)] |
| 1260 | | pub struct AgentGatesForm { |
| 1261 | | #[serde(rename = "_csrf")] |
| 1262 | | csrf_token: String, |
| 1263 | | |
| 1264 | | #[serde(default)] |
| 1265 | | agents: String, |
| 1266 | | } |
| 1267 | |
|
| 1268 | |
|
| 1269 | |
|
| 1270 | |
|
| 1271 | 1× | pub async fn agent_gates( |
| 1272 | 1× | State(app): State<Shared>, |
| 1273 | 1× | viewer: Viewer, |
| 1274 | 1× | Path((owner, name)): Path<(String, String)>, |
| 1275 | 1× | Form(f): Form<AgentGatesForm>, |
| 1276 | 1× | ) -> Result<Response, Error> { |
| 1277 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1278 | 1× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1279 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1280 | 1× | let on = f.agents == "on"; |
| 1281 | 1× | db::repos::set_agents_decide_gates(&app.db, row.id, on).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1282 | 1× | accounts::audit(&app.db, viewer.account(), "repo.agents_decide_gates", &format!("{owner}/{name}"), if on { "on" } else { "off" }) |
| 1283 | 1× | .await |
| 1284 | 1× | .ok(); |
| 1285 | 1× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1286 | 1× | let notice = if on { |
| 1287 | 1× | "Agents' tokens may now approve or reject this repository's approval gates; it says so on each." |
| 1288 | | } else { |
| 1289 | 0× | "Approval gates here are decided by people: agents' tokens are refused." |
| 1290 | | }; |
| 1291 | 1× | back(&row, Section::Gates, Some(notice.into()), None) |
| 1292 | 1× | } |
| 1293 | |
|
| 1294 | | #[derive(Deserialize)] |
| 1295 | | pub struct PushProtectionForm { |
| 1296 | | #[serde(rename = "_csrf")] |
| 1297 | | csrf_token: String, |
| 1298 | | |
| 1299 | | #[serde(default)] |
| 1300 | | protect: String, |
| 1301 | | } |
| 1302 | |
|
| 1303 | |
|
| 1304 | |
|
| 1305 | |
|
| 1306 | 4× | pub async fn push_protection( |
| 1307 | 4× | State(app): State<Shared>, |
| 1308 | 4× | viewer: Viewer, |
| 1309 | 4× | Path((owner, name)): Path<(String, String)>, |
| 1310 | 4× | Form(f): Form<PushProtectionForm>, |
| 1311 | 4× | ) -> Result<Response, Error> { |
| 1312 | 4× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1313 | 4× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1314 | 4× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1315 | 4× | let on = f.protect == "on"; |
| 1316 | 4× | db::push_protection::set_enabled(&app.db, row.id, on).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1317 | 4× | accounts::audit(&app.db, viewer.account(), "repo.push_protection", &format!("{owner}/{name}"), if on { "on" } else { "off" }) |
| 1318 | 4× | .await |
| 1319 | 4× | .ok(); |
| 1320 | 4× | if on { |
| 1321 | 2× | |
| 1322 | 2× | crate::history_scan::wake(&app, row.id); |
| 1323 | 2× | } |
| 1324 | 4× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1325 | 4× | let notice = if on { |
| 1326 | 2× | "Push protection is on: a push that adds what looks like a secret is refused, saying where." |
| 1327 | | } else { |
| 1328 | 2× | "Push protection is off: pushes are no longer scanned for secrets here." |
| 1329 | | }; |
| 1330 | 4× | back(&row, Section::PushProtection, Some(notice.into()), None) |
| 1331 | 4× | } |
| 1332 | |
|
| 1333 | | #[derive(Deserialize)] |
| 1334 | | pub struct SecretBypassForm { |
| 1335 | | #[serde(rename = "_csrf")] |
| 1336 | | csrf_token: String, |
| 1337 | | fingerprint: String, |
| 1338 | | #[serde(default)] |
| 1339 | | reason: String, |
| 1340 | | } |
| 1341 | |
|
| 1342 | |
|
| 1343 | |
|
| 1344 | |
|
| 1345 | 5× | pub async fn secret_bypass( |
| 1346 | 5× | State(app): State<Shared>, |
| 1347 | 5× | viewer: Viewer, |
| 1348 | 5× | Path((owner, name)): Path<(String, String)>, |
| 1349 | 5× | Form(f): Form<SecretBypassForm>, |
| 1350 | 5× | ) -> Result<Response, Error> { |
| 1351 | 5× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1352 | 5× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1353 | 4× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1354 | 4× | let fingerprint = f.fingerprint.trim().to_ascii_lowercase(); |
| 1355 | 4× | let reason = f.reason.trim(); |
| 1356 | 64× | let error = if fingerprint.len() != 16 || !fingerprint.chars().all(|c| c.is_ascii_hexdigit()) { |
| 1357 | 0× | Some("A fingerprint is the 16 hexadecimal digits a refused push printed.") |
| 1358 | 4× | } else if reason.is_empty() { |
| 1359 | 1× | Some("Say why this is not a secret (an example key, a test fixture): the reason is kept with the fingerprint and in the audit log.") |
| 1360 | 3× | } else if reason.chars().count() > 500 { |
| 1361 | 0× | Some("Keep the reason under 500 characters.") |
| 1362 | | } else { |
| 1363 | 3× | None |
| 1364 | | }; |
| 1365 | 4× | if let Some(e) = error { |
| 1366 | 1× | return back(&row, Section::PushProtection, None, Some(e.into())); |
| 1367 | 3× | } |
| 1368 | 3× | let rule = db::push_protection::rule_of(&app.db, row.id, &fingerprint) |
| 1369 | 3× | .await |
| 1370 | 3× | .map_err(|e| Error::from_db(shell.clone(), e))? |
| 1371 | 3× | .unwrap_or_default(); |
| 1372 | 3× | let by = viewer.account().map(|a| (a.id, a.username.as_str())); |
| 1373 | 3× | db::push_protection::allow(&app.db, row.id, &fingerprint, &rule, reason, by).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1374 | 3× | let detail = if rule.is_empty() { format!("{fingerprint}: {reason}") } else { format!("{fingerprint} ({rule}): {reason}") }; |
| 1375 | 3× | accounts::audit(&app.db, viewer.account(), "secret.bypass", &format!("{owner}/{name}"), &detail).await.ok(); |
| 1376 | 3× | let notice = format!("Fingerprint {fingerprint} is allowed here: a push adding that value is no longer refused for it."); |
| 1377 | 3× | back(&row, Section::PushProtection, Some(notice), None) |
| 1378 | 5× | } |
| 1379 | |
|
| 1380 | | #[derive(Deserialize)] |
| 1381 | | pub struct BypassRemoveForm { |
| 1382 | | #[serde(rename = "_csrf")] |
| 1383 | | csrf_token: String, |
| 1384 | | fingerprint: String, |
| 1385 | | } |
| 1386 | |
|
| 1387 | |
|
| 1388 | |
|
| 1389 | |
|
| 1390 | 3× | pub async fn secret_bypass_remove( |
| 1391 | 3× | State(app): State<Shared>, |
| 1392 | 3× | viewer: Viewer, |
| 1393 | 3× | Path((owner, name)): Path<(String, String)>, |
| 1394 | 3× | Form(f): Form<BypassRemoveForm>, |
| 1395 | 3× | ) -> Result<Response, Error> { |
| 1396 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1397 | 3× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1398 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1399 | 2× | let fingerprint = f.fingerprint.trim().to_ascii_lowercase(); |
| 1400 | 2× | let removed = db::push_protection::remove_bypass(&app.db, row.id, &fingerprint).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1401 | 2× | let Some(b) = removed else { |
| 1402 | 1× | return back(&row, Section::PushProtection, None, Some(format!("Fingerprint {fingerprint} is not allowed here."))); |
| 1403 | | }; |
| 1404 | 1× | let detail = if b.rule.is_empty() { |
| 1405 | 0× | format!("{fingerprint} (was: {})", b.reason) |
| 1406 | | } else { |
| 1407 | 1× | format!("{fingerprint} ({}) (was: {})", b.rule, b.reason) |
| 1408 | | }; |
| 1409 | 1× | accounts::audit(&app.db, viewer.account(), "secret.bypass_removed", &format!("{owner}/{name}"), &detail).await.ok(); |
| 1410 | 1× | let notice = format!("Fingerprint {fingerprint} is no longer allowed: a push adding that value is refused again."); |
| 1411 | 1× | back(&row, Section::PushProtection, Some(notice), None) |
| 1412 | 3× | } |
| 1413 | |
|
| 1414 | | #[derive(Deserialize)] |
| 1415 | | pub struct SecretResolveForm { |
| 1416 | | #[serde(rename = "_csrf")] |
| 1417 | | csrf_token: String, |
| 1418 | | finding: i64, |
| 1419 | | #[serde(default)] |
| 1420 | | reason: String, |
| 1421 | | } |
| 1422 | |
|
| 1423 | |
|
| 1424 | |
|
| 1425 | |
|
| 1426 | 4× | pub async fn secret_resolve( |
| 1427 | 4× | State(app): State<Shared>, |
| 1428 | 4× | viewer: Viewer, |
| 1429 | 4× | Path((owner, name)): Path<(String, String)>, |
| 1430 | 4× | Form(f): Form<SecretResolveForm>, |
| 1431 | 4× | ) -> Result<Response, Error> { |
| 1432 | 4× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1433 | 4× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1434 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1435 | 3× | let reason = f.reason.trim(); |
| 1436 | 3× | let error = if reason.is_empty() { |
| 1437 | 1× | Some("Say how it was resolved (the key was revoked, the token rotated): the reason is kept with the finding and in the audit log.") |
| 1438 | 2× | } else if reason.chars().count() > 500 { |
| 1439 | 0× | Some("Keep the reason under 500 characters.") |
| 1440 | | } else { |
| 1441 | 2× | None |
| 1442 | | }; |
| 1443 | 3× | if let Some(e) = error { |
| 1444 | 1× | return back(&row, Section::PushProtection, None, Some(e.into())); |
| 1445 | 2× | } |
| 1446 | 2× | let by = viewer.account().map(|a| a.username.as_str()).unwrap_or(""); |
| 1447 | 2× | let done = db::push_protection::resolve(&app.db, row.id, f.finding, by, reason).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1448 | 2× | let Some(found) = done else { |
| 1449 | 1× | return back(&row, Section::PushProtection, None, Some("That finding is not open here.".into())); |
| 1450 | | }; |
| 1451 | 1× | let detail = format!("{} ({}) at {}:{}: {reason}", found.fingerprint, found.rule, found.path, found.line); |
| 1452 | 1× | accounts::audit(&app.db, viewer.account(), "secret.resolved", &format!("{owner}/{name}"), &detail).await.ok(); |
| 1453 | 1× | let notice = format!("The finding at {}:{} is marked resolved.", found.path, found.line); |
| 1454 | 1× | back(&row, Section::PushProtection, Some(notice), None) |
| 1455 | 4× | } |
| 1456 | |
|
| 1457 | | #[derive(Deserialize)] |
| 1458 | | pub struct SecretScanForm { |
| 1459 | | #[serde(rename = "_csrf")] |
| 1460 | | csrf_token: String, |
| 1461 | | } |
| 1462 | |
|
| 1463 | |
|
| 1464 | |
|
| 1465 | |
|
| 1466 | 2× | pub async fn secret_scan( |
| 1467 | 2× | State(app): State<Shared>, |
| 1468 | 2× | viewer: Viewer, |
| 1469 | 2× | Path((owner, name)): Path<(String, String)>, |
| 1470 | 2× | Form(f): Form<SecretScanForm>, |
| 1471 | 2× | ) -> Result<Response, Error> { |
| 1472 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1473 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1474 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1475 | 1× | if !row.push_protection { |
| 1476 | 0× | return back( |
| 1477 | 0× | &row, |
| 1478 | 0× | Section::PushProtection, |
| 1479 | 0× | None, |
| 1480 | 0× | Some("Turn push protection on first: history is scanned only where it is on.".into()), |
| 1481 | | ); |
| 1482 | 1× | } |
| 1483 | 1× | crate::history_scan::request(&app, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1484 | 1× | accounts::audit(&app.db, viewer.account(), "secret.history_scan", &format!("{owner}/{name}"), "requested").await.ok(); |
| 1485 | 1× | let notice = "The whole history will be scanned again, before other repositories: findings appear below as they are found."; |
| 1486 | 1× | back(&row, Section::PushProtection, Some(notice.into()), None) |
| 1487 | 2× | } |
| 1488 | |
|
| 1489 | | #[derive(Deserialize)] |
| 1490 | | pub struct SupersedeForm { |
| 1491 | | #[serde(rename = "_csrf")] |
| 1492 | | csrf_token: String, |
| 1493 | | |
| 1494 | | |
| 1495 | | #[serde(default)] |
| 1496 | | running: String, |
| 1497 | | } |
| 1498 | |
|
| 1499 | |
|
| 1500 | |
|
| 1501 | |
|
| 1502 | 1× | pub async fn flow_supersede( |
| 1503 | 1× | State(app): State<Shared>, |
| 1504 | 1× | viewer: Viewer, |
| 1505 | 1× | Path((owner, name)): Path<(String, String)>, |
| 1506 | 1× | Form(f): Form<SupersedeForm>, |
| 1507 | 1× | ) -> Result<Response, Error> { |
| 1508 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1509 | 1× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1510 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1511 | 1× | let keep = f.running != "on"; |
| 1512 | 1× | db::repos::set_flow_keep_running(&app.db, row.id, keep).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1513 | 1× | accounts::audit(&app.db, viewer.account(), "repo.flow_keep_running", &format!("{owner}/{name}"), if keep { "on" } else { "off" }) |
| 1514 | 1× | .await |
| 1515 | 1× | .ok(); |
| 1516 | 1× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1517 | 1× | let notice = if keep { |
| 1518 | 0× | "A new push or proposal run now cancels only the older runs still queued; those already running finish." |
| 1519 | | } else { |
| 1520 | 1× | "A new push or proposal run now cancels the older runs it supersedes, queued or running." |
| 1521 | | }; |
| 1522 | 1× | back(&row, Section::Runs, Some(notice.into()), None) |
| 1523 | 1× | } |
| 1524 | |
|
| 1525 | | #[derive(Deserialize)] |
| 1526 | | pub struct ReuseForm { |
| 1527 | | #[serde(rename = "_csrf")] |
| 1528 | | csrf_token: String, |
| 1529 | | |
| 1530 | | #[serde(default)] |
| 1531 | | reuse: String, |
| 1532 | | } |
| 1533 | |
|
| 1534 | |
|
| 1535 | |
|
| 1536 | |
|
| 1537 | 0× | pub async fn flow_reuse( |
| 1538 | 0× | State(app): State<Shared>, |
| 1539 | 0× | viewer: Viewer, |
| 1540 | 0× | Path((owner, name)): Path<(String, String)>, |
| 1541 | 0× | Form(f): Form<ReuseForm>, |
| 1542 | 0× | ) -> Result<Response, Error> { |
| 1543 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1544 | 0× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1545 | 0× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1546 | 0× | let on = f.reuse == "on"; |
| 1547 | 0× | db::repos::set_flow_reuse(&app.db, row.id, on).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1548 | 0× | accounts::audit(&app.db, viewer.account(), "repo.flow_reuse", &format!("{owner}/{name}"), if on { "on" } else { "off" }).await.ok(); |
| 1549 | 0× | let notice = if on { |
| 1550 | 0× | "A push run now reuses a job's pass on the same state." |
| 1551 | | } else { |
| 1552 | 0× | "Every job of every push now runs, passed before or not." |
| 1553 | | }; |
| 1554 | 0× | back(&row, Section::Runs, Some(notice.into()), None) |
| 1555 | 0× | } |
| 1556 | |
|
| 1557 | | #[derive(Deserialize)] |
| 1558 | | pub struct SkipForm { |
| 1559 | | #[serde(rename = "_csrf")] |
| 1560 | | csrf_token: String, |
| 1561 | | |
| 1562 | | skip: String, |
| 1563 | | } |
| 1564 | |
|
| 1565 | |
|
| 1566 | |
|
| 1567 | |
|
| 1568 | 2× | pub async fn flow_skip( |
| 1569 | 2× | State(app): State<Shared>, |
| 1570 | 2× | viewer: Viewer, |
| 1571 | 2× | Path((owner, name)): Path<(String, String)>, |
| 1572 | 2× | Form(f): Form<SkipForm>, |
| 1573 | 2× | ) -> Result<Response, Error> { |
| 1574 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1575 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1576 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1577 | 2× | let notice = match f.skip.as_str() { |
| 1578 | 2× | "any" => "A push may now skip its run on any bookmark.".to_string(), |
| 1579 | 2× | "others" => format!("A push may now skip its run on any bookmark but {}.", row.default_bookmark), |
| 1580 | 1× | "never" => "Every push now runs; skip markers are ignored.".to_string(), |
| 1581 | 1× | _ => return back(&row, Section::Runs, None, Some("Choose where a push may skip its run.".into())), |
| 1582 | | }; |
| 1583 | 1× | db::repos::set_flow_skip(&app.db, row.id, &f.skip).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1584 | 1× | accounts::audit(&app.db, viewer.account(), "repo.flow_skip", &format!("{owner}/{name}"), &f.skip).await.ok(); |
| 1585 | 1× | back(&row, Section::Runs, Some(notice), None) |
| 1586 | 2× | } |
| 1587 | |
|
| 1588 | | #[derive(Deserialize)] |
| 1589 | | pub struct PriorityForm { |
| 1590 | | #[serde(rename = "_csrf")] |
| 1591 | | csrf_token: String, |
| 1592 | | |
| 1593 | | priority: String, |
| 1594 | | } |
| 1595 | |
|
| 1596 | |
|
| 1597 | |
|
| 1598 | |
|
| 1599 | 2× | pub async fn flow_priority( |
| 1600 | 2× | State(app): State<Shared>, |
| 1601 | 2× | viewer: Viewer, |
| 1602 | 2× | Path((owner, name)): Path<(String, String)>, |
| 1603 | 2× | Form(f): Form<PriorityForm>, |
| 1604 | 2× | ) -> Result<Response, Error> { |
| 1605 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1606 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1607 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1608 | 2× | let level = match f.priority.as_str() { |
| 1609 | 2× | "low" | "normal" | "high" => f.priority.as_str(), |
| 1610 | 0× | "default" => "", |
| 1611 | 0× | _ => return back(&row, Section::Runs, None, Some("Pick low, normal, high or the default.".into())), |
| 1612 | | }; |
| 1613 | 2× | if level == "high" && !viewer.is_admin() { |
| 1614 | 0× | return back(&row, Section::Runs, None, Some("Only site administrators raise a repository to high.".into())); |
| 1615 | 2× | } |
| 1616 | 2× | db::repos::set_flow_priority(&app.db, row.id, level).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1617 | 2× | accounts::audit( |
| 1618 | 2× | &app.db, |
| 1619 | 2× | viewer.account(), |
| 1620 | 2× | "repo.flow_priority", |
| 1621 | 2× | &format!("{owner}/{name}"), |
| 1622 | 2× | if level.is_empty() { "default" } else { level }, |
| 1623 | | ) |
| 1624 | 2× | .await |
| 1625 | 2× | .ok(); |
| 1626 | 2× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1627 | 2× | let notice = if level.is_empty() { |
| 1628 | 0× | "Its jobs follow the organisation's and the site's priority.".to_string() |
| 1629 | | } else { |
| 1630 | 2× | format!("Its jobs are {level} priority.") |
| 1631 | | }; |
| 1632 | 2× | back(&row, Section::Runs, Some(notice), None) |
| 1633 | 2× | } |
| 1634 | |
|
| 1635 | | #[derive(Deserialize)] |
| 1636 | | pub struct FlowForm { |
| 1637 | | #[serde(rename = "_csrf")] |
| 1638 | | csrf_token: String, |
| 1639 | | |
| 1640 | | flow: String, |
| 1641 | | } |
| 1642 | |
|
| 1643 | |
|
| 1644 | |
|
| 1645 | |
|
| 1646 | |
|
| 1647 | 9× | pub async fn flow( |
| 1648 | 9× | State(app): State<Shared>, |
| 1649 | 9× | viewer: Viewer, |
| 1650 | 9× | Path((owner, name)): Path<(String, String)>, |
| 1651 | 9× | Form(f): Form<FlowForm>, |
| 1652 | 9× | ) -> Result<Response, Error> { |
| 1653 | 9× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1654 | 9× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1655 | 9× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1656 | 9× | let setting = match f.flow.as_str() { |
| 1657 | 9× | "on" => "on", |
| 1658 | 6× | "own" => "own", |
| 1659 | 4× | "off" => "off", |
| 1660 | 3× | "default" => "", |
| 1661 | 2× | _ => return back(&row, Section::Pipelines, None, Some("Pick on, own runners, off or the site default.".into())), |
| 1662 | | }; |
| 1663 | 7× | if !viewer.is_admin() { |
| 1664 | 4× | if matches!(setting, "on" | "") { |
| 1665 | 2× | return back( |
| 1666 | 2× | &row, |
| 1667 | 2× | Section::Pipelines, |
| 1668 | 2× | None, |
| 1669 | 2× | Some("Only site administrators let the site's runners take this repository's jobs.".into()), |
| 1670 | | ); |
| 1671 | 2× | } |
| 1672 | 2× | if setting == "own" && !has_own_runner(&app, &row).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 1673 | 1× | return back( |
| 1674 | 1× | &row, |
| 1675 | 1× | Section::Pipelines, |
| 1676 | 1× | None, |
| 1677 | 1× | Some("Register a runner for this repository (Settings › Runners) or its owner first.".into()), |
| 1678 | | ); |
| 1679 | 1× | } |
| 1680 | 3× | } |
| 1681 | 4× | db::repos::set_flow(&app.db, row.id, setting).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1682 | 4× | let label = if setting.is_empty() { "default" } else { setting }; |
| 1683 | 4× | accounts::audit(&app.db, viewer.account(), "repo.flow", &format!("{owner}/{name}"), label).await.ok(); |
| 1684 | 4× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1685 | 4× | let notice = if row.flow == "own" { |
| 1686 | 1× | "OxygenFlow is on, on this repository's own runners: pushes with a .flow/pipeline.kdl start runs that only they take." |
| 1687 | 3× | } else if crate::flow::enabled(&app, &row) { |
| 1688 | 1× | "OxygenFlow is on: pushes with a .flow/pipeline.kdl start runs." |
| 1689 | | } else { |
| 1690 | 2× | "OxygenFlow is off for this repository." |
| 1691 | | }; |
| 1692 | 4× | back(&row, Section::Pipelines, Some(notice.into()), None) |
| 1693 | 9× | } |
| 1694 | |
|
| 1695 | 2× | pub async fn mirror( |
| 1696 | 2× | State(app): State<Shared>, |
| 1697 | 2× | viewer: Viewer, |
| 1698 | 2× | Path((owner, name)): Path<(String, String)>, |
| 1699 | 2× | Form(f): Form<MirrorForm>, |
| 1700 | 2× | ) -> Result<Response, Error> { |
| 1701 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1702 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 1703 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1704 | 2× | db::repos::set_git_mirror(&app.db, row.id, f.enabled.is_some()).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1705 | 2× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1706 | 2× | if row.git_mirror { |
| 1707 | 1× | crate::mirror::refresh(&app, &row).await.ok(); |
| 1708 | 1× | } |
| 1709 | 2× | accounts::audit(&app.db, viewer.account(), "repo.mirror", &format!("{owner}/{name}"), if row.git_mirror { "on" } else { "off" }) |
| 1710 | 2× | .await |
| 1711 | 2× | .ok(); |
| 1712 | 2× | let row = db::repos::get_by_id(&app.db, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?.unwrap_or(row); |
| 1713 | 2× | let notice = if row.git_mirror { "The Git mirror is on and up to date." } else { "The Git mirror is off." }; |
| 1714 | 2× | back(&row, Section::GitMirror, Some(notice.into()), None) |
| 1715 | 2× | } |
| 1716 | |
|
| 1717 | |
|
| 1718 | |
|
| 1719 | 2× | pub async fn runner_token( |
| 1720 | 2× | State(app): State<Shared>, |
| 1721 | 2× | viewer: Viewer, |
| 1722 | 2× | Path((owner, name)): Path<(String, String)>, |
| 1723 | 2× | Form(f): Form<super::session::CsrfForm>, |
| 1724 | 2× | ) -> Result<Page<RepoSettingsPage>, Error> { |
| 1725 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1726 | 2× | let (_, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1727 | 2× | csrf(&viewer, &f.csrf, &shell)?; |
| 1728 | 2× | let me = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 1729 | 2× | let token = format!("flowreg_{}", crate::auth::random_hex(24)); |
| 1730 | 2× | let scope = db::runners::Scope { owner: row.owner.clone(), repo_id: Some(row.id) }; |
| 1731 | 2× | db::runners::add_registration(&app.db, &blake3::hash(token.as_bytes()).to_hex(), &me, &scope) |
| 1732 | 2× | .await |
| 1733 | 2× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 1734 | 2× | accounts::audit(&app.db, viewer.account(), "flow.runner.token", &row.slug(), "registration token for this repository").await.ok(); |
| 1735 | 2× | let mut page = |
| 1736 | 2× | settings_page(&app, shell, row, role, Section::Runners, Some("Copy the token now; it is not shown again.".into()), None).await?; |
| 1737 | 2× | page.0.runner_token = Some(token); |
| 1738 | 2× | Ok(page) |
| 1739 | 2× | } |
| 1740 | |
|
| 1741 | |
|
| 1742 | |
|
| 1743 | 2× | pub async fn runner_remove( |
| 1744 | 2× | State(app): State<Shared>, |
| 1745 | 2× | viewer: Viewer, |
| 1746 | 2× | Path((owner, name, id)): Path<(String, String, i64)>, |
| 1747 | 2× | Form(f): Form<super::session::CsrfForm>, |
| 1748 | 2× | ) -> Result<Response, Error> { |
| 1749 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1750 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1751 | 2× | csrf(&viewer, &f.csrf, &shell)?; |
| 1752 | 2× | let e = |e| Error::from_db(shell.clone(), e); |
| 1753 | 2× | let Some(r) = db::runners::get(&app.db, id).await.map_err(e)?.filter(|r| r.scope_repo_id == Some(row.id)) else { |
| 1754 | 1× | return Err(Error::not_found(shell)); |
| 1755 | | }; |
| 1756 | 1× | if db::runners::running(&app.db).await.map_err(e)?.iter().any(|(rid, n)| *rid == id && *n > 0) { |
| 1757 | 0× | return back(&row, Section::Runners, None, Some(format!("{} is running jobs: wait for them to finish.", r.name))); |
| 1758 | 1× | } |
| 1759 | 1× | db::runners::remove(&app.db, id).await.map_err(e)?; |
| 1760 | 1× | accounts::audit(&app.db, viewer.account(), "flow.runner.removed", &r.name, &row.slug()).await.ok(); |
| 1761 | 1× | back(&row, Section::Runners, Some(format!("{} removed: its credential no longer works.", r.name)), None) |
| 1762 | 2× | } |
| 1763 | |
|
| 1764 | 404× | fn repo_assistants(row: &db::repos::RepoRow) -> crate::levels::At { |
| 1765 | 404× | crate::levels::At::repo(row) |
| 1766 | 404× | } |
| 1767 | |
|
| 1768 | |
|
| 1769 | |
|
| 1770 | 2× | pub async fn assistants( |
| 1771 | 2× | State(app): State<Shared>, |
| 1772 | 2× | viewer: Viewer, |
| 1773 | 2× | Path((owner, name)): Path<(String, String)>, |
| 1774 | 2× | Form(f): Form<super::assistants::AssistantForm>, |
| 1775 | 2× | ) -> Result<Response, Error> { |
| 1776 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1777 | 2× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1778 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1779 | 1× | match super::assistants::apply(&app, &viewer, &repo_assistants(&row), f).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 1780 | 1× | Ok(notice) => back(&row, Section::Assistants, Some(notice), None), |
| 1781 | 0× | Err(error) => back(&row, Section::Assistants, None, Some(error)), |
| 1782 | | } |
| 1783 | 2× | } |
| 1784 | |
|
| 1785 | |
|
| 1786 | |
|
| 1787 | 3× | pub async fn variables( |
| 1788 | 3× | State(app): State<Shared>, |
| 1789 | 3× | viewer: Viewer, |
| 1790 | 3× | Path((owner, name)): Path<(String, String)>, |
| 1791 | 3× | Form(f): Form<super::variables::VariableForm>, |
| 1792 | 3× | ) -> Result<Response, Error> { |
| 1793 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1794 | 3× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1795 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1796 | 3× | match super::variables::apply(&app, &viewer, &repo_assistants(&row), f).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 1797 | 2× | Ok(notice) => back(&row, Section::Variables, Some(notice), None), |
| 1798 | 1× | Err(error) => back(&row, Section::Variables, None, Some(error)), |
| 1799 | | } |
| 1800 | 3× | } |
| 1801 | |
|
| 1802 | |
|
| 1803 | |
|
| 1804 | 1× | pub async fn secrets( |
| 1805 | 1× | State(app): State<Shared>, |
| 1806 | 1× | viewer: Viewer, |
| 1807 | 1× | Path((owner, name)): Path<(String, String)>, |
| 1808 | 1× | Form(f): Form<super::secrets::SecretForm>, |
| 1809 | 1× | ) -> Result<Response, Error> { |
| 1810 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1811 | 1× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1812 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1813 | 1× | match super::secrets::apply(&app, &viewer, &repo_assistants(&row), f).await.map_err(|e| Error::from_db(shell.clone(), e))? { |
| 1814 | 1× | Ok(notice) => back(&row, Section::Secrets, Some(notice), None), |
| 1815 | 0× | Err(error) => back(&row, Section::Secrets, None, Some(error)), |
| 1816 | | } |
| 1817 | 1× | } |
| 1818 | |
|
| 1819 | |
|
| 1820 | 0× | pub async fn assistant_models( |
| 1821 | 0× | State(app): State<Shared>, |
| 1822 | 0× | viewer: Viewer, |
| 1823 | 0× | Path((owner, name)): Path<(String, String)>, |
| 1824 | 0× | Form(f): Form<super::assistants::ModelsForm>, |
| 1825 | 0× | ) -> Result<axum::Json<serde_json::Value>, Error> { |
| 1826 | 0× | let shell = Shell::new(&app, &format!("/{owner}/{name}/settings"), &viewer); |
| 1827 | 0× | let (_, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Admin).await?; |
| 1828 | 0× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 1829 | 0× | Ok(axum::Json(super::assistants::models(&app, &repo_assistants(&row), f).await.map_err(|e| Error::from_db(shell.clone(), e))?)) |
| 1830 | 0× | } |
| 1831 | |
|
| 1832 | |
|
| 1833 | |
|
| 1834 | 135× | async fn has_own_runner(app: &Shared, row: &db::repos::RepoRow) -> sqlx::Result<bool> { |
| 1835 | 135× | Ok(db::runners::serving(&app.db, &row.owner, row.id).await?.iter().any(|r| !r.scope().is_site())) |
| 1836 | 135× | } |