From 4411e2452fc4fc8cf6028ef91f4ec6a6d0a7af47 Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Fri, 30 Sep 2022 17:28:37 +0300 Subject: [PATCH] chore: Fix Gitlab raw file location url Signed-off-by: Igor Vinokur --- wsmaster/che-core-api-auth-gitlab/pom.xml | 4 ++++ .../oauth/GitLabOAuthAuthenticator.java | 8 ++++--- .../GitlabAuthorizingFileContentProvider.java | 16 +++++++++++++ .../api/factory/server/gitlab/GitlabUrl.java | 24 +++++++------------ .../server/gitlab/GitlabUrlParser.java | 19 ++++++++------- ...labAuthorizingFileContentProviderTest.java | 5 +--- .../factory/server/gitlab/GitlabUrlTest.java | 13 ++++------ 7 files changed, 51 insertions(+), 38 deletions(-) diff --git a/wsmaster/che-core-api-auth-gitlab/pom.xml b/wsmaster/che-core-api-auth-gitlab/pom.xml index fba3330068d..11e033b394d 100644 --- a/wsmaster/che-core-api-auth-gitlab/pom.xml +++ b/wsmaster/che-core-api-auth-gitlab/pom.xml @@ -59,6 +59,10 @@ org.eclipse.che.core che-core-commons-json + + org.eclipse.che.core + che-core-commons-lang + org.slf4j slf4j-api diff --git a/wsmaster/che-core-api-auth-gitlab/src/main/java/org/eclipse/che/security/oauth/GitLabOAuthAuthenticator.java b/wsmaster/che-core-api-auth-gitlab/src/main/java/org/eclipse/che/security/oauth/GitLabOAuthAuthenticator.java index 4fa2b2d4187..f40fa26df9c 100644 --- a/wsmaster/che-core-api-auth-gitlab/src/main/java/org/eclipse/che/security/oauth/GitLabOAuthAuthenticator.java +++ b/wsmaster/che-core-api-auth-gitlab/src/main/java/org/eclipse/che/security/oauth/GitLabOAuthAuthenticator.java @@ -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()); } diff --git a/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProvider.java b/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProvider.java index 78248b5e702..ed52d60640f 100644 --- a/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProvider.java +++ b/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProvider.java @@ -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; + } + } } diff --git a/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrl.java b/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrl.java index e8321efcd71..b89ee643310 100644 --- a/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrl.java +++ b/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrl.java @@ -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 String location() { * @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() { diff --git a/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlParser.java b/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlParser.java index 1b0e7bb8149..48884102b79 100644 --- a/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlParser.java +++ b/wsmaster/che-core-api-factory-gitlab/src/main/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlParser.java @@ -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 @@ private Optional getPatternMatcherByUrl(String url) { 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 @@ private Optional getPatternMatcherByUrl(String url) { } private Optional 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)); diff --git a/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProviderTest.java b/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProviderTest.java index 4707d2fbb60..73f648e9aad 100644 --- a/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProviderTest.java +++ b/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabAuthorizingFileContentProviderTest.java @@ -43,10 +43,7 @@ public void shouldExpandRelativePaths() throws Exception { 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 diff --git a/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlTest.java b/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlTest.java index f08f92d0d3b..73f454baf67 100644 --- a/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlTest.java +++ b/wsmaster/che-core-api-factory-gitlab/src/test/java/org/eclipse/che/api/factory/server/gitlab/GitlabUrlTest.java @@ -69,25 +69,22 @@ public void checkDevfileLocation(String repoUrl, String fileUrl) { @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" }, }; }