Skip to content

Commit

Permalink
fully parse on construction
Browse files Browse the repository at this point in the history
  • Loading branch information
pstreef committed Aug 2, 2024
1 parent 3d5d541 commit cbb7a1a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 3 additions & 17 deletions rewrite-core/src/main/java/org/openrewrite/scm/CloneUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.openrewrite.scm;

import org.openrewrite.internal.lang.Nullable;

public interface CloneUrl {

String getCloneUrl();
Expand All @@ -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;
}
}
22 changes: 16 additions & 6 deletions rewrite-core/src/main/java/org/openrewrite/scm/GitLabCloneUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
[email protected]: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
[email protected]: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,,,,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,,,
[email protected]:v3/org/project/repo.git, false,,,
Expand All @@ -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);
}
}
Expand Down

0 comments on commit cbb7a1a

Please sign in to comment.