From a6faefa953c170fece2ec093a4881f9945a80f3c Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Wed, 19 Oct 2022 17:59:17 +0300 Subject: [PATCH] fix: fix resolving Gitlab raw url (#371) Partially reverts 5aa763a in order to fix the Gitlab OAuth flow. The rawFileUrl method uses the API request now. If the request is passed without token it works for public repos, unlike the previous raw file request which fails for private projects, even with the Authorisation header. --- .../api/factory/server/gitlab/GitlabUrl.java | 25 ++++++++++++------- ...labAuthorizingFileContentProviderTest.java | 5 +++- .../factory/server/gitlab/GitlabUrlTest.java | 13 ++++++---- 3 files changed, 28 insertions(+), 15 deletions(-) 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 b89ee64331..7bbec9344e 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,7 +11,6 @@ */ 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; @@ -200,14 +199,22 @@ public class GitlabUrl implements RemoteFactoryUrl { * @return location of specified file in a repository */ public String rawFileLocation(String fileName) { - return new StringJoiner("/") - .add(hostName) - .add(username) - .add(project) - .add("-/raw") - .add(firstNonNull(branch, "HEAD")) - .add(fileName) - .toString(); + 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; } private String geProjectIdentifier() { 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 73f648e9aa..4707d2fbb6 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,7 +43,10 @@ public class GitlabAuthorizingFileContentProviderTest { when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(personalAccessToken); fileContentProvider.fetchContent("devfile.yaml"); verify(urlFetcher) - .fetch(eq("https://gitlab.net/eclipse/che/-/raw/HEAD/devfile.yaml"), eq("Bearer my-token")); + .fetch( + eq( + "https://gitlab.net/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw"), + 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 73f454baf6..f08f92d0d3 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,22 +69,25 @@ public class GitlabUrlTest { @DataProvider public static Object[][] urlsProvider() { return new Object[][] { - {"https://gitlab.net/eclipse/che.git", "https://gitlab.net/eclipse/che/-/raw/HEAD/%s"}, + { + "https://gitlab.net/eclipse/che.git", + "https://gitlab.net/api/v4/projects/eclipse%%2Fche/repository/files/%s/raw" + }, { "https://gitlab.net/eclipse/fooproj/che.git", - "https://gitlab.net/eclipse/fooproj/-/raw/HEAD/%s" + "https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw" }, { "https://gitlab.net/eclipse/fooproj/-/tree/master/", - "https://gitlab.net/eclipse/fooproj/-/raw/master/%s" + "https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj/repository/files/%s/raw?ref=master" }, { "https://gitlab.net/eclipse/fooproj/che/-/tree/foobranch/", - "https://gitlab.net/eclipse/fooproj/-/raw/foobranch/%s" + "https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw?ref=foobranch" }, { "https://gitlab.net/eclipse/fooproj/che/-/tree/foobranch/subfolder", - "https://gitlab.net/eclipse/fooproj/-/raw/foobranch/%s" + "https://gitlab.net/api/v4/projects/eclipse%%2Ffooproj%%2Fche/repository/files/%s/raw?ref=foobranch" }, }; }