Skip to content

Commit 9e52d56

Browse files
committed
fixup! Add Azure DevOps Server support
1 parent 0372ac8 commit 9e52d56

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

wsmaster/che-core-api-factory-gitlab-common/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<groupId>com.fasterxml.jackson.core</groupId>
3232
<artifactId>jackson-databind</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>com.google.code.gson</groupId>
36+
<artifactId>gson</artifactId>
37+
</dependency>
3438
<dependency>
3539
<groupId>com.google.guava</groupId>
3640
<artifactId>guava</artifactId>

wsmaster/che-core-api-factory-gitlab-common/src/main/java/org/eclipse/che/api/factory/server/gitlab/AbstractGitlabUrlParser.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static java.util.regex.Pattern.compile;
1717
import static org.eclipse.che.commons.lang.StringUtils.trimEnd;
1818

19+
import com.google.gson.JsonParser;
1920
import jakarta.validation.constraints.NotNull;
2021
import java.net.URI;
2122
import java.util.ArrayList;
@@ -112,15 +113,27 @@ private boolean isApiRequestRelevant(String repositoryUrl) {
112113
// belongs to Gitlab.
113114
gitlabApiClient.getOAuthTokenInfo("");
114115
} catch (ScmUnauthorizedException e) {
115-
// the error message is a JSON if it is a response from Gitlab.
116-
return e.getMessage().startsWith("{");
116+
// Some Git providers e.g. Azure Devops Server, may return unauthorized exception on invalid
117+
// API request, but Gitlab API returns unauthorized error message in JSON format, so to be
118+
// sure that the URL belongs to Gitlab, we need to check if the error message is a valid
119+
// JSON.
120+
return isJsonValid(e.getMessage());
117121
} catch (ScmItemNotFoundException | IllegalArgumentException | ScmCommunicationException e) {
118122
return false;
119123
}
120124
}
121125
return false;
122126
}
123127

128+
private boolean isJsonValid(String message) {
129+
try {
130+
new JsonParser().parse(message).getAsJsonObject();
131+
return true;
132+
} catch (Exception exception) {
133+
return false;
134+
}
135+
}
136+
124137
private Optional<Matcher> getPatternMatcherByUrl(String url) {
125138
URI uri =
126139
URI.create(

wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlParserTest.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ public void shouldValidateUrlByApiRequest() {
101101
String url = wireMockServer.url("/user/repo");
102102
stubFor(
103103
get(urlEqualTo("/oauth/token/info"))
104-
.willReturn(aResponse().withStatus(401).withBody("{error}")));
104+
.willReturn(
105+
aResponse()
106+
.withStatus(401)
107+
.withBody(
108+
"{\"error\":\"invalid_token\",\"error_description\":\"The access token is invalid\",\"state\":\"unauthorized\"}")));
105109

106110
// when
107111
boolean result = gitlabUrlParser.isValid(url);
@@ -110,6 +114,21 @@ public void shouldValidateUrlByApiRequest() {
110114
assertTrue(result);
111115
}
112116

117+
@Test
118+
public void shouldNotValidateUrlByApiRequestWithPlainStringResponse() {
119+
// given
120+
String url = wireMockServer.url("/user/repo");
121+
stubFor(
122+
get(urlEqualTo("/oauth/token/info"))
123+
.willReturn(aResponse().withStatus(401).withBody("plain string error")));
124+
125+
// when
126+
boolean result = gitlabUrlParser.isValid(url);
127+
128+
// then
129+
assertFalse(result);
130+
}
131+
113132
@Test
114133
public void shouldNotValidateUrlByApiRequest() {
115134
// given

0 commit comments

Comments
 (0)