diff --git a/rewrite-core/src/main/java/org/openrewrite/scm/AzureDevopsCloneUrl.java b/rewrite-core/src/main/java/org/openrewrite/scm/AzureDevopsCloneUrl.java index 91b4fd114e1..24b9112e354 100644 --- a/rewrite-core/src/main/java/org/openrewrite/scm/AzureDevopsCloneUrl.java +++ b/rewrite-core/src/main/java/org/openrewrite/scm/AzureDevopsCloneUrl.java @@ -7,13 +7,19 @@ public class AzureDevopsCloneUrl implements CloneUrl { String cloneUrl; String origin; String path; + String organization; + String project; + String repositoryName; - String getProject() { - try { - String path = getPath(); - return path.substring(path.indexOf("/") + 1, path.lastIndexOf("/")); - } catch (IndexOutOfBoundsException ex) { - throw new IllegalStateException("Azure DevOps clone url path must contain organization, project and repository", ex); + public AzureDevopsCloneUrl(String cloneUrl, String origin, String path) { + this.cloneUrl = cloneUrl; + this.origin = origin; + this.path = path; + if (!path.contains("/")) { + throw new IllegalArgumentException("Azure DevOps clone url path must contain organization, project and repository"); } + this.organization = path.substring(0, path.indexOf("/")); + this.project = path.substring(path.indexOf("/") + 1, path.lastIndexOf("/")); + this.repositoryName = path.substring(path.lastIndexOf("/") + 1); } } diff --git a/rewrite-core/src/main/java/org/openrewrite/scm/CloneUrl.java b/rewrite-core/src/main/java/org/openrewrite/scm/CloneUrl.java index a113954f04b..c995447df3d 100644 --- a/rewrite-core/src/main/java/org/openrewrite/scm/CloneUrl.java +++ b/rewrite-core/src/main/java/org/openrewrite/scm/CloneUrl.java @@ -15,8 +15,6 @@ */ package org.openrewrite.scm; -import org.openrewrite.internal.lang.Nullable; - public interface CloneUrl { String getCloneUrl(); @@ -25,20 +23,8 @@ public interface CloneUrl { String getPath(); - default String getRepositoryName() { - String path = getPath(); - if (path.contains("/")) { - return path.substring(path.lastIndexOf("/") + 1); - } - return path; - } + String getOrganization(); + + String getRepositoryName(); - @Nullable - default String getOrganization() { - String path = getPath(); - if (path.contains("/")) { - return path.substring(0, path.lastIndexOf("/")); - } - return null; - } } diff --git a/rewrite-core/src/main/java/org/openrewrite/scm/GitLabCloneUrl.java b/rewrite-core/src/main/java/org/openrewrite/scm/GitLabCloneUrl.java index 07d3a7d3979..daf310866b9 100644 --- a/rewrite-core/src/main/java/org/openrewrite/scm/GitLabCloneUrl.java +++ b/rewrite-core/src/main/java/org/openrewrite/scm/GitLabCloneUrl.java @@ -17,18 +17,28 @@ import lombok.Value; +import java.util.Arrays; +import java.util.List; + @Value public class GitLabCloneUrl implements CloneUrl { String cloneUrl; String origin; String path; + List groups; + String organization; + String repositoryName; - String getGroupPath() { - try { - String path = getPath(); - return path.substring(0, path.lastIndexOf("/")); - } catch (IndexOutOfBoundsException ex) { - throw new IllegalStateException("GitLab clone url path must contain at least 1 group", ex); + public GitLabCloneUrl(String cloneUrl, String origin, String path) { + this.cloneUrl = cloneUrl; + this.origin = origin; + this.path = path; + if (!this.path.contains("/")) { + throw new IllegalArgumentException("GitLab path must contain 1 or more groups and a repository name"); } + String[] parts = this.path.split("/"); + groups = Arrays.asList(parts).subList(0, parts.length - 1); + organization = String.join("/", groups); + repositoryName = parts[parts.length - 1]; } } diff --git a/rewrite-core/src/main/java/org/openrewrite/scm/SimpleCloneUrl.java b/rewrite-core/src/main/java/org/openrewrite/scm/SimpleCloneUrl.java index 971c3a45cc0..4dc09f3983d 100644 --- a/rewrite-core/src/main/java/org/openrewrite/scm/SimpleCloneUrl.java +++ b/rewrite-core/src/main/java/org/openrewrite/scm/SimpleCloneUrl.java @@ -18,8 +18,21 @@ import lombok.Value; @Value -public class SimpleCloneUrl implements CloneUrl{ +public class SimpleCloneUrl implements CloneUrl { String cloneUrl; String origin; String path; + String organization; + String repositoryName; + + public SimpleCloneUrl(String cloneUrl, String origin, String path) { + this.cloneUrl = cloneUrl; + this.origin = origin; + this.path = path; + if (!this.path.contains("/")) { + throw new IllegalArgumentException("Path must contain organizat a repository name"); + } + organization = path.substring(0, path.lastIndexOf("/")); + repositoryName = path.substring(path.lastIndexOf("/") + 1); + } } diff --git a/rewrite-core/src/test/java/org/openrewrite/scm/AzureDevOpsScmTest.java b/rewrite-core/src/test/java/org/openrewrite/scm/AzureDevOpsScmTest.java index 0d2d0d2fb12..a988b1e40f5 100644 --- a/rewrite-core/src/test/java/org/openrewrite/scm/AzureDevOpsScmTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/scm/AzureDevOpsScmTest.java @@ -24,8 +24,8 @@ class AzureDevOpsScmTest { @CsvSource(textBlock = """ - https://dev.azure.com/org/project/_git/repo.git, true, dev.azure.com, org/project/repo, org, project - git@ssh.dev.azure.com:v3/org/project/repo.git, true, dev.azure.com, org/project/repo, org, project + https://dev.azure.com/org/project/_git/repo.git, true, dev.azure.com, org/project/repo, project, org + git@ssh.dev.azure.com:v3/org/project/repo.git, true, dev.azure.com, org/project/repo, project, org https://github.com/org/repo, false,,,, https://gitlab.com/org/repo, false,,,, diff --git a/rewrite-core/src/test/java/org/openrewrite/scm/GitLabScmTest.java b/rewrite-core/src/test/java/org/openrewrite/scm/GitLabScmTest.java index 2e38b98f99e..c1d74ecea97 100644 --- a/rewrite-core/src/test/java/org/openrewrite/scm/GitLabScmTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/scm/GitLabScmTest.java @@ -4,13 +4,15 @@ import org.junit.jupiter.params.provider.CsvSource; import org.openrewrite.internal.lang.Nullable; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; class GitLabScmTest { @CsvSource(textBlock = """ https://gitlab.com/group/repo.git, true, gitlab.com, group/repo, group, group - https://gitlab.com/group/subgroup/repo.git, true, gitlab.com, group/subgroup/repo, group/subgroup, group + https://gitlab.com/group/subgroup/subergroup/subestgroup/repo.git, true, gitlab.com, group/subgroup/subergroup/subestgroup/repo, group/subgroup/subergroup/subestgroup, group https://dev.azure.com/org/project/_git/repo.git, false,,, git@ssh.dev.azure.com:v3/org/project/repo.git, false,,, @@ -29,7 +31,8 @@ void splitOriginPath(String cloneUrl, boolean matchesScm, @Nullable String expec GitLabCloneUrl gitLabCloneUrl = (GitLabCloneUrl) parsed; assertThat(gitLabCloneUrl.getOrigin()).isEqualTo(expectedOrigin); assertThat(gitLabCloneUrl.getPath()).isEqualTo(expectedPath); - assertThat(gitLabCloneUrl.getGroupPath()).isEqualTo(expectedGroupPath); + assertThat(expectedGroupPath).isNotNull(); + assertThat(gitLabCloneUrl.getGroups()).containsExactlyElementsOf(List.of(expectedGroupPath.split("/"))); assertThat(gitLabCloneUrl.getOrganization()).isEqualTo(expectedGroupPath); } }