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