Skip to content

Commit

Permalink
Omit extracting subfolder from a workspace URL
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Jun 12, 2023
1 parent 7c59801 commit 1c57aa0
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2023 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -13,7 +13,6 @@

import static org.eclipse.che.dto.server.DtoFactory.newDto;

import com.google.common.base.Strings;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Singleton;
Expand All @@ -40,9 +39,6 @@ public SourceStorageDto buildWorkspaceConfigSource(GithubUrl githubUrl) {
Map<String, String> parameters = new HashMap<>(2);
parameters.put("branch", githubUrl.getBranch());

if (!Strings.isNullOrEmpty(githubUrl.getSubfolder())) {
parameters.put("keepDir", githubUrl.getSubfolder());
}
return newDto(SourceStorageDto.class)
.withLocation(githubUrl.repositoryLocation())
.withType("github")
Expand All @@ -59,7 +55,6 @@ public SourceDto buildDevfileSource(GithubUrl githubUrl) {
return newDto(SourceDto.class)
.withLocation(githubUrl.repositoryLocation())
.withType("github")
.withBranch(githubUrl.getBranch())
.withSparseCheckoutDir(githubUrl.getSubfolder());
.withBranch(githubUrl.getBranch());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public GithubURLParser(
this.githubPattern =
compile(
format(
"^%s/(?<repoUser>[^/]++)/(?<repoName>[^/]++)((/)|(?:/tree/(?<branchName>[^/]++)(?:/(?<subFolder>.*))?)|(/pull/(?<pullRequestId>[^/]++)))?$",
"^%s/(?<repoUser>[^/]+)/(?<repoName>[^/]++)((/)|(?:/tree/(?<branchName>.++))|(/pull/(?<pullRequestId>d++)))?$",
endpoint));
}

Expand Down Expand Up @@ -173,7 +173,6 @@ private GithubUrl parse(String url, boolean authenticationRequired) throws ApiEx
.withDisableSubdomainIsolation(disableSubdomainIsolation)
.withBranch(branchName)
.withLatestCommit(latestCommit)
.withSubfolder(matcher.group("subFolder"))
.withDevfileFilenames(devfileFilenamesProvider.getConfiguredDevfileFilenames())
.withUrl(url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public class GithubUrl extends DefaultFactoryUrl {
/** SHA of the latest commit in the current branch */
private String latestCommit;

/** Subfolder if any */
private String subfolder;

private String serverUrl;

private boolean disableSubdomainIsolation;
Expand Down Expand Up @@ -143,26 +140,6 @@ protected GithubUrl withLatestCommit(String latestCommit) {
return this;
}

/**
* Gets subfolder of this github url
*
* @return the subfolder part
*/
public String getSubfolder() {
return this.subfolder;
}

/**
* Sets the subfolder represented by the URL.
*
* @param subfolder path inside the repository
* @return current github instance
*/
protected GithubUrl withSubfolder(String subfolder) {
this.subfolder = subfolder;
return this;
}

public GithubUrl withServerUrl(String serverUrl) {
this.serverUrl = serverUrl;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,13 @@ public void checkRegexp(String url) {

/** Compare parsing */
@Test(dataProvider = "parsing")
public void checkParsing(
String url, String username, String repository, String branch, String subfolder)
public void checkParsing(String url, String username, String repository, String branch)
throws ApiException {
GithubUrl githubUrl = githubUrlParser.parse(url);

assertEquals(githubUrl.getUsername(), username);
assertEquals(githubUrl.getRepository(), repository);
assertEquals(githubUrl.getBranch(), branch);
assertEquals(githubUrl.getSubfolder(), subfolder);
}

/** Compare parsing */
Expand Down Expand Up @@ -117,32 +115,19 @@ public Object[][] urls() {
@DataProvider(name = "parsing")
public Object[][] expectedParsing() {
return new Object[][] {
{"https://github.com/eclipse/che", "eclipse", "che", null, null},
{"https://github.com/eclipse/che123", "eclipse", "che123", null, null},
{"https://github.com/eclipse/che.git", "eclipse", "che", null, null},
{"https://github.com/eclipse/che.with.dot.git", "eclipse", "che.with.dot", null, null},
{"https://github.com/eclipse/-.git", "eclipse", "-", null, null},
{"https://github.com/eclipse/-j.git", "eclipse", "-j", null, null},
{"https://github.com/eclipse/-", "eclipse", "-", null, null},
{"https://github.com/eclipse/che-with-hyphen", "eclipse", "che-with-hyphen", null, null},
{"https://github.com/eclipse/che-with-hyphen.git", "eclipse", "che-with-hyphen", null, null},
{"https://github.com/eclipse/che/", "eclipse", "che", null, null},
{"https://github.com/eclipse/repositorygit", "eclipse", "repositorygit", null, null},
{"https://github.com/eclipse/che/tree/4.2.x", "eclipse", "che", "4.2.x", null},
{
"https://github.com/eclipse/che/tree/master/dashboard/",
"eclipse",
"che",
"master",
"dashboard/"
},
{
"https://github.com/eclipse/che/tree/master/plugins/plugin-git/che-plugin-git-ext-git",
"eclipse",
"che",
"master",
"plugins/plugin-git/che-plugin-git-ext-git"
}
{"https://github.com/eclipse/che", "eclipse", "che", null},
{"https://github.com/eclipse/che123", "eclipse", "che123", null},
{"https://github.com/eclipse/che.git", "eclipse", "che", null},
{"https://github.com/eclipse/che.with.dot.git", "eclipse", "che.with.dot", null},
{"https://github.com/eclipse/-.git", "eclipse", "-", null},
{"https://github.com/eclipse/-j.git", "eclipse", "-j", null},
{"https://github.com/eclipse/-", "eclipse", "-", null},
{"https://github.com/eclipse/che-with-hyphen", "eclipse", "che-with-hyphen", null},
{"https://github.com/eclipse/che-with-hyphen.git", "eclipse", "che-with-hyphen", null},
{"https://github.com/eclipse/che/", "eclipse", "che", null},
{"https://github.com/eclipse/repositorygit", "eclipse", "repositorygit", null},
{"https://github.com/eclipse/che/tree/4.2.x", "eclipse", "che", "4.2.x"},
{"https://github.com/eclipse/che/tree/master", "eclipse", "che", "master"}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ public FactoryDto visit(FactoryDto factory) {
newDto(SourceDto.class)
.withLocation(gitlabUrl.repositoryLocation())
.withType("git")
.withBranch(gitlabUrl.getBranch())
.withSparseCheckoutDir(gitlabUrl.getSubfolder()))
.withBranch(gitlabUrl.getBranch()))
.withName(gitlabUrl.getProject()),
project -> {
final String location = project.getSource().getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public class GitlabUrl extends DefaultFactoryUrl {
/** Branch name */
private String branch;

/** Subfolder if any */
private String subfolder;

/** Devfile filenames list */
private final List<String> devfileFilenames = new ArrayList<>();

Expand Down Expand Up @@ -129,26 +126,6 @@ protected GitlabUrl withBranch(String branch) {
return this;
}

/**
* Gets subfolder of this gitlab url
*
* @return the subfolder part
*/
public String getSubfolder() {
return this.subfolder;
}

/**
* Sets the subfolder represented by the URL.
*
* @param subfolder path inside the repository
* @return current gitlab URL instance
*/
protected GitlabUrl withSubfolder(String subfolder) {
this.subfolder = subfolder;
return this;
}

/**
* Provides list of configured devfile filenames with locations
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class GitlabUrlParser {
private final PersonalAccessTokenManager personalAccessTokenManager;
private static final List<String> gitlabUrlPatternTemplates =
List.of(
"^(?<host>%s)/(?<subgroups>([^/]++/?)+)/-/tree/(?<branch>[^/]++)(/)?(?<subfolder>[^/]++)?",
"^(?<host>%s)/(?<subgroups>([^/]++/?)+)/-/tree/(?<branch>.++)(/)?",
"^(?<host>%s)/(?<subgroups>.*)"); // a wider one, should be the last in the
// list
private final List<Pattern> gitlabUrlPatterns = new ArrayList<>();
Expand Down Expand Up @@ -168,23 +168,16 @@ private GitlabUrl parse(Matcher matcher) {
}

String branch = null;
String subfolder = null;
try {
branch = matcher.group("branch");
} catch (IllegalArgumentException e) {
// ok no such group
}
try {
subfolder = matcher.group("subfolder");
} catch (IllegalArgumentException e) {
// ok no such group
}

return new GitlabUrl()
.withHostName(host)
.withSubGroups(subGroups)
.withBranch(branch)
.withSubfolder(subfolder)
.withDevfileFilenames(devfileFilenamesProvider.getConfiguredDevfileFilenames());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ public void checkRegexp(String url) {

/** Compare parsing */
@Test(dataProvider = "parsing")
public void checkParsing(
String url, String project, String subGroups, String branch, String subfolder) {
public void checkParsing(String url, String project, String subGroups, String branch) {
GitlabUrl gitlabUrl = gitlabUrlParser.parse(url);

assertEquals(gitlabUrl.getProject(), project);
assertEquals(gitlabUrl.getSubGroups(), subGroups);
assertEquals(gitlabUrl.getBranch(), branch);
assertEquals(gitlabUrl.getSubfolder(), subfolder);
}

@Test
Expand Down Expand Up @@ -123,33 +121,28 @@ public Object[][] urls() {
@DataProvider(name = "parsing")
public Object[][] expectedParsing() {
return new Object[][] {
{"https://gitlab1.com/user/project1.git", "project1", "user/project1", null, null},
{"https://gitlab1.com/user/project/test1.git", "test1", "user/project/test1", null, null},
{"https://gitlab1.com/user/project1.git", "project1", "user/project1", null},
{"https://gitlab1.com/user/project/test1.git", "test1", "user/project/test1", null},
{
"https://gitlab1.com/user/project/group1/group2/test1.git",
"test1",
"user/project/group1/group2/test1",
null,
null
},
{"https://gitlab1.com/user/project/", "project", "user/project", null, null},
{"https://gitlab1.com/user/project/repo/", "repo", "user/project/repo", null, null},
{
"https://gitlab1.com/user/project/-/tree/master/", "project", "user/project", "master", null
},
{"https://gitlab1.com/user/project/", "project", "user/project", null},
{"https://gitlab1.com/user/project/repo/", "repo", "user/project/repo", null},
{"https://gitlab1.com/user/project/-/tree/master/", "project", "user/project", "master"},
{
"https://gitlab1.com/user/project/repo/-/tree/foo/subfolder",
"repo",
"user/project/repo",
"foo",
"subfolder"
"foo"
},
{
"https://gitlab1.com/user/project/group1/group2/repo/-/tree/foo/subfolder",
"repo",
"user/project/group1/group2/repo",
"foo",
"subfolder"
"foo"
}
};
}
Expand Down

0 comments on commit 1c57aa0

Please sign in to comment.