| 1 | //! The repos service: repository metadata, contents, forks, landing, and |
| 2 | //! git over HTTPS. |
| 3 | //! |
| 4 | //! Other services reach it over `POST /rpc/<method>`; see |
| 5 | //! `g1t_contracts::repos` for the methods and their arguments. Any other |
| 6 | //! request is treated as git's smart HTTP protocol. |
| 7 | |
| 8 | mod blame; |
| 9 | mod catch_up; |
| 10 | mod diff; |
| 11 | mod git_http; |
| 12 | mod import; |
| 13 | mod land; |
| 14 | mod listing; |
| 15 | mod refs; |
| 16 | mod registry; |
| 17 | mod run_access; |
| 18 | mod secret_scan; |
| 19 | mod store; |
| 20 | |
| 21 | use g1t_contracts::events::{ |
| 22 | Event, GitPush, NewEvent, Publish, RepoCreated, RepoForked, RepoUpdated, RepoVisibilityChanged, |
| 23 | WorkspaceRenamed, |
| 24 | }; |
| 25 | use g1t_contracts::repos::*; |
| 26 | use g1t_contracts::time::rfc3339; |
| 27 | use g1t_contracts::{FailureCode, Outcome, PrincipalKind, User, Viewer, is_valid_repo_name, new_id}; |
| 28 | use g1t_kit::{args, now_ms, reply, rpc_method}; |
| 29 | use std::collections::{HashMap, HashSet, VecDeque}; |
| 30 | |
| 31 | use serde::Serialize; |
| 32 | use worker::{Context, Env, Fetcher, MessageBatch, Method, Request, Response, Result, event}; |
| 33 | |
| 34 | use registry::{Registry, can_read, can_write, store_key}; |
| 35 | use store::{ArtifactsStore, GitRepo, GitStore, Scope}; |
| 36 | |
| 37 | /// Namespace that holds every pull request's fork: `pulls/<pull id>`. |
| 38 | const PULLS_NAMESPACE: &str = "pulls"; |
| 39 | const MAX_TEXT_BYTES: usize = 512 * 1024; |
| 40 | /// How far back a pull request may have forked and still be landed. |
| 41 | const MAX_ANCESTRY: u32 = 1000; |
| 42 | const MAX_DESCRIPTION_CHARS: usize = 200; |
| 43 | const SOURCE: &str = "repos"; |
| 44 | const UNVERIFIED: &str = "Confirm your email address first. Check your inbox, or resend the link from the banner on g1t.sh."; |
| 45 | |
| 46 | fn not_found<T>() -> Outcome<T> { |
| 47 | Outcome::fail(FailureCode::NotFound, "Repository not found.") |
| 48 | } |
| 49 | |
| 50 | /// Decoded text, or `None` when the file is too large or looks binary. |
| 51 | /// Whether a ref is a full commit hash rather than a branch name. |
| 52 | fn is_commit_hash(git_ref: &str) -> bool { |
| 53 | git_ref.len() == 40 && git_ref.bytes().all(|b| b.is_ascii_hexdigit()) |
| 54 | } |
| 55 | |
| 56 | fn text_of(bytes: Vec<u8>) -> Option<String> { |
| 57 | if bytes.len() > MAX_TEXT_BYTES || bytes.contains(&0) { |
| 58 | return None; |
| 59 | } |
| 60 | Some(String::from_utf8_lossy(&bytes).into_owned()) |
| 61 | } |
| 62 | |
| 63 | fn is_readme(name: &str) -> bool { |
| 64 | matches!( |
| 65 | name.to_lowercase().as_str(), |
| 66 | "readme" | "readme.md" | "readme.markdown" | "readme.txt" |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | /// Whether `ancestor` is reachable from the newest commit in `history`. |
| 71 | /// |
| 72 | /// `history` is the first-parent chain, which is all the store lists; a fork |
| 73 | /// that merged the target branch in has the target's head on a second |
| 74 | /// parent, so the walk follows every parent. |
| 75 | async fn descends_from<R: GitRepo>(repo: &R, history: &[Commit], ancestor: &str) -> Result<bool> { |
| 76 | let known: HashMap<&str, &[String]> = history |
| 77 | .iter() |
| 78 | .map(|commit| (commit.hash.as_str(), commit.parents.as_slice())) |
| 79 | .collect(); |
| 80 | let mut seen = HashSet::new(); |
| 81 | let mut queue: Vec<String> = history |
| 82 | .first() |
| 83 | .map(|c| c.hash.clone()) |
| 84 | .into_iter() |
| 85 | .collect(); |
| 86 | while let Some(hash) = queue.pop() { |
| 87 | if hash == ancestor { |
| 88 | return Ok(true); |
| 89 | } |
| 90 | if !seen.insert(hash.clone()) || seen.len() > MAX_ANCESTRY as usize { |
| 91 | continue; |
| 92 | } |
| 93 | match known.get(hash.as_str()) { |
| 94 | Some(parents) => queue.extend(parents.iter().cloned()), |
| 95 | None => queue.extend(repo.parents(&hash).await?.unwrap_or_default()), |
| 96 | } |
| 97 | } |
| 98 | Ok(false) |
| 99 | } |
| 100 | |
| 101 | /// The commit closest to the newest in `history` that is also in `shared`: |
| 102 | /// where a fork and the repository it came from last agreed. |
| 103 | async fn nearest_ancestor_in<R: GitRepo>( |
| 104 | repo: &R, |
| 105 | history: &[Commit], |
| 106 | shared: &HashSet<String>, |
| 107 | ) -> Result<Option<String>> { |
| 108 | let known: HashMap<&str, &[String]> = history |
| 109 | .iter() |
| 110 | .map(|commit| (commit.hash.as_str(), commit.parents.as_slice())) |
| 111 | .collect(); |
| 112 | let mut seen = HashSet::new(); |
| 113 | let mut queue: VecDeque<String> = history |
| 114 | .first() |
| 115 | .map(|c| c.hash.clone()) |
| 116 | .into_iter() |
| 117 | .collect(); |
| 118 | while let Some(hash) = queue.pop_front() { |
| 119 | if shared.contains(&hash) { |
| 120 | return Ok(Some(hash)); |
| 121 | } |
| 122 | if !seen.insert(hash.clone()) || seen.len() > MAX_ANCESTRY as usize { |
| 123 | continue; |
| 124 | } |
| 125 | match known.get(hash.as_str()) { |
| 126 | Some(parents) => queue.extend(parents.iter().cloned()), |
| 127 | None => queue.extend(repo.parents(&hash).await?.unwrap_or_default()), |
| 128 | } |
| 129 | } |
| 130 | Ok(None) |
| 131 | } |
| 132 | |
| 133 | struct Repos<S: GitStore> { |
| 134 | registry: Registry, |
| 135 | store: S, |
| 136 | events: Fetcher, |
| 137 | /// Asked during a push which secrets have been allowed. |
| 138 | security: Option<Fetcher>, |
| 139 | } |
| 140 | |
| 141 | impl<S: GitStore> Repos<S> { |
| 142 | async fn publish<T: Serialize>(&self, event: NewEvent<T>) -> Result<()> { |
| 143 | g1t_kit::call( |
| 144 | &self.events, |
| 145 | "publish", |
| 146 | &Publish { |
| 147 | events: vec![event], |
| 148 | }, |
| 149 | ) |
| 150 | .await |
| 151 | } |
| 152 | |
| 153 | /// Whether the viewer may read `repo`. A pull request's fork of a |
| 154 | /// private repository can be read by everyone who can read that |
| 155 | /// repository, so its members can review and check out the change, as |
| 156 | /// well as by whoever opened the pull request. |
| 157 | async fn may_read(&self, repo: &Repo, viewer: &Viewer) -> Result<bool> { |
| 158 | if can_read(repo, viewer) { |
| 159 | return Ok(true); |
| 160 | } |
| 161 | let Some(source_id) = &repo.fork_of else { |
| 162 | return Ok(false); |
| 163 | }; |
| 164 | Ok(self |
| 165 | .registry |
| 166 | .by_id(source_id) |
| 167 | .await? |
| 168 | .is_some_and(|source| can_read(&source, viewer))) |
| 169 | } |
| 170 | |
| 171 | /// `repo`, if there is one and the viewer may read it. |
| 172 | async fn visible(&self, repo: Option<Repo>, viewer: &Viewer) -> Result<Option<Repo>> { |
| 173 | Ok(match repo { |
| 174 | Some(repo) if self.may_read(&repo, viewer).await? => Some(repo), |
| 175 | _ => None, |
| 176 | }) |
| 177 | } |
| 178 | |
| 179 | /// Resolves a repo the viewer may read; private repos look missing. |
| 180 | async fn readable(&self, path: &RepoPath, viewer: &Viewer) -> Result<Option<Repo>> { |
| 181 | self.visible(self.registry.by_path(path).await?, viewer) |
| 182 | .await |
| 183 | } |
| 184 | |
| 185 | async fn get(&self, a: GetArgs) -> Result<Outcome<Repo>> { |
| 186 | Ok(self |
| 187 | .readable(&a.path, &a.viewer) |
| 188 | .await? |
| 189 | .map_or_else(not_found, Outcome::Ok)) |
| 190 | } |
| 191 | |
| 192 | async fn get_by_id(&self, a: GetByIdArgs) -> Result<Outcome<Repo>> { |
| 193 | Ok(self |
| 194 | .visible(self.registry.by_id(&a.id).await?, &a.viewer) |
| 195 | .await? |
| 196 | .map_or_else(not_found, Outcome::Ok)) |
| 197 | } |
| 198 | |
| 199 | async fn update(&self, a: UpdateArgs) -> Result<Outcome<Repo>> { |
| 200 | let viewer = Some(a.actor.clone()); |
| 201 | let Some(repo) = self.readable(&a.path, &viewer).await? else { |
| 202 | return Ok(not_found()); |
| 203 | }; |
| 204 | if repo.fork_of.is_some() || !can_write(&repo, &viewer) { |
| 205 | return Ok(Outcome::fail( |
| 206 | FailureCode::Forbidden, |
| 207 | "Only members of the repository's workspace can change its settings.", |
| 208 | )); |
| 209 | } |
| 210 | if !a.actor.verified { |
| 211 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 212 | } |
| 213 | let description = match a.description { |
| 214 | Some(text) => Some( |
| 215 | text.trim() |
| 216 | .chars() |
| 217 | .take(MAX_DESCRIPTION_CHARS) |
| 218 | .collect::<String>(), |
| 219 | ) |
| 220 | .filter(|text| !text.is_empty()), |
| 221 | None => repo.description.clone(), |
| 222 | }; |
| 223 | let is_private = a.is_private.unwrap_or(repo.is_private); |
| 224 | let protected = a.protected.unwrap_or(repo.protected); |
| 225 | let topics = match &a.topics { |
| 226 | Some(topics) => match clean_topics(topics) { |
| 227 | Ok(topics) => topics, |
| 228 | Err(reason) => return Ok(Outcome::fail(FailureCode::Invalid, reason)), |
| 229 | }, |
| 230 | None => repo.topics.clone(), |
| 231 | }; |
| 232 | self.registry |
| 233 | .update(&repo.id, description.as_deref(), is_private, protected, &topics) |
| 234 | .await?; |
| 235 | let visibility_changed = is_private != repo.is_private; |
| 236 | let updated = Repo { |
| 237 | description, |
| 238 | is_private, |
| 239 | protected, |
| 240 | topics, |
| 241 | ..repo |
| 242 | }; |
| 243 | // Search and anything else that shows the repository hears of it; |
| 244 | // a change of visibility is announced on its own as well, so that |
| 245 | // what was public stops being shown at once. |
| 246 | self.publish(NewEvent { |
| 247 | kind: "repo.updated", |
| 248 | source: SOURCE, |
| 249 | repo_id: Some(updated.id.clone()), |
| 250 | actor: Some(a.actor.id.clone()), |
| 251 | data: RepoUpdated { |
| 252 | repo_id: updated.id.clone(), |
| 253 | namespace: updated.namespace.clone(), |
| 254 | name: updated.name.clone(), |
| 255 | is_private, |
| 256 | visibility_changed, |
| 257 | }, |
| 258 | }) |
| 259 | .await?; |
| 260 | if visibility_changed { |
| 261 | self.publish(NewEvent { |
| 262 | kind: "repo.visibility_changed", |
| 263 | source: SOURCE, |
| 264 | repo_id: Some(updated.id.clone()), |
| 265 | actor: Some(a.actor.id), |
| 266 | data: RepoVisibilityChanged { |
| 267 | repo_id: updated.id.clone(), |
| 268 | is_private, |
| 269 | }, |
| 270 | }) |
| 271 | .await?; |
| 272 | } |
| 273 | Ok(Outcome::Ok(updated)) |
| 274 | } |
| 275 | |
| 276 | /// The repository with this id, if it is not a fork, and its store. |
| 277 | async fn stored(&self, repo_id: &str) -> Result<Option<S::Repo>> { |
| 278 | match self.registry.by_id(repo_id).await? { |
| 279 | Some(repo) if repo.fork_of.is_none() => Ok(Some(self.store.open(&store_key(&repo)).await?)), |
| 280 | _ => Ok(None), |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | async fn list_files(&self, a: ListFilesArgs) -> Result<FileList> { |
| 285 | let Some(repo) = self.registry.by_id(&a.repo_id).await?.filter(|repo| repo.fork_of.is_none()) else { |
| 286 | return Ok(FileList::default()); |
| 287 | }; |
| 288 | let git = self.store.open(&store_key(&repo)).await?; |
| 289 | let head = a.git_ref.unwrap_or_else(|| repo.default_branch.clone()); |
| 290 | listing::list(&git, None, &head, &a.skip_dirs, a.limit).await |
| 291 | } |
| 292 | |
| 293 | async fn changed_files(&self, a: ChangedFilesArgs) -> Result<FileList> { |
| 294 | let Some(git) = self.stored(&a.repo_id).await? else { |
| 295 | return Ok(FileList::default()); |
| 296 | }; |
| 297 | listing::list(&git, a.base.as_deref(), &a.head, &a.skip_dirs, a.limit).await |
| 298 | } |
| 299 | |
| 300 | async fn read_blobs(&self, a: ReadBlobsArgs) -> Result<Vec<BlobText>> { |
| 301 | let Some(git) = self.stored(&a.repo_id).await? else { |
| 302 | return Ok(Vec::new()); |
| 303 | }; |
| 304 | listing::read(&git, &a.hashes, a.max_bytes.min(MAX_TEXT_BYTES as u32)).await |
| 305 | } |
| 306 | |
| 307 | async fn create(&self, a: CreateArgs) -> Result<Outcome<Repo>> { |
| 308 | if !a.owner.verified { |
| 309 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 310 | } |
| 311 | let name = a.name.trim().to_lowercase(); |
| 312 | if !is_valid_repo_name(&name) { |
| 313 | return Ok(Outcome::fail( |
| 314 | FailureCode::Invalid, |
| 315 | "Use letters, digits, dots, hyphens and underscores only.", |
| 316 | )); |
| 317 | } |
| 318 | let namespace = a.namespace.trim().to_lowercase(); |
| 319 | if namespace.is_empty() { |
| 320 | return Ok(Outcome::fail( |
| 321 | FailureCode::Invalid, |
| 322 | "Say which workspace to create the repository in.", |
| 323 | )); |
| 324 | } |
| 325 | if !a.owner.is_member(&namespace) { |
| 326 | return Ok(Outcome::fail( |
| 327 | FailureCode::Forbidden, |
| 328 | "You are not a member of that workspace.", |
| 329 | )); |
| 330 | } |
| 331 | let path = RepoPath { namespace, name }; |
| 332 | if self.registry.by_path(&path).await?.is_some() { |
| 333 | return Ok(Outcome::fail( |
| 334 | FailureCode::Conflict, |
| 335 | "That workspace already has a repository with that name.", |
| 336 | )); |
| 337 | } |
| 338 | // An import is fetched before anything is created, so that an |
| 339 | // address that does not work leaves nothing behind. |
| 340 | let mut imported = None; |
| 341 | if let Some(url) = a |
| 342 | .import_url |
| 343 | .as_deref() |
| 344 | .map(str::trim) |
| 345 | .filter(|url| !url.is_empty()) |
| 346 | { |
| 347 | let Some(url) = import::clean_url(url) else { |
| 348 | return Ok(Outcome::fail( |
| 349 | FailureCode::Invalid, |
| 350 | "Give the https address of a public repository, such as https://github.com/owner/repo.", |
| 351 | )); |
| 352 | }; |
| 353 | let remote = match import::discover(&url).await? { |
| 354 | Ok(remote) => remote, |
| 355 | Err(reason) => return Ok(Outcome::fail(FailureCode::Invalid, reason)), |
| 356 | }; |
| 357 | let pack = match import::fetch(&url, &remote.head).await? { |
| 358 | Ok(pack) => pack, |
| 359 | Err(reason) => return Ok(Outcome::fail(FailureCode::Invalid, reason)), |
| 360 | }; |
| 361 | imported = Some((remote, pack)); |
| 362 | } |
| 363 | let now = now_ms(); |
| 364 | let repo = Repo { |
| 365 | id: new_id("rep", now), |
| 366 | namespace: path.namespace, |
| 367 | name: path.name, |
| 368 | description: a |
| 369 | .description |
| 370 | .map(|text| text.trim().to_owned()) |
| 371 | .filter(|text| !text.is_empty()), |
| 372 | is_private: a.is_private, |
| 373 | owner_id: a.owner.id.clone(), |
| 374 | default_branch: imported |
| 375 | .as_ref() |
| 376 | .map_or_else(|| "main".to_owned(), |(remote, _)| remote.branch.clone()), |
| 377 | fork_of: None, |
| 378 | protected: false, |
| 379 | created_at: rfc3339(now), |
| 380 | topics: Vec::new(), |
| 381 | }; |
| 382 | self.registry.claim_store_key(&repo).await?; |
| 383 | self.store |
| 384 | .create( |
| 385 | &store_key(&repo), |
| 386 | repo.description.as_deref(), |
| 387 | &repo.default_branch, |
| 388 | ) |
| 389 | .await?; |
| 390 | self.registry.insert(&repo).await?; |
| 391 | let mut pushed = None; |
| 392 | if let Some((remote, pack)) = imported { |
| 393 | let access = self |
| 394 | .store |
| 395 | .open(&store_key(&repo)) |
| 396 | .await? |
| 397 | .access(Scope::Write) |
| 398 | .await?; |
| 399 | let stored = |
| 400 | land::push_pack(&access, &repo.default_branch, None, &remote.head, pack).await?; |
| 401 | if let Err(reason) = stored { |
| 402 | self.registry.remove(&repo.id).await?; |
| 403 | return Ok(Outcome::fail( |
| 404 | FailureCode::Invalid, |
| 405 | format!("The repository could not be stored: {reason}"), |
| 406 | )); |
| 407 | } |
| 408 | pushed = Some(remote.head); |
| 409 | } |
| 410 | self.publish(NewEvent { |
| 411 | kind: "repo.created", |
| 412 | source: SOURCE, |
| 413 | repo_id: Some(repo.id.clone()), |
| 414 | actor: Some(a.owner.id), |
| 415 | data: RepoCreated { |
| 416 | repo_id: repo.id.clone(), |
| 417 | namespace: repo.namespace.clone(), |
| 418 | name: repo.name.clone(), |
| 419 | is_private: repo.is_private, |
| 420 | }, |
| 421 | }) |
| 422 | .await?; |
| 423 | if let Some(head) = pushed { |
| 424 | self.publish_push( |
| 425 | &repo, |
| 426 | &format!("refs/heads/{}", repo.default_branch), |
| 427 | None, |
| 428 | &head, |
| 429 | None, |
| 430 | ) |
| 431 | .await?; |
| 432 | } |
| 433 | Ok(Outcome::Ok(repo)) |
| 434 | } |
| 435 | |
| 436 | async fn tree(&self, a: TreeArgs) -> Result<Outcome<TreeView>> { |
| 437 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 438 | return Ok(not_found()); |
| 439 | }; |
| 440 | let git = self.store.open(&store_key(&repo)).await?; |
| 441 | let git_ref = a |
| 442 | .git_ref |
| 443 | .clone() |
| 444 | .unwrap_or_else(|| repo.default_branch.clone()); |
| 445 | |
| 446 | let Some(head) = git.log(&git_ref, 1).await?.into_iter().next() else { |
| 447 | // An unknown ref is an error; a repo with no commits is just empty. |
| 448 | if a.git_ref.is_some() { |
| 449 | return Ok(Outcome::fail( |
| 450 | FailureCode::NotFound, |
| 451 | "No such branch, tag or commit.", |
| 452 | )); |
| 453 | } |
| 454 | return Ok(Outcome::Ok(TreeView { |
| 455 | repo, |
| 456 | git_ref, |
| 457 | path: a.tree_path, |
| 458 | head: None, |
| 459 | entries: Vec::new(), |
| 460 | readme: None, |
| 461 | })); |
| 462 | }; |
| 463 | |
| 464 | let no_directory = || Outcome::fail(FailureCode::NotFound, "No such directory."); |
| 465 | let mut entries = git.read_tree(&head.tree_hash).await?; |
| 466 | for segment in a.tree_path.split('/').filter(|segment| !segment.is_empty()) { |
| 467 | let next = entries.as_ref().and_then(|entries| { |
| 468 | entries |
| 469 | .iter() |
| 470 | .find(|entry| entry.name == segment && entry.kind == EntryKind::Tree) |
| 471 | }); |
| 472 | let Some(next) = next else { |
| 473 | return Ok(no_directory()); |
| 474 | }; |
| 475 | entries = git.read_tree(&next.hash).await?; |
| 476 | } |
| 477 | let Some(mut entries) = entries else { |
| 478 | return Ok(no_directory()); |
| 479 | }; |
| 480 | // Directories first, then by name. |
| 481 | entries.sort_by(|a, b| { |
| 482 | (b.kind == EntryKind::Tree) |
| 483 | .cmp(&(a.kind == EntryKind::Tree)) |
| 484 | .then_with(|| a.name.cmp(&b.name)) |
| 485 | }); |
| 486 | |
| 487 | let readme_entry = entries |
| 488 | .iter() |
| 489 | .find(|entry| entry.kind == EntryKind::Blob && is_readme(&entry.name)); |
| 490 | let readme = match readme_entry { |
| 491 | Some(entry) => git.read_blob(&entry.hash).await?.map(|bytes| Readme { |
| 492 | name: entry.name.clone(), |
| 493 | text: text_of(bytes), |
| 494 | }), |
| 495 | None => None, |
| 496 | }; |
| 497 | Ok(Outcome::Ok(TreeView { |
| 498 | repo, |
| 499 | git_ref, |
| 500 | path: a.tree_path, |
| 501 | head: Some(head), |
| 502 | entries, |
| 503 | readme, |
| 504 | })) |
| 505 | } |
| 506 | |
| 507 | async fn blob(&self, a: BlobArgs) -> Result<Outcome<BlobView>> { |
| 508 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 509 | return Ok(not_found()); |
| 510 | }; |
| 511 | let bytes = if a.file_path.is_empty() { |
| 512 | None |
| 513 | } else { |
| 514 | let git = self.store.open(&store_key(&repo)).await?; |
| 515 | git.read_file(&a.git_ref, &a.file_path).await? |
| 516 | }; |
| 517 | let Some(bytes) = bytes else { |
| 518 | return Ok(Outcome::fail(FailureCode::NotFound, "No such file.")); |
| 519 | }; |
| 520 | Ok(Outcome::Ok(BlobView { |
| 521 | repo, |
| 522 | git_ref: a.git_ref, |
| 523 | path: a.file_path, |
| 524 | size: bytes.len() as u64, |
| 525 | text: text_of(bytes), |
| 526 | })) |
| 527 | } |
| 528 | |
| 529 | async fn blame(&self, a: BlameArgs) -> Result<Outcome<Blame>> { |
| 530 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 531 | return Ok(not_found()); |
| 532 | }; |
| 533 | let git = self.store.open(&store_key(&repo)).await?; |
| 534 | let git_ref = a.git_ref.unwrap_or_else(|| repo.default_branch.clone()); |
| 535 | Ok(match blame::blame(&git, &git_ref, &a.file_path).await? { |
| 536 | Some(blame) => Outcome::Ok(blame), |
| 537 | None => not_found(), |
| 538 | }) |
| 539 | } |
| 540 | |
| 541 | async fn log(&self, a: LogArgs) -> Result<Outcome<Vec<Commit>>> { |
| 542 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 543 | return Ok(not_found()); |
| 544 | }; |
| 545 | let git = self.store.open(&store_key(&repo)).await?; |
| 546 | let git_ref = a.git_ref.unwrap_or_else(|| repo.default_branch.clone()); |
| 547 | Ok(Outcome::Ok(git.log(&git_ref, a.limit).await?)) |
| 548 | } |
| 549 | |
| 550 | /// The repository's branches, default branch first. |
| 551 | async fn branches(&self, a: BranchesArgs) -> Result<Outcome<Vec<Branch>>> { |
| 552 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 553 | return Ok(not_found()); |
| 554 | }; |
| 555 | let mut branches = self.store.open(&store_key(&repo)).await?.branches().await?; |
| 556 | branches.sort_by_key(|branch| branch.name != repo.default_branch); |
| 557 | Ok(Outcome::Ok(branches)) |
| 558 | } |
| 559 | |
| 560 | /// Whether a pull request's source lacks commits that the branch it |
| 561 | /// would merge into has. |
| 562 | async fn behind(&self, a: BehindArgs) -> Result<bool> { |
| 563 | let Some(source) = self.registry.by_id(&a.source_id).await? else { |
| 564 | return Ok(false); |
| 565 | }; |
| 566 | let target = match &source.fork_of { |
| 567 | Some(id) => self.registry.by_id(id).await?, |
| 568 | None => Some(source.clone()), |
| 569 | }; |
| 570 | let Some(target) = target else { |
| 571 | return Ok(false); |
| 572 | }; |
| 573 | let branch = a.branch.unwrap_or_else(|| target.default_branch.clone()); |
| 574 | let target_head = self |
| 575 | .store |
| 576 | .open(&store_key(&target)) |
| 577 | .await? |
| 578 | .log(&target.default_branch, 1) |
| 579 | .await? |
| 580 | .into_iter() |
| 581 | .next() |
| 582 | .map(|commit| commit.hash); |
| 583 | let Some(target_head) = target_head else { |
| 584 | return Ok(false); |
| 585 | }; |
| 586 | let source_git = self.store.open(&store_key(&source)).await?; |
| 587 | let history = source_git.log(&branch, MAX_ANCESTRY).await?; |
| 588 | if history.is_empty() { |
| 589 | return Ok(false); |
| 590 | } |
| 591 | Ok(!descends_from(&source_git, &history, &target_head).await?) |
| 592 | } |
| 593 | |
| 594 | /// The files a pull request's source and the default branch it would |
| 595 | /// merge into each changed since they last agreed. Where the two lists |
| 596 | /// share no file, the merge cannot conflict; where they do, it may. |
| 597 | async fn divergence(&self, a: BehindArgs) -> Result<Option<Divergence>> { |
| 598 | let Some(source) = self.registry.by_id(&a.source_id).await? else { |
| 599 | return Ok(None); |
| 600 | }; |
| 601 | let target = match &source.fork_of { |
| 602 | Some(id) => self.registry.by_id(id).await?, |
| 603 | None => Some(source.clone()), |
| 604 | }; |
| 605 | let Some(target) = target else { |
| 606 | return Ok(None); |
| 607 | }; |
| 608 | let branch = a.branch.unwrap_or_else(|| target.default_branch.clone()); |
| 609 | let source_git = self.store.open(&store_key(&source)).await?; |
| 610 | let target_git = self.store.open(&store_key(&target)).await?; |
| 611 | let (history, target_history) = futures_util::future::try_join( |
| 612 | source_git.log(&branch, MAX_ANCESTRY), |
| 613 | target_git.log(&target.default_branch, MAX_ANCESTRY), |
| 614 | ) |
| 615 | .await?; |
| 616 | let (Some(head), Some(base)) = (history.first(), target_history.first()) else { |
| 617 | return Ok(None); |
| 618 | }; |
| 619 | let behind = !descends_from(&source_git, &history, &base.hash).await?; |
| 620 | let shared: HashSet<String> = target_history.iter().map(|commit| commit.hash.clone()).collect(); |
| 621 | let merge_base = nearest_ancestor_in(&source_git, &history, &shared).await?; |
| 622 | let mut divergence = Divergence { |
| 623 | head: head.hash.clone(), |
| 624 | base: base.hash.clone(), |
| 625 | merge_base: merge_base.clone(), |
| 626 | behind, |
| 627 | ..Divergence::default() |
| 628 | }; |
| 629 | let merge_base_tree = match &merge_base { |
| 630 | Some(hash) => target_history |
| 631 | .iter() |
| 632 | .find(|commit| commit.hash == *hash) |
| 633 | .map(|commit| commit.tree_hash.clone()), |
| 634 | None => None, |
| 635 | }; |
| 636 | let Some(merge_base_tree) = merge_base_tree else { |
| 637 | // No common history to compare from: say nothing is known. |
| 638 | divergence.truncated = true; |
| 639 | return Ok(Some(divergence)); |
| 640 | }; |
| 641 | let (ours, truncated_ours) = |
| 642 | diff::changed_paths(&source_git, Some(&merge_base_tree), &head.tree_hash).await?; |
| 643 | divergence.ours = ours; |
| 644 | divergence.truncated = truncated_ours; |
| 645 | if behind { |
| 646 | let (theirs, truncated_theirs) = |
| 647 | diff::changed_paths(&target_git, Some(&merge_base_tree), &base.tree_hash).await?; |
| 648 | divergence.theirs = theirs; |
| 649 | divergence.truncated |= truncated_theirs; |
| 650 | } |
| 651 | Ok(Some(divergence)) |
| 652 | } |
| 653 | |
| 654 | async fn head(&self, a: HeadArgs) -> Result<Option<String>> { |
| 655 | let Some(repo) = self.registry.by_id(&a.repo_id).await? else { |
| 656 | return Ok(None); |
| 657 | }; |
| 658 | let branch = if a.branch.is_empty() { &repo.default_branch } else { &a.branch }; |
| 659 | let git = self.store.open(&store_key(&repo)).await?; |
| 660 | Ok(git |
| 661 | .log(branch, 1) |
| 662 | .await? |
| 663 | .into_iter() |
| 664 | .next() |
| 665 | .map(|commit| commit.hash)) |
| 666 | } |
| 667 | |
| 668 | async fn delete_branch(&self, a: DeleteBranchArgs) -> Result<Outcome<bool>> { |
| 669 | if !a.branch.starts_with(G1T_BRANCH_PREFIX) { |
| 670 | return Ok(Outcome::fail( |
| 671 | FailureCode::Forbidden, |
| 672 | "Only branches g1t made for itself can be deleted this way.", |
| 673 | )); |
| 674 | } |
| 675 | let Some(repo) = self.registry.by_id(&a.repo_id).await? else { |
| 676 | return Ok(not_found()); |
| 677 | }; |
| 678 | let git = self.store.open(&store_key(&repo)).await?; |
| 679 | let Some(old) = git |
| 680 | .branches() |
| 681 | .await? |
| 682 | .into_iter() |
| 683 | .find(|branch| branch.name == a.branch) |
| 684 | .map(|branch| branch.hash) |
| 685 | else { |
| 686 | return Ok(Outcome::Ok(false)); |
| 687 | }; |
| 688 | let access = git.access(Scope::Write).await?; |
| 689 | if let Err(reason) = land::delete_ref(&access, &a.branch, &old).await? { |
| 690 | return Ok(Outcome::fail( |
| 691 | FailureCode::Conflict, |
| 692 | format!("{} could not be deleted: {reason}", a.branch), |
| 693 | )); |
| 694 | } |
| 695 | Ok(Outcome::Ok(true)) |
| 696 | } |
| 697 | |
| 698 | async fn fork_for_pull(&self, a: ForkArgs) -> Result<Outcome<Repo>> { |
| 699 | let viewer = Some(a.actor.clone()); |
| 700 | let Some(source) = self |
| 701 | .registry |
| 702 | .by_id(&a.source_id) |
| 703 | .await? |
| 704 | .filter(|repo| can_read(repo, &viewer)) |
| 705 | else { |
| 706 | return Ok(not_found()); |
| 707 | }; |
| 708 | let now = now_ms(); |
| 709 | let fork = Repo { |
| 710 | id: new_id("rep", now), |
| 711 | namespace: PULLS_NAMESPACE.to_owned(), |
| 712 | name: a.pull_id.clone(), |
| 713 | description: None, |
| 714 | // A fork is exactly as visible as the repo it came from. |
| 715 | is_private: source.is_private, |
| 716 | owner_id: a.actor.id.clone(), |
| 717 | default_branch: source.default_branch.clone(), |
| 718 | fork_of: Some(source.id.clone()), |
| 719 | protected: false, |
| 720 | created_at: rfc3339(now), |
| 721 | topics: Vec::new(), |
| 722 | }; |
| 723 | self.registry.claim_store_key(&fork).await?; |
| 724 | self.store |
| 725 | .open(&store_key(&source)) |
| 726 | .await? |
| 727 | .fork(&store_key(&fork)) |
| 728 | .await?; |
| 729 | self.registry.insert(&fork).await?; |
| 730 | self.publish(NewEvent { |
| 731 | kind: "repo.forked", |
| 732 | source: SOURCE, |
| 733 | repo_id: Some(source.id.clone()), |
| 734 | actor: Some(a.actor.id), |
| 735 | data: RepoForked { |
| 736 | repo_id: fork.id.clone(), |
| 737 | source_repo_id: source.id, |
| 738 | pull_id: a.pull_id, |
| 739 | }, |
| 740 | }) |
| 741 | .await?; |
| 742 | Ok(Outcome::Ok(fork)) |
| 743 | } |
| 744 | |
| 745 | async fn git_access(&self, a: GitAccessArgs) -> Result<Outcome<GitAccess>> { |
| 746 | let write = a.service == GitService::ReceivePack; |
| 747 | // Anonymous callers are asked to authenticate whether or not the repo |
| 748 | // exists, so private repos cannot be told apart from missing ones. |
| 749 | let denied = || match &a.viewer { |
| 750 | Some(_) => not_found(), |
| 751 | None => Outcome::fail(FailureCode::Unauthenticated, "Authentication required."), |
| 752 | }; |
| 753 | // An agent's token works through the API only: its sandbox has its |
| 754 | // own way to push, to its own pull request. |
| 755 | if a.viewer.as_ref().is_some_and(|user| user.kind == PrincipalKind::Agent) { |
| 756 | return Ok(Outcome::fail( |
| 757 | FailureCode::Forbidden, |
| 758 | "A g1t agent's token cannot be used with git.", |
| 759 | )); |
| 760 | } |
| 761 | if let (true, Some(user)) = (write, &a.viewer) |
| 762 | && !user.verified |
| 763 | { |
| 764 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 765 | } |
| 766 | |
| 767 | let repo = match self.registry.by_path(&a.path).await? { |
| 768 | Some(repo) => { |
| 769 | let allowed = if write { |
| 770 | can_write(&repo, &a.viewer) |
| 771 | } else { |
| 772 | self.may_read(&repo, &a.viewer).await? |
| 773 | }; |
| 774 | if !allowed { |
| 775 | return Ok(denied()); |
| 776 | } |
| 777 | repo |
| 778 | } |
| 779 | None => { |
| 780 | // Push to create, in a workspace the pusher belongs to. |
| 781 | let owner = a |
| 782 | .viewer |
| 783 | .as_ref() |
| 784 | .filter(|user| write && user.is_member(&a.path.namespace.to_lowercase())); |
| 785 | let Some(owner) = owner else { |
| 786 | return Ok(denied()); |
| 787 | }; |
| 788 | let created = self |
| 789 | .create(CreateArgs { |
| 790 | owner: owner.clone(), |
| 791 | namespace: a.path.namespace.clone(), |
| 792 | name: a.path.name.clone(), |
| 793 | description: None, |
| 794 | is_private: false, |
| 795 | import_url: None, |
| 796 | }) |
| 797 | .await?; |
| 798 | match created { |
| 799 | Outcome::Ok(repo) => repo, |
| 800 | Outcome::Fail(failure) => return Ok(Outcome::Fail(failure)), |
| 801 | } |
| 802 | } |
| 803 | }; |
| 804 | let git = self.store.open(&store_key(&repo)).await?; |
| 805 | let scope = if write { Scope::Write } else { Scope::Read }; |
| 806 | Ok(Outcome::Ok(git.access(scope).await?)) |
| 807 | } |
| 808 | |
| 809 | async fn land(&self, a: LandArgs) -> Result<Outcome<Landed>> { |
| 810 | let actor: Viewer = Some(a.actor.clone()); |
| 811 | let Some(source) = self.registry.by_id(&a.source_id).await? else { |
| 812 | return Ok(not_found()); |
| 813 | }; |
| 814 | // A fork lands on the repository it came from; a branch on its own. |
| 815 | let target = match &source.fork_of { |
| 816 | Some(id) => self.registry.by_id(id).await?, |
| 817 | None => Some(source.clone()), |
| 818 | }; |
| 819 | let Some(target) = target.filter(|repo| can_read(repo, &actor)) else { |
| 820 | return Ok(not_found()); |
| 821 | }; |
| 822 | if !can_write(&target, &actor) { |
| 823 | return Ok(Outcome::fail( |
| 824 | FailureCode::Forbidden, |
| 825 | "Only members of the repository's workspace can merge a pull request.", |
| 826 | )); |
| 827 | } |
| 828 | if !a.actor.verified { |
| 829 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 830 | } |
| 831 | |
| 832 | let branch = &target.default_branch; |
| 833 | let from_fork = source.id != target.id; |
| 834 | let source_branch = match a.branch { |
| 835 | Some(name) if !from_fork && name == *branch => { |
| 836 | return Ok(Outcome::fail( |
| 837 | FailureCode::Invalid, |
| 838 | format!("{branch} cannot be merged into itself."), |
| 839 | )); |
| 840 | } |
| 841 | Some(name) => name, |
| 842 | None if from_fork => branch.clone(), |
| 843 | None => { |
| 844 | return Ok(Outcome::fail( |
| 845 | FailureCode::Invalid, |
| 846 | "Say which branch to merge.", |
| 847 | )); |
| 848 | } |
| 849 | }; |
| 850 | |
| 851 | let source_git = self.store.open(&store_key(&source)).await?; |
| 852 | let target_git = self.store.open(&store_key(&target)).await?; |
| 853 | let history = source_git.log(&source_branch, MAX_ANCESTRY).await?; |
| 854 | let Some(new) = history.first().map(|commit| commit.hash.clone()) else { |
| 855 | return Ok(Outcome::fail( |
| 856 | FailureCode::Conflict, |
| 857 | "This pull request has no commits to merge.", |
| 858 | )); |
| 859 | }; |
| 860 | let old = target_git |
| 861 | .log(branch, 1) |
| 862 | .await? |
| 863 | .into_iter() |
| 864 | .next() |
| 865 | .map(|commit| commit.hash); |
| 866 | |
| 867 | if old.as_deref() == Some(new.as_str()) { |
| 868 | return Ok(Outcome::Ok(Landed { |
| 869 | commit: new, |
| 870 | previous: None, |
| 871 | })); |
| 872 | } |
| 873 | // Moving the branch to a commit that does not descend from its |
| 874 | // current head would discard whatever landed in between. |
| 875 | if let Some(old) = &old |
| 876 | && !descends_from(&source_git, &history, old).await? |
| 877 | { |
| 878 | let remedy = if from_fork { |
| 879 | format!("Pull {branch} into the pull request's fork, push, and merge again.") |
| 880 | } else { |
| 881 | format!("Merge {branch} into {source_branch}, push, and merge again.") |
| 882 | }; |
| 883 | return Ok(Outcome::fail( |
| 884 | FailureCode::Conflict, |
| 885 | format!("{branch} has moved since this pull request was opened. {remedy}"), |
| 886 | )); |
| 887 | } |
| 888 | |
| 889 | // For a branch the objects are already in the target; sending them |
| 890 | // again is harmless and keeps one way of moving a ref. |
| 891 | let source_access = source_git.access(Scope::Read).await?; |
| 892 | let target_access = target_git.access(Scope::Write).await?; |
| 893 | let pushed = |
| 894 | land::fast_forward(&source_access, &target_access, branch, old.as_deref(), &new) |
| 895 | .await?; |
| 896 | if let Err(reason) = pushed { |
| 897 | // Most often another pull request landed between the check and the push. |
| 898 | return Ok(Outcome::fail( |
| 899 | FailureCode::Conflict, |
| 900 | format!("{branch} could not be updated: {reason}"), |
| 901 | )); |
| 902 | } |
| 903 | self.publish_push( |
| 904 | &target, |
| 905 | &format!("refs/heads/{branch}"), |
| 906 | old.as_deref(), |
| 907 | &new, |
| 908 | Some(a.actor.id), |
| 909 | ) |
| 910 | .await?; |
| 911 | Ok(Outcome::Ok(Landed { |
| 912 | commit: new, |
| 913 | previous: old, |
| 914 | })) |
| 915 | } |
| 916 | |
| 917 | async fn compare(&self, a: CompareArgs) -> Result<Outcome<Comparison>> { |
| 918 | let Some(repo) = self |
| 919 | .visible(self.registry.by_id(&a.repo_id).await?, &a.viewer) |
| 920 | .await? |
| 921 | else { |
| 922 | return Ok(not_found()); |
| 923 | }; |
| 924 | let git = self.store.open(&store_key(&repo)).await?; |
| 925 | let head_ref = a.head.as_deref().unwrap_or(&repo.default_branch); |
| 926 | // The head's history is only searched when the base is worked out |
| 927 | // from another branch. |
| 928 | let depth = if a.base.is_some() || is_commit_hash(head_ref) { 1 } else { MAX_ANCESTRY }; |
| 929 | let history = git.log(head_ref, depth).await?; |
| 930 | let Some(head) = history.first() else { |
| 931 | return Ok(Outcome::fail( |
| 932 | FailureCode::Conflict, |
| 933 | "There are no commits to compare.", |
| 934 | )); |
| 935 | }; |
| 936 | |
| 937 | // Where the head's history meets the default branch of `against`. |
| 938 | let shared_with = async |against: &Repo| -> Result<Option<String>> { |
| 939 | let against_git = self.store.open(&store_key(against)).await?; |
| 940 | let shared: HashSet<String> = against_git |
| 941 | .log(&against.default_branch, MAX_ANCESTRY) |
| 942 | .await? |
| 943 | .into_iter() |
| 944 | .map(|commit| commit.hash) |
| 945 | .collect(); |
| 946 | nearest_ancestor_in(&git, &history, &shared).await |
| 947 | }; |
| 948 | let base = match (a.base, &repo.fork_of) { |
| 949 | (Some(base), _) => Some(base), |
| 950 | // A fork is compared with the last commit it shares with the |
| 951 | // repository it came from. |
| 952 | (None, Some(target_id)) => match self.registry.by_id(target_id).await? { |
| 953 | Some(target) => shared_with(&target).await?, |
| 954 | None => None, |
| 955 | }, |
| 956 | // A branch, with the point where it left the default branch. |
| 957 | // A single commit, with its first parent. |
| 958 | (None, None) if is_commit_hash(head_ref) => head.parents.first().cloned(), |
| 959 | (None, None) if head_ref != repo.default_branch => shared_with(&repo).await?, |
| 960 | (None, None) => head.parents.first().cloned(), |
| 961 | }; |
| 962 | let base_tree = match &base { |
| 963 | Some(base) => git |
| 964 | .log(base, 1) |
| 965 | .await? |
| 966 | .into_iter() |
| 967 | .next() |
| 968 | .map(|commit| commit.tree_hash), |
| 969 | None => None, |
| 970 | }; |
| 971 | let (files, truncated) = |
| 972 | diff::compare_trees(&git, base_tree.as_deref(), &head.tree_hash).await?; |
| 973 | Ok(Outcome::Ok(Comparison { |
| 974 | base, |
| 975 | head: head.hash.clone(), |
| 976 | files, |
| 977 | truncated, |
| 978 | })) |
| 979 | } |
| 980 | |
| 981 | /// Reports that `git_ref` of `repo` (a full ref) now points to `after`. |
| 982 | async fn publish_push( |
| 983 | &self, |
| 984 | repo: &Repo, |
| 985 | git_ref: &str, |
| 986 | before: Option<&str>, |
| 987 | after: &str, |
| 988 | actor: Option<String>, |
| 989 | ) -> Result<()> { |
| 990 | self.publish(NewEvent { |
| 991 | kind: "git.push", |
| 992 | source: SOURCE, |
| 993 | repo_id: Some(repo.id.clone()), |
| 994 | actor, |
| 995 | data: GitPush { |
| 996 | repo_id: repo.id.clone(), |
| 997 | git_ref: git_ref.to_owned(), |
| 998 | before: before.map(str::to_owned), |
| 999 | after: after.to_owned(), |
| 1000 | default_branch: git_ref.strip_prefix("refs/heads/") |
| 1001 | == Some(repo.default_branch.as_str()), |
| 1002 | }, |
| 1003 | }) |
| 1004 | .await |
| 1005 | } |
| 1006 | |
| 1007 | /// Git over HTTPS. |
| 1008 | async fn git_http(&self, request: Request, env: &Env) -> Result<Response> { |
| 1009 | let Some(git) = git_http::parse(&request.url()?) else { |
| 1010 | return Response::error("Not found", 404); |
| 1011 | }; |
| 1012 | // A workspace that was renamed: git follows a redirect when it |
| 1013 | // first asks for refs, and uses the new address from then on. |
| 1014 | if self.registry.by_path(&git.path).await?.is_none() |
| 1015 | && let Some(location) = git_http::renamed(&request.url()?, &env.service("IDENTITY")?).await? |
| 1016 | { |
| 1017 | return git_http::moved(&location, request.method() == Method::Get); |
| 1018 | } |
| 1019 | let viewer = git_http::viewer(&request, &env.service("IDENTITY")?).await?; |
| 1020 | // A run credential is checked against its grants, then acts as the |
| 1021 | // person it works for. See run_access.rs. |
| 1022 | let (request, viewer, audit) = match self.admit_git(request, &git, viewer).await? { |
| 1023 | run_access::Admitted::Go { request, viewer, entry } => (request, viewer, entry), |
| 1024 | run_access::Admitted::Refused(response) => return Ok(response), |
| 1025 | }; |
| 1026 | let access = self |
| 1027 | .git_access(GitAccessArgs { |
| 1028 | path: git.path.clone(), |
| 1029 | viewer: viewer.clone(), |
| 1030 | service: git.service, |
| 1031 | }) |
| 1032 | .await?; |
| 1033 | let access = match access { |
| 1034 | Outcome::Ok(access) => access, |
| 1035 | refused => { |
| 1036 | let response = git_http::refuse(refused)?; |
| 1037 | self.finish_git(audit, response.status_code(), None).await; |
| 1038 | return Ok(response); |
| 1039 | } |
| 1040 | }; |
| 1041 | // A protected default branch takes changes only from a merged pull |
| 1042 | // request, which lands without going through here. |
| 1043 | let protected = match self.registry.by_path(&git.path).await? { |
| 1044 | Some(repo) if repo.protected && repo.fork_of.is_none() => Some(repo.default_branch), |
| 1045 | _ => None, |
| 1046 | }; |
| 1047 | // Push protection: a push that adds a secret is refused. See secret_scan.rs. |
| 1048 | let scan = async |body: &[u8]| self.protect(&git.path, viewer.as_ref(), body).await; |
| 1049 | let forwarded = |
| 1050 | match git_http::forward(request, &git, &access, protected.as_deref(), scan).await? { |
| 1051 | git_http::Push::Forwarded(forwarded) => forwarded, |
| 1052 | git_http::Push::Refused(response) => { |
| 1053 | self.finish_git(audit, 403, Some("The push would change a protected branch.".to_owned())).await; |
| 1054 | return Ok(response); |
| 1055 | } |
| 1056 | git_http::Push::Blocked(response) => { |
| 1057 | self.finish_git(audit, 403, Some("The push adds a secret.".to_owned())).await; |
| 1058 | return Ok(response); |
| 1059 | } |
| 1060 | }; |
| 1061 | self.finish_git(audit, forwarded.response.status_code(), None).await; |
| 1062 | |
| 1063 | // What the push stored, for billing's storage meter. A failure only |
| 1064 | // leaves the count short. |
| 1065 | if forwarded.response.status_code() == 200 |
| 1066 | && forwarded.pack_bytes > 0 |
| 1067 | && let Some(repo) = self.registry.by_path(&git.path).await? |
| 1068 | && let Err(error) = self.registry.add_stored_bytes(&repo, forwarded.pack_bytes).await |
| 1069 | { |
| 1070 | worker::console_error!("stored bytes for {} not counted: {error}", git.path.name); |
| 1071 | } |
| 1072 | |
| 1073 | // Artifacts' own push notifications are per repository, which does |
| 1074 | // not fit a repo per pull request, so the front end reports pushes |
| 1075 | // itself: one event for each branch that moved. |
| 1076 | let accepted = forwarded.response.status_code() == 200 && !forwarded.pushed.is_empty(); |
| 1077 | if accepted && let Some(repo) = self.registry.by_path(&git.path).await? { |
| 1078 | let stored = self.store.open(&store_key(&repo)).await?; |
| 1079 | let actor = viewer.map(|user: User| user.id); |
| 1080 | for pushed in &forwarded.pushed { |
| 1081 | // The store can refuse one ref and accept another, so each |
| 1082 | // branch is checked against where it actually is. A tag the |
| 1083 | // store cannot read back is taken as pushed. |
| 1084 | let moved = match pushed.branch() { |
| 1085 | Some(branch) => stored |
| 1086 | .log(branch, 1) |
| 1087 | .await? |
| 1088 | .first() |
| 1089 | .is_some_and(|commit| commit.hash == pushed.after), |
| 1090 | None => stored.log(&pushed.git_ref, 1).await.map_or(true, |head| { |
| 1091 | head.first().is_none_or(|commit| commit.hash == pushed.after) |
| 1092 | }), |
| 1093 | }; |
| 1094 | if moved { |
| 1095 | self.publish_push( |
| 1096 | &repo, |
| 1097 | &pushed.git_ref, |
| 1098 | pushed.before.as_deref(), |
| 1099 | &pushed.after, |
| 1100 | actor.clone(), |
| 1101 | ) |
| 1102 | .await?; |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | Ok(forwarded.response) |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | #[event(fetch)] |
| 1111 | async fn fetch(mut request: Request, env: Env, _ctx: Context) -> Result<Response> { |
| 1112 | let repos = Repos { |
| 1113 | registry: Registry { db: env.d1("DB")? }, |
| 1114 | store: ArtifactsStore::new(&env)?, |
| 1115 | events: env.service("EVENTS")?, |
| 1116 | security: env.service("SECURITY").ok(), |
| 1117 | }; |
| 1118 | let Some(method) = rpc_method(&request) else { |
| 1119 | return repos.git_http(request, &env).await; |
| 1120 | }; |
| 1121 | let body: serde_json::Value = request.json().await?; |
| 1122 | |
| 1123 | match method.as_str() { |
| 1124 | "get" => reply(&repos.get(args(body)?).await?), |
| 1125 | "get_by_id" => reply(&repos.get_by_id(args(body)?).await?), |
| 1126 | "readable" => { |
| 1127 | let a: ReadableArgs = args(body)?; |
| 1128 | reply(&repos.registry.readable(&a.ids, &a.viewer).await?) |
| 1129 | } |
| 1130 | "public_namespaces" => { |
| 1131 | let a: PublicNamespacesArgs = args(body)?; |
| 1132 | reply(&repos.registry.public_namespaces(&a.owner_id).await?) |
| 1133 | } |
| 1134 | "path_by_id" => { |
| 1135 | let a: PathByIdArgs = args(body)?; |
| 1136 | reply( |
| 1137 | &repos |
| 1138 | .registry |
| 1139 | .by_id(&a.id) |
| 1140 | .await? |
| 1141 | .filter(|repo| repo.fork_of.is_none()) |
| 1142 | .map(|repo| RepoPath { |
| 1143 | namespace: repo.namespace, |
| 1144 | name: repo.name, |
| 1145 | }), |
| 1146 | ) |
| 1147 | } |
| 1148 | "list" => { |
| 1149 | let a: ListArgs = args(body)?; |
| 1150 | reply( |
| 1151 | &repos |
| 1152 | .registry |
| 1153 | .list( |
| 1154 | &a.viewer, |
| 1155 | a.query.as_deref(), |
| 1156 | a.namespace.as_deref(), |
| 1157 | a.member_only, |
| 1158 | ) |
| 1159 | .await?, |
| 1160 | ) |
| 1161 | } |
| 1162 | "create" => reply(&repos.create(args(body)?).await?), |
| 1163 | "update" => reply(&repos.update(args(body)?).await?), |
| 1164 | "tree" => reply(&repos.tree(args(body)?).await?), |
| 1165 | "blob" => reply(&repos.blob(args(body)?).await?), |
| 1166 | "log" => reply(&repos.log(args(body)?).await?), |
| 1167 | "blame" => reply(&repos.blame(args(body)?).await?), |
| 1168 | "fork_for_pull" => reply(&repos.fork_for_pull(args(body)?).await?), |
| 1169 | "git_access" => reply(&repos.git_access(args(body)?).await?), |
| 1170 | "branches" => reply(&repos.branches(args(body)?).await?), |
| 1171 | "head" => reply(&repos.head(args(body)?).await?), |
| 1172 | "behind" => reply(&repos.behind(args(body)?).await?), |
| 1173 | "divergence" => reply(&repos.divergence(args(body)?).await?), |
| 1174 | "land" => reply(&repos.land(args(body)?).await?), |
| 1175 | "update_pull_branch" => reply(&repos.update_pull_branch(args(body)?).await?), |
| 1176 | "delete_branch" => reply(&repos.delete_branch(args(body)?).await?), |
| 1177 | "compare" => reply(&repos.compare(args(body)?).await?), |
| 1178 | "scan_history" => reply(&repos.scan_history(args(body)?).await?), |
| 1179 | "find_lockfiles" => reply(&repos.find_lockfiles(args(body)?).await?), |
| 1180 | "list_files" => reply(&repos.list_files(args(body)?).await?), |
| 1181 | "changed_files" => reply(&repos.changed_files(args(body)?).await?), |
| 1182 | "read_blobs" => reply(&repos.read_blobs(args(body)?).await?), |
| 1183 | "visibility" => { |
| 1184 | let a: g1t_contracts::repos::VisibilityArgs = args(body)?; |
| 1185 | reply(&repos.registry.visibility(&a.paths).await?) |
| 1186 | } |
| 1187 | "storage" => reply(&repos.registry.storage().await?), |
| 1188 | "all_ids" => { |
| 1189 | let a: AllIdsArgs = args(body)?; |
| 1190 | let limit = a.limit.clamp(1, 500); |
| 1191 | let ids = repos.registry.ids_after(a.after.as_deref(), limit).await?; |
| 1192 | let next = (ids.len() == limit as usize).then(|| ids.last().cloned()).flatten(); |
| 1193 | reply(&IdPage { ids, next }) |
| 1194 | } |
| 1195 | _ => Response::error("Unknown method", 404), |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /// Events from the bus. Only a workspace's rename concerns this service: |
| 1200 | /// its repositories move to the workspace's current slug, asked of identity |
| 1201 | /// by id, so a repeated or late delivery lands in the same place. Their git |
| 1202 | /// store keys stay as they were. |
| 1203 | #[event(queue)] |
| 1204 | async fn queue(batch: MessageBatch<Event>, env: Env, _ctx: Context) -> Result<()> { |
| 1205 | let registry = Registry { db: env.d1("DB")? }; |
| 1206 | let identity = env.service("IDENTITY")?; |
| 1207 | for message in batch.messages()? { |
| 1208 | let event = message.body(); |
| 1209 | if event.kind != "workspace.renamed" { |
| 1210 | continue; |
| 1211 | } |
| 1212 | let Ok(renamed) = serde_json::from_value::<WorkspaceRenamed>(event.data.clone()) else { |
| 1213 | worker::console_error!("workspace.renamed {} could not be read", event.id); |
| 1214 | continue; |
| 1215 | }; |
| 1216 | let names: HashMap<String, String> = g1t_kit::call( |
| 1217 | &identity, |
| 1218 | "usernames", |
| 1219 | &g1t_contracts::identity::UsernamesArgs { |
| 1220 | ids: vec![renamed.workspace_id.clone()], |
| 1221 | }, |
| 1222 | ) |
| 1223 | .await?; |
| 1224 | let current = names |
| 1225 | .get(&renamed.workspace_id) |
| 1226 | .cloned() |
| 1227 | .unwrap_or_else(|| renamed.to.clone()); |
| 1228 | let left = registry |
| 1229 | .rename_namespace(&renamed.stale_slugs(¤t), ¤t) |
| 1230 | .await?; |
| 1231 | if left > 0 { |
| 1232 | worker::console_error!( |
| 1233 | "{left} repositories stayed under {} or {}: {current} already has repositories of the same names", |
| 1234 | renamed.from, |
| 1235 | renamed.to |
| 1236 | ); |
| 1237 | } |
| 1238 | } |
| 1239 | Ok(()) |
| 1240 | } |