chore: Fix Gitlab raw file location url (#363)

Rework the Gitlab raw file url function to return a public raw file url instead of an API request.
Trim the gitlabEndpoint if it ends with /
pull/365/head
Igor Vinokur 2022-10-03 09:52:14 +03:00 committed by GitHub
parent 6350fa9ab4
commit 5aa763aab3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 38 deletions

View File

@ -59,6 +59,10 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-json</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -12,6 +12,7 @@
package org.eclipse.che.security.oauth;
import static com.google.common.base.Strings.isNullOrEmpty;
import static org.eclipse.che.commons.lang.StringUtils.trimEnd;
import com.google.api.client.util.store.MemoryDataStoreFactory;
import jakarta.mail.internet.AddressException;
@ -42,14 +43,15 @@ public class GitLabOAuthAuthenticator extends OAuthAuthenticator {
public GitLabOAuthAuthenticator(
String clientId, String clientSecret, String gitlabEndpoint, String cheApiEndpoint)
throws IOException {
this.gitlabUserEndpoint = gitlabEndpoint + "/api/v4/user";
String trimmedGitlabEndpoint = trimEnd(gitlabEndpoint, '/');
this.gitlabUserEndpoint = trimmedGitlabEndpoint + "/api/v4/user";
this.cheApiEndpoint = cheApiEndpoint;
configure(
clientId,
clientSecret,
new String[] {},
gitlabEndpoint + "/oauth/authorize",
gitlabEndpoint + "/oauth/token",
trimmedGitlabEndpoint + "/oauth/authorize",
trimmedGitlabEndpoint + "/oauth/token",
new MemoryDataStoreFactory());
}

View File

@ -11,6 +11,7 @@
*/
package org.eclipse.che.api.factory.server.gitlab;
import java.io.IOException;
import org.eclipse.che.api.factory.server.scm.AuthorizingFileContentProvider;
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
@ -24,4 +25,19 @@ class GitlabAuthorizingFileContentProvider extends AuthorizingFileContentProvide
PersonalAccessTokenManager personalAccessTokenManager) {
super(gitlabUrl, urlFetcher, personalAccessTokenManager);
}
@Override
protected boolean isPublicRepository(GitlabUrl remoteFactoryUrl) {
try {
urlFetcher.fetch(
remoteFactoryUrl.getHostName()
+ '/'
+ remoteFactoryUrl.getUsername()
+ '/'
+ remoteFactoryUrl.getProject());
return true;
} catch (IOException e) {
return false;
}
}
}

View File

