pr_01m47d24b0e6n91zwymwxg0vpx/services/repos/src/registry.rs
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 | //! Repository metadata in D1. |
| 2 | ||
| 3 | use g1t_contracts::Viewer; | |
| 4 | use g1t_contracts::repos::{Repo, RepoPath}; | |
| 5 | use serde::Deserialize; | |
| 6 | use worker::wasm_bindgen::JsValue; | |
| 7 | use worker::{D1Database, Result}; | |
| 8 | ||
| 9 | #[derive(Deserialize)] | |
| 10 | struct RepoRow { | |
| 11 | id: String, | |
| 12 | namespace: String, | |
| 13 | name: String, | |
| 14 | description: Option<String>, | |
| 15 | is_private: u8, | |
| 16 | owner_id: String, | |
| 17 | default_branch: String, | |
| 18 | fork_of: Option<String>, | |
| RFC 3339 timestamps in identity and repos | 19 | created_at: String, |
| Rust repos service with shipping; pull requests kept in the model | 20 | } |
| 21 | ||
| 22 | impl From<RepoRow> for Repo { | |
| 23 | fn from(row: RepoRow) -> Self { | |
| 24 | Repo { | |
| 25 | id: row.id, | |
| 26 | namespace: row.namespace, | |
| 27 | name: row.name, | |
| 28 | description: row.description, | |
| 29 | is_private: row.is_private != 0, | |
| 30 | owner_id: row.owner_id, | |
| 31 | default_branch: row.default_branch, | |
| 32 | fork_of: row.fork_of, | |
| RFC 3339 timestamps in identity and repos | 33 | created_at: row.created_at, |
| Rust repos service with shipping; pull requests kept in the model | 34 | } |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | /// The key a repo is stored under in the git store. | |
| 39 | pub fn store_key(repo: &Repo) -> String { | |
| 40 | format!("{}--{}", repo.namespace, repo.name) | |
| 41 | } | |
| 42 | ||
| 43 | pub fn can_read(repo: &Repo, viewer: &Viewer) -> bool { | |
| 44 | !repo.is_private || can_write(repo, viewer) | |
| 45 | } | |
| 46 | ||
| 47 | pub fn can_write(repo: &Repo, viewer: &Viewer) -> bool { | |
| 48 | viewer.as_ref().is_some_and(|user| user.id == repo.owner_id) | |
| 49 | } | |
| 50 | ||
| 51 | fn optional(value: &Option<String>) -> JsValue { | |
| 52 | value.as_deref().map_or(JsValue::NULL, JsValue::from) | |
| 53 | } | |
| 54 | ||
| 55 | pub struct Registry { | |
| 56 | pub db: D1Database, | |
| 57 | } | |
| 58 | ||
| 59 | impl Registry { | |
| 60 | pub async fn by_path(&self, path: &RepoPath) -> Result<Option<Repo>> { | |
| 61 | Ok(self | |
| 62 | .db | |
| 63 | .prepare("SELECT * FROM repos WHERE namespace = ? AND name = ?") | |
| 64 | .bind(&[ | |
| 65 | path.namespace.to_lowercase().into(), | |
| 66 | path.name.to_lowercase().into(), | |
| 67 | ])? | |
| 68 | .first::<RepoRow>(None) | |
| 69 | .await? | |
| 70 | .map(Repo::from)) | |
| 71 | } | |
| 72 | ||
| 73 | pub async fn by_id(&self, id: &str) -> Result<Option<Repo>> { | |
| 74 | Ok(self | |
| 75 | .db | |
| 76 | .prepare("SELECT * FROM repos WHERE id = ?") | |
| 77 | .bind(&[id.into()])? | |
| 78 | .first::<RepoRow>(None) | |
| 79 | .await? | |
| 80 | .map(Repo::from)) | |
| 81 | } | |
| 82 | ||
| 83 | /// Repos the viewer may see, newest first. Excludes attempt forks. | |
| 84 | pub async fn list( | |
| 85 | &self, | |
| 86 | viewer: &Viewer, | |
| 87 | query: Option<&str>, | |
| 88 | namespace: Option<&str>, | |
| 89 | ) -> Result<Vec<Repo>> { | |
| 90 | let mut conditions = vec!["fork_of IS NULL", "(is_private = 0 OR owner_id = ?)"]; | |
| 91 | let viewer_id = viewer.as_ref().map_or("", |user| user.id.as_str()); | |
| 92 | let mut params: Vec<JsValue> = vec![viewer_id.into()]; | |
| 93 | if let Some(namespace) = namespace { | |
| 94 | conditions.push("namespace = ?"); | |
| 95 | params.push(namespace.to_lowercase().into()); | |
| 96 | } | |
| 97 | if let Some(query) = query.map(str::trim).filter(|query| !query.is_empty()) { | |
| 98 | conditions.push("(name LIKE ? ESCAPE '\\' OR description LIKE ? ESCAPE '\\')"); | |
| 99 | // LIKE wildcards in the query are matched literally. | |
| 100 | let escaped: String = query | |
| 101 | .chars() | |
| 102 | .flat_map(|c| match c { | |
| 103 | '\\' | '%' | '_' => vec!['\\', c], | |
| 104 | _ => vec![c], | |
| 105 | }) | |
| 106 | .collect(); | |
| 107 | let pattern = format!("%{escaped}%"); | |
| 108 | params.push(pattern.as_str().into()); | |
| 109 | params.push(pattern.into()); | |
| 110 | } | |
| 111 | let sql = format!( | |
| 112 | "SELECT * FROM repos WHERE {} ORDER BY created_at DESC, id DESC LIMIT 50", | |
| 113 | conditions.join(" AND ") | |
| 114 | ); | |
| 115 | let rows = self | |
| 116 | .db | |
| 117 | .prepare(sql) | |
| 118 | .bind(¶ms)? | |
| 119 | .all() | |
| 120 | .await? | |
| 121 | .results::<RepoRow>()?; | |
| 122 | Ok(rows.into_iter().map(Repo::from).collect()) | |
| 123 | } | |
| 124 | ||
| 125 | pub async fn insert(&self, repo: &Repo) -> Result<()> { | |
| 126 | self.db | |
| 127 | .prepare( | |
| 128 | "INSERT INTO repos | |
| 129 | (id, namespace, name, description, is_private, owner_id, | |
| 130 | default_branch, fork_of, created_at) | |
| 131 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", | |
| 132 | ) | |
| 133 | .bind(&[ | |
| 134 | repo.id.as_str().into(), | |
| 135 | repo.namespace.as_str().into(), | |
| 136 | repo.name.as_str().into(), | |
| 137 | optional(&repo.description), | |
| 138 | (repo.is_private as u8).into(), | |
| 139 | repo.owner_id.as_str().into(), | |
| 140 | repo.default_branch.as_str().into(), | |
| 141 | optional(&repo.fork_of), | |
| RFC 3339 timestamps in identity and repos | 142 | repo.created_at.as_str().into(), |
| Rust repos service with shipping; pull requests kept in the model | 143 | ])? |
| 144 | .run() | |
| 145 | .await?; | |
| 146 | Ok(()) | |
| 147 | } | |
| 148 | } |