From 4218cafeb4bc4904c3f25f8db4f9d5ba4b56e3b4 Mon Sep 17 00:00:00 2001 From: Justin Walsh Date: Thu, 11 Jul 2024 12:08:43 -0400 Subject: [PATCH 1/2] allows for depots with non-default stream depths --- src/handlers.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/handlers.rs b/src/handlers.rs index f9b6959..09216ca 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -21,6 +21,14 @@ pub struct Metrics { pub metadata_submit_requests: AtomicU64, } +fn find_last_starting_at(haystack: &str, needle: char, starting_index: usize) -> Option { + if let Some(slice) = haystack.get(starting_index..) { + slice.rfind(needle).map(|i| i + starting_index) + } else { + None + } +} + fn find_starting_at(haystack: &str, needle: char, starting_index: usize) -> Option { if let Some(slice) = haystack.get(starting_index..) { slice.find(needle).map(|i| i + starting_index) @@ -36,7 +44,7 @@ fn split_project_path(project_path: &str) -> Option<(String, String)> { } if let Some(stream_name_index) = find_starting_at(project_path, '/', 2) { - if let Some(project_index) = find_starting_at(project_path, '/', stream_name_index + 1) { + if let Some(project_index) = find_last_starting_at(project_path, '/', stream_name_index + 1) { if project_path.len() > project_index + 1 { Some(( normalize_stream(&project_path[0..project_index]), @@ -484,3 +492,27 @@ pub async fn metadata_submit( Ok((StatusCode::OK, "")) } + +#[cfg(test)] +mod test { + #[test] + fn test_split_project_path() { + const STREAM_DEPTH_DEFAULT: &str = "//depot/stream/project-name"; + const STREAM_DEPTH_3: &str = "//depot/stream/depth3/project-name-3"; + const STREAM_DEPTH_4: &str = "//depot/stream/depth3/depth4/project-name-4"; + + fn assert_split_project_path( + project_path: &str, + expected_stream: &str, + expected_project: &str, + ) { + let (stream, project) = super::split_project_path(project_path).unwrap(); + assert_eq!(stream, expected_stream); + assert_eq!(project, expected_project); + } + + assert_split_project_path(STREAM_DEPTH_DEFAULT, "//depot/stream", "project-name"); + assert_split_project_path(STREAM_DEPTH_3, "//depot/stream/depth3", "project-name-3"); + assert_split_project_path(STREAM_DEPTH_4, "//depot/stream/depth3/depth4", "project-name-4"); + } +} \ No newline at end of file From 62c99e609b5d2d26b624eb9dc040a0bf608f2673 Mon Sep 17 00:00:00 2001 From: Justin Walsh Date: Thu, 25 Jul 2024 09:19:52 -0400 Subject: [PATCH 2/2] add migration for stream depth support --- migrations/20240725130753_projects_stream_depth.sql | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 migrations/20240725130753_projects_stream_depth.sql diff --git a/migrations/20240725130753_projects_stream_depth.sql b/migrations/20240725130753_projects_stream_depth.sql new file mode 100644 index 0000000..b910ca7 --- /dev/null +++ b/migrations/20240725130753_projects_stream_depth.sql @@ -0,0 +1,5 @@ +UPDATE projects + SET + stream = concat(stream, '/', rtrim(rtrim(project, replace(project, rtrim(project, replace(project, '/', '')), '')), '/')), + project = replace(project, rtrim(project, replace(project, '/', '')), '') + WHERE project LIKE '%/%';