@ -11,6 +11,7 @@
*/
package org.eclipse.che.api.factory.server.gitlab;
import static com.google.common.base.MoreObjects.firstNonNull;
import static java.net.URLEncoder.encode;
import com.google.common.base.Charsets;
@ -199,21 +200,14 @@ public class GitlabUrl implements RemoteFactoryUrl {
* @return location of specified file in a repository
*/
public String rawFileLocation(String fileName) {
String resultUrl =
new StringJoiner("/")
.add(hostName)
.add("api/v4/projects")
// use URL-encoded path to the project as a selector instead of id
.add(geProjectIdentifier())
.add("repository")
.add("files")
.add(encode(fileName, Charsets.UTF_8))
.add("raw")
.toString();
if (branch != null) {
resultUrl = resultUrl + "?ref=" + branch;
}
return resultUrl;
return new StringJoiner("/")
.add(hostName)
.add(username)
.add(project)
.add("-/raw")
.add(firstNonNull(branch, "HEAD"))
.add(fileName)
.toString();
}
private String geProjectIdentifier() {

View File

@ -12,6 +12,7 @@
package org.eclipse.che.api.factory.server.gitlab;
import static java.lang.String.format;
import static java.util.regex.Pattern.compile;
import com.google.common.base.Splitter;
import jakarta.validation.constraints.NotNull;
@ -52,19 +53,21 @@ public class GitlabUrlParser {
@Inject
public GitlabUrlParser(
@Nullable @Named("che.integration.gitlab.server_endpoints") String bitbucketEndpoints,
@Nullable @Named("che.integration.gitlab.server_endpoints") String gitlabEndpoints,
DevfileFilenamesProvider devfileFilenamesProvider,
PersonalAccessTokenManager personalAccessTokenManager) {
this.devfileFilenamesProvider = devfileFilenamesProvider;
this.personalAccessTokenManager = personalAccessTokenManager;
if (bitbucketEndpoints != null) {
for (String bitbucketEndpoint : Splitter.on(",").split(bitbucketEndpoints)) {
String trimmedEndpoint = StringUtils.trimEnd(bitbucketEndpoint, '/');
if (gitlabEndpoints != null) {
for (String gitlabEndpoint : Splitter.on(",").split(gitlabEndpoints)) {
String trimmedEndpoint = StringUtils.trimEnd(gitlabEndpoint, '/');
for (String gitlabUrlPatternTemplate : gitlabUrlPatternTemplates) {
this.gitlabUrlPatterns.add(
Pattern.compile(format(gitlabUrlPatternTemplate, trimmedEndpoint)));
gitlabUrlPatterns.add(compile(format(gitlabUrlPatternTemplate, trimmedEndpoint)));
}
}
} else {
gitlabUrlPatternTemplates.forEach(
t -> gitlabUrlPatterns.add(compile(format(t, "https://gitlab.com"))));
}
}
@ -103,7 +106,7 @@ public class GitlabUrlParser {
if (serverUrlOptional.isPresent()) {
String serverUrl = serverUrlOptional.get();
return gitlabUrlPatternTemplates.stream()
.map(t -> Pattern.compile(format(t, serverUrl)).matcher(url))
.map(t -> compile(format(t, serverUrl)).matcher(url))
.filter(Matcher::matches)
.findAny();
}
@ -111,7 +114,7 @@ public class GitlabUrlParser {
}
private Optional<String> getServerUrl(String repositoryUrl) {
Matcher serverUrlMatcher = Pattern.compile("[^/|:]/").matcher(repositoryUrl);
Matcher serverUrlMatcher = compile("[^/|:]/").matcher(repositoryUrl);
if (serverUrlMatcher.find()) {
return Optional.of(
repositoryUrl.substring(0, repositoryUrl.indexOf(serverUrlMatcher.group()) + 1));

View File

@ -43,10 +43,7 @@ public class GitlabAuthorizingFileContentProviderTest {
when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(personalAccessToken);
fileContentProvider.fetchContent("devfile.yaml");
verify(urlFetcher)
.fetch(
eq(
"https://gitlab.net/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw"),
eq("Bearer my-token"));
.fetch(eq("https://gitlab.net/eclipse/che/-/raw/HEAD/devfile.yaml"), eq("Bearer my-token"));
}
@Test

View File

@ -69,25 +69,22 @@ public class GitlabUrlTest {
@DataProvider
public static Object[][] urlsProvider() {
return new Object[][] {
{
"https://gitlab.net/eclipse/che.git",
"https://gitlab.net/api/v4/projects/eclipse%%2Fche/repository/files/%s/raw"
},
{"https://gitlab.net/eclipse/che.git", "https://gitlab.net/eclipse/che/-/raw/HEAD/%s"},
{
"https://gitlab.net/eclipse/fooproj/che.git",
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw"
"https://gitlab.net/eclipse/fooproj/-/raw/HEAD/%s"
},
{
"https://gitlab.net/eclipse/fooproj/-/tree/master/",
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj/repository/files/%s/raw?ref=master"
"https://gitlab.net/eclipse/fooproj/-/raw/master/%s"
},
{
"https://gitlab.net/eclipse/fooproj/che/-/tree/foobranch/",
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw?ref=foobranch"
"https://gitlab.net/eclipse/fooproj/-/raw/foobranch/%s"
},
{
"https://gitlab.net/eclipse/fooproj/che/-/tree/foobranch/subfolder",
"https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw?ref=foobranch"
"https://gitlab.net/eclipse/fooproj/-/raw/foobranch/%s"
},
};
}