| 1 | use std::time::SystemTime; |
| 2 | |
| 3 | use crate::domain::{ |
| 4 | Actor, DomainError, ImportSource, ImportState, OrgName, RefName, RepoId, RepoName, RepoWrite, |
| 5 | Repository, Visibility, |
| 6 | repository::{ |
| 7 | IssueRepository, MembershipRepository, OrgRepository, PullRequestRepository, RepoRepository, |
| 8 | }, |
| 9 | }; |
| 10 | |
| 11 | use super::{ |
| 12 | access::{Stores, changeable_repo, is_org_member, is_org_owner, visible_repo}, |
| 13 | error::{Error, Result}, |
| 14 | paging::Paged, |
| 15 | port::{GitQuery, GitStorage, GitStorageError}, |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | pub const REPO_LIMIT: usize = 50; |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 30 | pub struct NewRepo { |
| 31 | pub name: String, |
| 32 | pub description: Option<String>, |
| 33 | pub visibility: Visibility, |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | pub async fn create_repo( |
| 49 | actor: &Actor, |
| 50 | handle: &OrgName, |
| 51 | spec: &NewRepo, |
| 52 | now: SystemTime, |
| 53 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 54 | storage: &impl GitStorage, |
| 55 | ) -> Result<Repository> { |
| 56 | let Some(org) = stores.orgs.find_by_name(handle).await? else { |
| 57 | return Err(DomainError::NotFound { |
| 58 | entity: "repository owner", |
| 59 | } |
| 60 | .into()); |
| 61 | }; |
| 62 | |
| 63 | if !is_org_owner(&org, actor, stores.memberships).await? { |
| 64 | return Err(DomainError::Forbidden.into()); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | let repo = Repository::new( |
| 70 | RepoId::generate(), |
| 71 | org.id.clone(), |
| 72 | spec.name.clone(), |
| 73 | spec.description.clone(), |
| 74 | spec.visibility, |
| 75 | now, |
| 76 | )?; |
| 77 | |
| 78 | if stores |
| 79 | .repos |
| 80 | .find_by_org_and_name(&org.id, &repo.name) |
| 81 | .await? |
| 82 | .is_some() |
| 83 | { |
| 84 | return Err(taken()); |
| 85 | } |
| 86 | |
| 87 | storage |
| 88 | .init_bare(handle, &repo.name) |
| 89 | .await |
| 90 | .map_err(|error| { |
| 91 | match error { |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | GitStorageError::AlreadyExists => taken(), |
| 98 | other => Error::GitStorage(other), |
| 99 | } |
| 100 | })?; |
| 101 | |
| 102 | if let Err(error) = stores.repos.save(&repo).await { |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | let _ = storage.remove(handle, &repo.name).await; |
| 110 | return Err(error.into()); |
| 111 | } |
| 112 | |
| 113 | Ok(repo) |
| 114 | } |
| 115 | |
| 116 | |
| 117 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 118 | pub struct RepoView { |
| 119 | |
| 120 | pub handle: OrgName, |
| 121 | pub name: RepoName, |
| 122 | pub description: Option<String>, |
| 123 | pub visibility: Visibility, |
| 124 | |
| 125 | |
| 126 | pub updated_at: SystemTime, |
| 127 | |
| 128 | |
| 129 | |
| 130 | pub pinned: bool, |
| 131 | |
| 132 | |
| 133 | |
| 134 | pub archived: bool, |
| 135 | |
| 136 | |
| 137 | pub viewer_is_owner: bool, |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | pub import: Option<ImportStatus>, |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | pub open_issues: u64, |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | pub open_pulls: u64, |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 164 | pub struct ImportStatus { |
| 165 | pub state: ImportState, |
| 166 | pub source: Option<ImportSource>, |
| 167 | pub error: Option<String>, |
| 168 | } |
| 169 | |
| 170 | impl ImportStatus { |
| 171 | |
| 172 | fn of(repo: &Repository) -> Option<Self> { |
| 173 | Some(Self { |
| 174 | state: repo.import_state?, |
| 175 | source: repo.import_source.clone(), |
| 176 | error: repo.import_error.clone(), |
| 177 | }) |
| 178 | } |
| 179 | |
| 180 | pub fn is_running(&self) -> bool { |
| 181 | self.state == ImportState::Importing |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | pub async fn view_repo( |
| 198 | handle: &OrgName, |
| 199 | name: &RepoName, |
| 200 | actor: &Actor, |
| 201 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 202 | issues: &impl IssueRepository, |
| 203 | pulls: &impl PullRequestRepository, |
| 204 | ) -> Result<Option<RepoView>> { |
| 205 | let Some((org, repo)) = visible_repo(actor, handle, name, stores).await? else { |
| 206 | return Ok(None); |
| 207 | }; |
| 208 | |
| 209 | |
| 210 | |
| 211 | let (issue_counts, pull_counts) = |
| 212 | tokio::try_join!(issues.counts(&repo.id), pulls.counts(&repo.id))?; |
| 213 | |
| 214 | Ok(Some(RepoView { |
| 215 | handle: org.name.clone(), |
| 216 | import: ImportStatus::of(&repo), |
| 217 | name: repo.name, |
| 218 | description: repo.description, |
| 219 | visibility: repo.visibility, |
| 220 | updated_at: repo.updated_at, |
| 221 | pinned: repo.pinned, |
| 222 | archived: repo.archived, |
| 223 | viewer_is_owner: is_org_owner(&org, actor, stores.memberships).await?, |
| 224 | open_issues: issue_counts.open, |
| 225 | open_pulls: pull_counts.open, |
| 226 | })) |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 234 | pub struct RepoSummary { |
| 235 | pub name: RepoName, |
| 236 | pub description: Option<String>, |
| 237 | pub visibility: Visibility, |
| 238 | pub updated_at: SystemTime, |
| 239 | |
| 240 | |
| 241 | |
| 242 | pub pinned: bool, |
| 243 | |
| 244 | |
| 245 | pub archived: bool, |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | pub import_state: Option<ImportState>, |
| 253 | } |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | pub async fn list_repos( |
| 267 | handle: &OrgName, |
| 268 | actor: &Actor, |
| 269 | page: Option<usize>, |
| 270 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 271 | ) -> Result<Option<Paged<RepoSummary>>> { |
| 272 | let Some(org) = stores.orgs.find_by_name(handle).await? else { |
| 273 | return Ok(None); |
| 274 | }; |
| 275 | |
| 276 | |
| 277 | |
| 278 | let is_member = is_org_member(&org, actor, stores.memberships).await?; |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | let visible: Vec<RepoSummary> = stores |
| 284 | .repos |
| 285 | .list_by_org(&org.id) |
| 286 | .await? |
| 287 | .into_iter() |
| 288 | .filter(|repo| repo.visibility.is_public() || is_member) |
| 289 | .map(|repo| RepoSummary { |
| 290 | import_state: repo.import_state, |
| 291 | name: repo.name, |
| 292 | description: repo.description, |
| 293 | visibility: repo.visibility, |
| 294 | updated_at: repo.updated_at, |
| 295 | pinned: repo.pinned, |
| 296 | archived: repo.archived, |
| 297 | }) |
| 298 | .collect(); |
| 299 | |
| 300 | Ok(Some( |
| 301 | |
| 302 | |
| 303 | |
| 304 | match page { |
| 305 | Some(page) => Paged::slice(visible, page, REPO_LIMIT), |
| 306 | None => { |
| 307 | let size = visible.len().max(1); |
| 308 | Paged::slice(visible, 1, size) |
| 309 | } |
| 310 | }, |
| 311 | )) |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 321 | pub struct RepoEdit { |
| 322 | pub description: Option<String>, |
| 323 | pub visibility: Visibility, |
| 324 | |
| 325 | |
| 326 | pub pinned: bool, |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | pub async fn update_repo( |
| 343 | actor: &Actor, |
| 344 | handle: &OrgName, |
| 345 | name: &RepoName, |
| 346 | edit: &RepoEdit, |
| 347 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 348 | ) -> Result<Repository> { |
| 349 | let existing = changeable_repo(actor, handle, name, stores).await?; |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | let mut updated = Repository::new( |
| 356 | existing.id, |
| 357 | existing.org_id.clone(), |
| 358 | existing.name.as_str(), |
| 359 | edit.description.clone(), |
| 360 | edit.visibility, |
| 361 | |
| 362 | |
| 363 | |
| 364 | existing.updated_at, |
| 365 | )?; |
| 366 | updated.pinned = edit.pinned; |
| 367 | |
| 368 | |
| 369 | updated.archived = existing.archived; |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | let updated = updated.with_import( |
| 378 | existing.import_state, |
| 379 | existing.import_source.clone(), |
| 380 | existing.import_error.clone(), |
| 381 | ); |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | if updated.pinned { |
| 392 | for other in stores.repos.list_by_org(&existing.org_id).await? { |
| 393 | if other.pinned && other.id != updated.id { |
| 394 | stores |
| 395 | .repos |
| 396 | .save(&Repository { |
| 397 | pinned: false, |
| 398 | ..other |
| 399 | }) |
| 400 | .await?; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | stores.repos.save(&updated).await?; |
| 406 | |
| 407 | Ok(updated) |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | pub async fn rename_repo( |
| 434 | actor: &Actor, |
| 435 | handle: &OrgName, |
| 436 | name: &RepoName, |
| 437 | new_name: &str, |
| 438 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 439 | storage: &impl GitStorage, |
| 440 | ) -> Result<Repository> { |
| 441 | let existing = changeable_repo(actor, handle, name, stores).await?; |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | existing.refuse_writes(RepoWrite::Settings)?; |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | let mut renamed = Repository::new( |
| 452 | existing.id.clone(), |
| 453 | existing.org_id.clone(), |
| 454 | new_name, |
| 455 | existing.description.clone(), |
| 456 | existing.visibility, |
| 457 | |
| 458 | existing.updated_at, |
| 459 | )?; |
| 460 | renamed.pinned = existing.pinned; |
| 461 | renamed.archived = existing.archived; |
| 462 | |
| 463 | |
| 464 | |
| 465 | let renamed = renamed.with_import( |
| 466 | existing.import_state, |
| 467 | existing.import_source.clone(), |
| 468 | existing.import_error.clone(), |
| 469 | ); |
| 470 | |
| 471 | if renamed.name == existing.name { |
| 472 | return Ok(existing); |
| 473 | } |
| 474 | |
| 475 | if stores |
| 476 | .repos |
| 477 | .find_by_org_and_name(&existing.org_id, &renamed.name) |
| 478 | .await? |
| 479 | .is_some() |
| 480 | { |
| 481 | return Err(taken()); |
| 482 | } |
| 483 | |
| 484 | storage |
| 485 | .rename(handle, &existing.name, &renamed.name) |
| 486 | .await |
| 487 | .map_err(|error| match error { |
| 488 | |
| 489 | |
| 490 | GitStorageError::AlreadyExists => taken(), |
| 491 | other => Error::GitStorage(other), |
| 492 | })?; |
| 493 | |
| 494 | if let Err(error) = stores.repos.save(&renamed).await { |
| 495 | |
| 496 | |
| 497 | let _ = storage.rename(handle, &renamed.name, &existing.name).await; |
| 498 | return Err(error.into()); |
| 499 | } |
| 500 | |
| 501 | Ok(renamed) |
| 502 | } |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | pub async fn set_default_branch( |
| 519 | actor: &Actor, |
| 520 | handle: &OrgName, |
| 521 | name: &RepoName, |
| 522 | branch: &str, |
| 523 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 524 | queries: &impl GitQuery, |
| 525 | storage: &impl GitStorage, |
| 526 | ) -> Result<RefName> { |
| 527 | let repo = changeable_repo(actor, handle, name, stores).await?; |
| 528 | |
| 529 | |
| 530 | |
| 531 | let branch = RefName::new(branch)?; |
| 532 | |
| 533 | let exists = queries |
| 534 | .branches(handle, &repo.name) |
| 535 | .await? |
| 536 | .iter() |
| 537 | .any(|row| row.name == branch); |
| 538 | |
| 539 | if !exists { |
| 540 | return Err(DomainError::NotFound { entity: "branch" }.into()); |
| 541 | } |
| 542 | |
| 543 | storage |
| 544 | .set_default_branch(handle, &repo.name, &branch) |
| 545 | .await?; |
| 546 | |
| 547 | Ok(branch) |
| 548 | } |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | pub async fn set_repo_archived( |
| 568 | actor: &Actor, |
| 569 | handle: &OrgName, |
| 570 | name: &RepoName, |
| 571 | archived: bool, |
| 572 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 573 | ) -> Result<Repository> { |
| 574 | let existing = changeable_repo(actor, handle, name, stores).await?; |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | existing.refuse_writes(RepoWrite::Settings)?; |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | let updated = Repository { |
| 585 | archived, |
| 586 | ..existing |
| 587 | }; |
| 588 | |
| 589 | stores.repos.save(&updated).await?; |
| 590 | |
| 591 | Ok(updated) |
| 592 | } |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | pub async fn delete_repo( |
| 610 | actor: &Actor, |
| 611 | handle: &OrgName, |
| 612 | name: &RepoName, |
| 613 | stores: &Stores<'_, impl OrgRepository, impl MembershipRepository, impl RepoRepository>, |
| 614 | storage: &impl GitStorage, |
| 615 | ) -> Result<()> { |
| 616 | let repo = changeable_repo(actor, handle, name, stores).await?; |
| 617 | |
| 618 | stores.repos.delete(&repo.id).await?; |
| 619 | |
| 620 | if let Err(error) = storage.remove(handle, &repo.name).await { |
| 621 | eprintln!( |
| 622 | "steid: repository {handle}/{} deleted, but its directory could not be removed: {error}", |
| 623 | repo.name |
| 624 | ); |
| 625 | } |
| 626 | |
| 627 | Ok(()) |
| 628 | } |
| 629 | |
| 630 | fn taken() -> Error { |
| 631 | DomainError::AlreadyExists { |
| 632 | entity: "repository", |
| 633 | } |
| 634 | .into() |
| 635 | } |
| 636 | |
| 637 | #[cfg(test)] |
| 638 | #[path = "repo_tests.rs"] |
| 639 | mod tests; |