sentry_options/
schema_fetch.rs1use serde::Deserialize;
7use std::collections::HashMap;
8use std::fs;
9use std::path::Path;
10use std::process::Command;
11use std::thread;
12use std::time::Duration;
13use thiserror::Error;
14
15#[derive(Debug, Error)]
16pub enum SchemaFetchError {
17 #[error("{0}")]
18 Validation(String),
19
20 #[error("I/O error: {0}")]
21 Io(#[from] std::io::Error),
22
23 #[error("JSON error: {0}")]
24 Json(#[from] serde_json::Error),
25
26 #[error("Git command failed: {0}")]
27 Git(String),
28}
29
30pub type SchemaFetchResult<T> = std::result::Result<T, SchemaFetchError>;
31
32#[derive(Debug, Deserialize)]
33#[serde(deny_unknown_fields)]
34pub struct RepoSchemaConfig {
35 pub url: String,
36 #[allow(dead_code)]
39 #[serde(default)]
40 pub sha: Option<String>,
41 pub path: String,
42}
43
44#[derive(Debug, Deserialize)]
45#[serde(deny_unknown_fields)]
46pub struct RepoSchemaConfigs {
47 pub repos: HashMap<String, RepoSchemaConfig>,
48}
49
50impl RepoSchemaConfigs {
51 pub fn from_file(path: &Path) -> SchemaFetchResult<Self> {
52 let content = fs::read_to_string(path)?;
53 Ok(serde_json::from_str(&content)?)
54 }
55}
56
57pub fn fetch_schemas(config: &RepoSchemaConfigs, out_dir: &Path) -> SchemaFetchResult<()> {
59 if out_dir.exists() {
60 return Err(SchemaFetchError::Validation(format!(
61 "Output directory already exists: {}",
62 out_dir.display()
63 )));
64 }
65 fs::create_dir_all(out_dir)?;
66
67 let mut repo_names: Vec<_> = config.repos.keys().collect();
69 repo_names.sort();
70
71 let results: Vec<_> = thread::scope(|scope| {
72 let handles: Vec<_> = repo_names
73 .iter()
74 .map(|repo_name| {
75 let source = &config.repos[*repo_name];
76 let name = repo_name.as_str();
77 (
78 name,
79 scope.spawn(move || fetch_repo_schemas(name, source, out_dir)),
80 )
81 })
82 .collect();
83
84 handles
85 .into_iter()
86 .map(|(name, handle)| {
87 let result = match handle.join() {
88 Ok(Ok(())) => Ok(()),
89 Ok(Err(error)) => Err(error.to_string()),
90 Err(_) => Err(format!("{}: thread panicked", name)),
91 };
92 (name, result)
93 })
94 .collect::<Vec<_>>()
95 });
96
97 let mut errors = Vec::new();
98 for (_repo_name, result) in results {
99 match result {
100 Ok(()) => {}
101 Err(error) => errors.push(error),
102 }
103 }
104
105 if errors.is_empty() {
106 Ok(())
107 } else {
108 Err(SchemaFetchError::Git(errors.join("\n")))
109 }
110}
111
112pub fn fetch_schemas_from_file(config_path: &Path, out_dir: &Path) -> SchemaFetchResult<()> {
114 let config = RepoSchemaConfigs::from_file(config_path)?;
115 fetch_schemas(&config, out_dir)
116}
117
118const MAX_RETRIES: u32 = 3;
119const INITIAL_RETRY_DELAY: Duration = Duration::from_secs(3);
120
121fn fetch_repo_schemas(
122 repo_name: &str,
123 source: &RepoSchemaConfig,
124 out_dir: &Path,
125) -> SchemaFetchResult<()> {
126 let mut delay = INITIAL_RETRY_DELAY;
127 let mut last_error = None;
128
129 for attempt in 0..=MAX_RETRIES {
130 if attempt > 0 {
131 thread::sleep(delay);
132 delay *= 2;
133 }
134
135 match try_fetch_repo_schemas(repo_name, source, out_dir) {
136 Ok(()) => return Ok(()),
137 Err(error) => last_error = Some(error),
138 }
139 }
140
141 Err(last_error.expect("at least one fetch attempt must run"))
142}
143
144fn try_fetch_repo_schemas(
145 repo_name: &str,
146 source: &RepoSchemaConfig,
147 out_dir: &Path,
148) -> SchemaFetchResult<()> {
149 let temp_dir = tempfile::tempdir()?;
150 let repo_path = temp_dir.path().join("repo");
151 let repo_str = repo_path.to_str().ok_or_else(|| {
152 SchemaFetchError::Validation(format!("{}: temp path contains invalid UTF-8", repo_name))
153 })?;
154
155 git(
156 &[
157 "clone",
158 "--filter=blob:none",
159 "--sparse",
160 &source.url,
161 repo_str,
162 ],
163 None,
164 repo_name,
165 )?;
166 git(
167 &["sparse-checkout", "set", &source.path],
168 Some(&repo_path),
169 repo_name,
170 )?;
171
172 copy_schemas(&repo_path.join(&source.path), out_dir)
173}
174
175fn git(args: &[&str], cwd: Option<&Path>, repo_name: &str) -> SchemaFetchResult<()> {
176 let mut command = Command::new("git");
177 command.args(args);
178 if let Some(directory) = cwd {
179 command.current_dir(directory);
180 }
181 let output = command.output()?;
182 if output.status.success() {
183 Ok(())
184 } else {
185 Err(SchemaFetchError::Git(format!(
186 "{}: {}",
187 repo_name,
188 String::from_utf8_lossy(&output.stderr).trim()
189 )))
190 }
191}
192
193fn copy_schemas(src: &Path, dest: &Path) -> SchemaFetchResult<()> {
194 if !src.exists() {
195 return Err(SchemaFetchError::Validation(format!(
196 "Schemas path does not exist: {}",
197 src.display()
198 )));
199 }
200
201 for entry in fs::read_dir(src)? {
202 let entry = entry?;
203 if entry.file_type()?.is_dir() {
204 let namespace = entry.file_name();
205 let src_schema = entry.path().join("schema.json");
206 if src_schema.exists() {
207 let dest_namespace = dest.join(&namespace);
208 fs::create_dir_all(&dest_namespace)?;
209 fs::copy(src_schema, dest_namespace.join("schema.json"))?;
210 }
211 }
212 }
213 Ok(())
214}
215
216#[cfg(test)]
217mod tests {
218 use super::*;
219 use tempfile::TempDir;
220
221 #[test]
222 fn test_from_file_valid_json() {
223 let dir = TempDir::new().unwrap();
224 let path = dir.path().join("repos.json");
225 fs::write(
226 &path,
227 r#"{
228 "repos": {
229 "sentry": {
230 "url": "https://github.com/getsentry/sentry",
231 "sha": "abc123",
232 "path": "schemas/"
233 }
234 }
235 }"#,
236 )
237 .unwrap();
238
239 let config = RepoSchemaConfigs::from_file(&path).unwrap();
240 assert_eq!(config.repos.len(), 1);
241 let sentry = config.repos.get("sentry").unwrap();
242 assert_eq!(sentry.url, "https://github.com/getsentry/sentry");
243 assert_eq!(sentry.sha, Some("abc123".to_string()));
244 assert_eq!(sentry.path, "schemas/");
245 }
246
247 #[test]
248 fn test_from_file_rejects_unknown_fields() {
249 let dir = TempDir::new().unwrap();
250 let path = dir.path().join("repos.json");
251 fs::write(
252 &path,
253 r#"{
254 "repos": {
255 "sentry": {
256 "url": "https://github.com/getsentry/sentry",
257 "path": "schemas/",
258 "unknown_field": true
259 }
260 }
261 }"#,
262 )
263 .unwrap();
264
265 let error = RepoSchemaConfigs::from_file(&path).unwrap_err();
266 assert!(error.to_string().contains("unknown field `unknown_field`"));
267 }
268
269 #[test]
270 fn test_copy_schemas() {
271 let src_dir = TempDir::new().unwrap();
272 let dest_dir = TempDir::new().unwrap();
273 let namespace = src_dir.path().join("relay");
274 fs::create_dir_all(&namespace).unwrap();
275 fs::write(namespace.join("schema.json"), r#"{"version": "1.0"}"#).unwrap();
276
277 copy_schemas(src_dir.path(), dest_dir.path()).unwrap();
278
279 assert!(dest_dir.path().join("relay/schema.json").exists());
280 }
281
282 #[test]
283 fn test_fetch_schemas_errors_if_output_exists() {
284 let out_dir = TempDir::new().unwrap();
285 let config = RepoSchemaConfigs {
286 repos: HashMap::new(),
287 };
288
289 let error = fetch_schemas(&config, out_dir.path()).unwrap_err();
290 assert!(
291 error
292 .to_string()
293 .contains("Output directory already exists")
294 );
295 }
296}