|
1 | 1 | mod git_repository_scanner; |
2 | 2 | mod rules_manager; |
3 | 3 |
|
| 4 | +use git2::Repository; |
4 | 5 | use parking_lot::Mutex; |
5 | 6 | use pyo3::exceptions; |
6 | 7 | use pyo3::prelude::*; |
@@ -213,6 +214,52 @@ impl GitRepositoryScanner { |
213 | 214 | Ok(matches.lock().to_object(py)) |
214 | 215 | } |
215 | 216 | } |
| 217 | + |
| 218 | + /// Scan a git repository for secrets. Rules shuld be loaded before calling this function. |
| 219 | + /// |
| 220 | + /// input: |
| 221 | + /// url: str -> URL of a git repository |
| 222 | + /// repository_path: str -> The path to clone the repository to |
| 223 | + /// branch_glob_pattern: str -> A blob pattern to match against the git branches names. |
| 224 | + /// Only matched branches will be scanned. |
| 225 | + /// from_timestamp: int = 0 -> Unix epoch timestamp to start the scan from. |
| 226 | + /// |
| 227 | + /// returns: |
| 228 | + /// list[dict] -> List of matches |
| 229 | + /// |
| 230 | + /// example: |
| 231 | + /// grs.scan_from_url( |
| 232 | + /// url="https://github.com/rust-lang/git2-rs", |
| 233 | + /// repository_path="/path/to/repository", |
| 234 | + /// branch_glob_pattern="*", |
| 235 | + /// ) |
| 236 | + #[text_signature = "(url, repository_path, branch_glob_pattern, from_timestamp, /)"] |
| 237 | + fn scan_from_url( |
| 238 | + &self, |
| 239 | + py: Python, |
| 240 | + url: &str, |
| 241 | + repository_path: &str, |
| 242 | + branch_glob_pattern: Option<&str>, |
| 243 | + from_timestamp: Option<i64>, |
| 244 | + ) -> PyResult<Py<PyAny>> { |
| 245 | + let matches = Arc::new(Mutex::new(Vec::<HashMap<&str, String>>::with_capacity(10000))); |
| 246 | + |
| 247 | + if let Err(error) = Repository::clone(url, repository_path) { |
| 248 | + return Err(exceptions::PyRuntimeError::new_err(error.to_string())); |
| 249 | + }; |
| 250 | + |
| 251 | + if let Err(error) = git_repository_scanner::scan_repository( |
| 252 | + repository_path, |
| 253 | + branch_glob_pattern.unwrap_or("*"), |
| 254 | + from_timestamp.unwrap_or(0), |
| 255 | + &self.rules_manager, |
| 256 | + matches.clone(), |
| 257 | + ) { |
| 258 | + Err(exceptions::PyRuntimeError::new_err(error.to_string())) |
| 259 | + } else { |
| 260 | + Ok(matches.lock().to_object(py)) |
| 261 | + } |
| 262 | + } |
216 | 263 | } |
217 | 264 |
|
218 | 265 | /// PyRepScan is a Python library written in Rust. The library prodives an API to scan git repositories |
|
0 commit comments