pr_01m47d15m3e54sn21z27rpy5n9/services/repos/src/lib.rs
| 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 diff; |
| 10 | mod git_http; |
| 11 | mod import; |
| 12 | mod land; |
| 13 | mod refs; |
| 14 | mod registry; |
| 15 | mod store; |
| 16 | |
| 17 | use g1t_contracts::events::{GitPush, NewEvent, Publish, RepoCreated, RepoForked}; |
| 18 | use g1t_contracts::repos::*; |
| 19 | use g1t_contracts::time::rfc3339; |
| 20 | use g1t_contracts::{FailureCode, Outcome, PrincipalKind, User, Viewer, is_valid_repo_name, new_id}; |
| 21 | use g1t_kit::{args, now_ms, reply, rpc_method}; |
| 22 | use std::collections::{HashMap, HashSet, VecDeque}; |
| 23 | |
| 24 | use serde::Serialize; |
| 25 | use worker::{Context, Env, Fetcher, Request, Response, Result, event}; |
| 26 | |
| 27 | use registry::{Registry, can_read, can_write, store_key}; |
| 28 | use store::{ArtifactsStore, GitRepo, GitStore, Scope}; |
| 29 | |
| 30 | /// Namespace that holds every pull request's fork: `pulls/<pull id>`. |
| 31 | const PULLS_NAMESPACE: &str = "pulls"; |
| 32 | const MAX_TEXT_BYTES: usize = 512 * 1024; |
| 33 | /// How far back a pull request may have forked and still be landed. |
| 34 | const MAX_ANCESTRY: u32 = 1000; |
| 35 | const MAX_DESCRIPTION_CHARS: usize = 200; |
| 36 | const SOURCE: &str = "repos"; |
| 37 | const UNVERIFIED: &str = "Confirm your email address first. Check your inbox, or resend the link from the banner on g1t.sh."; |
| 38 | |
| 39 | fn not_found<T>() -> Outcome<T> { |
| 40 | Outcome::fail(FailureCode::NotFound, "Repository not found.") |
| 41 | } |
| 42 | |
| 43 | /// Decoded text, or `None` when the file is too large or looks binary. |
| 44 | /// Whether a ref is a full commit hash rather than a branch name. |
| 45 | fn is_commit_hash(git_ref: &str) -> bool { |
| 46 | git_ref.len() == 40 && git_ref.bytes().all(|b| b.is_ascii_hexdigit()) |
| 47 | } |
| 48 | |
| 49 | fn text_of(bytes: Vec<u8>) -> Option<String> { |
| 50 | if bytes.len() > MAX_TEXT_BYTES || bytes.contains(&0) { |
| 51 | return None; |
| 52 | } |
| 53 | Some(String::from_utf8_lossy(&bytes).into_owned()) |
| 54 | } |
| 55 | |
| 56 | fn is_readme(name: &str) -> bool { |
| 57 | matches!( |
| 58 | name.to_lowercase().as_str(), |
| 59 | "readme" | "readme.md" | "readme.markdown" | "readme.txt" |
| 60 | ) |
| 61 | } |
| 62 | |
| 63 | /// Whether `ancestor` is reachable from the newest commit in `history`. |
| 64 | /// |
| 65 | /// `history` is the first-parent chain, which is all the store lists; a fork |
| 66 | /// that merged the target branch in has the target's head on a second |
| 67 | /// parent, so the walk follows every parent. |
| 68 | async fn descends_from<R: GitRepo>(repo: &R, history: &[Commit], ancestor: &str) -> Result<bool> { |
| 69 | let known: HashMap<&str, &[String]> = history |
| 70 | .iter() |
| 71 | .map(|commit| (commit.hash.as_str(), commit.parents.as_slice())) |
| 72 | .collect(); |
| 73 | let mut seen = HashSet::new(); |
| 74 | let mut queue: Vec<String> = history |
| 75 | .first() |
| 76 | .map(|c| c.hash.clone()) |
| 77 | .into_iter() |
| 78 | .collect(); |
| 79 | while let Some(hash) = queue.pop() { |
| 80 | if hash == ancestor { |
| 81 | return Ok(true); |
| 82 | } |
| 83 | if !seen.insert(hash.clone()) || seen.len() > MAX_ANCESTRY as usize { |
| 84 | continue; |
| 85 | } |
| 86 | match known.get(hash.as_str()) { |
| 87 | Some(parents) => queue.extend(parents.iter().cloned()), |
| 88 | None => queue.extend(repo.parents(&hash).await?.unwrap_or_default()), |
| 89 | } |
| 90 | } |
| 91 | Ok(false) |
| 92 | } |
| 93 | |
| 94 | /// The commit closest to the newest in `history` that is also in `shared`: |
| 95 | /// where a fork and the repository it came from last agreed. |
| 96 | async fn nearest_ancestor_in<R: GitRepo>( |
| 97 | repo: &R, |
| 98 | history: &[Commit], |
| 99 | shared: &HashSet<String>, |
| 100 | ) -> Result<Option<String>> { |
| 101 | let known: HashMap<&str, &[String]> = history |
| 102 | .iter() |
| 103 | .map(|commit| (commit.hash.as_str(), commit.parents.as_slice())) |
| 104 | .collect(); |
| 105 | let mut seen = HashSet::new(); |
| 106 | let mut queue: VecDeque<String> = history |
| 107 | .first() |
| 108 | .map(|c| c.hash.clone()) |
| 109 | .into_iter() |
| 110 | .collect(); |
| 111 | while let Some(hash) = queue.pop_front() { |
| 112 | if shared.contains(&hash) { |
| 113 | return Ok(Some(hash)); |
| 114 | } |
| 115 | if !seen.insert(hash.clone()) || seen.len() > MAX_ANCESTRY as usize { |
| 116 | continue; |
| 117 | } |
| 118 | match known.get(hash.as_str()) { |
| 119 | Some(parents) => queue.extend(parents.iter().cloned()), |
| 120 | None => queue.extend(repo.parents(&hash).await?.unwrap_or_default()), |
| 121 | } |
| 122 | } |
| 123 | Ok(None) |
| 124 | } |
| 125 | |
| 126 | struct Repos<S: GitStore> { |
| 127 | registry: Registry, |
| 128 | store: S, |
| 129 | events: Fetcher, |
| 130 | } |
| 131 | |
| 132 | impl<S: GitStore> Repos<S> { |
| 133 | async fn publish<T: Serialize>(&self, event: NewEvent<T>) -> Result<()> { |
| 134 | g1t_kit::call( |
| 135 | &self.events, |
| 136 | "publish", |
| 137 | &Publish { |
| 138 | events: vec![event], |
| 139 | }, |
| 140 | ) |
| 141 | .await |
| 142 | } |
| 143 | |
| 144 | /// Whether the viewer may read `repo`. A pull request's fork of a |
| 145 | /// private repository can be read by everyone who can read that |
| 146 | /// repository, so its members can review and check out the change, as |
| 147 | /// well as by whoever opened the pull request. |
| 148 | async fn may_read(&self, repo: &Repo, viewer: &Viewer) -> Result<bool> { |
| 149 | if can_read(repo, viewer) { |
| 150 | return Ok(true); |
| 151 | } |
| 152 | let Some(source_id) = &repo.fork_of else { |
| 153 | return Ok(false); |
| 154 | }; |
| 155 | Ok(self |
| 156 | .registry |
| 157 | .by_id(source_id) |
| 158 | .await? |
| 159 | .is_some_and(|source| can_read(&source, viewer))) |
| 160 | } |
| 161 | |
| 162 | /// `repo`, if there is one and the viewer may read it. |
| 163 | async fn visible(&self, repo: Option<Repo>, viewer: &Viewer) -> Result<Option<Repo>> { |
| 164 | Ok(match repo { |
| 165 | Some(repo) if self.may_read(&repo, viewer).await? => Some(repo), |
| 166 | _ => None, |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | /// Resolves a repo the viewer may read; private repos look missing. |
| 171 | async fn readable(&self, path: &RepoPath, viewer: &Viewer) -> Result<Option<Repo>> { |
| 172 | self.visible(self.registry.by_path(path).await?, viewer) |
| 173 | .await |
| 174 | } |
| 175 | |
| 176 | async fn get(&self, a: GetArgs) -> Result<Outcome<Repo>> { |
| 177 | Ok(self |
| 178 | .readable(&a.path, &a.viewer) |
| 179 | .await? |
| 180 | .map_or_else(not_found, Outcome::Ok)) |
| 181 | } |
| 182 | |
| 183 | async fn get_by_id(&self, a: GetByIdArgs) -> Result<Outcome<Repo>> { |
| 184 | Ok(self |
| 185 | .visible(self.registry.by_id(&a.id).await?, &a.viewer) |
| 186 | .await? |
| 187 | .map_or_else(not_found, Outcome::Ok)) |
| 188 | } |
| 189 | |
| 190 | async fn update(&self, a: UpdateArgs) -> Result<Outcome<Repo>> { |
| 191 | let viewer = Some(a.actor.clone()); |
| 192 | let Some(repo) = self.readable(&a.path, &viewer).await? else { |
| 193 | return Ok(not_found()); |
| 194 | }; |
| 195 | if repo.fork_of.is_some() || !can_write(&repo, &viewer) { |
| 196 | return Ok(Outcome::fail( |
| 197 | FailureCode::Forbidden, |
| 198 | "Only members of the repository's workspace can change its settings.", |
| 199 | )); |
| 200 | } |
| 201 | if !a.actor.verified { |
| 202 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 203 | } |
| 204 | let description = match a.description { |
| 205 | Some(text) => Some( |
| 206 | text.trim() |
| 207 | .chars() |
| 208 | .take(MAX_DESCRIPTION_CHARS) |
| 209 | .collect::<String>(), |
| 210 | ) |
| 211 | .filter(|text| !text.is_empty()), |
| 212 | None => repo.description.clone(), |
| 213 | }; |
| 214 | let is_private = a.is_private.unwrap_or(repo.is_private); |
| 215 | let protected = a.protected.unwrap_or(repo.protected); |
| 216 | self.registry |
| 217 | .update(&repo.id, description.as_deref(), is_private, protected) |
| 218 | .await?; |
| 219 | Ok(Outcome::Ok(Repo { |
| 220 | description, |
| 221 | is_private, |
| 222 | protected, |
| 223 | ..repo |
| 224 | })) |
| 225 | } |
| 226 | |
| 227 | async fn create(&self, a: CreateArgs) -> Result<Outcome<Repo>> { |
| 228 | if !a.owner.verified { |
| 229 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 230 | } |
| 231 | let name = a.name.trim().to_lowercase(); |
| 232 | if !is_valid_repo_name(&name) { |
| 233 | return Ok(Outcome::fail( |
| 234 | FailureCode::Invalid, |
| 235 | "Use letters, digits, dots, hyphens and underscores only.", |
| 236 | )); |
| 237 | } |
| 238 | let namespace = a.namespace.trim().to_lowercase(); |
| 239 | if namespace.is_empty() { |
| 240 | return Ok(Outcome::fail( |
| 241 | FailureCode::Invalid, |
| 242 | "Say which workspace to create the repository in.", |
| 243 | )); |
| 244 | } |
| 245 | if !a.owner.is_member(&namespace) { |
| 246 | return Ok(Outcome::fail( |
| 247 | FailureCode::Forbidden, |
| 248 | "You are not a member of that workspace.", |
| 249 | )); |
| 250 | } |
| 251 | let path = RepoPath { namespace, name }; |
| 252 | if self.registry.by_path(&path).await?.is_some() { |
| 253 | return Ok(Outcome::fail( |
| 254 | FailureCode::Conflict, |
| 255 | "That workspace already has a repository with that name.", |
| 256 | )); |
| 257 | } |
| 258 | // An import is fetched before anything is created, so that an |
| 259 | // address that does not work leaves nothing behind. |
| 260 | let mut imported = None; |
| 261 | if let Some(url) = a |
| 262 | .import_url |
| 263 | .as_deref() |
| 264 | .map(str::trim) |
| 265 | .filter(|url| !url.is_empty()) |
| 266 | { |
| 267 | let Some(url) = import::clean_url(url) else { |
| 268 | return Ok(Outcome::fail( |
| 269 | FailureCode::Invalid, |
| 270 | "Give the https address of a public repository, such as https://github.com/owner/repo.", |
| 271 | )); |
| 272 | }; |
| 273 | let remote = match import::discover(&url).await? { |
| 274 | Ok(remote) => remote, |
| 275 | Err(reason) => return Ok(Outcome::fail(FailureCode::Invalid, reason)), |
| 276 | }; |
| 277 | let pack = match import::fetch(&url, &remote.head).await? { |
| 278 | Ok(pack) => pack, |
| 279 | Err(reason) => return Ok(Outcome::fail(FailureCode::Invalid, reason)), |
| 280 | }; |
| 281 | imported = Some((remote, pack)); |
| 282 | } |
| 283 | let now = now_ms(); |
| 284 | let repo = Repo { |
| 285 | id: new_id("rep", now), |
| 286 | namespace: path.namespace, |
| 287 | name: path.name, |
| 288 | description: a |
| 289 | .description |
| 290 | .map(|text| text.trim().to_owned()) |
| 291 | .filter(|text| !text.is_empty()), |
| 292 | is_private: a.is_private, |
| 293 | owner_id: a.owner.id.clone(), |
| 294 | default_branch: imported |
| 295 | .as_ref() |
| 296 | .map_or_else(|| "main".to_owned(), |(remote, _)| remote.branch.clone()), |
| 297 | fork_of: None, |
| 298 | protected: false, |
| 299 | created_at: rfc3339(now), |
| 300 | }; |
| 301 | self.store |
| 302 | .create( |
| 303 | &store_key(&repo), |
| 304 | repo.description.as_deref(), |
| 305 | &repo.default_branch, |
| 306 | ) |
| 307 | .await?; |
| 308 | self.registry.insert(&repo).await?; |
| 309 | let mut pushed = None; |
| 310 | if let Some((remote, pack)) = imported { |
| 311 | let access = self |
| 312 | .store |
| 313 | .open(&store_key(&repo)) |
| 314 | .await? |
| 315 | .access(Scope::Write) |
| 316 | .await?; |
| 317 | let stored = |
| 318 | land::push_pack(&access, &repo.default_branch, None, &remote.head, pack).await?; |
| 319 | if let Err(reason) = stored { |
| 320 | self.registry.remove(&repo.id).await?; |
| 321 | return Ok(Outcome::fail( |
| 322 | FailureCode::Invalid, |
| 323 | format!("The repository could not be stored: {reason}"), |
| 324 | )); |
| 325 | } |
| 326 | pushed = Some(remote.head); |
| 327 | } |
| 328 | self.publish(NewEvent { |
| 329 | kind: "repo.created", |
| 330 | source: SOURCE, |
| 331 | repo_id: Some(repo.id.clone()), |
| 332 | actor: Some(a.owner.id), |
| 333 | data: RepoCreated { |
| 334 | repo_id: repo.id.clone(), |
| 335 | namespace: repo.namespace.clone(), |
| 336 | name: repo.name.clone(), |
| 337 | is_private: repo.is_private, |
| 338 | }, |
| 339 | }) |
| 340 | .await?; |
| 341 | if let Some(head) = pushed { |
| 342 | self.publish_push(&repo, &repo.default_branch, &head, None) |
| 343 | .await?; |
| 344 | } |
| 345 | Ok(Outcome::Ok(repo)) |
| 346 | } |
| 347 | |
| 348 | async fn tree(&self, a: TreeArgs) -> Result<Outcome<TreeView>> { |
| 349 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 350 | return Ok(not_found()); |
| 351 | }; |
| 352 | let git = self.store.open(&store_key(&repo)).await?; |
| 353 | let git_ref = a |
| 354 | .git_ref |
| 355 | .clone() |
| 356 | .unwrap_or_else(|| repo.default_branch.clone()); |
| 357 | |
| 358 | let Some(head) = git.log(&git_ref, 1).await?.into_iter().next() else { |
| 359 | // An unknown ref is an error; a repo with no commits is just empty. |
| 360 | if a.git_ref.is_some() { |
| 361 | return Ok(Outcome::fail( |
| 362 | FailureCode::NotFound, |
| 363 | "No such branch, tag or commit.", |
| 364 | )); |
| 365 | } |
| 366 | return Ok(Outcome::Ok(TreeView { |
| 367 | repo, |
| 368 | git_ref, |
| 369 | path: a.tree_path, |
| 370 | head: None, |
| 371 | entries: Vec::new(), |
| 372 | readme: None, |
| 373 | })); |
| 374 | }; |
| 375 | |
| 376 | let no_directory = || Outcome::fail(FailureCode::NotFound, "No such directory."); |
| 377 | let mut entries = git.read_tree(&head.tree_hash).await?; |
| 378 | for segment in a.tree_path.split('/').filter(|segment| !segment.is_empty()) { |
| 379 | let next = entries.as_ref().and_then(|entries| { |
| 380 | entries |
| 381 | .iter() |
| 382 | .find(|entry| entry.name == segment && entry.kind == EntryKind::Tree) |
| 383 | }); |
| 384 | let Some(next) = next else { |
| 385 | return Ok(no_directory()); |
| 386 | }; |
| 387 | entries = git.read_tree(&next.hash).await?; |
| 388 | } |
| 389 | let Some(mut entries) = entries else { |
| 390 | return Ok(no_directory()); |
| 391 | }; |
| 392 | // Directories first, then by name. |
| 393 | entries.sort_by(|a, b| { |
| 394 | (b.kind == EntryKind::Tree) |
| 395 | .cmp(&(a.kind == EntryKind::Tree)) |
| 396 | .then_with(|| a.name.cmp(&b.name)) |
| 397 | }); |
| 398 | |
| 399 | let readme_entry = entries |
| 400 | .iter() |
| 401 | .find(|entry| entry.kind == EntryKind::Blob && is_readme(&entry.name)); |
| 402 | let readme = match readme_entry { |
| 403 | Some(entry) => git.read_blob(&entry.hash).await?.map(|bytes| Readme { |
| 404 | name: entry.name.clone(), |
| 405 | text: text_of(bytes), |
| 406 | }), |
| 407 | None => None, |
| 408 | }; |
| 409 | Ok(Outcome::Ok(TreeView { |
| 410 | repo, |
| 411 | git_ref, |
| 412 | path: a.tree_path, |
| 413 | head: Some(head), |
| 414 | entries, |
| 415 | readme, |
| 416 | })) |
| 417 | } |
| 418 | |
| 419 | async fn blob(&self, a: BlobArgs) -> Result<Outcome<BlobView>> { |
| 420 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 421 | return Ok(not_found()); |
| 422 | }; |
| 423 | let bytes = if a.file_path.is_empty() { |
| 424 | None |
| 425 | } else { |
| 426 | let git = self.store.open(&store_key(&repo)).await?; |
| 427 | git.read_file(&a.git_ref, &a.file_path).await? |
| 428 | }; |
| 429 | let Some(bytes) = bytes else { |
| 430 | return Ok(Outcome::fail(FailureCode::NotFound, "No such file.")); |
| 431 | }; |
| 432 | Ok(Outcome::Ok(BlobView { |
| 433 | repo, |
| 434 | git_ref: a.git_ref, |
| 435 | path: a.file_path, |
| 436 | size: bytes.len() as u64, |
| 437 | text: text_of(bytes), |
| 438 | })) |
| 439 | } |
| 440 | |
| 441 | async fn blame(&self, a: BlameArgs) -> Result<Outcome<Blame>> { |
| 442 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 443 | return Ok(not_found()); |
| 444 | }; |
| 445 | let git = self.store.open(&store_key(&repo)).await?; |
| 446 | let git_ref = a.git_ref.unwrap_or_else(|| repo.default_branch.clone()); |
| 447 | Ok(match blame::blame(&git, &git_ref, &a.file_path).await? { |
| 448 | Some(blame) => Outcome::Ok(blame), |
| 449 | None => not_found(), |
| 450 | }) |
| 451 | } |
| 452 | |
| 453 | async fn log(&self, a: LogArgs) -> Result<Outcome<Vec<Commit>>> { |
| 454 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 455 | return Ok(not_found()); |
| 456 | }; |
| 457 | let git = self.store.open(&store_key(&repo)).await?; |
| 458 | let git_ref = a.git_ref.unwrap_or_else(|| repo.default_branch.clone()); |
| 459 | Ok(Outcome::Ok(git.log(&git_ref, a.limit).await?)) |
| 460 | } |
| 461 | |
| 462 | /// The repository's branches, default branch first. |
| 463 | async fn branches(&self, a: BranchesArgs) -> Result<Outcome<Vec<Branch>>> { |
| 464 | let Some(repo) = self.readable(&a.path, &a.viewer).await? else { |
| 465 | return Ok(not_found()); |
| 466 | }; |
| 467 | let mut branches = self.store.open(&store_key(&repo)).await?.branches().await?; |
| 468 | branches.sort_by_key(|branch| branch.name != repo.default_branch); |
| 469 | Ok(Outcome::Ok(branches)) |
| 470 | } |
| 471 | |
| 472 | /// Whether a pull request's source lacks commits that the branch it |
| 473 | /// would merge into has. |
| 474 | async fn behind(&self, a: BehindArgs) -> Result<bool> { |
| 475 | let Some(source) = self.registry.by_id(&a.source_id).await? else { |
| 476 | return Ok(false); |
| 477 | }; |
| 478 | let target = match &source.fork_of { |
| 479 | Some(id) => self.registry.by_id(id).await?, |
| 480 | None => Some(source.clone()), |
| 481 | }; |
| 482 | let Some(target) = target else { |
| 483 | return Ok(false); |
| 484 | }; |
| 485 | let branch = a.branch.unwrap_or_else(|| target.default_branch.clone()); |
| 486 | let target_head = self |
| 487 | .store |
| 488 | .open(&store_key(&target)) |
| 489 | .await? |
| 490 | .log(&target.default_branch, 1) |
| 491 | .await? |
| 492 | .into_iter() |
| 493 | .next() |
| 494 | .map(|commit| commit.hash); |
| 495 | let Some(target_head) = target_head else { |
| 496 | return Ok(false); |
| 497 | }; |
| 498 | let source_git = self.store.open(&store_key(&source)).await?; |
| 499 | let history = source_git.log(&branch, MAX_ANCESTRY).await?; |
| 500 | if history.is_empty() { |
| 501 | return Ok(false); |
| 502 | } |
| 503 | Ok(!descends_from(&source_git, &history, &target_head).await?) |
| 504 | } |
| 505 | |
| 506 | async fn head(&self, a: HeadArgs) -> Result<Option<String>> { |
| 507 | let Some(repo) = self.registry.by_id(&a.repo_id).await? else { |
| 508 | return Ok(None); |
| 509 | }; |
| 510 | let git = self.store.open(&store_key(&repo)).await?; |
| 511 | Ok(git |
| 512 | .log(&a.branch, 1) |
| 513 | .await? |
| 514 | .into_iter() |
| 515 | .next() |
| 516 | .map(|commit| commit.hash)) |
| 517 | } |
| 518 | |
| 519 | async fn fork_for_pull(&self, a: ForkArgs) -> Result<Outcome<Repo>> { |
| 520 | let viewer = Some(a.actor.clone()); |
| 521 | let Some(source) = self |
| 522 | .registry |
| 523 | .by_id(&a.source_id) |
| 524 | .await? |
| 525 | .filter(|repo| can_read(repo, &viewer)) |
| 526 | else { |
| 527 | return Ok(not_found()); |
| 528 | }; |
| 529 | let now = now_ms(); |
| 530 | let fork = Repo { |
| 531 | id: new_id("rep", now), |
| 532 | namespace: PULLS_NAMESPACE.to_owned(), |
| 533 | name: a.pull_id.clone(), |
| 534 | description: None, |
| 535 | // A fork is exactly as visible as the repo it came from. |
| 536 | is_private: source.is_private, |
| 537 | owner_id: a.actor.id.clone(), |
| 538 | default_branch: source.default_branch.clone(), |
| 539 | fork_of: Some(source.id.clone()), |
| 540 | protected: false, |
| 541 | created_at: rfc3339(now), |
| 542 | }; |
| 543 | self.store |
| 544 | .open(&store_key(&source)) |
| 545 | .await? |
| 546 | .fork(&store_key(&fork)) |
| 547 | .await?; |
| 548 | self.registry.insert(&fork).await?; |
| 549 | self.publish(NewEvent { |
| 550 | kind: "repo.forked", |
| 551 | source: SOURCE, |
| 552 | repo_id: Some(source.id.clone()), |
| 553 | actor: Some(a.actor.id), |
| 554 | data: RepoForked { |
| 555 | repo_id: fork.id.clone(), |
| 556 | source_repo_id: source.id, |
| 557 | pull_id: a.pull_id, |
| 558 | }, |
| 559 | }) |
| 560 | .await?; |
| 561 | Ok(Outcome::Ok(fork)) |
| 562 | } |
| 563 | |
| 564 | async fn git_access(&self, a: GitAccessArgs) -> Result<Outcome<GitAccess>> { |
| 565 | let write = a.service == GitService::ReceivePack; |
| 566 | // Anonymous callers are asked to authenticate whether or not the repo |
| 567 | // exists, so private repos cannot be told apart from missing ones. |
| 568 | let denied = || match &a.viewer { |
| 569 | Some(_) => not_found(), |
| 570 | None => Outcome::fail(FailureCode::Unauthenticated, "Authentication required."), |
| 571 | }; |
| 572 | // An agent's token works through the API only: its sandbox has its |
| 573 | // own way to push, to its own pull request. |
| 574 | if a.viewer.as_ref().is_some_and(|user| user.kind == PrincipalKind::Agent) { |
| 575 | return Ok(Outcome::fail( |
| 576 | FailureCode::Forbidden, |
| 577 | "A g1t agent's token cannot be used with git.", |
| 578 | )); |
| 579 | } |
| 580 | if let (true, Some(user)) = (write, &a.viewer) |
| 581 | && !user.verified |
| 582 | { |
| 583 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 584 | } |
| 585 | |
| 586 | let repo = match self.registry.by_path(&a.path).await? { |
| 587 | Some(repo) => { |
| 588 | let allowed = if write { |
| 589 | can_write(&repo, &a.viewer) |
| 590 | } else { |
| 591 | self.may_read(&repo, &a.viewer).await? |
| 592 | }; |
| 593 | if !allowed { |
| 594 | return Ok(denied()); |
| 595 | } |
| 596 | repo |
| 597 | } |
| 598 | None => { |
| 599 | // Push to create, in a workspace the pusher belongs to. |
| 600 | let owner = a |
| 601 | .viewer |
| 602 | .as_ref() |
| 603 | .filter(|user| write && user.is_member(&a.path.namespace.to_lowercase())); |
| 604 | let Some(owner) = owner else { |
| 605 | return Ok(denied()); |
| 606 | }; |
| 607 | let created = self |
| 608 | .create(CreateArgs { |
| 609 | owner: owner.clone(), |
| 610 | namespace: a.path.namespace.clone(), |
| 611 | name: a.path.name.clone(), |
| 612 | description: None, |
| 613 | is_private: false, |
| 614 | import_url: None, |
| 615 | }) |
| 616 | .await?; |
| 617 | match created { |
| 618 | Outcome::Ok(repo) => repo, |
| 619 | Outcome::Fail(failure) => return Ok(Outcome::Fail(failure)), |
| 620 | } |
| 621 | } |
| 622 | }; |
| 623 | let git = self.store.open(&store_key(&repo)).await?; |
| 624 | let scope = if write { Scope::Write } else { Scope::Read }; |
| 625 | Ok(Outcome::Ok(git.access(scope).await?)) |
| 626 | } |
| 627 | |
| 628 | async fn land(&self, a: LandArgs) -> Result<Outcome<Landed>> { |
| 629 | let actor: Viewer = Some(a.actor.clone()); |
| 630 | let Some(source) = self.registry.by_id(&a.source_id).await? else { |
| 631 | return Ok(not_found()); |
| 632 | }; |
| 633 | // A fork lands on the repository it came from; a branch on its own. |
| 634 | let target = match &source.fork_of { |
| 635 | Some(id) => self.registry.by_id(id).await?, |
| 636 | None => Some(source.clone()), |
| 637 | }; |
| 638 | let Some(target) = target.filter(|repo| can_read(repo, &actor)) else { |
| 639 | return Ok(not_found()); |
| 640 | }; |
| 641 | if !can_write(&target, &actor) { |
| 642 | return Ok(Outcome::fail( |
| 643 | FailureCode::Forbidden, |
| 644 | "Only members of the repository's workspace can merge a pull request.", |
| 645 | )); |
| 646 | } |
| 647 | if !a.actor.verified { |
| 648 | return Ok(Outcome::fail(FailureCode::Forbidden, UNVERIFIED)); |
| 649 | } |
| 650 | |
| 651 | let branch = &target.default_branch; |
| 652 | let from_fork = source.id != target.id; |
| 653 | let source_branch = match a.branch { |
| 654 | Some(name) if !from_fork && name == *branch => { |
| 655 | return Ok(Outcome::fail( |
| 656 | FailureCode::Invalid, |
| 657 | format!("{branch} cannot be merged into itself."), |
| 658 | )); |
| 659 | } |
| 660 | Some(name) => name, |
| 661 | None if from_fork => branch.clone(), |
| 662 | None => { |
| 663 | return Ok(Outcome::fail( |
| 664 | FailureCode::Invalid, |
| 665 | "Say which branch to merge.", |
| 666 | )); |
| 667 | } |
| 668 | }; |
| 669 | |
| 670 | let source_git = self.store.open(&store_key(&source)).await?; |
| 671 | let target_git = self.store.open(&store_key(&target)).await?; |
| 672 | let history = source_git.log(&source_branch, MAX_ANCESTRY).await?; |
| 673 | let Some(new) = history.first().map(|commit| commit.hash.clone()) else { |
| 674 | return Ok(Outcome::fail( |
| 675 | FailureCode::Conflict, |
| 676 | "This pull request has no commits to merge.", |
| 677 | )); |
| 678 | }; |
| 679 | let old = target_git |
| 680 | .log(branch, 1) |
| 681 | .await? |
| 682 | .into_iter() |
| 683 | .next() |
| 684 | .map(|commit| commit.hash); |
| 685 | |
| 686 | if old.as_deref() == Some(new.as_str()) { |
| 687 | return Ok(Outcome::Ok(Landed { |
| 688 | commit: new, |
| 689 | previous: None, |
| 690 | })); |
| 691 | } |
| 692 | // Moving the branch to a commit that does not descend from its |
| 693 | // current head would discard whatever landed in between. |
| 694 | if let Some(old) = &old |
| 695 | && !descends_from(&source_git, &history, old).await? |
| 696 | { |
| 697 | let remedy = if from_fork { |
| 698 | format!("Pull {branch} into the pull request's fork, push, and merge again.") |
| 699 | } else { |
| 700 | format!("Merge {branch} into {source_branch}, push, and merge again.") |
| 701 | }; |
| 702 | return Ok(Outcome::fail( |
| 703 | FailureCode::Conflict, |
| 704 | format!("{branch} has moved since this pull request was opened. {remedy}"), |
| 705 | )); |
| 706 | } |
| 707 | |
| 708 | // For a branch the objects are already in the target; sending them |
| 709 | // again is harmless and keeps one way of moving a ref. |
| 710 | let source_access = source_git.access(Scope::Read).await?; |
| 711 | let target_access = target_git.access(Scope::Write).await?; |
| 712 | let pushed = |
| 713 | land::fast_forward(&source_access, &target_access, branch, old.as_deref(), &new) |
| 714 | .await?; |
| 715 | if let Err(reason) = pushed { |
| 716 | // Most often another pull request landed between the check and the push. |
| 717 | return Ok(Outcome::fail( |
| 718 | FailureCode::Conflict, |
| 719 | format!("{branch} could not be updated: {reason}"), |
| 720 | )); |
| 721 | } |
| 722 | self.publish_push(&target, branch, &new, Some(a.actor.id)) |
| 723 | .await?; |
| 724 | Ok(Outcome::Ok(Landed { |
| 725 | commit: new, |
| 726 | previous: old, |
| 727 | })) |
| 728 | } |
| 729 | |
| 730 | async fn compare(&self, a: CompareArgs) -> Result<Outcome<Comparison>> { |
| 731 | let Some(repo) = self |
| 732 | .visible(self.registry.by_id(&a.repo_id).await?, &a.viewer) |
| 733 | .await? |
| 734 | else { |
| 735 | return Ok(not_found()); |
| 736 | }; |
| 737 | let git = self.store.open(&store_key(&repo)).await?; |
| 738 | let head_ref = a.head.as_deref().unwrap_or(&repo.default_branch); |
| 739 | // The head's history is only searched when the base is worked out |
| 740 | // from another branch. |
| 741 | let depth = if a.base.is_some() || is_commit_hash(head_ref) { 1 } else { MAX_ANCESTRY }; |
| 742 | let history = git.log(head_ref, depth).await?; |
| 743 | let Some(head) = history.first() else { |
| 744 | return Ok(Outcome::fail( |
| 745 | FailureCode::Conflict, |
| 746 | "There are no commits to compare.", |
| 747 | )); |
| 748 | }; |
| 749 | |
| 750 | // Where the head's history meets the default branch of `against`. |
| 751 | let shared_with = async |against: &Repo| -> Result<Option<String>> { |
| 752 | let against_git = self.store.open(&store_key(against)).await?; |
| 753 | let shared: HashSet<String> = against_git |
| 754 | .log(&against.default_branch, MAX_ANCESTRY) |
| 755 | .await? |
| 756 | .into_iter() |
| 757 | .map(|commit| commit.hash) |
| 758 | .collect(); |
| 759 | nearest_ancestor_in(&git, &history, &shared).await |
| 760 | }; |
| 761 | let base = match (a.base, &repo.fork_of) { |
| 762 | (Some(base), _) => Some(base), |
| 763 | // A fork is compared with the last commit it shares with the |
| 764 | // repository it came from. |
| 765 | (None, Some(target_id)) => match self.registry.by_id(target_id).await? { |
| 766 | Some(target) => shared_with(&target).await?, |
| 767 | None => None, |
| 768 | }, |
| 769 | // A branch, with the point where it left the default branch. |
| 770 | // A single commit, with its first parent. |
| 771 | (None, None) if is_commit_hash(head_ref) => head.parents.first().cloned(), |
| 772 | (None, None) if head_ref != repo.default_branch => shared_with(&repo).await?, |
| 773 | (None, None) => head.parents.first().cloned(), |
| 774 | }; |
| 775 | let base_tree = match &base { |
| 776 | Some(base) => git |
| 777 | .log(base, 1) |
| 778 | .await? |
| 779 | .into_iter() |
| 780 | .next() |
| 781 | .map(|commit| commit.tree_hash), |
| 782 | None => None, |
| 783 | }; |
| 784 | let (files, truncated) = |
| 785 | diff::compare_trees(&git, base_tree.as_deref(), &head.tree_hash).await?; |
| 786 | Ok(Outcome::Ok(Comparison { |
| 787 | base, |
| 788 | head: head.hash.clone(), |
| 789 | files, |
| 790 | truncated, |
| 791 | })) |
| 792 | } |
| 793 | |
| 794 | /// Reports that `branch` of `repo` now points to `after`. |
| 795 | async fn publish_push( |
| 796 | &self, |
| 797 | repo: &Repo, |
| 798 | branch: &str, |
| 799 | after: &str, |
| 800 | actor: Option<String>, |
| 801 | ) -> Result<()> { |
| 802 | self.publish(NewEvent { |
| 803 | kind: "git.push", |
| 804 | source: SOURCE, |
| 805 | repo_id: Some(repo.id.clone()), |
| 806 | actor, |
| 807 | data: GitPush { |
| 808 | repo_id: repo.id.clone(), |
| 809 | git_ref: format!("refs/heads/{branch}"), |
| 810 | after: after.to_owned(), |
| 811 | default_branch: branch == repo.default_branch, |
| 812 | }, |
| 813 | }) |
| 814 | .await |
| 815 | } |
| 816 | |
| 817 | /// Git over HTTPS. |
| 818 | async fn git_http(&self, request: Request, env: &Env) -> Result<Response> { |
| 819 | let Some(git) = git_http::parse(&request.url()?) else { |
| 820 | return Response::error("Not found", 404); |
| 821 | }; |
| 822 | let viewer = git_http::viewer(&request, &env.service("IDENTITY")?).await?; |
| 823 | let access = self |
| 824 | .git_access(GitAccessArgs { |
| 825 | path: git.path.clone(), |
| 826 | viewer: viewer.clone(), |
| 827 | service: git.service, |
| 828 | }) |
| 829 | .await?; |
| 830 | let access = match access { |
| 831 | Outcome::Ok(access) => access, |
| 832 | refused => return git_http::refuse(refused), |
| 833 | }; |
| 834 | // A protected default branch takes changes only from a merged pull |
| 835 | // request, which lands without going through here. |
| 836 | let protected = match self.registry.by_path(&git.path).await? { |
| 837 | Some(repo) if repo.protected && repo.fork_of.is_none() => Some(repo.default_branch), |
| 838 | _ => None, |
| 839 | }; |
| 840 | let forwarded = |
| 841 | match git_http::forward(request, &git, &access, protected.as_deref()).await? { |
| 842 | git_http::Push::Forwarded(forwarded) => forwarded, |
| 843 | git_http::Push::Refused(response) => return Ok(response), |
| 844 | }; |
| 845 | |
| 846 | // Artifacts' own push notifications are per repository, which does |
| 847 | // not fit a repo per pull request, so the front end reports pushes |
| 848 | // itself: one event for each branch that moved. |
| 849 | let accepted = forwarded.response.status_code() == 200 && !forwarded.pushed.is_empty(); |
| 850 | if accepted && let Some(repo) = self.registry.by_path(&git.path).await? { |
| 851 | let stored = self.store.open(&store_key(&repo)).await?; |
| 852 | let actor = viewer.map(|user: User| user.id); |
| 853 | for (branch, pushed) in &forwarded.pushed { |
| 854 | // The store can refuse one ref and accept another, so each |
| 855 | // is checked against where the branch actually is. |
| 856 | let head = stored.log(branch, 1).await?; |
| 857 | if head.first().is_some_and(|commit| commit.hash == *pushed) { |
| 858 | self.publish_push(&repo, branch, pushed, actor.clone()) |
| 859 | .await?; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | Ok(forwarded.response) |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | #[event(fetch)] |
| 868 | async fn fetch(mut request: Request, env: Env, _ctx: Context) -> Result<Response> { |
| 869 | let repos = Repos { |
| 870 | registry: Registry { db: env.d1("DB")? }, |
| 871 | store: ArtifactsStore::new(&env)?, |
| 872 | events: env.service("EVENTS")?, |
| 873 | }; |
| 874 | let Some(method) = rpc_method(&request) else { |
| 875 | return repos.git_http(request, &env).await; |
| 876 | }; |
| 877 | let body: serde_json::Value = request.json().await?; |
| 878 | |
| 879 | match method.as_str() { |
| 880 | "get" => reply(&repos.get(args(body)?).await?), |
| 881 | "get_by_id" => reply(&repos.get_by_id(args(body)?).await?), |
| 882 | "list" => { |
| 883 | let a: ListArgs = args(body)?; |
| 884 | reply( |
| 885 | &repos |
| 886 | .registry |
| 887 | .list( |
| 888 | &a.viewer, |
| 889 | a.query.as_deref(), |
| 890 | a.namespace.as_deref(), |
| 891 | a.member_only, |
| 892 | ) |
| 893 | .await?, |
| 894 | ) |
| 895 | } |
| 896 | "create" => reply(&repos.create(args(body)?).await?), |
| 897 | "update" => reply(&repos.update(args(body)?).await?), |
| 898 | "tree" => reply(&repos.tree(args(body)?).await?), |
| 899 | "blob" => reply(&repos.blob(args(body)?).await?), |
| 900 | "log" => reply(&repos.log(args(body)?).await?), |
| 901 | "blame" => reply(&repos.blame(args(body)?).await?), |
| 902 | "fork_for_pull" => reply(&repos.fork_for_pull(args(body)?).await?), |
| 903 | "git_access" => reply(&repos.git_access(args(body)?).await?), |
| 904 | "branches" => reply(&repos.branches(args(body)?).await?), |
| 905 | "head" => reply(&repos.head(args(body)?).await?), |
| 906 | "behind" => reply(&repos.behind(args(body)?).await?), |
| 907 | "land" => reply(&repos.land(args(body)?).await?), |
| 908 | "compare" => reply(&repos.compare(args(body)?).await?), |
| 909 | _ => Response::error("Unknown method", 404), |
| 910 | } |
| 911 | } |