Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix Gitlab raw file location url #363

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions wsmaster/che-core-api-auth-gitlab/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"))));
}
}

Expand Down Expand Up @@ -103,15 +106,15 @@ private Optional<Matcher> 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();
}
return Optional.empty();
}

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
};
}
Expand Down