Skip to content

Commit dd9b2f2

Browse files
author
Oleksandr Garagatyi
authored
Merge pull request eclipse-che#6679 from kameshsampath/issue-6479
eclipse-che#6479 - Create workspace with factory json for urls ending with .git
2 parents 1f44008 + 3776302 commit dd9b2f2

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

plugins/plugin-urlfactory/src/main/java/org/eclipse/che/plugin/urlfactory/URLFactoryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
public class URLFactoryBuilder {
4141

4242
/** Default docker image (if repository has no dockerfile) */
43-
protected static final String DEFAULT_DOCKER_IMAGE = "codenvy/ubuntu_jdk8";
43+
protected static final String DEFAULT_DOCKER_IMAGE = "eclipse/ubuntu_jdk8";
4444

4545
/** Default docker type (if repository has no dockerfile) */
4646
protected static final String MEMORY_LIMIT_BYTES = Long.toString(2000L * 1024L * 1024L);

plugins/plugin-urlfactory/src/main/java/org/eclipse/che/plugin/urlfactory/URLFetcher.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.InputStreamReader;
2020
import java.net.URL;
2121
import java.net.URLConnection;
22+
import java.util.regex.Matcher;
23+
import java.util.regex.Pattern;
2224
import java.util.stream.Collectors;
2325
import javax.inject.Singleton;
2426
import javax.validation.constraints.NotNull;
@@ -40,6 +42,9 @@ public class URLFetcher {
4042
/** Maximum size of allowed data. (30KB) */
4143
protected static final long MAXIMUM_READ_BYTES = 30 * 1000;
4244

45+
/** The Compiled REGEX PATTERN that can be used for http|https git urls */
46+
final Pattern GIT_HTTP_URL_PATTERN = Pattern.compile("(?<sanitized>^http[s]?://.*)\\.git$");
47+
4348
/**
4449
* Fetch the url provided and return its content To prevent DOS attack, limit the amount of the
4550
* collected data
@@ -50,7 +55,7 @@ public class URLFetcher {
5055
public String fetch(@NotNull final String url) {
5156
requireNonNull(url, "url parameter can't be null");
5257
try {
53-
return fetch(new URL(url).openConnection());
58+
return fetch(new URL(sanitized(url)).openConnection());
5459
} catch (IOException e) {
5560
// we shouldn't fetch if check is done before
5661
LOG.debug("Invalid URL", e);
@@ -89,4 +94,22 @@ public String fetch(@NotNull URLConnection urlConnection) {
8994
protected long getLimit() {
9095
return MAXIMUM_READ_BYTES;
9196
}
97+
98+
/**
99+
* Simple method to sanitize the Git urls like &quot;https://github.com/demo.git&quot; or
100+
* &quot;http://myowngit.example.com/demo.git&quot;
101+
*
102+
* @param url - the String format of the url
103+
* @return if the url ends with .git will return the url without .git otherwise return the url as
104+
* it is
105+
*/
106+
String sanitized(String url) {
107+
if (url != null) {
108+
final Matcher matcher = GIT_HTTP_URL_PATTERN.matcher(url);
109+
if (matcher.find()) {
110+
return matcher.group("sanitized");
111+
}
112+
}
113+
return url;
114+
}
92115
}

plugins/plugin-urlfactory/src/test/java/org/eclipse/che/plugin/urlfactory/URLFetcherTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public void checkUrlFileIsInvalid() {
6666
assertNull(result);
6767
}
6868

69+
/** Check Sanitizing of Git URL works */
70+
@Test
71+
public void checkDotGitRemovedFromURL() {
72+
String result = URLFetcher.sanitized("https://github.com/acme/demo.git");
73+
assertEquals("https://github.com/acme/demo", result);
74+
75+
result = URLFetcher.sanitized("http://github.com/acme/demo.git");
76+
assertEquals("http://github.com/acme/demo", result);
77+
}
78+
6979
/** Check that when url doesn't exist */
7080
@Test
7181
public void checkMissingContent() {

0 commit comments

Comments
 (0)