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